add_courses_to_usergroup.php 12 KB

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