add_courses_to_usergroup.php 12 KB

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