course_edit.php 16 KB

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