Merge branch 'MDL-72353' of https://github.com/paulholden/moodle
[moodle.git] / course / editsection.php
blob9cae5dfad2add8d1b179c8b6c661ccd675b16637
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Edit the section basic information and availability
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 require_once("../config.php");
27 require_once("lib.php");
28 require_once($CFG->libdir . '/formslib.php');
30 $id = required_param('id', PARAM_INT); // course_sections.id
31 $sectionreturn = optional_param('sr', null, PARAM_INT);
32 $deletesection = optional_param('delete', 0, PARAM_BOOL);
33 $showonly = optional_param('showonly', 0, PARAM_TAGLIST);
35 $returnparams = [];
36 $params = ['id' => $id];
37 if (!is_null($sectionreturn)) {
38 $params['sr'] = $sectionreturn;
39 $returnparams['sr'] = $sectionreturn;
41 if (!empty($showonly)) {
42 $params['showonly'] = $showonly;
44 $PAGE->set_url('/course/editsection.php', $params);
46 $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST);
47 $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
48 $sectionnum = $section->section;
50 require_login($course);
51 $context = context_course::instance($course->id);
52 require_capability('moodle/course:update', $context);
54 // Get section_info object with all availability options.
55 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
57 // Deleting the section.
58 if ($deletesection) {
59 $cancelurl = course_get_url($course, $sectioninfo, $returnparams);
60 if (course_can_delete_section($course, $sectioninfo)) {
61 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
62 if (!$confirm && optional_param('sesskey', null, PARAM_RAW) !== null &&
63 empty($sectioninfo->summary) && empty($sectioninfo->sequence) && confirm_sesskey()) {
64 // Do not ask for confirmation if section is empty and sesskey is already provided.
65 $confirm = true;
67 if ($confirm) {
68 course_delete_section($course, $sectioninfo, true, true);
69 $courseurl = course_get_url($course, $sectioninfo->section - 1, $returnparams);
70 redirect($courseurl);
71 } else {
72 if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) {
73 $strdelete = get_string('deletesection', 'format_' . $course->format);
74 } else {
75 $strdelete = get_string('deletesection');
77 $PAGE->navbar->add($strdelete);
78 $PAGE->set_title($strdelete);
79 $PAGE->set_heading($course->fullname);
80 echo $OUTPUT->header();
81 echo $OUTPUT->box_start('noticebox');
82 $optionsyes = array('id' => $id, 'confirm' => 1, 'delete' => 1, 'sesskey' => sesskey());
83 $deleteurl = new moodle_url('/course/editsection.php', $optionsyes);
84 $formcontinue = new single_button($deleteurl, get_string('delete'));
85 $formcancel = new single_button($cancelurl, get_string('cancel'), 'get');
86 echo $OUTPUT->confirm(get_string('confirmdeletesection', '',
87 get_section_name($course, $sectioninfo)), $formcontinue, $formcancel);
88 echo $OUTPUT->box_end();
89 echo $OUTPUT->footer();
90 exit;
92 } else {
93 notice(get_string('nopermissions', 'error', get_string('deletesection')), $cancelurl);
97 $editoroptions = array(
98 'context' => $context,
99 'maxfiles' => EDITOR_UNLIMITED_FILES,
100 'maxbytes' => $CFG->maxbytes,
101 'trusttext' => false,
102 'noclean' => true,
103 'subdirs' => true
106 $courseformat = course_get_format($course);
108 if ($sectioninfo->is_delegated()) {
109 $defaultsectionname = $sectioninfo->name;
110 } else {
111 $defaultsectionname = $courseformat->get_default_section_name($section);
114 $customdata = [
115 'cs' => $sectioninfo,
116 'editoroptions' => $editoroptions,
117 'defaultsectionname' => $defaultsectionname,
118 'showonly' => $showonly,
121 $mform = $courseformat->editsection_form($PAGE->url, $customdata);
123 // set current value, make an editable copy of section_info object
124 // this will retrieve all format-specific options as well
125 $initialdata = convert_to_array($sectioninfo);
126 if (!empty($CFG->enableavailability)) {
127 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability;
129 $mform->set_data($initialdata);
130 if (!empty($showonly)) {
131 $mform->filter_shown_headers(explode(',', $showonly));
134 if ($mform->is_cancelled()){
135 // Form cancelled, return to course.
136 redirect(course_get_url($course, $section, $returnparams));
137 } else if ($data = $mform->get_data()) {
138 // Data submitted and validated, update and return to course.
140 // For consistency, we set the availability field to 'null' if it is empty.
141 if (!empty($CFG->enableavailability)) {
142 // Renamed field.
143 $data->availability = $data->availabilityconditionsjson;
144 unset($data->availabilityconditionsjson);
145 if ($data->availability === '') {
146 $data->availability = null;
149 course_update_section($course, $section, $data);
151 $PAGE->navigation->clear_cache();
152 redirect(course_get_url($course, $section, $returnparams));
155 // The edit form is displayed for the first time or if there was validation error on the previous step.
156 $sectionname = get_section_name($course, $sectionnum);
157 $stredit = get_string('editsectiontitle', '', $sectionname);
158 $strsummaryof = get_string('editsectionsettings');
160 $PAGE->set_title($stredit . moodle_page::TITLE_SEPARATOR . $course->shortname);
161 $PAGE->set_heading($course->fullname);
162 $PAGE->navbar->add($stredit);
163 echo $OUTPUT->header();
165 echo $OUTPUT->heading($strsummaryof);
167 $mform->display();
168 echo $OUTPUT->footer();