PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/course/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 501 lines | 373 code | 76 blank | 52 comment | 71 complexity | 2e24c50b7a6af3b094b1f87c5e27c536 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. defined('MOODLE_INTERNAL') || die;
  3. require_once($CFG->libdir.'/formslib.php');
  4. require_once($CFG->libdir.'/completionlib.php');
  5. /**
  6. * The form for handling editing a course.
  7. */
  8. class course_edit_form extends moodleform {
  9. protected $course;
  10. protected $context;
  11. /**
  12. * Form definition.
  13. */
  14. function definition() {
  15. global $CFG, $PAGE;
  16. $mform = $this->_form;
  17. $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
  18. array(array('formid' => $mform->getAttribute('id'))));
  19. $course = $this->_customdata['course']; // this contains the data of this form
  20. $category = $this->_customdata['category'];
  21. $editoroptions = $this->_customdata['editoroptions'];
  22. $returnto = $this->_customdata['returnto'];
  23. $returnurl = $this->_customdata['returnurl'];
  24. $systemcontext = context_system::instance();
  25. $categorycontext = context_coursecat::instance($category->id);
  26. if (!empty($course->id)) {
  27. $coursecontext = context_course::instance($course->id);
  28. $context = $coursecontext;
  29. } else {
  30. $coursecontext = null;
  31. $context = $categorycontext;
  32. }
  33. $courseconfig = get_config('moodlecourse');
  34. $this->course = $course;
  35. $this->context = $context;
  36. // Form definition with new course defaults.
  37. $mform->addElement('header','general', get_string('general', 'form'));
  38. $mform->addElement('hidden', 'returnto', null);
  39. $mform->setType('returnto', PARAM_ALPHANUM);
  40. $mform->setConstant('returnto', $returnto);
  41. $mform->addElement('hidden', 'returnurl', null);
  42. $mform->setType('returnurl', PARAM_LOCALURL);
  43. $mform->setConstant('returnurl', $returnurl);
  44. $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
  45. $mform->addHelpButton('fullname', 'fullnamecourse');
  46. $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  47. $mform->setType('fullname', PARAM_TEXT);
  48. if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
  49. $mform->hardFreeze('fullname');
  50. $mform->setConstant('fullname', $course->fullname);
  51. }
  52. $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
  53. $mform->addHelpButton('shortname', 'shortnamecourse');
  54. $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  55. $mform->setType('shortname', PARAM_TEXT);
  56. if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
  57. $mform->hardFreeze('shortname');
  58. $mform->setConstant('shortname', $course->shortname);
  59. }
  60. // Verify permissions to change course category or keep current.
  61. if (empty($course->id)) {
  62. if (has_capability('moodle/course:create', $categorycontext)) {
  63. $displaylist = core_course_category::make_categories_list('moodle/course:create');
  64. $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
  65. $mform->addRule('category', null, 'required', null, 'client');
  66. $mform->addHelpButton('category', 'coursecategory');
  67. $mform->setDefault('category', $category->id);
  68. } else {
  69. $mform->addElement('hidden', 'category', null);
  70. $mform->setType('category', PARAM_INT);
  71. $mform->setConstant('category', $category->id);
  72. }
  73. } else {
  74. if (has_capability('moodle/course:changecategory', $coursecontext)) {
  75. $displaylist = core_course_category::make_categories_list('moodle/course:changecategory');
  76. if (!isset($displaylist[$course->category])) {
  77. //always keep current
  78. $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
  79. ->get_formatted_name();
  80. }
  81. $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
  82. $mform->addRule('category', null, 'required', null, 'client');
  83. $mform->addHelpButton('category', 'coursecategory');
  84. } else {
  85. //keep current
  86. $mform->addElement('hidden', 'category', null);
  87. $mform->setType('category', PARAM_INT);
  88. $mform->setConstant('category', $course->category);
  89. }
  90. }
  91. $choices = array();
  92. $choices['0'] = get_string('hide');
  93. $choices['1'] = get_string('show');
  94. $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
  95. $mform->addHelpButton('visible', 'coursevisibility');
  96. $mform->setDefault('visible', $courseconfig->visible);
  97. if (!empty($course->id)) {
  98. if (!has_capability('moodle/course:visibility', $coursecontext)) {
  99. $mform->hardFreeze('visible');
  100. $mform->setConstant('visible', $course->visible);
  101. }
  102. } else {
  103. if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
  104. $mform->hardFreeze('visible');
  105. $mform->setConstant('visible', $courseconfig->visible);
  106. }
  107. }
  108. // Download course content.
  109. if ($CFG->downloadcoursecontentallowed) {
  110. $downloadchoices = [
  111. DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
  112. DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
  113. ];
  114. $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
  115. $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
  116. $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
  117. $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
  118. $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
  119. $mform->setDefault('downloadcontent', $downloadselectdefault);
  120. if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
  121. (empty($course->id) &&
  122. !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
  123. $mform->hardFreeze('downloadcontent');
  124. $mform->setConstant('downloadcontent', $downloadselectdefault);
  125. }
  126. }
  127. $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
  128. $mform->addHelpButton('startdate', 'startdate');
  129. $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
  130. $date->modify('+1 day');
  131. $mform->setDefault('startdate', $date->getTimestamp());
  132. $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
  133. $mform->addHelpButton('enddate', 'enddate');
  134. if (!empty($CFG->enablecourserelativedates)) {
  135. $attributes = [
  136. 'aria-describedby' => 'relativedatesmode_warning'
  137. ];
  138. if (!empty($course->id)) {
  139. $attributes['disabled'] = true;
  140. }
  141. $relativeoptions = [
  142. 0 => get_string('no'),
  143. 1 => get_string('yes'),
  144. ];
  145. $relativedatesmodegroup = [];
  146. $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
  147. $relativeoptions, $attributes);
  148. $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
  149. '', ['id' => 'relativedatesmode_warning']));
  150. $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
  151. $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
  152. }
  153. $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
  154. $mform->addHelpButton('idnumber', 'idnumbercourse');
  155. $mform->setType('idnumber', PARAM_RAW);
  156. if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
  157. $mform->hardFreeze('idnumber');
  158. $mform->setConstants('idnumber', $course->idnumber);
  159. }
  160. // Description.
  161. $mform->addElement('header', 'descriptionhdr', get_string('description'));
  162. $mform->setExpanded('descriptionhdr');
  163. $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
  164. $mform->addHelpButton('summary_editor', 'coursesummary');
  165. $mform->setType('summary_editor', PARAM_RAW);
  166. $summaryfields = 'summary_editor';
  167. if ($overviewfilesoptions = course_overviewfiles_options($course)) {
  168. $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
  169. $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
  170. $summaryfields .= ',overviewfiles_filemanager';
  171. }
  172. if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
  173. // Remove the description header it does not contain anything any more.
  174. $mform->removeElement('descriptionhdr');
  175. $mform->hardFreeze($summaryfields);
  176. }
  177. // Course format.
  178. $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
  179. $courseformats = get_sorted_course_formats(true);
  180. $formcourseformats = array();
  181. foreach ($courseformats as $courseformat) {
  182. $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
  183. }
  184. if (isset($course->format)) {
  185. $course->format = course_get_format($course)->get_format(); // replace with default if not found
  186. if (!in_array($course->format, $courseformats)) {
  187. // this format is disabled. Still display it in the dropdown
  188. $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
  189. get_string('pluginname', 'format_'.$course->format));
  190. }
  191. }
  192. $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
  193. $mform->addHelpButton('format', 'format');
  194. $mform->setDefault('format', $courseconfig->format);
  195. // Button to update format-specific options on format change (will be hidden by JavaScript).
  196. $mform->registerNoSubmitButton('updatecourseformat');
  197. $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
  198. // Just a placeholder for the course format options.
  199. $mform->addElement('hidden', 'addcourseformatoptionshere');
  200. $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
  201. // Appearance.
  202. $mform->addElement('header', 'appearancehdr', get_string('appearance'));
  203. if (!empty($CFG->allowcoursethemes)) {
  204. $themeobjects = get_list_of_themes();
  205. $themes=array();
  206. $themes[''] = get_string('forceno');
  207. foreach ($themeobjects as $key=>$theme) {
  208. if (empty($theme->hidefromselector)) {
  209. $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
  210. }
  211. }
  212. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  213. }
  214. $languages=array();
  215. $languages[''] = get_string('forceno');
  216. $languages += get_string_manager()->get_list_of_translations();
  217. if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
  218. || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
  219. $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
  220. $mform->setDefault('lang', $courseconfig->lang);
  221. }
  222. // Multi-Calendar Support - see MDL-18375.
  223. $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
  224. // We do not want to show this option unless there is more than one calendar type to display.
  225. if (count($calendartypes) > 1) {
  226. $calendars = array();
  227. $calendars[''] = get_string('forceno');
  228. $calendars += $calendartypes;
  229. $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
  230. }
  231. $options = range(0, 10);
  232. $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
  233. $courseconfig = get_config('moodlecourse');
  234. $mform->setDefault('newsitems', $courseconfig->newsitems);
  235. $mform->addHelpButton('newsitems', 'newsitemsnumber');
  236. $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
  237. $mform->addHelpButton('showgrades', 'showgrades');
  238. $mform->setDefault('showgrades', $courseconfig->showgrades);
  239. $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
  240. $mform->addHelpButton('showreports', 'showreports');
  241. $mform->setDefault('showreports', $courseconfig->showreports);
  242. // Show activity dates.
  243. $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
  244. $mform->addHelpButton('showactivitydates', 'showactivitydates');
  245. $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
  246. // Files and uploads.
  247. $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
  248. if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
  249. if (empty($course->legacyfiles)) {
  250. //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
  251. $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
  252. } else {
  253. $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
  254. }
  255. $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
  256. $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
  257. if (!isset($courseconfig->legacyfiles)) {
  258. // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
  259. $courseconfig->legacyfiles = 0;
  260. }
  261. $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
  262. }
  263. // Handle non-existing $course->maxbytes on course creation.
  264. $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
  265. // Let's prepare the maxbytes popup.
  266. $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
  267. $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
  268. $mform->addHelpButton('maxbytes', 'maximumupload');
  269. $mform->setDefault('maxbytes', $courseconfig->maxbytes);
  270. // Completion tracking.
  271. if (completion_info::is_enabled_for_site()) {
  272. $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
  273. $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
  274. $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
  275. $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
  276. $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
  277. $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
  278. $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
  279. $mform->setDefault('showcompletionconditions', $showcompletionconditions);
  280. $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED);
  281. } else {
  282. $mform->addElement('hidden', 'enablecompletion');
  283. $mform->setType('enablecompletion', PARAM_INT);
  284. $mform->setDefault('enablecompletion', 0);
  285. }
  286. enrol_course_edit_form($mform, $course, $context);
  287. $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
  288. $choices = array();
  289. $choices[NOGROUPS] = get_string('groupsnone', 'group');
  290. $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
  291. $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
  292. $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
  293. $mform->addHelpButton('groupmode', 'groupmode', 'group');
  294. $mform->setDefault('groupmode', $courseconfig->groupmode);
  295. $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
  296. $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
  297. $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
  298. //default groupings selector
  299. $options = array();
  300. $options[0] = get_string('none');
  301. $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
  302. if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
  303. || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
  304. // Customizable role names in this course.
  305. $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
  306. $mform->addHelpButton('rolerenaming', 'rolerenaming');
  307. if ($roles = get_all_roles()) {
  308. $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
  309. $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
  310. foreach ($roles as $role) {
  311. $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
  312. $mform->setType('role_' . $role->id, PARAM_TEXT);
  313. }
  314. }
  315. }
  316. if (core_tag_tag::is_enabled('core', 'course') &&
  317. ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
  318. || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
  319. $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  320. $mform->addElement('tags', 'tags', get_string('tags'),
  321. array('itemtype' => 'course', 'component' => 'core'));
  322. }
  323. // Add custom fields to the form.
  324. $handler = core_course\customfield\course_handler::create();
  325. $handler->set_parent_context($categorycontext); // For course handler only.
  326. $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
  327. // When two elements we need a group.
  328. $buttonarray = array();
  329. $classarray = array('class' => 'form-submit');
  330. if ($returnto !== 0) {
  331. $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
  332. }
  333. $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
  334. $buttonarray[] = &$mform->createElement('cancel');
  335. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  336. $mform->closeHeaderBefore('buttonar');
  337. $mform->addElement('hidden', 'id', null);
  338. $mform->setType('id', PARAM_INT);
  339. // Prepare custom fields data.
  340. $handler->instance_form_before_set_data($course);
  341. // Finally set the current form data
  342. $this->set_data($course);
  343. }
  344. /**
  345. * Fill in the current page data for this course.
  346. */
  347. function definition_after_data() {
  348. global $DB;
  349. $mform = $this->_form;
  350. // add available groupings
  351. $courseid = $mform->getElementValue('id');
  352. if ($courseid and $mform->elementExists('defaultgroupingid')) {
  353. $options = array();
  354. if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
  355. foreach ($groupings as $grouping) {
  356. $options[$grouping->id] = format_string($grouping->name);
  357. }
  358. }
  359. core_collator::asort($options);
  360. $gr_el =& $mform->getElement('defaultgroupingid');
  361. $gr_el->load($options);
  362. }
  363. // add course format options
  364. $formatvalue = $mform->getElementValue('format');
  365. if (is_array($formatvalue) && !empty($formatvalue)) {
  366. $params = array('format' => $formatvalue[0]);
  367. // Load the course as well if it is available, course formats may need it to work out
  368. // they preferred course end date.
  369. if ($courseid) {
  370. $params['id'] = $courseid;
  371. }
  372. $courseformat = course_get_format((object)$params);
  373. $elements = $courseformat->create_edit_form_elements($mform);
  374. for ($i = 0; $i < count($elements); $i++) {
  375. $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
  376. 'addcourseformatoptionshere');
  377. }
  378. // Remove newsitems element if format does not support news.
  379. if (!$courseformat->supports_news()) {
  380. $mform->removeElement('newsitems');
  381. }
  382. }
  383. // Tweak the form with values provided by custom fields in use.
  384. $handler = core_course\customfield\course_handler::create();
  385. $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
  386. }
  387. /**
  388. * Validation.
  389. *
  390. * @param array $data
  391. * @param array $files
  392. * @return array the errors that were found
  393. */
  394. function validation($data, $files) {
  395. global $DB;
  396. $errors = parent::validation($data, $files);
  397. // Add field validation check for duplicate shortname.
  398. if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
  399. if (empty($data['id']) || $course->id != $data['id']) {
  400. $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
  401. }
  402. }
  403. // Add field validation check for duplicate idnumber.
  404. if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
  405. if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
  406. if (empty($data['id']) || $course->id != $data['id']) {
  407. $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
  408. }
  409. }
  410. }
  411. if ($errorcode = course_validate_dates($data)) {
  412. $errors['enddate'] = get_string($errorcode, 'error');
  413. }
  414. $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
  415. $courseformat = course_get_format((object)array('format' => $data['format']));
  416. $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
  417. if (!empty($formaterrors) && is_array($formaterrors)) {
  418. $errors = array_merge($errors, $formaterrors);
  419. }
  420. // Add the custom fields validation.
  421. $handler = core_course\customfield\course_handler::create();
  422. $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
  423. return $errors;
  424. }
  425. }