course_edit.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 = 'admin';
  8. $cidReset = true;
  9. require_once '../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  14. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  15. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  16. $course_code = isset($_GET['course_code']) ? $_GET['course_code'] : $_POST['code'];
  17. $noPHP_SELF = true;
  18. $tool_name = get_lang('ModifyCourseInfo');
  19. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  20. $interbreadcrumb[] = array ("url" => "course_list.php", "name" => get_lang('CourseList'));
  21. // Get all course categories
  22. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  23. //Get the course infos
  24. $sql = "SELECT * FROM $course_table WHERE code='".Database::escape_string($course_code)."'";
  25. $result = Database::query($sql);
  26. if (Database::num_rows($result) != 1) {
  27. header('Location: course_list.php');
  28. exit();
  29. }
  30. $course = Database::fetch_array($result, 'ASSOC');
  31. $course_info = api_get_course_info($course_code);
  32. // Get course teachers
  33. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  34. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  35. $sql = "SELECT user.user_id,lastname,firstname FROM $table_user as user,$table_course_user as course_user WHERE course_user.status='1' AND course_user.user_id=user.user_id AND course_user.course_code='".$course_code."'".$order_clause;
  36. $res = Database::query($sql);
  37. $course_teachers = array();
  38. while ($obj = Database::fetch_object($res)) {
  39. $course_teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  40. }
  41. // Get all possible teachers without the course teachers
  42. if (api_is_multiple_url_enabled()) {
  43. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  44. $sql = "SELECT u.user_id,lastname,firstname
  45. FROM $table_user as u
  46. INNER JOIN $access_url_rel_user_table url_rel_user
  47. ON (u.user_id=url_rel_user.user_id)
  48. WHERE url_rel_user.access_url_id=".api_get_current_access_url_id()." AND status=1".$order_clause;
  49. } else {
  50. $sql = "SELECT user_id, lastname, firstname
  51. FROM $table_user WHERE status='1'".$order_clause;
  52. }
  53. $res = Database::query($sql);
  54. $teachers = array();
  55. $allTeachers = array();
  56. $platform_teachers[0] = '-- '.get_lang('NoManager').' --';
  57. while ($obj = Database::fetch_object($res)) {
  58. $allTeachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  59. if (!array_key_exists($obj->user_id, $course_teachers)) {
  60. $teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  61. }
  62. if (isset($course_teachers[$obj->user_id]) && $course['tutor_name'] == $course_teachers[$obj->user_id]) {
  63. $course['tutor_name'] = $obj->user_id;
  64. }
  65. // We add in the array platform teachers
  66. $platform_teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  67. }
  68. // Case where there is no teacher in the course
  69. if (count($course_teachers) == 0) {
  70. $sql='SELECT tutor_name FROM '.$course_table.' WHERE code="'.$course_code.'"';
  71. $res = Database::query($sql);
  72. $tutor_name = Database::result($res, 0, 0);
  73. $course['tutor_name'] = array_search($tutor_name, $platform_teachers);
  74. }
  75. // Build the form
  76. $form = new FormValidator('update_course');
  77. $form->addElement('header', get_lang('Course').' #'.$course_info['real_id'].' '.$course_code);
  78. $form->addElement('hidden', 'code', $course_code);
  79. //title
  80. $form->add_textfield('title', get_lang('Title'), true, array ('class' => 'span6'));
  81. $form->applyFilter('title', 'html_filter');
  82. $form->applyFilter('title', 'trim');
  83. // Code
  84. $element = $form->addElement('text', 'real_code', array(get_lang('CourseCode'), get_lang('ThisValueCantBeChanged')));
  85. $element->freeze();
  86. // Visual code
  87. $form->add_textfield('visual_code', array(get_lang('VisualCode'), get_lang('OnlyLettersAndNumbers'), get_lang('ThisValueIsUsedInTheCourseURL')), true, array('class' => 'span4'));
  88. $form->applyFilter('visual_code', 'strtoupper');
  89. $form->applyFilter('visual_code', 'html_filter');
  90. $group = array(
  91. $form->createElement('select', 'platform_teachers', '', $teachers, ' id="platform_teachers" multiple=multiple size="4" style="width:300px;"'),
  92. $form->createElement('select', 'course_teachers', '', $course_teachers, ' id="course_teachers" multiple=multiple size="4" style="width:300px;"')
  93. );
  94. $element_template = <<<EOT
  95. <div class="control-group">
  96. <label>
  97. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  98. </label>
  99. <div class="controls">
  100. <table cellpadding="0" cellspacing="0">
  101. <tr>
  102. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> <td>{element}</td>
  103. </tr>
  104. </table>
  105. </div>
  106. </div>
  107. EOT;
  108. $renderer = $form->defaultRenderer();
  109. $renderer->setElementTemplate($element_template, 'group');
  110. $form->addGroup($group, 'group', get_lang('CourseTeachers'), '</td><td width="80" align="center">'.
  111. '<input class="arrowr" style="width:30px;height:30px;padding-right:12px" type="button" onclick="moveItem(document.getElementById(\'platform_teachers\'), document.getElementById(\'course_teachers\'))"><br><br>'.
  112. '<input class="arrowl" style="width:30px;height:30px;padding-left:13px" type="button" onclick="moveItem(document.getElementById(\'course_teachers\'), document.getElementById(\'platform_teachers\'))"></td><td>'
  113. );
  114. if (array_key_exists('add_teachers_to_sessions_courses', $course)) {
  115. $form->addElement('checkbox', 'add_teachers_to_sessions_courses', null, get_lang('TeachersWillBeAddedAsCoachInAllCourseSessions'));
  116. }
  117. $coursesInSession = SessionManager::get_session_by_course($course['code']);
  118. if (!empty($coursesInSession)) {
  119. foreach ($coursesInSession as $session) {
  120. $sessionId = $session['id'];
  121. $coaches = SessionManager::getCoachesByCourseSession($sessionId, $course['code']);
  122. $teachers = $allTeachers;
  123. $sessionTeachers = array();
  124. foreach ($coaches as $coachId) {
  125. $userInfo = api_get_user_info($coachId);
  126. $sessionTeachers[$coachId] = $userInfo['complete_name'];
  127. if (isset($teachers[$coachId])) {
  128. unset($teachers[$coachId]);
  129. }
  130. }
  131. $groupName = 'session_coaches['.$sessionId.']';
  132. $platformTeacherId = 'platform_teachers_by_session_'.$sessionId;
  133. $coachId = 'coaches_by_session_'.$sessionId;
  134. $platformTeacherName = 'platform_teachers_by_session';
  135. $coachName = 'coaches_by_session';
  136. $group = array(
  137. $form->createElement('select', $platformTeacherName, '', $teachers, ' id="'.$platformTeacherId.'" multiple=multiple size="4" style="width:300px;"'),
  138. $form->createElement('select', $coachName, '', $sessionTeachers, ' id="'.$coachId.'" multiple=multiple size="4" style="width:300px;"')
  139. );
  140. $renderer = $form->defaultRenderer();
  141. $renderer->setElementTemplate($element_template, $groupName);
  142. $sessionUrl = api_get_path(WEB_CODE_PATH).'admin/resume_session.php?id_session='.$sessionId;
  143. $form->addGroup($group, $groupName, Display::url($session['name'], $sessionUrl, array('target' => '_blank')).' - '.get_lang('Coaches'), '</td>
  144. <td width="80" align="center">
  145. <input class="arrowr" style="width:30px;height:30px;padding-right:12px" type="button" onclick="moveItem(document.getElementById(\''.$platformTeacherId.'\'), document.getElementById(\''.$coachId.'\'));">
  146. <br><br>
  147. <input class="arrowl" style="width:30px;height:30px;padding-left:13px" type="button" onclick="moveItem(document.getElementById(\''.$coachId.'\'), document.getElementById(\''.$platformTeacherId.'\'));">
  148. </td><td>'
  149. );
  150. }
  151. }
  152. // Category code
  153. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  154. $categoryList = array();
  155. if (!empty($course['category_code'])) {
  156. $data = getCategory($course['category_code']);
  157. $categoryList[] = array('id' => $data['code'], 'text' => $data['name']);
  158. }
  159. $form->addElement('select_ajax', 'category_code', get_lang('CourseFaculty'), null, array('url' => $url, 'defaults' => $categoryList));
  160. $form->add_textfield('department_name', get_lang('CourseDepartment'), false, array('size' => '60'));
  161. $form->applyFilter('department_name', 'html_filter');
  162. $form->applyFilter('department_name', 'trim');
  163. $form->add_textfield('department_url', get_lang('CourseDepartmentURL'), false, array('size' => '60'));
  164. $form->applyFilter('department_url', 'html_filter');
  165. $form->applyFilter('department_url', 'trim');
  166. $form->addElement('select_language', 'course_language', get_lang('CourseLanguage'));
  167. $form->applyFilter('select_language', 'html_filter');
  168. $group = array();
  169. $group[]= $form->createElement('radio', 'visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  170. $group[]= $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  171. $group[]= $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  172. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  173. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  174. $form->addGroup($group, '', get_lang('CourseAccess'), '<br />');
  175. $group = array();
  176. $group[]= $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  177. $group[]= $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  178. $form->addGroup($group, '', get_lang('Subscription'), '<br />');
  179. $group = array();
  180. $group[]= $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  181. $group[]= $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  182. $form->addGroup($group, '', get_lang('Unsubscription'), '<br />');
  183. $form->addElement('text', 'disk_quota', array(get_lang('CourseQuota'), null, get_lang('MB')));
  184. $form->addRule('disk_quota', get_lang('ThisFieldIsRequired'), 'required');
  185. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  186. $list_course_extra_field = CourseManager::get_course_extra_field_list($course_code);
  187. //@todo this is wrong
  188. foreach ($list_course_extra_field as $extra_field) {
  189. switch ($extra_field['field_type']) {
  190. case CourseManager::COURSE_FIELD_TYPE_CHECKBOX:
  191. $checked = (array_key_exists('extra_field_value', $extra_field) && $extra_field['extra_field_value'] == 1)? array('checked'=>'checked'): '';
  192. $form->addElement('hidden', '_extra_'.$extra_field['field_variable'], 0);
  193. $field_display_text = $extra_field['field_display_text'];
  194. $form->addElement('checkbox', 'extra_'.$extra_field['field_variable'], array(null, get_lang('AllUsersAreAutomaticallyRegistered')), get_lang('SpecialCourse'), $checked);
  195. break;
  196. }
  197. }
  198. $form->addElement('style_submit_button', 'button', get_lang('ModifyCourseInfo'), 'onclick="valide()"; class="save"');
  199. // Set some default values
  200. $course['disk_quota'] = round(DocumentManager::get_course_quota($course_code) /1024/1024, 1);
  201. $course['title'] = api_html_entity_decode($course['title'], ENT_QUOTES, $charset);
  202. $course['real_code'] = $course['code'];
  203. $course['add_teachers_to_sessions_courses'] = isset($course['add_teachers_to_sessions_courses']) ? $course['add_teachers_to_sessions_courses'] : 0;
  204. $form->setDefaults($course);
  205. // Validate form
  206. if ($form->validate()) {
  207. $course = $form->getSubmitValues();
  208. $visibility = $course['visibility'];
  209. global $_configuration;
  210. $urlId = api_get_current_access_url_id();
  211. if (isset($_configuration[$urlId]) &&
  212. isset($_configuration[$urlId]['hosting_limit_active_courses']) &&
  213. $_configuration[$urlId]['hosting_limit_active_courses'] > 0
  214. ) {
  215. // Check if
  216. if ($course_info['visibility'] == COURSE_VISIBILITY_HIDDEN &&
  217. $visibility != $course_info['visibility']
  218. ) {
  219. $num = CourseManager::countActiveCourses($urlId);
  220. if ($num >= $_configuration[$urlId]['hosting_limit_active_courses']) {
  221. api_warn_hosting_contact('hosting_limit_active_courses');
  222. api_set_failure(get_lang('PortalActiveCoursesLimitReached'));
  223. header('Location: course_list.php?action=show_msg&warn=' . urlencode(get_lang('PortalActiveCoursesLimitReached')));
  224. exit;
  225. }
  226. }
  227. }
  228. $course_code = $course['code'];
  229. $visual_code = $course['visual_code'];
  230. $visual_code = generate_course_code($visual_code);
  231. // Check if the visual code is already used by *another* course
  232. $visual_code_is_used = false;
  233. $warn = get_lang('TheFollowingCoursesAlreadyUseThisVisualCode').':';
  234. if (!empty($visual_code)) {
  235. $list = CourseManager::get_courses_info_from_visual_code($visual_code);
  236. foreach ($list as $course_temp) {
  237. if ($course_temp['code'] != $course_code) {
  238. $visual_code_is_used = true;
  239. $warn .= ' '.$course_temp['title'].' ('.$course_temp['code'].'),';
  240. }
  241. }
  242. $warn = substr($warn, 0, -1);
  243. }
  244. // an extra field
  245. $extras = array();
  246. foreach ($course as $key => $value) {
  247. if (substr($key, 0, 6) == 'extra_') {
  248. $extras[substr($key, 6)] = $value;
  249. }
  250. if (substr($key, 0, 7) == '_extra_') {
  251. if (!array_key_exists(substr($key, 7), $extras)) {
  252. $extras[substr($key, 7)] = $value;
  253. }
  254. }
  255. }
  256. $tutor_id = isset($course['tutor_name']) ? $course['tutor_name'] : null;
  257. $tutor_name = isset($platform_teachers[$tutor_id]) ? $platform_teachers[$tutor_id] : null;
  258. $teachers = $course['group']['course_teachers'];
  259. $title = $course['title'];
  260. $category_code = $course['category_code'];
  261. $department_name = $course['department_name'];
  262. $department_url = $course['department_url'];
  263. $course_language = $course['course_language'];
  264. $course['disk_quota'] = $course['disk_quota']*1024*1024;
  265. $disk_quota = $course['disk_quota'];
  266. $subscribe = $course['subscribe'];
  267. $unsubscribe = $course['unsubscribe'];
  268. if (!stristr($department_url, 'http://')) {
  269. $department_url = 'http://'.$department_url;
  270. }
  271. $sql = "UPDATE $course_table SET course_language='".Database::escape_string($course_language)."',
  272. title='".Database::escape_string($title)."',
  273. category_code='".Database::escape_string($category_code)."',
  274. tutor_name='".Database::escape_string($tutor_name)."',
  275. visual_code='".Database::escape_string($visual_code)."',
  276. department_name='".Database::escape_string($department_name)."',
  277. department_url='".Database::escape_string($department_url)."',
  278. disk_quota='".Database::escape_string($disk_quota)."',
  279. visibility = '".Database::escape_string($visibility)."',
  280. subscribe = '".Database::escape_string($subscribe)."',
  281. unsubscribe='".Database::escape_string($unsubscribe)."'
  282. WHERE code='".Database::escape_string($course_code)."'";
  283. Database::query($sql);
  284. // update the extra fields
  285. if (count($extras) > 0) {
  286. foreach ($extras as $key => $value) {
  287. CourseManager::update_course_extra_field_value($course_code, $key, $value);
  288. }
  289. }
  290. $addTeacherToSessionCourses = isset($course['add_teachers_to_sessions_courses']) && !empty($course['add_teachers_to_sessions_courses']) ? 1 : 0;
  291. // Updating teachers
  292. if ($addTeacherToSessionCourses) {
  293. // Updating session coaches
  294. $sessionCoaches = $course['session_coaches'];
  295. if (!empty($sessionCoaches)) {
  296. foreach ($sessionCoaches as $sessionId => $teacherInfo) {
  297. $coachesToSubscribe = $teacherInfo['coaches_by_session'];
  298. SessionManager::updateCoaches($sessionId, $course['code'], $coachesToSubscribe, true);
  299. }
  300. }
  301. CourseManager::updateTeachers($course_code, $teachers, true, true, false);
  302. } else {
  303. // Normal behaviour
  304. CourseManager::updateTeachers($course_code, $teachers, true, false);
  305. // Updating session coaches
  306. $sessionCoaches = $course['session_coaches'];
  307. if (!empty($sessionCoaches)) {
  308. foreach ($sessionCoaches as $sessionId => $teacherInfo) {
  309. $coachesToSubscribe = isset($teacherInfo['coaches_by_session']) ? $teacherInfo['coaches_by_session'] : null;
  310. if (!empty($coachesToSubscribe)) {
  311. SessionManager::updateCoaches(
  312. $sessionId,
  313. $course['code'],
  314. $coachesToSubscribe,
  315. true
  316. );
  317. }
  318. }
  319. }
  320. }
  321. $sql = "INSERT IGNORE INTO ".$course_user_table . " SET
  322. course_code = '".Database::escape_string($course_code). "',
  323. user_id = '".$tutor_id . "',
  324. status = '1',
  325. role = '',
  326. tutor_id='0',
  327. sort='0',
  328. user_course_cat='0'";
  329. Database::query($sql);
  330. if (array_key_exists('add_teachers_to_sessions_courses', $course_info)) {
  331. $sql = "UPDATE $course_table SET add_teachers_to_sessions_courses = '$addTeacherToSessionCourses'
  332. WHERE id = ".$course_info['real_id'];
  333. Database::query($sql);
  334. }
  335. $course_id = $course_info['real_id'];
  336. /*$forum_config_table = Database::get_course_table(TOOL_FORUM_CONFIG_TABLE);
  337. $sql = "UPDATE ".$forum_config_table." SET default_lang='".Database::escape_string($course_language)."' WHERE c_id = $course_id ";*/
  338. if ($visual_code_is_used) {
  339. header('Location: course_list.php?action=show_msg&warn='.urlencode($warn));
  340. } else {
  341. header('Location: course_list.php');
  342. }
  343. exit;
  344. }
  345. Display::display_header($tool_name);
  346. echo '<div class="actions">';
  347. echo Display::url(Display::return_icon('back.png', get_lang('Back')), api_get_path(WEB_CODE_PATH).'admin/course_list.php');
  348. echo Display::url(Display::return_icon('course_home.png', get_lang('CourseHome')), $course_info['course_public_url'], array('target' => '_blank'));
  349. echo '</div>';
  350. echo "<script>
  351. function moveItem(origin , destination) {
  352. for (var i = 0 ; i<origin.options.length ; i++) {
  353. if (origin.options[i].selected) {
  354. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  355. origin.options[i]=null;
  356. i = i-1;
  357. }
  358. }
  359. destination.selectedIndex = -1;
  360. sortOptions(destination.options);
  361. }
  362. function sortOptions(options) {
  363. newOptions = new Array();
  364. for (i = 0 ; i<options.length ; i++) {
  365. newOptions[i] = options[i];
  366. }
  367. newOptions = newOptions.sort(mysort);
  368. options.length = 0;
  369. for (i = 0 ; i < newOptions.length ; i++) {
  370. options[i] = newOptions[i];
  371. }
  372. }
  373. function mysort(a, b) {
  374. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  375. return 1;
  376. }
  377. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  378. return -1;
  379. }
  380. return 0;
  381. }
  382. function valide() {
  383. // Checking all multiple
  384. $('select').filter(function() {
  385. if ($(this).attr('multiple')) {
  386. $(this).find('option').each(function() {
  387. $(this).attr('selected', true);
  388. });
  389. }
  390. });
  391. //document.update_course.submit();
  392. }
  393. </script>";
  394. // Display the form
  395. $form->display();
  396. Display :: display_footer();