MDL-81035 core: Fix redirect with sub-path
[moodle.git] / course / editsection_form.php
blob4575dc1e9604f62c20e256e2ca14285bdb1939e7
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->libdir.'/formslib.php');
8 require_once($CFG->libdir.'/filelib.php');
9 require_once($CFG->libdir.'/completionlib.php');
10 require_once($CFG->libdir.'/gradelib.php');
12 /**
13 * Default form for editing course section
15 * Course format plugins may specify different editing form to use
17 class editsection_form extends moodleform {
19 function definition() {
20 global $CFG, $OUTPUT;
22 $mform = $this->_form;
23 $course = $this->_customdata['course'];
24 $sectioninfo = $this->_customdata['cs'];
26 $mform->addElement('header', 'generalhdr', get_string('general'));
28 $mform->addElement('defaultcustom', 'name', get_string('sectionname'), [
29 'defaultvalue' => $this->_customdata['defaultsectionname'],
30 'customvalue' => $sectioninfo->name,
31 ], ['size' => 30, 'maxlength' => 255]);
32 $mform->setDefault('name', false);
33 $mform->addGroupRule('name', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
35 /// Prepare course and the editor
37 $mform->addElement('editor', 'summary_editor', get_string('description'), null, $this->_customdata['editoroptions']);
38 $mform->setType('summary_editor', PARAM_RAW);
40 $mform->addElement('hidden', 'id');
41 $mform->setType('id', PARAM_INT);
43 // additional fields that course format has defined
44 $courseformat = course_get_format($course);
45 $formatoptions = $courseformat->section_format_options(true);
46 if (!empty($formatoptions)) {
47 $elements = $courseformat->create_edit_form_elements($mform, true);
50 if (!empty($CFG->enableavailability)) {
51 $mform->addElement('header', 'availabilityconditions',
52 get_string('restrictaccess', 'availability'));
53 $mform->setExpanded('availabilityconditions', false);
55 // Availability field. This is just a textarea; the user interface
56 // interaction is all implemented in JavaScript. The field is named
57 // availabilityconditionsjson for consistency with moodleform_mod.
58 $mform->addElement('textarea', 'availabilityconditionsjson',
59 get_string('accessrestrictions', 'availability'),
60 ['class' => 'd-none']
62 // Availability loading indicator.
63 $loadingcontainer = $OUTPUT->container(
64 $OUTPUT->render_from_template('core/loading', []),
65 'd-flex justify-content-center py-5 icon-size-5',
66 'availabilityconditions-loading'
68 $mform->addElement('html', $loadingcontainer);
71 $mform->_registerCancelButton('cancel');
74 public function definition_after_data() {
75 global $CFG;
77 $mform = $this->_form;
78 $course = $this->_customdata['course'];
80 if (!empty($CFG->enableavailability)) {
81 \core_availability\frontend::include_all_javascript($course, null,
82 $this->_customdata['cs']);
85 $this->add_action_buttons();
88 /**
89 * Load in existing data as form defaults
91 * @param stdClass|array $default_values object or array of default values
93 function set_data($default_values) {
94 if (!is_object($default_values)) {
95 // we need object for file_prepare_standard_editor
96 $default_values = (object)$default_values;
98 $editoroptions = $this->_customdata['editoroptions'];
99 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
100 $editoroptions['context'], 'course', 'section', $default_values->id);
101 if (strval($default_values->name) === '') {
102 $default_values->name = false;
104 parent::set_data($default_values);
108 * Return submitted data if properly submitted or returns NULL if validation fails or
109 * if there is no submitted data.
111 * @return object submitted data; NULL if not valid or not submitted or cancelled
113 function get_data() {
114 $data = parent::get_data();
115 if ($data !== null) {
116 $editoroptions = $this->_customdata['editoroptions'];
117 // Set name as an empty string if use default section name is checked.
118 if ($data->name === false) {
119 $data->name = '';
121 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
122 $editoroptions['context'], 'course', 'section', $data->id);
123 $course = $this->_customdata['course'];
124 foreach (course_get_format($course)->section_format_options() as $option => $unused) {
125 // fix issue with unset checkboxes not being returned at all
126 if (!isset($data->$option)) {
127 $data->$option = null;
131 return $data;
134 public function validation($data, $files) {
135 global $CFG;
136 $errors = array();
138 // Availability: Check availability field does not have errors.
139 if (!empty($CFG->enableavailability)) {
140 \core_availability\frontend::report_validation_errors($data, $errors);
143 return $errors;