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 '../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. } else {
  125. $list = CourseManager::get_courses_list(0, 0, 2, 'ASC', -1, $needle);
  126. }
  127. if ($type=='single') {
  128. } else {
  129. $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
  130. foreach ($list as $row) {
  131. if (!in_array($row['id'], array_keys($elements_in))) {
  132. $return .= '<option value="'.$row['id'].'">'.$row['title'].' ('.$row['visual_code'].')</option>';
  133. }
  134. }
  135. $return .= '</select>';
  136. $xajax_response->addAssign('ajax_list_multiple', 'innerHTML', api_utf8_encode($return));
  137. }
  138. }
  139. return $xajax_response;
  140. }
  141. $xajax->processRequests();
  142. Display::display_header($tool_name);
  143. if ($add_type == 'multiple') {
  144. $link_add_type_unique = '<a href="'.api_get_self().'?add='.$add.'&add_type=unique">'.
  145. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  146. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  147. } else {
  148. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  149. $link_add_type_multiple = '<a href="'.api_get_self().'?add='.$add.'&add_type=multiple">'.
  150. Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  151. }
  152. echo '<div class="actions">';
  153. echo '<a href="usergroups.php">'.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).'</a>';
  154. echo Display::url(get_lang('AdvancedSearch'), '#', array('class' => 'advanced_options', 'id' => 'advanced_search'));
  155. echo '</div>';
  156. echo '<div id="advanced_search_options" style="display:none">';
  157. $searchForm->display();
  158. echo '</div>';
  159. ?>
  160. <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();"';}?>>
  161. <?php echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
  162. echo Display::input('hidden', 'id', $id);
  163. echo Display::input('hidden', 'form_sent', '1');
  164. echo Display::input('hidden', 'add_type', null);
  165. if (!empty($errorMsg)) {
  166. Display::display_normal_message($errorMsg); //main API
  167. }
  168. ?>
  169. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  170. <tr>
  171. <td align="center"><b><?php echo get_lang('CoursesInPlatform') ?> :</b>
  172. </td>
  173. <td></td>
  174. <td align="center"><b><?php echo get_lang('CoursesInGroup') ?> :</b></td>
  175. </tr>
  176. <?php if ($add_type=='multiple') { ?>
  177. <tr>
  178. <td align="center">
  179. <?php echo get_lang('FirstLetterCourseTitle'); ?> :
  180. <select name="firstLetterUser" onchange = "xajax_search(this.value,'multiple')" >
  181. <option value = "%">--</option>
  182. <?php
  183. echo Display :: get_alphabet_options();
  184. ?>
  185. </select>
  186. </td>
  187. <td align="center">&nbsp;</td>
  188. </tr>
  189. <?php } ?>
  190. <tr>
  191. <td align="center">
  192. <div id="content_source">
  193. <?php
  194. if (!($add_type=='multiple')) {
  195. ?>
  196. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  197. <div id="ajax_list_users_single"></div>
  198. <?php
  199. } else {
  200. ?>
  201. <div id="ajax_list_multiple">
  202. <?php echo Display::select('elements_not_in_name', $elements_not_in, '', array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?>
  203. </div>
  204. <?php
  205. }
  206. ?>
  207. </div>
  208. </td>
  209. <td width="10%" valign="middle" align="center">
  210. <?php
  211. if ($ajax_search) {
  212. ?>
  213. <button class="btn bt-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
  214. <i class="fa fa-arrow-left"></i>
  215. </button>
  216. <?php
  217. } else {
  218. ?>
  219. <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'))">
  220. <i class="fa fa-arrow-right"></i>
  221. </button>
  222. <br /><br />
  223. <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'))">
  224. <i class="fa fa-arrow-left"></i>
  225. </button>
  226. <?php
  227. }
  228. ?>
  229. <br /><br /><br /><br /><br /><br />
  230. </td>
  231. <td align="center">
  232. <?php
  233. echo Display::select('elements_in_name[]', $elements_in, '', array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false );
  234. unset($sessionUsersList);
  235. ?>
  236. </td>
  237. </tr>
  238. <tr>
  239. <td colspan="3" align="center">
  240. <br />
  241. <?php
  242. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToCourses').'</button>';
  243. ?>
  244. </td>
  245. </tr>
  246. </table>
  247. </form>
  248. <script type="text/javascript">
  249. function moveItem(origin , destination) {
  250. for(var i = 0 ; i<origin.options.length ; i++) {
  251. if(origin.options[i].selected) {
  252. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  253. origin.options[i]=null;
  254. i = i-1;
  255. }
  256. }
  257. destination.selectedIndex = -1;
  258. sortOptions(destination.options);
  259. }
  260. function sortOptions(options) {
  261. newOptions = new Array();
  262. for (i = 0 ; i<options.length ; i++)
  263. newOptions[i] = options[i];
  264. newOptions = newOptions.sort(mysort);
  265. options.length = 0;
  266. for(i = 0 ; i < newOptions.length ; i++)
  267. options[i] = newOptions[i];
  268. }
  269. function mysort(a, b){
  270. if(a.text.toLowerCase() > b.text.toLowerCase()){
  271. return 1;
  272. }
  273. if(a.text.toLowerCase() < b.text.toLowerCase()){
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. function valide(){
  279. var options = document.getElementById('elements_in').options;
  280. for (i = 0 ; i<options.length ; i++)
  281. options[i].selected = true;
  282. document.forms.formulaire.submit();
  283. }
  284. function loadUsersInSelect(select) {
  285. var xhr_object = null;
  286. if(window.XMLHttpRequest) // Firefox
  287. xhr_object = new XMLHttpRequest();
  288. else if(window.ActiveXObject) // Internet Explorer
  289. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  290. else // XMLHttpRequest non supporté par le navigateur
  291. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  292. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  293. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  294. nosessionUsers = makepost(document.getElementById('elements_not_in'));
  295. sessionUsers = makepost(document.getElementById('elements_in'));
  296. nosessionClasses = makepost(document.getElementById('origin_classes'));
  297. sessionClasses = makepost(document.getElementById('destination_classes'));
  298. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  299. xhr_object.onreadystatechange = function() {
  300. if(xhr_object.readyState == 4) {
  301. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  302. }
  303. }
  304. }
  305. function makepost(select){
  306. var options = select.options;
  307. var ret = "";
  308. for (i = 0 ; i<options.length ; i++)
  309. ret = ret + options[i].value +'::'+options[i].text+";;";
  310. return ret;
  311. }
  312. </script>
  313. <?php
  314. Display::display_footer();