Merge branch 'MDL-79059-master-2' of https://github.com/junpataleta/moodle
[moodle.git] / course / editsection_form.php
blob5983ebac687c09ff22db8f30d16149585b917b06
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;
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('summary'), null, $this->_customdata['editoroptions']);
38 $mform->addHelpButton('summary_editor', 'summary');
39 $mform->setType('summary_editor', PARAM_RAW);
41 $mform->addElement('hidden', 'id');
42 $mform->setType('id', PARAM_INT);
44 // additional fields that course format has defined
45 $courseformat = course_get_format($course);
46 $formatoptions = $courseformat->section_format_options(true);
47 if (!empty($formatoptions)) {
48 $elements = $courseformat->create_edit_form_elements($mform, true);
51 if (!empty($CFG->enableavailability)) {
52 $mform->addElement('header', 'availabilityconditions',
53 get_string('restrictaccess', 'availability'));
54 $mform->setExpanded('availabilityconditions', false);
56 // Availability field. This is just a textarea; the user interface
57 // interaction is all implemented in JavaScript. The field is named
58 // availabilityconditionsjson for consistency with moodleform_mod.
59 $mform->addElement('textarea', 'availabilityconditionsjson',
60 get_string('accessrestrictions', 'availability'),
61 ['class' => 'd-none']
65 $mform->_registerCancelButton('cancel');
68 public function definition_after_data() {
69 global $CFG;
71 $mform = $this->_form;
72 $course = $this->_customdata['course'];
74 if (!empty($CFG->enableavailability)) {
75 \core_availability\frontend::include_all_javascript($course, null,
76 $this->_customdata['cs']);
79 $this->add_action_buttons();
82 /**
83 * Load in existing data as form defaults
85 * @param stdClass|array $default_values object or array of default values
87 function set_data($default_values) {
88 if (!is_object($default_values)) {
89 // we need object for file_prepare_standard_editor
90 $default_values = (object)$default_values;
92 $editoroptions = $this->_customdata['editoroptions'];
93 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
94 $editoroptions['context'], 'course', 'section', $default_values->id);
95 if (strval($default_values->name) === '') {
96 $default_values->name = false;
98 parent::set_data($default_values);
102 * Return submitted data if properly submitted or returns NULL if validation fails or
103 * if there is no submitted data.
105 * @return object submitted data; NULL if not valid or not submitted or cancelled
107 function get_data() {
108 $data = parent::get_data();
109 if ($data !== null) {
110 $editoroptions = $this->_customdata['editoroptions'];
111 // Set name as an empty string if use default section name is checked.
112 if ($data->name === false) {
113 $data->name = '';
115 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
116 $editoroptions['context'], 'course', 'section', $data->id);
117 $course = $this->_customdata['course'];
118 foreach (course_get_format($course)->section_format_options() as $option => $unused) {
119 // fix issue with unset checkboxes not being returned at all
120 if (!isset($data->$option)) {
121 $data->$option = null;
125 return $data;
128 public function validation($data, $files) {
129 global $CFG;
130 $errors = array();
132 // Availability: Check availability field does not have errors.
133 if (!empty($CFG->enableavailability)) {
134 \core_availability\frontend::report_validation_errors($data, $errors);
137 return $errors;