PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/course/edit_form.php

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