MDL-67264 core_course: Activity chooser new feature
[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'];
f840bf03 23 $sectioninfo = $this->_customdata['cs'];
0f4ab67d 24
35143197
FM
25 $mform->addElement('header', 'generalhdr', get_string('general'));
26
f840bf03
MG
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))));
0f4ab67d 33
e34a326f 34 /// Prepare course and the editor
09eb2151
DM
35
36 $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
b54a0547 37 $mform->addHelpButton('summary_editor', 'summary');
09eb2151 38 $mform->setType('summary_editor', PARAM_RAW);
644d506a 39
40 $mform->addElement('hidden', 'id');
41 $mform->setType('id', PARAM_INT);
42
fc79ede5
MG
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);
48 }
49
ce4dfd27 50 $mform->_registerCancelButton('cancel');
51 }
52
53 public function definition_after_data() {
54 global $CFG, $DB;
55
56 $mform = $this->_form;
57 $course = $this->_customdata['course'];
3a09f4dc 58 $context = context_course::instance($course->id);
ce4dfd27 59
60 if (!empty($CFG->enableavailability)) {
400c0fd2 61 $mform->addElement('header', 'availabilityconditions',
62 get_string('restrictaccess', 'availability'));
35143197 63 $mform->setExpanded('availabilityconditions', false);
ce4dfd27 64
400c0fd2 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']);
ce4dfd27 72 }
73
644d506a 74 $this->add_action_buttons();
ce4dfd27 75 }
76
fc79ede5
MG
77 /**
78 * Load in existing data as form defaults
79 *
80 * @param stdClass|array $default_values object or array of default values
81 */
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;
86 }
87 $editoroptions = $this->_customdata['editoroptions'];
88 $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
89 $editoroptions['context'], 'course', 'section', $default_values->id);
f840bf03
MG
90 if (strval($default_values->name) === '') {
91 $default_values->name = false;
92 }
fc79ede5
MG
93 parent::set_data($default_values);
94 }
95
96 /**
97 * Return submitted data if properly submitted or returns NULL if validation fails or
98 * if there is no submitted data.
99 *
100 * @return object submitted data; NULL if not valid or not submitted or cancelled
101 */
102 function get_data() {
103 $data = parent::get_data();
104 if ($data !== null) {
105 $editoroptions = $this->_customdata['editoroptions'];
eb66223c 106 // Set name as an empty string if use default section name is checked.
f840bf03 107 if ($data->name === false) {
eb66223c 108 $data->name = '';
fc79ede5
MG
109 }
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;
117 }
118 }
119 }
120 return $data;
121 }
400c0fd2 122
123 public function validation($data, $files) {
124 global $CFG;
125 $errors = array();
126
127 // Availability: Check availability field does not have errors.
128 if (!empty($CFG->enableavailability)) {
129 \core_availability\frontend::report_validation_errors($data, $errors);
130 }
131
132 return $errors;
133 }
644d506a 134}