25055235b89db183326f11247be348c5c95c384e
[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         $mform->setDefault('usedefaultname', true);
42         $mform->setType('name', PARAM_TEXT);
43         $mform->disabledIf('name','usedefaultname','checked');
45         /// Prepare course and the editor
47         $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
48         $mform->addHelpButton('summary_editor', 'summary');
49         $mform->setType('summary_editor', PARAM_RAW);
51         $mform->addElement('hidden', 'id');
52         $mform->setType('id', PARAM_INT);
54         // additional fields that course format has defined
55         $courseformat = course_get_format($course);
56         $formatoptions = $courseformat->section_format_options(true);
57         if (!empty($formatoptions)) {
58             $elements = $courseformat->create_edit_form_elements($mform, true);
59         }
61         $mform->_registerCancelButton('cancel');
62     }
64     public function definition_after_data() {
65         global $CFG, $DB;
67         $mform  = $this->_form;
68         $course = $this->_customdata['course'];
69         $context = context_course::instance($course->id);
71         if (!empty($CFG->enableavailability)) {
72             $mform->addElement('header', 'availabilityconditions',
73                     get_string('restrictaccess', 'availability'));
74             $mform->setExpanded('availabilityconditions', false);
76             // Availability field. This is just a textarea; the user interface
77             // interaction is all implemented in JavaScript. The field is named
78             // availabilityconditionsjson for consistency with moodleform_mod.
79             $mform->addElement('textarea', 'availabilityconditionsjson',
80                     get_string('accessrestrictions', 'availability'));
81             \core_availability\frontend::include_all_javascript($course, null,
82                     $this->_customdata['cs']);
83         }
85         $this->add_action_buttons();
86     }
88     /**
89      * Load in existing data as form defaults
90      *
91      * @param stdClass|array $default_values object or array of default values
92      */
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;
97         }
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         $default_values->usedefaultname = (is_null($default_values->name));
102         parent::set_data($default_values);
103     }
105     /**
106      * Return submitted data if properly submitted or returns NULL if validation fails or
107      * if there is no submitted data.
108      *
109      * @return object submitted data; NULL if not valid or not submitted or cancelled
110      */
111     function get_data() {
112         $data = parent::get_data();
113         if ($data !== null) {
114             $editoroptions = $this->_customdata['editoroptions'];
115             $trimmedname = $data->name;
116             if (!empty($data->usedefaultname) || empty($trimmedname)) {
117                 $data->name = null;
118             }
119             $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
120                     $editoroptions['context'], 'course', 'section', $data->id);
121             $course = $this->_customdata['course'];
122             foreach (course_get_format($course)->section_format_options() as $option => $unused) {
123                 // fix issue with unset checkboxes not being returned at all
124                 if (!isset($data->$option)) {
125                     $data->$option = null;
126                 }
127             }
128         }
129         return $data;
130     }
132     public function validation($data, $files) {
133         global $CFG;
134         $errors = array();
136         // Availability: Check availability field does not have errors.
137         if (!empty($CFG->enableavailability)) {
138             \core_availability\frontend::report_validation_errors($data, $errors);
139         }
141         return $errors;
142     }