course_edit.php 18 KB

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