MDL-78324 core: Convert core/tag to esm
[moodle.git] / course / editsection.php
bloba63a0860c7e8adb48208d10099b87a6d9bdcaa3b
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', 0, PARAM_INT);
32 $deletesection = optional_param('delete', 0, PARAM_BOOL);
33 $showonly = optional_param('showonly', 0, PARAM_TAGLIST);
35 $params = ['id' => $id, 'sr' => $sectionreturn];
36 if (!empty($showonly)) {
37 $params['showonly'] = $showonly;
39 $PAGE->set_url('/course/editsection.php', $params);
41 $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST);
42 $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
43 $sectionnum = $section->section;
45 require_login($course);
46 $context = context_course::instance($course->id);
47 require_capability('moodle/course:update', $context);
49 // Get section_info object with all availability options.
50 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
52 // Deleting the section.
53 if ($deletesection) {
54 $cancelurl = course_get_url($course, $sectioninfo, array('sr' => $sectionreturn));
55 if (course_can_delete_section($course, $sectioninfo)) {
56 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
57 if (!$confirm && optional_param('sesskey', null, PARAM_RAW) !== null &&
58 empty($sectioninfo->summary) && empty($sectioninfo->sequence) && confirm_sesskey()) {
59 // Do not ask for confirmation if section is empty and sesskey is already provided.
60 $confirm = true;
62 if ($confirm) {
63 course_delete_section($course, $sectioninfo, true, true);
64 $courseurl = course_get_url($course, $sectioninfo->section - 1, array('sr' => $sectionreturn));
65 redirect($courseurl);
66 } else {
67 if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) {
68 $strdelete = get_string('deletesection', 'format_' . $course->format);
69 } else {
70 $strdelete = get_string('deletesection');
72 $PAGE->navbar->add($strdelete);
73 $PAGE->set_title($strdelete);
74 $PAGE->set_heading($course->fullname);
75 echo $OUTPUT->header();
76 echo $OUTPUT->box_start('noticebox');
77 $optionsyes = array('id' => $id, 'confirm' => 1, 'delete' => 1, 'sesskey' => sesskey());
78 $deleteurl = new moodle_url('/course/editsection.php', $optionsyes);
79 $formcontinue = new single_button($deleteurl, get_string('delete'));
80 $formcancel = new single_button($cancelurl, get_string('cancel'), 'get');
81 echo $OUTPUT->confirm(get_string('confirmdeletesection', '',
82 get_section_name($course, $sectioninfo)), $formcontinue, $formcancel);
83 echo $OUTPUT->box_end();
84 echo $OUTPUT->footer();
85 exit;
87 } else {
88 notice(get_string('nopermissions', 'error', get_string('deletesection')), $cancelurl);
92 $editoroptions = array(
93 'context' => $context,
94 'maxfiles' => EDITOR_UNLIMITED_FILES,
95 'maxbytes' => $CFG->maxbytes,
96 'trusttext' => false,
97 'noclean' => true,
98 'subdirs' => true
101 $courseformat = course_get_format($course);
102 $defaultsectionname = $courseformat->get_default_section_name($section);
104 $customdata = [
105 'cs' => $sectioninfo,
106 'editoroptions' => $editoroptions,
107 'defaultsectionname' => $defaultsectionname,
108 'showonly' => $showonly,
111 $mform = $courseformat->editsection_form($PAGE->url, $customdata);
113 // set current value, make an editable copy of section_info object
114 // this will retrieve all format-specific options as well
115 $initialdata = convert_to_array($sectioninfo);
116 if (!empty($CFG->enableavailability)) {
117 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability;
119 $mform->set_data($initialdata);
120 if (!empty($showonly)) {
121 $mform->filter_shown_headers(explode(',', $showonly));
124 if ($mform->is_cancelled()){
125 // Form cancelled, return to course.
126 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
127 } else if ($data = $mform->get_data()) {
128 // Data submitted and validated, update and return to course.
130 // For consistency, we set the availability field to 'null' if it is empty.
131 if (!empty($CFG->enableavailability)) {
132 // Renamed field.
133 $data->availability = $data->availabilityconditionsjson;
134 unset($data->availabilityconditionsjson);
135 if ($data->availability === '') {
136 $data->availability = null;
139 course_update_section($course, $section, $data);
141 $PAGE->navigation->clear_cache();
142 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
145 // The edit form is displayed for the first time or if there was validation error on the previous step.
146 $sectionname = get_section_name($course, $sectionnum);
147 $stredit = get_string('edita', '', " $sectionname");
148 $strsummaryof = get_string('summaryof', '', " $sectionname");
150 $PAGE->set_title($stredit);
151 $PAGE->set_heading($course->fullname);
152 $PAGE->navbar->add($stredit);
153 echo $OUTPUT->header();
155 echo $OUTPUT->heading($strsummaryof);
157 $mform->display();
158 echo $OUTPUT->footer();