course_edit.php 16 KB

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