course_edit.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Repository\CourseCategoryRepository;
  4. use Chamilo\CoreBundle\Entity\CourseCategory;
  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[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  30. $interbreadcrumb[] = array("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 = array();
  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 = array();
  67. $allTeachers = array();
  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. array(get_lang('CourseCode'), get_lang('ThisValueCantBeChanged'))
  106. );
  107. $element->freeze();
  108. // Visual code
  109. $form->addText(
  110. 'visual_code',
  111. array(
  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($urlId);
  126. if ($countCategories >= 100) {
  127. // Category code
  128. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  129. $categorySelect = $form->addElement(
  130. 'select_ajax',
  131. 'category_code',
  132. get_lang('CourseFaculty'),
  133. null,
  134. ['url' => $url]
  135. );
  136. if (!empty($courseInfo['categoryCode'])) {
  137. $data = \CourseCategory::getCategory($courseInfo['categoryCode']);
  138. $categorySelect->addOption($data['name'], $data['code']);
  139. }
  140. } else {
  141. $courseInfo['category_code'] = $courseInfo['categoryCode'];
  142. $categories = $courseCategoriesRepo->findAllInAccessUrl($urlId);
  143. $categoriesOptions = [null => get_lang('None')];
  144. /** @var CourseCategory $category */
  145. foreach ($categories as $category) {
  146. $categoriesOptions[$category->getCode()] = (string) $category;
  147. }
  148. $form->addSelect(
  149. 'category_code',
  150. get_lang('CourseFaculty'),
  151. $categoriesOptions
  152. );
  153. }
  154. $courseTeacherNames = [];
  155. foreach ($course_teachers as $courseTeacherId) {
  156. /** @var User $courseTeacher */
  157. $courseTeacher = UserManager::getRepository()->find($courseTeacherId);
  158. $courseTeacherNames[$courseTeacher->getUserId()] = $courseTeacher->getCompleteNameWithUsername();
  159. }
  160. $form->addSelectAjax(
  161. 'course_teachers',
  162. get_lang('CourseTeachers'),
  163. $courseTeacherNames,
  164. ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=teacher_to_basis_course', 'multiple' => 'multiple']
  165. );
  166. $courseInfo['course_teachers'] = $course_teachers;
  167. if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
  168. $form->addElement(
  169. 'checkbox',
  170. 'add_teachers_to_sessions_courses',
  171. null,
  172. get_lang('TeachersWillBeAddedAsCoachInAllCourseSessions')
  173. );
  174. }
  175. $coursesInSession = SessionManager::get_session_by_course($courseInfo['real_id']);
  176. if (!empty($coursesInSession)) {
  177. foreach ($coursesInSession as $session) {
  178. $sessionId = $session['id'];
  179. $coaches = SessionManager::getCoachesByCourseSession(
  180. $sessionId,
  181. $courseInfo['real_id']
  182. );
  183. $teachers = $allTeachers;
  184. $sessionTeachers = array();
  185. foreach ($coaches as $coachId) {
  186. $userInfo = api_get_user_info($coachId);
  187. $sessionTeachers[] = $coachId;
  188. if (isset($teachers[$coachId])) {
  189. unset($teachers[$coachId]);
  190. }
  191. }
  192. $groupName = 'session_coaches['.$sessionId.']';
  193. $platformTeacherId = 'platform_teachers_by_session_'.$sessionId;
  194. $coachId = 'coaches_by_session_'.$sessionId;
  195. $platformTeacherName = 'platform_teachers_by_session';
  196. $coachName = 'coaches_by_session';
  197. $sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$sessionId;
  198. $form->addElement(
  199. 'advmultiselect',
  200. $groupName,
  201. Display::url(
  202. $session['name'],
  203. $sessionUrl,
  204. array('target' => '_blank')
  205. ).' - '.get_lang('Coaches'),
  206. $allTeachers
  207. );
  208. $courseInfo[$groupName] = $sessionTeachers;
  209. }
  210. }
  211. $form->addText('department_name', get_lang('CourseDepartment'), false, array('size' => '60'));
  212. $form->applyFilter('department_name', 'html_filter');
  213. $form->applyFilter('department_name', 'trim');
  214. $form->addText('department_url', get_lang('CourseDepartmentURL'), false, array('size' => '60'));
  215. $form->applyFilter('department_url', 'html_filter');
  216. $form->applyFilter('department_url', 'trim');
  217. $form->addSelectLanguage('course_language', get_lang('CourseLanguage'));
  218. $group = array();
  219. $group[] = $form->createElement('radio', 'visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  220. $group[] = $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  221. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  222. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  223. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  224. $form->addGroup($group, '', get_lang('CourseAccess'));
  225. $group = array();
  226. $group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  227. $group[] = $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  228. $form->addGroup($group, '', get_lang('Subscription'));
  229. $group = array();
  230. $group[] = $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  231. $group[] = $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  232. $form->addGroup($group, '', get_lang('Unsubscription'));
  233. $form->addElement('text', 'disk_quota', array(get_lang('CourseQuota'), null, get_lang('MB')));
  234. $form->addRule('disk_quota', get_lang('ThisFieldIsRequired'), 'required');
  235. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  236. // Extra fields
  237. $extra_field = new ExtraField('course');
  238. $extra = $extra_field->addElements(
  239. $form,
  240. $courseId,
  241. [],
  242. false,
  243. false,
  244. [],
  245. [],
  246. true
  247. );
  248. $htmlHeadXtra[] = '
  249. <script>
  250. $(function() {
  251. ' . $extra['jquery_ready_content'].'
  252. });
  253. </script>';
  254. $form->addButtonUpdate(get_lang('ModifyCourseInfo'));
  255. // Set some default values
  256. $courseInfo['disk_quota'] = round(DocumentManager::get_course_quota($courseInfo['code']) / 1024 / 1024, 1);
  257. $courseInfo['real_code'] = $courseInfo['code'];
  258. $courseInfo['add_teachers_to_sessions_courses'] = isset($courseInfo['add_teachers_to_sessions_courses']) ? $courseInfo['add_teachers_to_sessions_courses'] : 0;
  259. $form->setDefaults($courseInfo);
  260. // Validate form
  261. if ($form->validate()) {
  262. $course = $form->getSubmitValues();
  263. $visibility = $course['visibility'];
  264. global $_configuration;
  265. if (isset($_configuration[$urlId]) &&
  266. isset($_configuration[$urlId]['hosting_limit_active_courses']) &&
  267. $_configuration[$urlId]['hosting_limit_active_courses'] > 0
  268. ) {
  269. // Check if
  270. if ($courseInfo['visibility'] == COURSE_VISIBILITY_HIDDEN &&
  271. $visibility != $courseInfo['visibility']
  272. ) {
  273. $num = CourseManager::countActiveCourses($urlId);
  274. if ($num >= $_configuration[$urlId]['hosting_limit_active_courses']) {
  275. api_warn_hosting_contact('hosting_limit_active_courses');
  276. Display::addFlash(
  277. Display::return_message(get_lang('PortalActiveCoursesLimitReached'))
  278. );
  279. header('Location: course_list.php');
  280. exit;
  281. }
  282. }
  283. }
  284. $visual_code = $course['visual_code'];
  285. $visual_code = CourseManager::generate_course_code($visual_code);
  286. // Check if the visual code is already used by *another* course
  287. $visual_code_is_used = false;
  288. $warn = get_lang('TheFollowingCoursesAlreadyUseThisVisualCode');
  289. if (!empty($visual_code)) {
  290. $list = CourseManager::get_courses_info_from_visual_code($visual_code);
  291. foreach ($list as $course_temp) {
  292. if ($course_temp['code'] != $course_code) {
  293. $visual_code_is_used = true;
  294. $warn .= ' '.$course_temp['title'].' ('.$course_temp['code'].'),';
  295. }
  296. }
  297. $warn = substr($warn, 0, -1);
  298. }
  299. $teachers = isset($course['course_teachers']) ? $course['course_teachers'] : '';
  300. $title = $course['title'];
  301. $category_code = isset($course['category_code']) ? $course['category_code'] : '';
  302. $department_name = $course['department_name'];
  303. $department_url = $course['department_url'];
  304. $course_language = $course['course_language'];
  305. $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
  306. $disk_quota = $course['disk_quota'];
  307. $subscribe = $course['subscribe'];
  308. $unsubscribe = $course['unsubscribe'];
  309. $course['course_code'] = $course_code;
  310. if (!stristr($department_url, 'http://')) {
  311. $department_url = 'http://'.$department_url;
  312. }
  313. Database::query($sql);
  314. $title = str_replace('&amp;', '&', $title);
  315. $params = [
  316. 'course_language' => $course_language,
  317. 'title' => $title,
  318. 'category_code' => $category_code,
  319. 'visual_code' => $visual_code,
  320. 'department_name' => $department_name,
  321. 'department_url' => $department_url,
  322. 'disk_quota' => $disk_quota,
  323. 'visibility' => $visibility,
  324. 'subscribe' => $subscribe,
  325. 'unsubscribe' => $unsubscribe,
  326. ];
  327. Database::update($course_table, $params, ['id = ?' => $courseId]);
  328. // update the extra fields
  329. $courseFieldValue = new ExtraFieldValue('course');
  330. $courseFieldValue->saveFieldValues($course);
  331. $addTeacherToSessionCourses = isset($course['add_teachers_to_sessions_courses']) && !empty($course['add_teachers_to_sessions_courses']) ? 1 : 0;
  332. // Updating teachers
  333. if ($addTeacherToSessionCourses) {
  334. // Updating session coaches
  335. $sessionCoaches = $course['session_coaches'];
  336. if (!empty($sessionCoaches)) {
  337. foreach ($sessionCoaches as $sessionId => $teacherInfo) {
  338. $coachesToSubscribe = $teacherInfo['coaches_by_session'];
  339. SessionManager::updateCoaches(
  340. $sessionId,
  341. $courseId,
  342. $coachesToSubscribe,
  343. true
  344. );
  345. }
  346. }
  347. CourseManager::updateTeachers(
  348. $courseInfo,
  349. $teachers,
  350. true,
  351. true,
  352. false
  353. );
  354. } else {
  355. // Normal behaviour
  356. CourseManager::updateTeachers($courseInfo, $teachers, true, false);
  357. // Updating session coaches
  358. $sessionCoaches = isset($course['session_coaches']) ? $course['session_coaches'] : [];
  359. if (!empty($sessionCoaches)) {
  360. foreach ($sessionCoaches as $sessionId => $coachesToSubscribe) {
  361. if (!empty($coachesToSubscribe)) {
  362. SessionManager::updateCoaches(
  363. $sessionId,
  364. $courseId,
  365. $coachesToSubscribe,
  366. true
  367. );
  368. }
  369. }
  370. }
  371. }
  372. if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
  373. $sql = "UPDATE $course_table SET
  374. add_teachers_to_sessions_courses = '$addTeacherToSessionCourses'
  375. WHERE id = ".$courseInfo['real_id'];
  376. Database::query($sql);
  377. }
  378. $course_id = $courseInfo['real_id'];
  379. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  380. if ($visual_code_is_used) {
  381. Display::addFlash(Display::return_message($warn));
  382. header('Location: course_list.php');
  383. } else {
  384. header('Location: course_list.php');
  385. }
  386. exit;
  387. }
  388. Display::display_header($tool_name);
  389. echo '<div class="actions">';
  390. echo Display::url(Display::return_icon('back.png', get_lang('Back')), api_get_path(WEB_CODE_PATH).'admin/course_list.php');
  391. echo Display::url(Display::return_icon('course_home.png', get_lang('CourseHome')), $courseInfo['course_public_url'], array('target' => '_blank'));
  392. echo '</div>';
  393. echo "<script>
  394. function moveItem(origin , destination) {
  395. for (var i = 0 ; i<origin.options.length ; i++) {
  396. if (origin.options[i].selected) {
  397. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  398. origin.options[i]=null;
  399. i = i-1;
  400. }
  401. }
  402. destination.selectedIndex = -1;
  403. sortOptions(destination.options);
  404. }
  405. function sortOptions(options) {
  406. newOptions = new Array();
  407. for (i = 0 ; i<options.length ; i++) {
  408. newOptions[i] = options[i];
  409. }
  410. newOptions = newOptions.sort(mysort);
  411. options.length = 0;
  412. for (i = 0 ; i < newOptions.length ; i++) {
  413. options[i] = newOptions[i];
  414. }
  415. }
  416. function mysort(a, b) {
  417. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  418. return 1;
  419. }
  420. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. function valide() {
  426. // Checking all multiple
  427. $('select').filter(function() {
  428. if ($(this).attr('multiple')) {
  429. $(this).find('option').each(function() {
  430. $(this).attr('selected', true);
  431. });
  432. }
  433. });
  434. //document.update_course.submit();
  435. }
  436. </script>";
  437. // Display the form
  438. $form->display();
  439. Display :: display_footer();