Merge branch 'MDL-81420-main' of https://github.com/sarjona/moodle
[moodle.git] / course / editsection_form.php
blob2e4f5babafd0b1ae4978707e64fc0c047e737fcd
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(
29 'text',
30 'name',
31 get_string('sectionname'),
33 'placeholder' => $this->_customdata['defaultsectionname'],
34 'size' => 30,
35 'maxlength' => 255,
38 $mform->setType('name', PARAM_RAW);
39 $mform->setDefault('name', $sectioninfo->name);
40 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
42 /// Prepare course and the editor
44 $mform->addElement('editor', 'summary_editor', get_string('description'), null, $this->_customdata['editoroptions']);
45 $mform->setType('summary_editor', PARAM_RAW);
47 $mform->addElement('hidden', 'id');
48 $mform->setType('id', PARAM_INT);
50 // additional fields that course format has defined
51 $courseformat = course_get_format($course);
52 $formatoptions = $courseformat->section_format_options(true);
53 if (!empty($formatoptions)) {
54 $elements = $courseformat->create_edit_form_elements($mform, true);
57 if (!empty($CFG->enableavailability)) {
58 $mform->addElement('header', 'availabilityconditions',
59 get_string('restrictaccess', 'availability'));
60 $mform->setExpanded('availabilityconditions', false);
62 // Availability field. This is just a textarea; the user interface
63 // interaction is all implemented in JavaScript. The field is named
64 // availabilityconditionsjson for consistency with moodleform_mod.
65 $mform->addElement('textarea', 'availabilityconditionsjson',
66 get_string('accessrestrictions', 'availability'),
67 ['class' => 'd-none']
69 // Availability loading indicator.
70 $loadingcontainer = $OUTPUT->container(
71 $OUTPUT->render_from_template('core/loading', []),
72 'd-flex justify-content-center py-5 icon-size-5',
73 'availabilityconditions-loading'
75 $mform->addElement('html', $loadingcontainer);
78 $mform->_registerCancelButton('cancel');
81 public function definition_after_data() {
82 global $CFG;
84 $mform = $this->_form;
85 $course = $this->_customdata['course'];
87 if (!empty($CFG->enableavailability)) {
88 \core_availability\frontend::include_all_javascript($course, null,
89 $this->_customdata['cs']);
92 $this->add_action_buttons();
95 /**
96 * Load in existing data as form defaults
98 * @param stdClass|array $default_values object or array of default values
100 function set_data($default_values) {
101 if (!is_object($default_values)) {
102 // we need object for file_prepare_standard_editor
103 $default_values = (object)$default_values;
105 $editoroptions = $this->_customdata['editoroptions'];
106 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
107 $editoroptions['context'], 'course', 'section', $default_values->id);
108 parent::set_data($default_values);
112 * Return submitted data if properly submitted or returns NULL if validation fails or
113 * if there is no submitted data.
115 * @return object submitted data; NULL if not valid or not submitted or cancelled
117 function get_data() {
118 $data = parent::get_data();
119 if ($data !== null) {
120 $editoroptions = $this->_customdata['editoroptions'];
121 // Set name as an empty string if use default section name is checked.
122 if ($data->name === false) {
123 $data->name = '';
125 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
126 $editoroptions['context'], 'course', 'section', $data->id);
127 $course = $this->_customdata['course'];
128 foreach (course_get_format($course)->section_format_options() as $option => $unused) {
129 // fix issue with unset checkboxes not being returned at all
130 if (!isset($data->$option)) {
131 $data->$option = null;
135 return $data;
138 public function validation($data, $files) {
139 global $CFG;
140 $errors = array();
142 // Availability: Check availability field does not have errors.
143 if (!empty($CFG->enableavailability)) {
144 \core_availability\frontend::report_validation_errors($data, $errors);
147 return $errors;