add_courses_to_usergroup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 '../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. header('Location: usergroups.php');
  69. exit;
  70. }
  71. }
  72. // Filters
  73. $filters = array(
  74. array('type' => 'text', 'name' => 'code', 'label' => get_lang('CourseCode')),
  75. array('type' => 'text', 'name' => 'title', 'label' => get_lang('Title')),
  76. /*array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
  77. array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
  78. array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))*/
  79. );
  80. $searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
  81. $searchForm->addHeader(get_lang('AdvancedSearch'));
  82. $renderer =& $searchForm->defaultRenderer();
  83. $searchForm->addElement('hidden', 'id', $id);
  84. foreach ($filters as $param) {
  85. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  86. }
  87. $searchForm->addButtonSearch();
  88. $filterData = array();
  89. if ($searchForm->validate()) {
  90. $filterData = $searchForm->getSubmitValues();
  91. }
  92. $conditions = array();
  93. if (!empty($filters) && !empty($filterData)) {
  94. foreach ($filters as $filter) {
  95. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  96. $value = $filterData[$filter['name']];
  97. if (!empty($value)) {
  98. $conditions[$filter['name']] = $value;
  99. }
  100. }
  101. }
  102. }
  103. $data = $usergroup->get($id);
  104. $course_list_in = $usergroup->get_courses_by_usergroup($id, true);
  105. $course_list = CourseManager::get_courses_list(0, 0, 'title', 'asc', -1, null, api_get_current_access_url_id(), false, $conditions);
  106. $elements_not_in = $elements_in = array();
  107. foreach ($course_list_in as $course) {
  108. $elements_in[$course['id']] = $course['title']." (".$course['visual_code'].")";
  109. }
  110. if (!empty($course_list)) {
  111. foreach ($course_list as $item) {
  112. $elements_not_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
  113. }
  114. }
  115. $ajax_search = $add_type == 'unique' ? true : false;
  116. //checking for extra field with filter on
  117. function search($needle,$type)
  118. {
  119. global $elements_in;
  120. $xajax_response = new xajaxResponse();
  121. $return = '';
  122. if (!empty($needle) && !empty($type)) {
  123. if ($type != 'single') {
  124. $list = CourseManager::get_courses_list(0, 0, 2, 'ASC', -1, $needle);
  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">'.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).'</a>';
  152. echo Display::url(get_lang('AdvancedSearch'), '#', array('class' => 'advanced_options', 'id' => 'advanced_search'));
  153. echo '</div>';
  154. echo '<div id="advanced_search_options" style="display:none">';
  155. $searchForm->display();
  156. echo '</div>';
  157. ?>
  158. <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();"';}?>>
  159. <?php echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
  160. echo Display::input('hidden', 'id', $id);
  161. echo Display::input('hidden', 'form_sent', '1');
  162. echo Display::input('hidden', 'add_type', null);
  163. if (!empty($errorMsg)) {
  164. Display::display_normal_message($errorMsg); //main API
  165. }
  166. ?>
  167. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  168. <tr>
  169. <td align="center"><b><?php echo get_lang('CoursesInPlatform') ?> :</b>
  170. </td>
  171. <td></td>
  172. <td align="center"><b><?php echo get_lang('CoursesInGroup') ?> :</b></td>
  173. </tr>
  174. <?php if ($add_type=='multiple') { ?>
  175. <tr>
  176. <td align="center">
  177. <?php echo get_lang('FirstLetterCourseTitle'); ?> :
  178. <select name="firstLetterUser" onchange = "xajax_search(this.value,'multiple')" >
  179. <option value = "%">--</option>
  180. <?php
  181. echo Display :: get_alphabet_options();
  182. ?>
  183. </select>
  184. </td>
  185. <td align="center">&nbsp;</td>
  186. </tr>
  187. <?php } ?>
  188. <tr>
  189. <td align="center">
  190. <div id="content_source">
  191. <?php
  192. if (!($add_type=='multiple')) {
  193. ?>
  194. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  195. <div id="ajax_list_users_single"></div>
  196. <?php
  197. } else {
  198. ?>
  199. <div id="ajax_list_multiple">
  200. <?php echo Display::select('elements_not_in_name', $elements_not_in, '', array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?>
  201. </div>
  202. <?php
  203. }
  204. ?>
  205. </div>
  206. </td>
  207. <td width="10%" valign="middle" align="center">
  208. <?php
  209. if ($ajax_search) {
  210. ?>
  211. <button class="btn bt-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
  212. <em class="fa fa-arrow-left"></em>
  213. </button>
  214. <?php
  215. } else {
  216. ?>
  217. <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'))">
  218. <em class="fa fa-arrow-right"></em>
  219. </button>
  220. <br /><br />
  221. <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'))">
  222. <em class="fa fa-arrow-left"></em>
  223. </button>
  224. <?php
  225. }
  226. ?>
  227. <br /><br /><br /><br /><br /><br />
  228. </td>
  229. <td align="center">
  230. <?php
  231. echo Display::select('elements_in_name[]', $elements_in, '', array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false );
  232. unset($sessionUsersList);
  233. ?>
  234. </td>
  235. </tr>
  236. <tr>
  237. <td colspan="3" align="center">
  238. <br />
  239. <?php
  240. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToCourses').'</button>';
  241. ?>
  242. </td>
  243. </tr>
  244. </table>
  245. </form>
  246. <script type="text/javascript">
  247. function moveItem(origin , destination) {
  248. for(var i = 0 ; i<origin.options.length ; i++) {
  249. if(origin.options[i].selected) {
  250. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  251. origin.options[i]=null;
  252. i = i-1;
  253. }
  254. }
  255. destination.selectedIndex = -1;
  256. sortOptions(destination.options);
  257. }
  258. function sortOptions(options) {
  259. newOptions = new Array();
  260. for (i = 0 ; i<options.length ; i++)
  261. newOptions[i] = options[i];
  262. newOptions = newOptions.sort(mysort);
  263. options.length = 0;
  264. for(i = 0 ; i < newOptions.length ; i++)
  265. options[i] = newOptions[i];
  266. }
  267. function mysort(a, b){
  268. if(a.text.toLowerCase() > b.text.toLowerCase()){
  269. return 1;
  270. }
  271. if(a.text.toLowerCase() < b.text.toLowerCase()){
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. function valide(){
  277. var options = document.getElementById('elements_in').options;
  278. for (i = 0 ; i<options.length ; i++)
  279. options[i].selected = true;
  280. document.forms.formulaire.submit();
  281. }
  282. function loadUsersInSelect(select) {
  283. var xhr_object = null;
  284. if(window.XMLHttpRequest) // Firefox
  285. xhr_object = new XMLHttpRequest();
  286. else if(window.ActiveXObject) // Internet Explorer
  287. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  288. else // XMLHttpRequest non supporté par le navigateur
  289. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  290. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  291. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  292. nosessionUsers = makepost(document.getElementById('elements_not_in'));
  293. sessionUsers = makepost(document.getElementById('elements_in'));
  294. nosessionClasses = makepost(document.getElementById('origin_classes'));
  295. sessionClasses = makepost(document.getElementById('destination_classes'));
  296. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  297. xhr_object.onreadystatechange = function() {
  298. if(xhr_object.readyState == 4) {
  299. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  300. }
  301. }
  302. }
  303. function makepost(select){
  304. var options = select.options;
  305. var ret = "";
  306. for (i = 0 ; i<options.length ; i++)
  307. ret = ret + options[i].value +'::'+options[i].text+";;";
  308. return ret;
  309. }
  310. </script>
  311. <?php
  312. Display::display_footer();