3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
26 require_once("../config.php");
27 require_once("lib.php");
28 require_once($CFG->libdir . '/conditionlib.php');
30 $id = required_param('id', PARAM_INT); // course_sections.id
31 $sectionreturn = optional_param('sr', 0, PARAM_INT);
33 $PAGE->set_url('/course/editsection.php', array('id'=>$id, 'sr'=> $sectionreturn));
35 $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST);
36 $course = get_course($section->course);
37 $sectionnum = $section->section;
39 require_login($course);
40 $context = context_course::instance($course->id);
41 require_capability('moodle/course:update', $context);
43 // Get section_info object with all availability options.
44 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
46 $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
47 $mform = course_get_format($course->id)->editsection_form($PAGE->url,
48 array('cs' => $sectioninfo, 'editoroptions' => $editoroptions));
49 // set current value, make an editable copy of section_info object
50 // this will retrieve all format-specific options as well
51 $mform->set_data(convert_to_array($sectioninfo));
53 if ($mform->is_cancelled()){
54 // Form cancelled, return to course.
55 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
56 } else if ($data = $mform->get_data()) {
57 // Data submitted and validated, update and return to course.
58 $DB->update_record('course_sections', $data);
59 rebuild_course_cache($course->id, true);
60 if (isset($data->section)) {
61 // Usually edit form does not change relative section number but just in case.
62 $sectionnum = $data->section;
64 if (!empty($CFG->enableavailability)) {
65 // Update grade and completion conditions.
66 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
67 condition_info_section::update_section_from_form($sectioninfo, $data);
68 rebuild_course_cache($course->id, true);
70 course_get_format($course->id)->update_section_format_options($data);
72 // Set section info, as this might not be present in form_data.
73 if (!isset($data->section)) {
74 $data->section = $sectionnum;
76 // Trigger an event for course section update.
77 $event = \core\event\course_section_updated::create(
79 'objectid' => $data->id,
80 'courseid' => $course->id,
81 'context' => $context,
82 'other' => array('sectionnum' => $data->section)
87 $PAGE->navigation->clear_cache();
88 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
91 // The edit form is displayed for the first time or if there was validation error on the previous step.
92 $sectionname = get_section_name($course, $sectionnum);
93 $stredit = get_string('edita', '', " $sectionname");
94 $strsummaryof = get_string('summaryof', '', " $sectionname");
96 $PAGE->set_title($stredit);
97 $PAGE->set_heading($course->fullname);
98 $PAGE->navbar->add($stredit);
99 echo $OUTPUT->header();
101 echo $OUTPUT->heading($strsummaryof);
104 echo $OUTPUT->footer();