Merge branch 'MDL-41975-master' of git://github.com/FMCorz/moodle
[moodle.git] / course / editsection_form.php
blobc6144706b507b83fd138322444e50742d4278b50
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() {
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'));
28 $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '', get_string('sectionusedefaultname'));
29 $mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false);
30 $mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
32 $mform->setDefault('usedefaultname', true);
33 $mform->setType('name', PARAM_TEXT);
34 $mform->disabledIf('name','usedefaultname','checked');
36 /// Prepare course and the editor
38 $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
39 $mform->addHelpButton('summary_editor', 'summary');
40 $mform->setType('summary_editor', PARAM_RAW);
42 $mform->addElement('hidden', 'id');
43 $mform->setType('id', PARAM_INT);
45 // additional fields that course format has defined
46 $courseformat = course_get_format($course);
47 $formatoptions = $courseformat->section_format_options(true);
48 if (!empty($formatoptions)) {
49 $elements = $courseformat->create_edit_form_elements($mform, true);
52 $mform->_registerCancelButton('cancel');
55 public function definition_after_data() {
56 global $CFG, $DB;
58 $mform = $this->_form;
59 $course = $this->_customdata['course'];
60 $context = context_course::instance($course->id);
62 if (!empty($CFG->enableavailability)) {
63 $mform->addElement('header', 'availabilityconditions',
64 get_string('restrictaccess', 'availability'));
65 $mform->setExpanded('availabilityconditions', false);
67 // Availability field. This is just a textarea; the user interface
68 // interaction is all implemented in JavaScript. The field is named
69 // availabilityconditionsjson for consistency with moodleform_mod.
70 $mform->addElement('textarea', 'availabilityconditionsjson',
71 get_string('accessrestrictions', 'availability'));
72 \core_availability\frontend::include_all_javascript($course, null,
73 $this->_customdata['cs']);
76 $this->add_action_buttons();
79 /**
80 * Load in existing data as form defaults
82 * @param stdClass|array $default_values object or array of default values
84 function set_data($default_values) {
85 if (!is_object($default_values)) {
86 // we need object for file_prepare_standard_editor
87 $default_values = (object)$default_values;
89 $editoroptions = $this->_customdata['editoroptions'];
90 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
91 $editoroptions['context'], 'course', 'section', $default_values->id);
92 $default_values->usedefaultname = (is_null($default_values->name));
93 parent::set_data($default_values);
96 /**
97 * Return submitted data if properly submitted or returns NULL if validation fails or
98 * if there is no submitted data.
100 * @return object submitted data; NULL if not valid or not submitted or cancelled
102 function get_data() {
103 $data = parent::get_data();
104 if ($data !== null) {
105 $editoroptions = $this->_customdata['editoroptions'];
106 if (!empty($data->usedefaultname)) {
107 $data->name = null;
109 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
110 $editoroptions['context'], 'course', 'section', $data->id);
111 $course = $this->_customdata['course'];
112 foreach (course_get_format($course)->section_format_options() as $option => $unused) {
113 // fix issue with unset checkboxes not being returned at all
114 if (!isset($data->$option)) {
115 $data->$option = null;
119 return $data;
122 public function validation($data, $files) {
123 global $CFG;
124 $errors = array();
126 // Availability: Check availability field does not have errors.
127 if (!empty($CFG->enableavailability)) {
128 \core_availability\frontend::report_validation_errors($data, $errors);
131 return $errors;