course_edit.php 18 KB

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