PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/course/edit_form.php

https://bitbucket.org/geek745/moodle-db2
PHP | 492 lines | 387 code | 75 blank | 30 comment | 64 complexity | 170ddfd02ee903e823750b608ec8fda4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php //$Id: edit_form.php,v 1.37.2.19 2009/10/01 07:47:28 nicolasconnault Exp $
  2. require_once($CFG->libdir.'/formslib.php');
  3. class course_edit_form extends moodleform {
  4. function definition() {
  5. global $USER, $CFG;
  6. $courseconfig = get_config('moodlecourse');
  7. $mform =& $this->_form;
  8. $course = $this->_customdata['course'];
  9. $category = $this->_customdata['category'];
  10. $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  11. $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
  12. $disable_meta = false; // basic meta course state protection; server-side security checks not needed
  13. if (!empty($course)) {
  14. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  15. $context = $coursecontext;
  16. if (course_in_meta($course)) {
  17. $disable_meta = get_string('metaalreadyinmeta');
  18. } else if ($course->metacourse) {
  19. if (count_records('course_meta', 'parent_course', $course->id) > 0) {
  20. $disable_meta = get_string('metaalreadyhascourses');
  21. }
  22. } else {
  23. // if users already enrolled directly into coures, do not allow switching to meta,
  24. // users with metacourse manage permission are exception
  25. // please note that we do not need exact results - anything unexpected here prevents metacourse
  26. $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
  27. $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
  28. if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
  29. foreach($users as $user) {
  30. if (!isset($managers[$user->id])) {
  31. $disable_meta = get_string('metaalreadyhasenrolments');
  32. break;
  33. }
  34. }
  35. }
  36. unset($managers);
  37. unset($users);
  38. unset($enrolroles);
  39. }
  40. } else {
  41. $coursecontext = null;
  42. $context = $categorycontext;
  43. }
  44. /// form definition with new course defaults
  45. //--------------------------------------------------------------------------------
  46. $mform->addElement('header','general', get_string('general', 'form'));
  47. // Must have create course capability in both categories in order to move course
  48. if (has_capability('moodle/course:create', $categorycontext)) {
  49. $displaylist = array();
  50. $parentlist = array();
  51. make_categories_list($displaylist, $parentlist, 'moodle/course:create');
  52. $mform->addElement('select', 'category', get_string('category'), $displaylist);
  53. } else {
  54. $mform->addElement('hidden', 'category', null);
  55. $mform->setType('category', PARAM_INT);
  56. }
  57. $mform->setHelpButton('category', array('coursecategory', get_string('category')));
  58. $mform->setDefault('category', $category->id);
  59. $mform->setType('category', PARAM_INT);
  60. if ($course and !has_capability('moodle/course:changecategory', $coursecontext)) {
  61. $mform->hardFreeze('category');
  62. $mform->setConstant('category', $category->id);
  63. }
  64. $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
  65. $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
  66. $mform->setDefault('fullname', get_string('defaultcoursefullname'));
  67. $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  68. $mform->setType('fullname', PARAM_MULTILANG);
  69. if ($course 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('shortname'),'maxlength="100" size="20"');
  74. $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
  75. $mform->setDefault('shortname', get_string('defaultcourseshortname'));
  76. $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  77. $mform->setType('shortname', PARAM_MULTILANG);
  78. if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
  79. $mform->hardFreeze('shortname');
  80. $mform->setConstant('shortname', $course->shortname);
  81. }
  82. $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
  83. $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
  84. $mform->setType('idnumber', PARAM_RAW);
  85. if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
  86. $mform->hardFreeze('idnumber');
  87. $mform->setConstants('idnumber', $course->idnumber);
  88. }
  89. $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
  90. $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
  91. $mform->setType('summary', PARAM_RAW);
  92. if ($course and !has_capability('moodle/course:changesummary', $coursecontext)) {
  93. $mform->hardFreeze('summary');
  94. }
  95. $courseformats = get_list_of_plugins('course/format');
  96. $formcourseformats = array();
  97. foreach ($courseformats as $courseformat) {
  98. $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
  99. if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
  100. $formcourseformats["$courseformat"] = get_string("format$courseformat");
  101. }
  102. }
  103. $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
  104. $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
  105. $mform->setDefault('format', $courseconfig->format);
  106. for ($i=1; $i<=52; $i++) {
  107. $sectionmenu[$i] = "$i";
  108. }
  109. $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
  110. $mform->setDefault('numsections', $courseconfig->numsections);
  111. $mform->addElement('date_selector', 'startdate', get_string('startdate'));
  112. $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
  113. $mform->setDefault('startdate', time() + 3600 * 24);
  114. $choices = array();
  115. $choices['0'] = get_string('hiddensectionscollapsed');
  116. $choices['1'] = get_string('hiddensectionsinvisible');
  117. $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
  118. $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
  119. $mform->setDefault('hiddensections', $courseconfig->hiddensections);
  120. $options = range(0, 10);
  121. $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
  122. $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
  123. $mform->setDefault('newsitems', $courseconfig->newsitems);
  124. $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
  125. $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
  126. $mform->setDefault('showgrades', $courseconfig->showgrades);
  127. $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
  128. $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
  129. $mform->setDefault('showreports', $courseconfig->showreports);
  130. $choices = get_max_upload_sizes($CFG->maxbytes);
  131. $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
  132. $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
  133. $mform->setDefault('maxbytes', $courseconfig->maxbytes);
  134. if (!empty($CFG->allowcoursethemes)) {
  135. $themes=array();
  136. $themes[''] = get_string('forceno');
  137. $themes += get_list_of_themes();
  138. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  139. }
  140. $meta=array();
  141. $meta[0] = get_string('no');
  142. $meta[1] = get_string('yes');
  143. if ($disable_meta === false) {
  144. $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
  145. $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
  146. $mform->setDefault('metacourse', $courseconfig->metacourse);
  147. } else {
  148. // no metacourse element - we do not want to change it anyway!
  149. $mform->addElement('static', 'nometacourse', get_string('managemeta'),
  150. ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
  151. $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
  152. }
  153. //--------------------------------------------------------------------------------
  154. $mform->addElement('header','enrolhdr', get_string('enrolments'));
  155. $choices = array();
  156. $modules = explode(',', $CFG->enrol_plugins_enabled);
  157. foreach ($modules as $module) {
  158. $name = get_string('enrolname', "enrol_$module");
  159. $plugin = enrolment_factory::factory($module);
  160. if (method_exists($plugin, 'print_entry')) {
  161. $choices[$name] = $module;
  162. }
  163. }
  164. asort($choices);
  165. $choices = array_flip($choices);
  166. $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
  167. $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
  168. $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
  169. $roles = get_assignable_roles($context);
  170. if (!empty($course)) {
  171. // add current default role, so that it is selectable even when user can not assign it
  172. if ($current_role = get_record('role', 'id', $course->defaultrole)) {
  173. $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
  174. }
  175. }
  176. $choices = array();
  177. if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
  178. $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
  179. } else {
  180. $choices[0] = get_string('sitedefault');
  181. }
  182. $choices = $choices + $roles;
  183. // fix for MDL-9197
  184. foreach ($choices as $choiceid => $choice) {
  185. $choices[$choiceid] = format_string($choice);
  186. }
  187. $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
  188. $mform->setDefault('defaultrole', 0);
  189. $radio = array();
  190. $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
  191. $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
  192. $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
  193. $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
  194. $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
  195. $mform->setDefault('enrollable', 1);
  196. $enroldatestartgrp = array();
  197. $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
  198. $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
  199. $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
  200. $mform->setDefault('enrolstartdate', 0);
  201. $mform->setDefault('enrolstartdisabled', 1);
  202. $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
  203. $enroldateendgrp = array();
  204. $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
  205. $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
  206. $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
  207. $mform->setDefault('enrolenddate', 0);
  208. $mform->setDefault('enrolenddisabled', 1);
  209. $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
  210. $periodmenu=array();
  211. $periodmenu[0] = get_string('unlimited');
  212. for ($i=1; $i<=365; $i++) {
  213. $seconds = $i * 86400;
  214. $periodmenu[$seconds] = get_string('numdays', '', $i);
  215. }
  216. $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
  217. $mform->setDefault('enrolperiod', 0);
  218. //--------------------------------------------------------------------------------
  219. $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
  220. $choices = array();
  221. $choices['0'] = get_string('no');
  222. $choices['1'] = get_string('yes');
  223. $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
  224. $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
  225. $mform->setDefault('expirynotify', 0);
  226. $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
  227. $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
  228. $mform->setDefault('notifystudents', 0);
  229. $thresholdmenu=array();
  230. for ($i=1; $i<=30; $i++) {
  231. $seconds = $i * 86400;
  232. $thresholdmenu[$seconds] = get_string('numdays', '', $i);
  233. }
  234. $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
  235. $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
  236. $mform->setDefault('expirythreshold', 10 * 86400);
  237. //--------------------------------------------------------------------------------
  238. $mform->addElement('header','', get_string('groups', 'group'));
  239. $choices = array();
  240. $choices[NOGROUPS] = get_string('groupsnone', 'group');
  241. $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
  242. $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
  243. $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
  244. $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
  245. $mform->setDefault('groupmode', 0);
  246. $choices = array();
  247. $choices['0'] = get_string('no');
  248. $choices['1'] = get_string('yes');
  249. $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
  250. $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
  251. $mform->setDefault('groupmodeforce', 0);
  252. if (!empty($CFG->enablegroupings)) {
  253. //default groupings selector
  254. $options = array();
  255. $options[0] = get_string('none');
  256. $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
  257. }
  258. //--------------------------------------------------------------------------------
  259. $mform->addElement('header','', get_string('availability'));
  260. $choices = array();
  261. $choices['0'] = get_string('courseavailablenot');
  262. $choices['1'] = get_string('courseavailable');
  263. $mform->addElement('select', 'visible', get_string('availability'), $choices);
  264. $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
  265. $mform->setDefault('visible', 1);
  266. if ($course and !has_capability('moodle/course:visibility', $coursecontext)) {
  267. $mform->hardFreeze('visible');
  268. $mform->setConstant('visible', $course->visible);
  269. }
  270. $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
  271. $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
  272. $mform->setDefault('enrolpassword', '');
  273. $mform->setType('enrolpassword', PARAM_RAW);
  274. if (empty($course) or ($course->password !== '' and $course->id != SITEID)) {
  275. // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
  276. if (!empty($CFG->enrol_manual_requirekey)) {
  277. $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
  278. }
  279. }
  280. $choices = array();
  281. $choices['0'] = get_string('guestsno');
  282. $choices['1'] = get_string('guestsyes');
  283. $choices['2'] = get_string('guestskey');
  284. $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
  285. $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
  286. $mform->setDefault('guest', 0);
  287. // If we are creating a course, its enrol method isn't yet chosen, BUT the site has a default enrol method which we can use here
  288. $enrol_object = $CFG;
  289. if (!empty($course)) {
  290. $enrol_object = $course;
  291. }
  292. // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
  293. if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
  294. $costgroup=array();
  295. $currencies = get_list_of_currencies();
  296. $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
  297. $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
  298. $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
  299. //defining a rule for a form element within a group :
  300. $costgrprules=array();
  301. //set the message to null to tell Moodle to use a default message
  302. //available for most rules, fetched from language pack (err_{rulename}).
  303. $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
  304. $mform->addGroupRule('costgrp',$costgrprules);
  305. $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
  306. $mform->setDefault('cost', '');
  307. $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
  308. }
  309. //--------------------------------------------------------------------------------
  310. $mform->addElement('header','', get_string('language'));
  311. $languages=array();
  312. $languages[''] = get_string('forceno');
  313. $languages += get_list_of_languages();
  314. $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
  315. //--------------------------------------------------------------------------------
  316. if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
  317. $mform->addElement('header', '', get_string('restrictmodules'));
  318. $options = array();
  319. $options['0'] = get_string('no');
  320. $options['1'] = get_string('yes');
  321. $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
  322. $mods = array(0=>get_string('allownone'));
  323. $mods += get_records_menu('modules', '','','','id, name');
  324. $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
  325. array('multiple'=>'multiple', 'size'=>'10'));
  326. $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
  327. } else {
  328. $mform->addElement('hidden', 'restrictmodules', null);
  329. $mform->setType('restrictmodules', PARAM_INT);
  330. }
  331. if ($CFG->restrictmodulesfor == 'all') {
  332. $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
  333. if (!empty($CFG->restrictbydefault)) {
  334. $mform->setDefault('restrictmodules', 1);
  335. }
  336. }
  337. $mform->setType('restrictmodules', PARAM_INT);
  338. /// customizable role names in this course
  339. //--------------------------------------------------------------------------------
  340. $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
  341. $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
  342. if ($roles = get_records('role')) {
  343. foreach ($roles as $role) {
  344. $mform->addElement('text', 'role_'.$role->id, $role->name);
  345. $mform->setType('role_'.$role->id, PARAM_TEXT);
  346. if ($coursecontext) {
  347. if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
  348. $mform->setDefault('role_'.$role->id, $rolename->name);
  349. }
  350. }
  351. }
  352. }
  353. //--------------------------------------------------------------------------------
  354. $this->add_action_buttons();
  355. //--------------------------------------------------------------------------------
  356. $mform->addElement('hidden', 'id', null);
  357. $mform->setType('id', PARAM_INT);
  358. // fill in default teacher and student names to keep backwards compatibility for a while
  359. $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
  360. $mform->setType('teacher', PARAM_RAW);
  361. $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
  362. $mform->setType('teachers', PARAM_RAW);
  363. $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
  364. $mform->setType('student', PARAM_RAW);
  365. $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
  366. $mform->setType('students', PARAM_RAW);
  367. }
  368. function definition_after_data() {
  369. global $CFG;
  370. $mform =& $this->_form;
  371. // add availabe groupings
  372. if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
  373. $options = array();
  374. if ($groupings = get_records('groupings', 'courseid', $courseid)) {
  375. foreach ($groupings as $grouping) {
  376. $options[$grouping->id] = format_string($grouping->name);
  377. }
  378. }
  379. $gr_el =& $mform->getElement('defaultgroupingid');
  380. $gr_el->load($options);
  381. }
  382. }
  383. /// perform some extra moodle validation
  384. function validation($data, $files) {
  385. global $CFG;
  386. $errors = parent::validation($data, $files);
  387. if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
  388. if (!empty($data['id'])) {
  389. unset($foundcourses[$data['id']]);
  390. }
  391. if (!empty($foundcourses)) {
  392. foreach ($foundcourses as $foundcourse) {
  393. $foundcoursenames[] = $foundcourse->fullname;
  394. }
  395. $foundcoursenamestring = implode(',', $foundcoursenames);
  396. $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
  397. }
  398. }
  399. if (empty($data['enrolenddisabled'])){
  400. if ($data['enrolenddate'] <= $data['enrolstartdate']){
  401. $errors['enroldateendgrp'] = get_string('enrolenddaterror');
  402. }
  403. }
  404. if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
  405. $course = $this->_customdata['course'];
  406. if ($course->password !== $data['enrolpassword']) {
  407. // enforce password policy only if changing password - backwards compatibility
  408. $errmsg = '';
  409. if (!check_password_policy($data['enrolpassword'], $errmsg)) {
  410. $errors['enrolpassword'] = $errmsg;
  411. }
  412. }
  413. }
  414. return $errors;
  415. }
  416. }
  417. ?>