add_courses_to_usergroup.php 13 KB

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