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