add_courses_to_usergroup.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // Resetting the course id.
  7. $cidReset = true;
  8. // Including some necessary files.
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  11. $usergroup = new UserGroup();
  12. $data = $usergroup->get($id);
  13. $usergroup->protectScript($data);
  14. $xajax = new xajax();
  15. $xajax->registerFunction('search');
  16. // Setting the section (for the tabs).
  17. $this_section = SECTION_PLATFORM_ADMIN;
  18. // Setting breadcrumbs.
  19. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  20. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  21. // Setting the name of the tool.
  22. $tool_name = get_lang('SubscribeClassToCourses');
  23. $add_type = 'multiple';
  24. if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
  25. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  26. }
  27. $add = isset($_GET['add']) ? Security::remove_XSS($_GET['add']) : null;
  28. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  29. $htmlHeadXtra[] = '<script>
  30. function remove_item(origin) {
  31. for(var i = 0 ; i<origin.options.length ; i++) {
  32. if(origin.options[i].selected) {
  33. origin.options[i]=null;
  34. i = i-1;
  35. }
  36. }
  37. }
  38. </script>';
  39. $form_sent = 0;
  40. $errorMsg = '';
  41. $sessions = [];
  42. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  43. $form_sent = $_POST['form_sent'];
  44. $elements_posted = $_POST['elements_in_name'];
  45. if (!is_array($elements_posted)) {
  46. $elements_posted = [];
  47. }
  48. if ($form_sent == 1) {
  49. $usergroup->subscribe_courses_to_usergroup($id, $elements_posted);
  50. Display::addFlash(Display::return_message(get_lang('Updated')));
  51. header('Location: usergroups.php');
  52. exit;
  53. }
  54. }
  55. // Filters
  56. $filters = [
  57. ['type' => 'text', 'name' => 'code', 'label' => get_lang('CourseCode')],
  58. ['type' => 'text', 'name' => 'title', 'label' => get_lang('Title')],
  59. ];
  60. $searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
  61. $searchForm->addHeader(get_lang('AdvancedSearch'));
  62. $renderer = &$searchForm->defaultRenderer();
  63. $searchForm->addElement('hidden', 'id', $id);
  64. foreach ($filters as $param) {
  65. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  66. }
  67. $searchForm->addButtonSearch();
  68. $filterData = [];
  69. if ($searchForm->validate()) {
  70. $filterData = $searchForm->getSubmitValues();
  71. }
  72. $conditions = [];
  73. if (!empty($filters) && !empty($filterData)) {
  74. foreach ($filters as $filter) {
  75. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  76. $value = $filterData[$filter['name']];
  77. if (!empty($value)) {
  78. $conditions[$filter['name']] = $value;
  79. }
  80. }
  81. }
  82. }
  83. $course_list_in = $usergroup->get_courses_by_usergroup($id, true);
  84. $onlyThisCourseList = [];
  85. if ($usergroup->allowTeachers()) {
  86. $userId = api_get_user_id();
  87. $courseList = CourseManager::getCoursesFollowedByUser($userId, COURSEMANAGER);
  88. if (!empty($courseList)) {
  89. $onlyThisCourseList = array_column($courseList, 'id');
  90. }
  91. }
  92. $course_list = CourseManager::get_courses_list(
  93. 0,
  94. 0,
  95. 'title',
  96. 'asc',
  97. -1,
  98. null,
  99. api_get_current_access_url_id(),
  100. false,
  101. $conditions,
  102. $onlyThisCourseList
  103. );
  104. $elements_not_in = $elements_in = [];
  105. foreach ($course_list_in as $course) {
  106. $elements_in[$course['id']] = $course['title']." (".$course['visual_code'].")";
  107. }
  108. if (!empty($course_list)) {
  109. foreach ($course_list as $item) {
  110. if (isset($elements_in[$item['id']])) {
  111. continue;
  112. }
  113. $elements_not_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
  114. }
  115. }
  116. $ajax_search = $add_type == 'unique' ? true : false;
  117. // checking for extra field with filter on
  118. function search($needle, $type)
  119. {
  120. global $elements_in;
  121. $xajax_response = new xajaxResponse();
  122. $return = '';
  123. if (!empty($needle) && !empty($type)) {
  124. if ($type != 'single') {
  125. $list = CourseManager::get_courses_list(
  126. 0,
  127. 0,
  128. 2,
  129. 'ASC',
  130. -1,
  131. $needle
  132. );
  133. }
  134. if ($type != 'single') {
  135. $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
  136. foreach ($list as $row) {
  137. if (!in_array($row['id'], array_keys($elements_in))) {
  138. $return .= '<option value="'.$row['id'].'">'.$row['title'].' ('.$row['visual_code'].')</option>';
  139. }
  140. }
  141. $return .= '</select>';
  142. $xajax_response->addAssign('ajax_list_multiple', 'innerHTML', api_utf8_encode($return));
  143. }
  144. }
  145. return $xajax_response;
  146. }
  147. $xajax->processRequests();
  148. Display::display_header($tool_name);
  149. if ($add_type == 'multiple') {
  150. $link_add_type_unique = '<a href="'.api_get_self().'?add='.$add.'&add_type=unique">'.
  151. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  152. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  153. } else {
  154. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  155. $link_add_type_multiple = '<a href="'.api_get_self().'?add='.$add.'&add_type=multiple">'.
  156. Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  157. }
  158. echo '<div class="actions">';
  159. echo '<a href="usergroups.php">';
  160. echo Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM).'</a>';
  161. echo Display::url(get_lang('AdvancedSearch'), '#', ['class' => 'advanced_options', 'id' => 'advanced_search']);
  162. echo '</div>';
  163. echo '<div id="advanced_search_options" style="display:none">';
  164. $searchForm->display();
  165. echo '</div>';
  166. ?>
  167. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if (!empty($_GET['add'])) {
  168. echo '&add=true';
  169. } ?>" style="margin:0px;" <?php if ($ajax_search) {
  170. echo ' onsubmit="valide();"';
  171. }?>>
  172. <?php echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
  173. echo Display::input('hidden', 'id', $id);
  174. echo Display::input('hidden', 'form_sent', '1');
  175. echo Display::input('hidden', 'add_type', null);
  176. if (!empty($errorMsg)) {
  177. echo Display::return_message($errorMsg, 'normal'); //main API
  178. }
  179. ?>
  180. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  181. <tr>
  182. <td align="center"><b><?php echo get_lang('CoursesInPlatform'); ?> :</b>
  183. </td>
  184. <td></td>
  185. <td align="center"><b><?php echo get_lang('CoursesInGroup'); ?> :</b></td>
  186. </tr>
  187. <?php if ($add_type == 'multiple') {
  188. ?>
  189. <tr>
  190. <td align="center">
  191. <?php echo get_lang('FirstLetterCourseTitle'); ?> :
  192. <select name="firstLetterUser" onchange = "xajax_search(this.value,'multiple')" >
  193. <option value = "%">--</option>
  194. <?php
  195. echo Display :: get_alphabet_options(); ?>
  196. </select>
  197. </td>
  198. <td align="center">&nbsp;</td>
  199. </tr>
  200. <?php
  201. } ?>
  202. <tr>
  203. <td align="center">
  204. <div id="content_source">
  205. <?php
  206. if (!($add_type == 'multiple')) {
  207. ?>
  208. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  209. <div id="ajax_list_users_single"></div>
  210. <?php
  211. } else {
  212. ?>
  213. <div id="ajax_list_multiple">
  214. <?php
  215. echo Display::select(
  216. 'elements_not_in_name',
  217. $elements_not_in,
  218. '',
  219. ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_not_in', 'size' => '15px'],
  220. false
  221. ); ?>
  222. </div>
  223. <?php
  224. }
  225. ?>
  226. </div>
  227. </td>
  228. <td width="10%" valign="middle" align="center">
  229. <?php
  230. if ($ajax_search) {
  231. ?>
  232. <button class="btn bt-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
  233. <em class="fa fa-arrow-left"></em>
  234. </button>
  235. <?php
  236. } else {
  237. ?>
  238. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))">
  239. <em class="fa fa-arrow-right"></em>
  240. </button>
  241. <br /><br />
  242. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))">
  243. <em class="fa fa-arrow-left"></em>
  244. </button>
  245. <?php
  246. }
  247. ?>
  248. <br /><br /><br /><br /><br /><br />
  249. </td>
  250. <td align="center">
  251. <?php
  252. echo Display::select(
  253. 'elements_in_name[]',
  254. $elements_in,
  255. '',
  256. ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_in', 'size' => '15px'],
  257. false
  258. );
  259. unset($sessionUsersList);
  260. ?>
  261. </td>
  262. </tr>
  263. <tr>
  264. <td colspan="3" align="center">
  265. <br />
  266. <?php
  267. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToCourses').'</button>';
  268. ?>
  269. </td>
  270. </tr>
  271. </table>
  272. </form>
  273. <script>
  274. function moveItem(origin , destination) {
  275. for(var i = 0 ; i<origin.options.length ; i++) {
  276. if(origin.options[i].selected) {
  277. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  278. origin.options[i]=null;
  279. i = i-1;
  280. }
  281. }
  282. destination.selectedIndex = -1;
  283. sortOptions(destination.options);
  284. }
  285. function sortOptions(options) {
  286. newOptions = new Array();
  287. for (i = 0 ; i<options.length ; i++)
  288. newOptions[i] = options[i];
  289. newOptions = newOptions.sort(mysort);
  290. options.length = 0;
  291. for(i = 0 ; i < newOptions.length ; i++)
  292. options[i] = newOptions[i];
  293. }
  294. function mysort(a, b) {
  295. if(a.text.toLowerCase() > b.text.toLowerCase()){
  296. return 1;
  297. }
  298. if(a.text.toLowerCase() < b.text.toLowerCase()){
  299. return -1;
  300. }
  301. return 0;
  302. }
  303. function valide() {
  304. var options = document.getElementById('elements_in').options;
  305. for (i = 0 ; i<options.length ; i++)
  306. options[i].selected = true;
  307. document.forms.formulaire.submit();
  308. }
  309. </script>
  310. <?php
  311. Display::display_footer();