add_courses_to_usergroup.php 9.9 KB

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