add_courses_to_usergroup.php 12 KB

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