Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]
1 <?php 2 3 defined('MOODLE_INTERNAL') || die; 4 5 require_once($CFG->libdir.'/formslib.php'); 6 require_once($CFG->libdir.'/completionlib.php'); 7 require_once($CFG->libdir . '/pdflib.php'); 8 9 /** 10 * The form for handling editing a course. 11 */ 12 class course_edit_form extends moodleform { 13 protected $course; 14 protected $context; 15 16 /** 17 * Form definition. 18 */ 19 function definition() { 20 global $CFG, $PAGE; 21 22 $mform = $this->_form; 23 $PAGE->requires->js_call_amd('core_course/formatchooser', 'init'); 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 'data-formatchooser-field' => 'selector', 225 ]); 226 $mform->addHelpButton('format', 'format'); 227 $mform->setDefault('format', $courseconfig->format); 228 229 // Button to update format-specific options on format change (will be hidden by JavaScript). 230 $mform->registerNoSubmitButton('updatecourseformat'); 231 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'), [ 232 'data-formatchooser-field' => 'updateButton', 233 'class' => 'd-none', 234 ]); 235 236 // Just a placeholder for the course format options. 237 $mform->addElement('hidden', 'addcourseformatoptionshere'); 238 $mform->setType('addcourseformatoptionshere', PARAM_BOOL); 239 240 // Appearance. 241 $mform->addElement('header', 'appearancehdr', get_string('appearance')); 242 243 if (!empty($CFG->allowcoursethemes)) { 244 $themeobjects = get_list_of_themes(); 245 $themes=array(); 246 $themes[''] = get_string('forceno'); 247 foreach ($themeobjects as $key=>$theme) { 248 if (empty($theme->hidefromselector)) { 249 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name); 250 } 251 } 252 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); 253 } 254 255 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext)) 256 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) { 257 258 $languages = ['' => get_string('forceno')]; 259 $languages += get_string_manager()->get_list_of_translations(); 260 261 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages); 262 $mform->setDefault('lang', $courseconfig->lang); 263 } 264 265 // Multi-Calendar Support - see MDL-18375. 266 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types(); 267 // We do not want to show this option unless there is more than one calendar type to display. 268 if (count($calendartypes) > 1) { 269 $calendars = array(); 270 $calendars[''] = get_string('forceno'); 271 $calendars += $calendartypes; 272 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars); 273 } 274 275 $options = range(0, 10); 276 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options); 277 $courseconfig = get_config('moodlecourse'); 278 $mform->setDefault('newsitems', $courseconfig->newsitems); 279 $mform->addHelpButton('newsitems', 'newsitemsnumber'); 280 281 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades')); 282 $mform->addHelpButton('showgrades', 'showgrades'); 283 $mform->setDefault('showgrades', $courseconfig->showgrades); 284 285 $mform->addElement('selectyesno', 'showreports', get_string('showreports')); 286 $mform->addHelpButton('showreports', 'showreports'); 287 $mform->setDefault('showreports', $courseconfig->showreports); 288 289 // Show activity dates. 290 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates')); 291 $mform->addHelpButton('showactivitydates', 'showactivitydates'); 292 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates); 293 294 // Files and uploads. 295 $mform->addElement('header', 'filehdr', get_string('filesanduploads')); 296 297 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) { 298 if (empty($course->legacyfiles)) { 299 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet 300 $choices = array('0'=>get_string('no'), '2'=>get_string('yes')); 301 } else { 302 $choices = array('1'=>get_string('no'), '2'=>get_string('yes')); 303 } 304 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices); 305 $mform->addHelpButton('legacyfiles', 'courselegacyfiles'); 306 if (!isset($courseconfig->legacyfiles)) { 307 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses 308 $courseconfig->legacyfiles = 0; 309 } 310 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles); 311 } 312 313 // Handle non-existing $course->maxbytes on course creation. 314 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes; 315 316 // Let's prepare the maxbytes popup. 317 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes); 318 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices); 319 $mform->addHelpButton('maxbytes', 'maximumupload'); 320 $mform->setDefault('maxbytes', $courseconfig->maxbytes); 321 322 // PDF font. 323 if (!empty($CFG->enablepdfexportfont)) { 324 $pdf = new \pdf; 325 $fontlist = $pdf->get_export_fontlist(); 326 // Show the option if the font is defined more than one. 327 if (count($fontlist) > 1) { 328 $defaultfont = $courseconfig->pdfexportfont ?? 'freesans'; 329 if (empty($fontlist[$defaultfont])) { 330 $defaultfont = current($fontlist); 331 } 332 $mform->addElement('select', 'pdfexportfont', get_string('pdfexportfont', 'course'), $fontlist); 333 $mform->addHelpButton('pdfexportfont', 'pdfexportfont', 'course'); 334 $mform->setDefault('pdfexportfont', $defaultfont); 335 } 336 } 337 338 // Completion tracking. 339 if (completion_info::is_enabled_for_site()) { 340 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion')); 341 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion')); 342 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion); 343 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion'); 344 345 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS; 346 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion')); 347 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion'); 348 $mform->setDefault('showcompletionconditions', $showcompletionconditions); 349 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED); 350 } else { 351 $mform->addElement('hidden', 'enablecompletion'); 352 $mform->setType('enablecompletion', PARAM_INT); 353 $mform->setDefault('enablecompletion', 0); 354 } 355 356 enrol_course_edit_form($mform, $course, $context); 357 358 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group')); 359 360 $choices = array(); 361 $choices[NOGROUPS] = get_string('groupsnone', 'group'); 362 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group'); 363 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group'); 364 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices); 365 $mform->addHelpButton('groupmode', 'groupmode', 'group'); 366 $mform->setDefault('groupmode', $courseconfig->groupmode); 367 368 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group')); 369 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group'); 370 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce); 371 372 //default groupings selector 373 $options = array(); 374 $options[0] = get_string('none'); 375 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options); 376 377 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext)) 378 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) { 379 // Customizable role names in this course. 380 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming')); 381 $mform->addHelpButton('rolerenaming', 'rolerenaming'); 382 383 if ($roles = get_all_roles()) { 384 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL); 385 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE); 386 foreach ($roles as $role) { 387 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname)); 388 $mform->setType('role_' . $role->id, PARAM_TEXT); 389 } 390 } 391 } 392 393 if (core_tag_tag::is_enabled('core', 'course') && 394 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext)) 395 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) { 396 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); 397 $mform->addElement('tags', 'tags', get_string('tags'), 398 array('itemtype' => 'course', 'component' => 'core')); 399 } 400 401 // Add custom fields to the form. 402 $handler = core_course\customfield\course_handler::create(); 403 $handler->set_parent_context($categorycontext); // For course handler only. 404 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id); 405 406 // When two elements we need a group. 407 $buttonarray = array(); 408 $classarray = array('class' => 'form-submit'); 409 if ($returnto !== 0) { 410 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray); 411 } 412 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray); 413 $buttonarray[] = &$mform->createElement('cancel'); 414 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 415 $mform->closeHeaderBefore('buttonar'); 416 417 $mform->addElement('hidden', 'id', null); 418 $mform->setType('id', PARAM_INT); 419 420 // Prepare custom fields data. 421 $handler->instance_form_before_set_data($course); 422 // Finally set the current form data 423 $this->set_data($course); 424 } 425 426 /** 427 * Fill in the current page data for this course. 428 */ 429 function definition_after_data() { 430 global $DB; 431 432 $mform = $this->_form; 433 434 // add available groupings 435 $courseid = $mform->getElementValue('id'); 436 if ($courseid and $mform->elementExists('defaultgroupingid')) { 437 $options = array(); 438 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) { 439 foreach ($groupings as $grouping) { 440 $options[$grouping->id] = format_string($grouping->name); 441 } 442 } 443 core_collator::asort($options); 444 $gr_el =& $mform->getElement('defaultgroupingid'); 445 $gr_el->load($options); 446 } 447 448 // add course format options 449 $formatvalue = $mform->getElementValue('format'); 450 if (is_array($formatvalue) && !empty($formatvalue)) { 451 452 $params = array('format' => $formatvalue[0]); 453 // Load the course as well if it is available, course formats may need it to work out 454 // they preferred course end date. 455 if ($courseid) { 456 $params['id'] = $courseid; 457 } 458 $courseformat = course_get_format((object)$params); 459 460 $elements = $courseformat->create_edit_form_elements($mform); 461 for ($i = 0; $i < count($elements); $i++) { 462 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false), 463 'addcourseformatoptionshere'); 464 } 465 466 // Remove newsitems element if format does not support news. 467 if (!$courseformat->supports_news()) { 468 $mform->removeElement('newsitems'); 469 } 470 } 471 472 // Tweak the form with values provided by custom fields in use. 473 $handler = core_course\customfield\course_handler::create(); 474 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid); 475 } 476 477 /** 478 * Validation. 479 * 480 * @param array $data 481 * @param array $files 482 * @return array the errors that were found 483 */ 484 function validation($data, $files) { 485 global $DB; 486 487 $errors = parent::validation($data, $files); 488 489 // Add field validation check for duplicate shortname. 490 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) { 491 if (empty($data['id']) || $course->id != $data['id']) { 492 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname); 493 } 494 } 495 496 // Add field validation check for duplicate idnumber. 497 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) { 498 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) { 499 if (empty($data['id']) || $course->id != $data['id']) { 500 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname); 501 } 502 } 503 } 504 505 if ($errorcode = course_validate_dates($data)) { 506 $errors['enddate'] = get_string($errorcode, 'error'); 507 } 508 509 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context)); 510 511 $courseformat = course_get_format((object)array('format' => $data['format'])); 512 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors); 513 if (!empty($formaterrors) && is_array($formaterrors)) { 514 $errors = array_merge($errors, $formaterrors); 515 } 516 517 // Add the custom fields validation. 518 $handler = core_course\customfield\course_handler::create(); 519 $errors = array_merge($errors, $handler->instance_form_validation($data, $files)); 520 521 return $errors; 522 } 523 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body