Merge branch 'install_38_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / course / editsection_form.php
blob264ae96ac0dfabdd40ef7d3e172e97b4478e6ea1
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'];
23 $sectioninfo = $this->_customdata['cs'];
25 $mform->addElement('header', 'generalhdr', get_string('general'));
27 $mform->addElement('defaultcustom', 'name', get_string('sectionname'), [
28 'defaultvalue' => $this->_customdata['defaultsectionname'],
29 'customvalue' => $sectioninfo->name,
30 ], ['size' => 30, 'maxlength' => 255]);
31 $mform->setDefault('name', false);
32 $mform->addGroupRule('name', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
34 /// Prepare course and the editor
36 $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
37 $mform->addHelpButton('summary_editor', 'summary');
38 $mform->setType('summary_editor', PARAM_RAW);
40 $mform->addElement('hidden', 'id');
41 $mform->setType('id', PARAM_INT);
43 // additional fields that course format has defined
44 $courseformat = course_get_format($course);
45 $formatoptions = $courseformat->section_format_options(true);
46 if (!empty($formatoptions)) {
47 $elements = $courseformat->create_edit_form_elements($mform, true);
50 $mform->_registerCancelButton('cancel');
53 public function definition_after_data() {
54 global $CFG, $DB;
56 $mform = $this->_form;
57 $course = $this->_customdata['course'];
58 $context = context_course::instance($course->id);
60 if (!empty($CFG->enableavailability)) {
61 $mform->addElement('header', 'availabilityconditions',
62 get_string('restrictaccess', 'availability'));
63 $mform->setExpanded('availabilityconditions', false);
65 // Availability field. This is just a textarea; the user interface
66 // interaction is all implemented in JavaScript. The field is named
67 // availabilityconditionsjson for consistency with moodleform_mod.
68 $mform->addElement('textarea', 'availabilityconditionsjson',
69 get_string('accessrestrictions', 'availability'));
70 \core_availability\frontend::include_all_javascript($course, null,
71 $this->_customdata['cs']);
74 $this->add_action_buttons();
77 /**
78 * Load in existing data as form defaults
80 * @param stdClass|array $default_values object or array of default values
82 function set_data($default_values) {
83 if (!is_object($default_values)) {
84 // we need object for file_prepare_standard_editor
85 $default_values = (object)$default_values;
87 $editoroptions = $this->_customdata['editoroptions'];
88 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
89 $editoroptions['context'], 'course', 'section', $default_values->id);
90 if (strval($default_values->name) === '') {
91 $default_values->name = false;
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 // Set name as an empty string if use default section name is checked.
107 if ($data->name === false) {
108 $data->name = '';
110 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
111 $editoroptions['context'], 'course', 'section', $data->id);
112 $course = $this->_customdata['course'];
113 foreach (course_get_format($course)->section_format_options() as $option => $unused) {
114 // fix issue with unset checkboxes not being returned at all
115 if (!isset($data->$option)) {
116 $data->$option = null;
120 return $data;
123 public function validation($data, $files) {
124 global $CFG;
125 $errors = array();
127 // Availability: Check availability field does not have errors.
128 if (!empty($CFG->enableavailability)) {
129 \core_availability\frontend::report_validation_errors($data, $errors);
132 return $errors;