PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/course/edit_form.php

https://bitbucket.org/ngmares/moodle
PHP | 340 lines | 264 code | 55 blank | 21 comment | 35 complexity | 6fa8ee3a820e718b031951d9730ba197 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. class course_edit_form extends moodleform {
  6. protected $course;
  7. protected $context;
  8. function definition() {
  9. global $USER, $CFG, $DB;
  10. $mform = $this->_form;
  11. $course = $this->_customdata['course']; // this contains the data of this form
  12. $category = $this->_customdata['category'];
  13. $editoroptions = $this->_customdata['editoroptions'];
  14. $returnto = $this->_customdata['returnto'];
  15. $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  16. $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
  17. if (!empty($course->id)) {
  18. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  19. $context = $coursecontext;
  20. } else {
  21. $coursecontext = null;
  22. $context = $categorycontext;
  23. }
  24. $courseconfig = get_config('moodlecourse');
  25. $this->course = $course;
  26. $this->context = $context;
  27. /// form definition with new course defaults
  28. //--------------------------------------------------------------------------------
  29. $mform->addElement('header','general', get_string('general', 'form'));
  30. $mform->addElement('hidden', 'returnto', null);
  31. $mform->setType('returnto', PARAM_ALPHANUM);
  32. $mform->setConstant('returnto', $returnto);
  33. // verify permissions to change course category or keep current
  34. if (empty($course->id)) {
  35. if (has_capability('moodle/course:create', $categorycontext)) {
  36. $displaylist = array();
  37. $parentlist = array();
  38. make_categories_list($displaylist, $parentlist, 'moodle/course:create');
  39. $mform->addElement('select', 'category', get_string('category'), $displaylist);
  40. $mform->addHelpButton('category', 'category');
  41. $mform->setDefault('category', $category->id);
  42. } else {
  43. $mform->addElement('hidden', 'category', null);
  44. $mform->setType('category', PARAM_INT);
  45. $mform->setConstant('category', $category->id);
  46. }
  47. } else {
  48. if (has_capability('moodle/course:changecategory', $coursecontext)) {
  49. $displaylist = array();
  50. $parentlist = array();
  51. make_categories_list($displaylist, $parentlist, 'moodle/course:create');
  52. if (!isset($displaylist[$course->category])) {
  53. //always keep current
  54. $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
  55. }
  56. $mform->addElement('select', 'category', get_string('category'), $displaylist);
  57. $mform->addHelpButton('category', 'category');
  58. } else {
  59. //keep current
  60. $mform->addElement('hidden', 'category', null);
  61. $mform->setType('category', PARAM_INT);
  62. $mform->setConstant('category', $course->category);
  63. }
  64. }
  65. $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
  66. $mform->addHelpButton('fullname', 'fullnamecourse');
  67. $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  68. $mform->setType('fullname', PARAM_MULTILANG);
  69. if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
  70. $mform->hardFreeze('fullname');
  71. $mform->setConstant('fullname', $course->fullname);
  72. }
  73. $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
  74. $mform->addHelpButton('shortname', 'shortnamecourse');
  75. $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  76. $mform->setType('shortname', PARAM_MULTILANG);
  77. if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
  78. $mform->hardFreeze('shortname');
  79. $mform->setConstant('shortname', $course->shortname);
  80. }
  81. $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
  82. $mform->addHelpButton('idnumber', 'idnumbercourse');
  83. $mform->setType('idnumber', PARAM_RAW);
  84. if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
  85. $mform->hardFreeze('idnumber');
  86. $mform->setConstants('idnumber', $course->idnumber);
  87. }
  88. $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
  89. $mform->addHelpButton('summary_editor', 'coursesummary');
  90. $mform->setType('summary_editor', PARAM_RAW);
  91. if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
  92. $mform->hardFreeze('summary_editor');
  93. }
  94. $courseformats = get_plugin_list('format');
  95. $formcourseformats = array();
  96. foreach ($courseformats as $courseformat => $formatdir) {
  97. $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
  98. }
  99. $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
  100. $mform->addHelpButton('format', 'format');
  101. $mform->setDefault('format', $courseconfig->format);
  102. $mform->addElement('select', 'coursedisplay', get_string('coursedisplay'),
  103. array(COURSE_DISPLAY_SINGLEPAGE => get_string('coursedisplay_single'),
  104. COURSE_DISPLAY_MULTIPAGE => get_string('coursedisplay_multi')));
  105. $mform->addHelpButton('coursedisplay', 'coursedisplay');
  106. $mform->setDefault('coursedisplay', COURSE_DISPLAY_SINGLEPAGE);
  107. for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
  108. $sectionmenu[$i] = "$i";
  109. }
  110. $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
  111. $mform->setDefault('numsections', $courseconfig->numsections);
  112. $mform->addElement('date_selector', 'startdate', get_string('startdate'));
  113. $mform->addHelpButton('startdate', 'startdate');
  114. $mform->setDefault('startdate', time() + 3600 * 24);
  115. $choices = array();
  116. $choices['0'] = get_string('hiddensectionscollapsed');
  117. $choices['1'] = get_string('hiddensectionsinvisible');
  118. $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
  119. $mform->addHelpButton('hiddensections', 'hiddensections');
  120. $mform->setDefault('hiddensections', $courseconfig->hiddensections);
  121. $options = range(0, 10);
  122. $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
  123. $mform->addHelpButton('newsitems', 'newsitemsnumber');
  124. $mform->setDefault('newsitems', $courseconfig->newsitems);
  125. $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
  126. $mform->addHelpButton('showgrades', 'showgrades');
  127. $mform->setDefault('showgrades', $courseconfig->showgrades);
  128. $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
  129. $mform->addHelpButton('showreports', 'showreports');
  130. $mform->setDefault('showreports', $courseconfig->showreports);
  131. $choices = get_max_upload_sizes($CFG->maxbytes);
  132. $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
  133. $mform->addHelpButton('maxbytes', 'maximumupload');
  134. $mform->setDefault('maxbytes', $courseconfig->maxbytes);
  135. if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
  136. if (empty($course->legacyfiles)) {
  137. //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
  138. $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
  139. } else {
  140. $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
  141. }
  142. $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
  143. $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
  144. if (!isset($courseconfig->legacyfiles)) {
  145. // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
  146. $courseconfig->legacyfiles = 0;
  147. }
  148. $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
  149. }
  150. if (!empty($CFG->allowcoursethemes)) {
  151. $themeobjects = get_list_of_themes();
  152. $themes=array();
  153. $themes[''] = get_string('forceno');
  154. foreach ($themeobjects as $key=>$theme) {
  155. if (empty($theme->hidefromselector)) {
  156. $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
  157. }
  158. }
  159. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  160. }
  161. //--------------------------------------------------------------------------------
  162. enrol_course_edit_form($mform, $course, $context);
  163. //--------------------------------------------------------------------------------
  164. $mform->addElement('header','', get_string('groups', 'group'));
  165. $choices = array();
  166. $choices[NOGROUPS] = get_string('groupsnone', 'group');
  167. $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
  168. $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
  169. $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
  170. $mform->addHelpButton('groupmode', 'groupmode', 'group');
  171. $mform->setDefault('groupmode', $courseconfig->groupmode);
  172. $choices = array();
  173. $choices['0'] = get_string('no');
  174. $choices['1'] = get_string('yes');
  175. $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
  176. $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
  177. $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
  178. //default groupings selector
  179. $options = array();
  180. $options[0] = get_string('none');
  181. $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
  182. //--------------------------------------------------------------------------------
  183. $mform->addElement('header','', get_string('availability'));
  184. $choices = array();
  185. $choices['0'] = get_string('courseavailablenot');
  186. $choices['1'] = get_string('courseavailable');
  187. $mform->addElement('select', 'visible', get_string('availability'), $choices);
  188. $mform->addHelpButton('visible', 'availability');
  189. $mform->setDefault('visible', $courseconfig->visible);
  190. if (!has_capability('moodle/course:visibility', $context)) {
  191. $mform->hardFreeze('visible');
  192. if (!empty($course->id)) {
  193. $mform->setConstant('visible', $course->visible);
  194. } else {
  195. $mform->setConstant('visible', $category->visible);
  196. }
  197. }
  198. //--------------------------------------------------------------------------------
  199. $mform->addElement('header','', get_string('language'));
  200. $languages=array();
  201. $languages[''] = get_string('forceno');
  202. $languages += get_string_manager()->get_list_of_translations();
  203. $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
  204. $mform->setDefault('lang', $courseconfig->lang);
  205. //--------------------------------------------------------------------------------
  206. if (completion_info::is_enabled_for_site()) {
  207. $mform->addElement('header','', get_string('progress','completion'));
  208. $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
  209. array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
  210. $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
  211. $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
  212. $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
  213. $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
  214. } else {
  215. $mform->addElement('hidden', 'enablecompletion');
  216. $mform->setType('enablecompletion', PARAM_INT);
  217. $mform->setDefault('enablecompletion',0);
  218. $mform->addElement('hidden', 'completionstartonenrol');
  219. $mform->setType('completionstartonenrol', PARAM_INT);
  220. $mform->setDefault('completionstartonenrol',0);
  221. }
  222. /// customizable role names in this course
  223. //--------------------------------------------------------------------------------
  224. $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
  225. $mform->addHelpButton('rolerenaming', 'rolerenaming');
  226. if ($roles = get_all_roles()) {
  227. if ($coursecontext) {
  228. $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
  229. }
  230. $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
  231. foreach ($roles as $role) {
  232. $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
  233. if (isset($role->localname)) {
  234. $mform->setDefault('role_'.$role->id, $role->localname);
  235. }
  236. $mform->setType('role_'.$role->id, PARAM_TEXT);
  237. if (!in_array($role->id, $assignableroles)) {
  238. $mform->setAdvanced('role_'.$role->id);
  239. }
  240. }
  241. }
  242. //--------------------------------------------------------------------------------
  243. $this->add_action_buttons();
  244. //--------------------------------------------------------------------------------
  245. $mform->addElement('hidden', 'id', null);
  246. $mform->setType('id', PARAM_INT);
  247. /// finally set the current form data
  248. //--------------------------------------------------------------------------------
  249. $this->set_data($course);
  250. }
  251. function definition_after_data() {
  252. global $DB;
  253. $mform = $this->_form;
  254. // add available groupings
  255. if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
  256. $options = array();
  257. if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
  258. foreach ($groupings as $grouping) {
  259. $options[$grouping->id] = format_string($grouping->name);
  260. }
  261. }
  262. $gr_el =& $mform->getElement('defaultgroupingid');
  263. $gr_el->load($options);
  264. }
  265. }
  266. /// perform some extra moodle validation
  267. function validation($data, $files) {
  268. global $DB, $CFG;
  269. $errors = parent::validation($data, $files);
  270. if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
  271. if (!empty($data['id'])) {
  272. unset($foundcourses[$data['id']]);
  273. }
  274. if (!empty($foundcourses)) {
  275. foreach ($foundcourses as $foundcourse) {
  276. $foundcoursenames[] = $foundcourse->fullname;
  277. }
  278. $foundcoursenamestring = implode(',', $foundcoursenames);
  279. $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
  280. }
  281. }
  282. $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
  283. return $errors;
  284. }
  285. }