course_edit.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../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);
  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).'session/resume_session.php?id_session='.$sessionId;
  134. $form->addElement(
  135. 'advmultiselect',
  136. $groupName,
  137. Display::url(
  138. $session['name'],
  139. $sessionUrl,
  140. array('target' => '_blank')
  141. ).' - '.get_lang('Coaches'),
  142. $allTeachers
  143. );
  144. $courseInfo[$groupName] = $sessionTeachers;
  145. }
  146. }
  147. // Category code
  148. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  149. $categorySelect = $form->addElement(
  150. 'select_ajax',
  151. 'category_code',
  152. get_lang('CourseFaculty'),
  153. null,
  154. array('url' => $url)
  155. );
  156. if (!empty($courseInfo['categoryCode'])) {
  157. $data = CourseCategory::getCategory($courseInfo['categoryCode']);
  158. $categorySelect->addOption($data['name'], $data['code']);
  159. }
  160. $form->addText('department_name', get_lang('CourseDepartment'), false, array('size' => '60'));
  161. $form->applyFilter('department_name', 'html_filter');
  162. $form->applyFilter('department_name', 'trim');
  163. $form->addText('department_url', get_lang('CourseDepartmentURL'), false, array('size' => '60'));
  164. $form->applyFilter('department_url', 'html_filter');
  165. $form->applyFilter('department_url', 'trim');
  166. $form->addSelectLanguage('course_language', get_lang('CourseLanguage'));
  167. $group = array();
  168. $group[] = $form->createElement('radio', 'visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  169. $group[] = $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  170. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  171. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  172. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  173. $form->addGroup($group, '', get_lang('CourseAccess'));
  174. $group = array();
  175. $group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  176. $group[] = $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  177. $form->addGroup($group, '', get_lang('Subscription'));
  178. $group = array();
  179. $group[] = $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  180. $group[] = $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  181. $form->addGroup($group, '', get_lang('Unsubscription'));
  182. $form->addElement('text', 'disk_quota', array(get_lang('CourseQuota'), null, get_lang('MB')));
  183. $form->addRule('disk_quota', get_lang('ThisFieldIsRequired'), 'required');
  184. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  185. // Extra fields
  186. $extra_field = new ExtraField('course');
  187. $extra = $extra_field->addElements($form, $courseId);
  188. $htmlHeadXtra[] = '
  189. <script>
  190. $(function() {
  191. ' . $extra['jquery_ready_content'].'
  192. });
  193. </script>';
  194. $form->addButtonUpdate(get_lang('ModifyCourseInfo'));
  195. // Set some default values
  196. $courseInfo['disk_quota'] = round(DocumentManager::get_course_quota($courseInfo['code']) / 1024 / 1024, 1);
  197. $courseInfo['real_code'] = $courseInfo['code'];
  198. $courseInfo['add_teachers_to_sessions_courses'] = isset($courseInfo['add_teachers_to_sessions_courses']) ? $courseInfo['add_teachers_to_sessions_courses'] : 0;
  199. $form->setDefaults($courseInfo);
  200. // Validate form
  201. if ($form->validate()) {
  202. $course = $form->getSubmitValues();
  203. $visibility = $course['visibility'];
  204. global $_configuration;
  205. $urlId = api_get_current_access_url_id();
  206. if (isset($_configuration[$urlId]) &&
  207. isset($_configuration[$urlId]['hosting_limit_active_courses']) &&
  208. $_configuration[$urlId]['hosting_limit_active_courses'] > 0
  209. ) {
  210. // Check if
  211. if ($courseInfo['visibility'] == COURSE_VISIBILITY_HIDDEN &&
  212. $visibility != $courseInfo['visibility']
  213. ) {
  214. $num = CourseManager::countActiveCourses($urlId);
  215. if ($num >= $_configuration[$urlId]['hosting_limit_active_courses']) {
  216. api_warn_hosting_contact('hosting_limit_active_courses');
  217. Display::addFlash(
  218. Display::return_message(get_lang('PortalActiveCoursesLimitReached'))
  219. );
  220. header('Location: course_list.php');
  221. exit;
  222. }
  223. }
  224. }
  225. $visual_code = $course['visual_code'];
  226. $visual_code = CourseManager::generate_course_code($visual_code);
  227. // Check if the visual code is already used by *another* course
  228. $visual_code_is_used = false;
  229. $warn = get_lang('TheFollowingCoursesAlreadyUseThisVisualCode');
  230. if (!empty($visual_code)) {
  231. $list = CourseManager::get_courses_info_from_visual_code($visual_code);
  232. foreach ($list as $course_temp) {
  233. if ($course_temp['code'] != $course_code) {
  234. $visual_code_is_used = true;
  235. $warn .= ' '.$course_temp['title'].' ('.$course_temp['code'].'),';
  236. }
  237. }
  238. $warn = substr($warn, 0, -1);
  239. }
  240. $teachers = isset($course['course_teachers']) ? $course['course_teachers'] : '';
  241. $title = $course['title'];
  242. $category_code = isset($course['category_code']) ? $course['category_code'] : '';
  243. $department_name = $course['department_name'];
  244. $department_url = $course['department_url'];
  245. $course_language = $course['course_language'];
  246. $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
  247. $disk_quota = $course['disk_quota'];
  248. $subscribe = $course['subscribe'];
  249. $unsubscribe = $course['unsubscribe'];
  250. $course['course_code'] = $course_code;
  251. if (!stristr($department_url, 'http://')) {
  252. $department_url = 'http://'.$department_url;
  253. }
  254. Database::query($sql);
  255. $params = [
  256. 'course_language' => $course_language,
  257. 'title' => $title,
  258. 'category_code' => $category_code,
  259. 'visual_code' => $visual_code,
  260. 'department_name' => $department_name,
  261. 'department_url' => $department_url,
  262. 'disk_quota' => $disk_quota,
  263. 'visibility' => $visibility,
  264. 'subscribe' => $subscribe,
  265. 'unsubscribe' => $unsubscribe,
  266. ];
  267. Database::update($course_table, $params, ['id = ?' => $courseId]);
  268. // update the extra fields
  269. $courseFieldValue = new ExtraFieldValue('course');
  270. $courseFieldValue->saveFieldValues($course);
  271. $addTeacherToSessionCourses = isset($course['add_teachers_to_sessions_courses']) && !empty($course['add_teachers_to_sessions_courses']) ? 1 : 0;
  272. // Updating teachers
  273. if ($addTeacherToSessionCourses) {
  274. // Updating session coaches
  275. $sessionCoaches = $course['session_coaches'];
  276. if (!empty($sessionCoaches)) {
  277. foreach ($sessionCoaches as $sessionId => $teacherInfo) {
  278. $coachesToSubscribe = $teacherInfo['coaches_by_session'];
  279. SessionManager::updateCoaches(
  280. $sessionId,
  281. $courseId,
  282. $coachesToSubscribe,
  283. true
  284. );
  285. }
  286. }
  287. CourseManager::updateTeachers($courseInfo, $teachers, true, true, false);
  288. } else {
  289. // Normal behaviour
  290. CourseManager::updateTeachers($courseInfo, $teachers, true, false);
  291. // Updating session coaches
  292. $sessionCoaches = isset($course['session_coaches']) ? $course['session_coaches'] : [];
  293. if (!empty($sessionCoaches)) {
  294. foreach ($sessionCoaches as $sessionId => $coachesToSubscribe) {
  295. if (!empty($coachesToSubscribe)) {
  296. SessionManager::updateCoaches(
  297. $sessionId,
  298. $courseId,
  299. $coachesToSubscribe,
  300. true
  301. );
  302. }
  303. }
  304. }
  305. }
  306. if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
  307. $sql = "UPDATE $course_table SET
  308. add_teachers_to_sessions_courses = '$addTeacherToSessionCourses'
  309. WHERE id = ".$courseInfo['real_id'];
  310. Database::query($sql);
  311. }
  312. $course_id = $courseInfo['real_id'];
  313. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  314. if ($visual_code_is_used) {
  315. Display::addFlash(Display::return_message($warn));
  316. header('Location: course_list.php');
  317. } else {
  318. header('Location: course_list.php');
  319. }
  320. exit;
  321. }
  322. Display::display_header($tool_name);
  323. echo '<div class="actions">';
  324. echo Display::url(Display::return_icon('back.png', get_lang('Back')), api_get_path(WEB_CODE_PATH).'admin/course_list.php');
  325. echo Display::url(Display::return_icon('course_home.png', get_lang('CourseHome')), $courseInfo['course_public_url'], array('target' => '_blank'));
  326. echo '</div>';
  327. echo "<script>
  328. function moveItem(origin , destination) {
  329. for (var i = 0 ; i<origin.options.length ; i++) {
  330. if (origin.options[i].selected) {
  331. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  332. origin.options[i]=null;
  333. i = i-1;
  334. }
  335. }
  336. destination.selectedIndex = -1;
  337. sortOptions(destination.options);
  338. }
  339. function sortOptions(options) {
  340. newOptions = new Array();
  341. for (i = 0 ; i<options.length ; i++) {
  342. newOptions[i] = options[i];
  343. }
  344. newOptions = newOptions.sort(mysort);
  345. options.length = 0;
  346. for (i = 0 ; i < newOptions.length ; i++) {
  347. options[i] = newOptions[i];
  348. }
  349. }
  350. function mysort(a, b) {
  351. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  352. return 1;
  353. }
  354. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  355. return -1;
  356. }
  357. return 0;
  358. }
  359. function valide() {
  360. // Checking all multiple
  361. $('select').filter(function() {
  362. if ($(this).attr('multiple')) {
  363. $(this).find('option').each(function() {
  364. $(this).attr('selected', true);
  365. });
  366. }
  367. });
  368. //document.update_course.submit();
  369. }
  370. </script>";
  371. // Display the form
  372. $form->display();
  373. Display :: display_footer();