PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/course/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 381 lines | 277 code | 62 blank | 42 comment | 50 complexity | 739ad6e8a378989b092ac3e9bc038a19 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. defined('MOODLE_INTERNAL') || die;
  3. require_once($CFG->libdir.'/formslib.php');
  4. require_once($CFG->libdir.'/completionlib.php');
  5. require_once($CFG->libdir. '/coursecatlib.php');
  6. /**
  7. * The form for handling editing a course.
  8. */
  9. class course_edit_form extends moodleform {
  10. protected $course;
  11. protected $context;
  12. /**
  13. * Form definition.
  14. */
  15. function definition() {
  16. global $CFG, $PAGE;
  17. $mform = $this->_form;
  18. $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
  19. array(array('formid' => $mform->getAttribute('id'))));
  20. $course = $this->_customdata['course']; // this contains the data of this form
  21. $category = $this->_customdata['category'];
  22. $editoroptions = $this->_customdata['editoroptions'];
  23. $returnto = $this->_customdata['returnto'];
  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('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
  42. $mform->addHelpButton('fullname', 'fullnamecourse');
  43. $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  44. $mform->setType('fullname', PARAM_TEXT);
  45. if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
  46. $mform->hardFreeze('fullname');
  47. $mform->setConstant('fullname', $course->fullname);
  48. }
  49. $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
  50. $mform->addHelpButton('shortname', 'shortnamecourse');
  51. $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  52. $mform->setType('shortname', PARAM_TEXT);
  53. if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
  54. $mform->hardFreeze('shortname');
  55. $mform->setConstant('shortname', $course->shortname);
  56. }
  57. // Verify permissions to change course category or keep current.
  58. if (empty($course->id)) {
  59. if (has_capability('moodle/course:create', $categorycontext)) {
  60. $displaylist = coursecat::make_categories_list('moodle/course:create');
  61. $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
  62. $mform->addHelpButton('category', 'coursecategory');
  63. $mform->setDefault('category', $category->id);
  64. } else {
  65. $mform->addElement('hidden', 'category', null);
  66. $mform->setType('category', PARAM_INT);
  67. $mform->setConstant('category', $category->id);
  68. }
  69. } else {
  70. if (has_capability('moodle/course:changecategory', $coursecontext)) {
  71. $displaylist = coursecat::make_categories_list('moodle/course:create');
  72. if (!isset($displaylist[$course->category])) {
  73. //always keep current
  74. $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
  75. }
  76. $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
  77. $mform->addHelpButton('category', 'coursecategory');
  78. } else {
  79. //keep current
  80. $mform->addElement('hidden', 'category', null);
  81. $mform->setType('category', PARAM_INT);
  82. $mform->setConstant('category', $course->category);
  83. }
  84. }
  85. $choices = array();
  86. $choices['0'] = get_string('hide');
  87. $choices['1'] = get_string('show');
  88. $mform->addElement('select', 'visible', get_string('visible'), $choices);
  89. $mform->addHelpButton('visible', 'visible');
  90. $mform->setDefault('visible', $courseconfig->visible);
  91. if (!empty($course->id)) {
  92. if (!has_capability('moodle/course:visibility', $coursecontext)) {
  93. $mform->hardFreeze('visible');
  94. $mform->setConstant('visible', $course->visible);
  95. }
  96. } else {
  97. if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
  98. $mform->hardFreeze('visible');
  99. $mform->setConstant('visible', $courseconfig->visible);
  100. }
  101. }
  102. $mform->addElement('date_selector', 'startdate', get_string('startdate'));
  103. $mform->addHelpButton('startdate', 'startdate');
  104. $mform->setDefault('startdate', time() + 3600 * 24);
  105. $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
  106. $mform->addHelpButton('idnumber', 'idnumbercourse');
  107. $mform->setType('idnumber', PARAM_RAW);
  108. if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
  109. $mform->hardFreeze('idnumber');
  110. $mform->setConstants('idnumber', $course->idnumber);
  111. }
  112. // Description.
  113. $mform->addElement('header', 'descriptionhdr', get_string('description'));
  114. $mform->setExpanded('descriptionhdr');
  115. $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
  116. $mform->addHelpButton('summary_editor', 'coursesummary');
  117. $mform->setType('summary_editor', PARAM_RAW);
  118. $summaryfields = 'summary_editor';
  119. if ($overviewfilesoptions = course_overviewfiles_options($course)) {
  120. $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
  121. $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
  122. $summaryfields .= ',overviewfiles_filemanager';
  123. }
  124. if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
  125. // Remove the description header it does not contain anything any more.
  126. $mform->removeElement('descriptionhdr');
  127. $mform->hardFreeze($summaryfields);
  128. }
  129. // Course format.
  130. $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
  131. $courseformats = get_sorted_course_formats(true);
  132. $formcourseformats = array();
  133. foreach ($courseformats as $courseformat) {
  134. $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
  135. }
  136. if (isset($course->format)) {
  137. $course->format = course_get_format($course)->get_format(); // replace with default if not found
  138. if (!in_array($course->format, $courseformats)) {
  139. // this format is disabled. Still display it in the dropdown
  140. $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
  141. get_string('pluginname', 'format_'.$course->format));
  142. }
  143. }
  144. $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
  145. $mform->addHelpButton('format', 'format');
  146. $mform->setDefault('format', $courseconfig->format);
  147. // Button to update format-specific options on format change (will be hidden by JavaScript).
  148. $mform->registerNoSubmitButton('updatecourseformat');
  149. $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
  150. // Just a placeholder for the course format options.
  151. $mform->addElement('hidden', 'addcourseformatoptionshere');
  152. $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
  153. // Appearance.
  154. $mform->addElement('header', 'appearancehdr', get_string('appearance'));
  155. if (!empty($CFG->allowcoursethemes)) {
  156. $themeobjects = get_list_of_themes();
  157. $themes=array();
  158. $themes[''] = get_string('forceno');
  159. foreach ($themeobjects as $key=>$theme) {
  160. if (empty($theme->hidefromselector)) {
  161. $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
  162. }
  163. }
  164. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  165. }
  166. $languages=array();
  167. $languages[''] = get_string('forceno');
  168. $languages += get_string_manager()->get_list_of_translations();
  169. $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
  170. $mform->setDefault('lang', $courseconfig->lang);
  171. // Multi-Calendar Support - see MDL-18375.
  172. $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
  173. // We do not want to show this option unless there is more than one calendar type to display.
  174. if (count($calendartypes) > 1) {
  175. $calendars = array();
  176. $calendars[''] = get_string('forceno');
  177. $calendars += $calendartypes;
  178. $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
  179. }
  180. $options = range(0, 10);
  181. $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
  182. $mform->addHelpButton('newsitems', 'newsitemsnumber');
  183. $mform->setDefault('newsitems', $courseconfig->newsitems);
  184. $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
  185. $mform->addHelpButton('showgrades', 'showgrades');
  186. $mform->setDefault('showgrades', $courseconfig->showgrades);
  187. $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
  188. $mform->addHelpButton('showreports', 'showreports');
  189. $mform->setDefault('showreports', $courseconfig->showreports);
  190. // Files and uploads.
  191. $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
  192. if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
  193. if (empty($course->legacyfiles)) {
  194. //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
  195. $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
  196. } else {
  197. $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
  198. }
  199. $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
  200. $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
  201. if (!isset($courseconfig->legacyfiles)) {
  202. // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
  203. $courseconfig->legacyfiles = 0;
  204. }
  205. $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
  206. }
  207. // Handle non-existing $course->maxbytes on course creation.
  208. $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
  209. // Let's prepare the maxbytes popup.
  210. $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
  211. $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
  212. $mform->addHelpButton('maxbytes', 'maximumupload');
  213. $mform->setDefault('maxbytes', $courseconfig->maxbytes);
  214. // Completion tracking.
  215. if (completion_info::is_enabled_for_site()) {
  216. $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
  217. $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
  218. $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
  219. $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
  220. } else {
  221. $mform->addElement('hidden', 'enablecompletion');
  222. $mform->setType('enablecompletion', PARAM_INT);
  223. $mform->setDefault('enablecompletion', 0);
  224. }
  225. enrol_course_edit_form($mform, $course, $context);
  226. $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
  227. $choices = array();
  228. $choices[NOGROUPS] = get_string('groupsnone', 'group');
  229. $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
  230. $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
  231. $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
  232. $mform->addHelpButton('groupmode', 'groupmode', 'group');
  233. $mform->setDefault('groupmode', $courseconfig->groupmode);
  234. $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
  235. $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
  236. $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
  237. //default groupings selector
  238. $options = array();
  239. $options[0] = get_string('none');
  240. $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
  241. // Customizable role names in this course.
  242. $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
  243. $mform->addHelpButton('rolerenaming', 'rolerenaming');
  244. if ($roles = get_all_roles()) {
  245. $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
  246. $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
  247. foreach ($roles as $role) {
  248. $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
  249. $mform->setType('role_'.$role->id, PARAM_TEXT);
  250. }
  251. }
  252. $this->add_action_buttons();
  253. $mform->addElement('hidden', 'id', null);
  254. $mform->setType('id', PARAM_INT);
  255. // Finally set the current form data
  256. $this->set_data($course);
  257. }
  258. /**
  259. * Fill in the current page data for this course.
  260. */
  261. function definition_after_data() {
  262. global $DB;
  263. $mform = $this->_form;
  264. // add available groupings
  265. if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
  266. $options = array();
  267. if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
  268. foreach ($groupings as $grouping) {
  269. $options[$grouping->id] = format_string($grouping->name);
  270. }
  271. }
  272. core_collator::asort($options);
  273. $gr_el =& $mform->getElement('defaultgroupingid');
  274. $gr_el->load($options);
  275. }
  276. // add course format options
  277. $formatvalue = $mform->getElementValue('format');
  278. if (is_array($formatvalue) && !empty($formatvalue)) {
  279. $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
  280. $elements = $courseformat->create_edit_form_elements($mform);
  281. for ($i = 0; $i < count($elements); $i++) {
  282. $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
  283. 'addcourseformatoptionshere');
  284. }
  285. }
  286. }
  287. /**
  288. * Validation.
  289. *
  290. * @param array $data
  291. * @param array $files
  292. * @return array the errors that were found
  293. */
  294. function validation($data, $files) {
  295. global $DB;
  296. $errors = parent::validation($data, $files);
  297. // Add field validation check for duplicate shortname.
  298. if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
  299. if (empty($data['id']) || $course->id != $data['id']) {
  300. $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
  301. }
  302. }
  303. // Add field validation check for duplicate idnumber.
  304. if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
  305. if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
  306. if (empty($data['id']) || $course->id != $data['id']) {
  307. $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
  308. }
  309. }
  310. }
  311. $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
  312. $courseformat = course_get_format((object)array('format' => $data['format']));
  313. $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
  314. if (!empty($formaterrors) && is_array($formaterrors)) {
  315. $errors = array_merge($errors, $formaterrors);
  316. }
  317. return $errors;
  318. }
  319. }