course_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  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' => Container::getRouter()->generate('administration') , "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->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'));
  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'));
  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'));
  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. //Extra fields
  187. $extra_field = new ExtraField('course');
  188. $extra = $extra_field->addElements($form, $courseId);
  189. $htmlHeadXtra[] = '
  190. <script>
  191. $(function() {
  192. ' . $extra['jquery_ready_content'] . '
  193. });
  194. </script>';
  195. $form->addButtonUpdate(get_lang('ModifyCourseInfo'));
  196. // Set some default values
  197. $courseInfo['disk_quota'] = round(DocumentManager::get_course_quota($courseInfo['code']) / 1024 / 1024, 1);
  198. $courseInfo['real_code'] = $courseInfo['code'];
  199. $courseInfo['add_teachers_to_sessions_courses'] = isset($courseInfo['add_teachers_to_sessions_courses']) ? $courseInfo['add_teachers_to_sessions_courses'] : 0;
  200. $form->setDefaults($courseInfo);
  201. // Validate form
  202. if ($form->validate()) {
  203. $course = $form->getSubmitValues();
  204. $visibility = $course['visibility'];
  205. $visual_code = $course['visual_code'];
  206. $visual_code = CourseManager::generate_course_code($visual_code);
  207. // Check if the visual code is already used by *another* course
  208. $visual_code_is_used = false;
  209. $warn = get_lang('TheFollowingCoursesAlreadyUseThisVisualCode');
  210. if (!empty($visual_code)) {
  211. $list = CourseManager::get_courses_info_from_visual_code($visual_code);
  212. foreach ($list as $course_temp) {
  213. if ($course_temp['code'] != $course_code) {
  214. $visual_code_is_used = true;
  215. $warn .= ' ' . $course_temp['title'] . ' (' . $course_temp['code'] . '),';
  216. }
  217. }
  218. $warn = substr($warn, 0, -1);
  219. }
  220. $teachers = isset($course['course_teachers']) ? $course['course_teachers'] : '';
  221. $title = $course['title'];
  222. $category_code = isset($course['category_code']) ? $course['category_code'] : '';
  223. $department_name = $course['department_name'];
  224. $department_url = $course['department_url'];
  225. $course_language = $course['course_language'];
  226. $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
  227. $disk_quota = $course['disk_quota'];
  228. $subscribe = $course['subscribe'];
  229. $unsubscribe = isset($course['unsubscribe']) ? $course['unsubscribe'] : '';
  230. $course['course_code'] = $course_code;
  231. if (!stristr($department_url, 'http://')) {
  232. $department_url = 'http://' . $department_url;
  233. }
  234. $manager = Database::getManager();
  235. $courseObj = $manager->getRepository('ChamiloCoreBundle:Course')->find($courseId);
  236. if ($courseObj) {
  237. $url = $manager->getRepository('ChamiloCoreBundle:AccessUrl')->find(api_get_current_access_url_id());
  238. $courseObj
  239. ->setTitle($title)
  240. ->setCourseLanguage($course_language)
  241. ->setCategoryCode($category_code)
  242. ->setVisualCode($visual_code)
  243. ->setDepartmentName($department_name)
  244. ->setDepartmentUrl($department_url)
  245. ->setDiskQuota(intval($disk_quota))
  246. ->setVisibility($visibility)
  247. ->setSubscribe(intval($subscribe))
  248. ->setUnsubscribe(intval($unsubscribe))
  249. ->setCurrentUrl($url)
  250. ;
  251. $manager->persist($courseObj);
  252. $manager->flush();
  253. }
  254. // update the extra fields
  255. $courseFieldValue = new ExtraFieldValue('course');
  256. $courseFieldValue->saveFieldValues($course);
  257. $addTeacherToSessionCourses = isset($course['add_teachers_to_sessions_courses']) && !empty($course['add_teachers_to_sessions_courses']) ? 1 : 0;
  258. $sessionCoaches = isset($course['session_coaches']) ? $course['session_coaches'] : '';
  259. // Updating teachers
  260. if ($addTeacherToSessionCourses) {
  261. if (!empty($sessionCoaches)) {
  262. foreach ($sessionCoaches as $sessionId => $teacherInfo) {
  263. $coachesToSubscribe = $teacherInfo['coaches_by_session'];
  264. SessionManager::updateCoaches(
  265. $sessionId,
  266. $courseId,
  267. $coachesToSubscribe,
  268. true
  269. );
  270. }
  271. }
  272. CourseManager::updateTeachers($courseInfo, $teachers, true, true, false);
  273. } else {
  274. // Normal behaviour
  275. CourseManager::updateTeachers($courseInfo, $teachers, true, false);
  276. // Updating session coaches
  277. $sessionCoaches = $course['session_coaches'];
  278. if (!empty($sessionCoaches)) {
  279. foreach ($sessionCoaches as $sessionId => $coachesToSubscribe) {
  280. if (!empty($coachesToSubscribe)) {
  281. SessionManager::updateCoaches(
  282. $sessionId,
  283. $courseId,
  284. $coachesToSubscribe,
  285. true
  286. );
  287. }
  288. }
  289. }
  290. }
  291. if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
  292. $sql = "UPDATE $course_table SET
  293. add_teachers_to_sessions_courses = '$addTeacherToSessionCourses'
  294. WHERE id = " . $courseInfo['real_id'];
  295. Database::query($sql);
  296. }
  297. $course_id = $courseInfo['real_id'];
  298. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  299. if ($visual_code_is_used) {
  300. Display::addFlash(Display::return_message($warn));
  301. header('Location: course_list.php');
  302. } else {
  303. header('Location: course_list.php');
  304. }
  305. exit;
  306. }
  307. Display::display_header($tool_name);
  308. echo '<div class="actions">';
  309. echo Display::url(Display::return_icon('back.png', get_lang('Back')), api_get_path(WEB_CODE_PATH).'admin/course_list.php');
  310. echo Display::url(Display::return_icon('course_home.png', get_lang('CourseHome')), $courseInfo['course_public_url'], array('target' => '_blank'));
  311. echo '</div>';
  312. echo "<script>
  313. function moveItem(origin , destination) {
  314. for (var i = 0 ; i<origin.options.length ; i++) {
  315. if (origin.options[i].selected) {
  316. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  317. origin.options[i]=null;
  318. i = i-1;
  319. }
  320. }
  321. destination.selectedIndex = -1;
  322. sortOptions(destination.options);
  323. }
  324. function sortOptions(options) {
  325. newOptions = new Array();
  326. for (i = 0 ; i<options.length ; i++) {
  327. newOptions[i] = options[i];
  328. }
  329. newOptions = newOptions.sort(mysort);
  330. options.length = 0;
  331. for (i = 0 ; i < newOptions.length ; i++) {
  332. options[i] = newOptions[i];
  333. }
  334. }
  335. function mysort(a, b) {
  336. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  337. return 1;
  338. }
  339. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. function valide() {
  345. // Checking all multiple
  346. $('select').filter(function() {
  347. if ($(this).attr('multiple')) {
  348. $(this).find('option').each(function() {
  349. $(this).attr('selected', true);
  350. });
  351. }
  352. });
  353. //document.update_course.submit();
  354. }
  355. </script>";
  356. // Display the form
  357. $form->display();
  358. Display :: display_footer();