MDL-51250 course: Add validation for section name
[moodle.git] / course / editsection_form.php
CommitLineData
aa6c1ced 1<?php
644d506a 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
644d506a 7require_once($CFG->libdir.'/formslib.php');
fc79ede5
MG
8require_once($CFG->libdir.'/filelib.php');
9require_once($CFG->libdir.'/completionlib.php');
10require_once($CFG->libdir.'/gradelib.php');
11
12/**
13 * Default form for editing course section
14 *
15 * Course format plugins may specify different editing form to use
16 */
644d506a 17class editsection_form extends moodleform {
18
19 function definition() {
644d506a 20
21 $mform = $this->_form;
e34a326f 22 $course = $this->_customdata['course'];
0f4ab67d 23
35143197
FM
24 $mform->addElement('header', 'generalhdr', get_string('general'));
25
a5b884fe 26 $elementgroup = array();
e2cceaff 27 $elementgroup[] = $mform->createElement('text', 'name', '', array('size' => '30', 'maxlength' => '255'));
f1bfc897
JP
28
29 // Get default section name.
30 $defaultsectionname = $this->_customdata['defaultsectionname'];
31 if ($defaultsectionname) {
32 $defaultsectionname = ' [' . $defaultsectionname . ']';
33 }
34
35 $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '',
36 get_string('sectionusedefaultname') . $defaultsectionname);
37
a5b884fe 38 $mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false);
e2cceaff 39 $mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
a5b884fe 40
d264cbbb
JP
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');
44
a5b884fe 45 $mform->setDefault('usedefaultname', true);
0f4ab67d
SH
46 $mform->setType('name', PARAM_TEXT);
47 $mform->disabledIf('name','usedefaultname','checked');
48
e34a326f 49 /// Prepare course and the editor
09eb2151
DM
50
51 $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
b54a0547 52 $mform->addHelpButton('summary_editor', 'summary');
09eb2151 53 $mform->setType('summary_editor', PARAM_RAW);
644d506a 54
55 $mform->addElement('hidden', 'id');
56 $mform->setType('id', PARAM_INT);
57
fc79ede5
MG
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 }
64
ce4dfd27 65 $mform->_registerCancelButton('cancel');
66 }
67
68 public function definition_after_data() {
69 global $CFG, $DB;
70
71 $mform = $this->_form;
72 $course = $this->_customdata['course'];
3a09f4dc 73 $context = context_course::instance($course->id);
ce4dfd27 74
75 if (!empty($CFG->enableavailability)) {
400c0fd2 76 $mform->addElement('header', 'availabilityconditions',
77 get_string('restrictaccess', 'availability'));
35143197 78 $mform->setExpanded('availabilityconditions', false);
ce4dfd27 79
400c0fd2 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']);
ce4dfd27 87 }
88
644d506a 89 $this->add_action_buttons();
ce4dfd27 90 }
91
fc79ede5
MG
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 }
108
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'];
d264cbbb 119 if (!empty($data->usedefaultname)) {
fc79ede5
MG
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 }
400c0fd2 134
135 public function validation($data, $files) {
136 global $CFG;
137 $errors = array();
138
139 // Availability: Check availability field does not have errors.
140 if (!empty($CFG->enableavailability)) {
141 \core_availability\frontend::report_validation_errors($data, $errors);
142 }
143
d264cbbb
JP
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 }
152
400c0fd2 153 return $errors;
154 }
644d506a 155}