MDL-51250 course: Add validation for section name
[moodle.git] / course / editsection_form.php
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5 }
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
14  *
15  * Course format plugins may specify different editing form to use
16  */
17 class editsection_form extends moodleform {
19     function definition() {
21         $mform  = $this->_form;
22         $course = $this->_customdata['course'];
24         $mform->addElement('header', 'generalhdr', get_string('general'));
26         $elementgroup = array();
27         $elementgroup[] = $mform->createElement('text', 'name', '', array('size' => '30', 'maxlength' => '255'));
29         // Get default section name.
30         $defaultsectionname = $this->_customdata['defaultsectionname'];
31         if ($defaultsectionname) {
32             $defaultsectionname = ' [' . $defaultsectionname . ']';
33         }
35         $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '',
36                                                 get_string('sectionusedefaultname') . $defaultsectionname);
38         $mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false);
39         $mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
41         // Add rule for name_group to make sure that the section name is not blank if 'Use default section name'
42         // checkbox is unchecked.
43         $mform->addRule('name_group', get_string('required'), 'required', null, 'client');
45         $mform->setDefault('usedefaultname', true);
46         $mform->setType('name', PARAM_TEXT);
47         $mform->disabledIf('name','usedefaultname','checked');
49         /// Prepare course and the editor
51         $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
52         $mform->addHelpButton('summary_editor', 'summary');
53         $mform->setType('summary_editor', PARAM_RAW);
55         $mform->addElement('hidden', 'id');
56         $mform->setType('id', PARAM_INT);
58         // additional fields that course format has defined
59         $courseformat = course_get_format($course);
60         $formatoptions = $courseformat->section_format_options(true);
61         if (!empty($formatoptions)) {
62             $elements = $courseformat->create_edit_form_elements($mform, true);
63         }
65         $mform->_registerCancelButton('cancel');
66     }
68     public function definition_after_data() {
69         global $CFG, $DB;
71         $mform  = $this->_form;
72         $course = $this->_customdata['course'];
73         $context = context_course::instance($course->id);
75         if (!empty($CFG->enableavailability)) {
76             $mform->addElement('header', 'availabilityconditions',
77                     get_string('restrictaccess', 'availability'));
78             $mform->setExpanded('availabilityconditions', false);
80             // Availability field. This is just a textarea; the user interface
81             // interaction is all implemented in JavaScript. The field is named
82             // availabilityconditionsjson for consistency with moodleform_mod.
83             $mform->addElement('textarea', 'availabilityconditionsjson',
84                     get_string('accessrestrictions', 'availability'));
85             \core_availability\frontend::include_all_javascript($course, null,
86                     $this->_customdata['cs']);
87         }
89         $this->add_action_buttons();
90     }
92     /**
93      * Load in existing data as form defaults
94      *
95      * @param stdClass|array $default_values object or array of default values
96      */
97     function set_data($default_values) {
98         if (!is_object($default_values)) {
99             // we need object for file_prepare_standard_editor
100             $default_values = (object)$default_values;
101         }
102         $editoroptions = $this->_customdata['editoroptions'];
103         $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
104                 $editoroptions['context'], 'course', 'section', $default_values->id);
105         $default_values->usedefaultname = (is_null($default_values->name));
106         parent::set_data($default_values);
107     }
109     /**
110      * Return submitted data if properly submitted or returns NULL if validation fails or
111      * if there is no submitted data.
112      *
113      * @return object submitted data; NULL if not valid or not submitted or cancelled
114      */
115     function get_data() {
116         $data = parent::get_data();
117         if ($data !== null) {
118             $editoroptions = $this->_customdata['editoroptions'];
119             if (!empty($data->usedefaultname)) {
120                 $data->name = null;
121             }
122             $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
123                     $editoroptions['context'], 'course', 'section', $data->id);
124             $course = $this->_customdata['course'];
125             foreach (course_get_format($course)->section_format_options() as $option => $unused) {
126                 // fix issue with unset checkboxes not being returned at all
127                 if (!isset($data->$option)) {
128                     $data->$option = null;
129                 }
130             }
131         }
132         return $data;
133     }
135     public function validation($data, $files) {
136         global $CFG;
137         $errors = array();
139         // Availability: Check availability field does not have errors.
140         if (!empty($CFG->enableavailability)) {
141             \core_availability\frontend::report_validation_errors($data, $errors);
142         }
144         // Validate section name if 'Use default section name' is unchecked.
145         if (empty($data['usedefaultname'])) {
146             // Make sure the trimmed value of section name is not empty.
147             $trimmedname = trim($data['name']);
148             if (empty($trimmedname)) {
149                 $errors['name_group'] = get_string('required');
150             }
151         }
153         return $errors;
154     }