add_courses_to_usergroup.php 10 KB

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