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
. '/formslib.php');
29 require_once($CFG->libdir
. '/conditionlib.php');
31 $id = required_param('id', PARAM_INT
); // course_sections.id
32 $sectionreturn = optional_param('sr', 0, PARAM_INT
);
34 $PAGE->set_url('/course/editsection.php', array('id'=>$id, 'sr'=> $sectionreturn));
36 $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST
);
37 $course = $DB->get_record('course', array('id' => $section->course
), '*', MUST_EXIST
);
38 $sectionnum = $section->section
;
40 require_login($course);
41 $context = context_course
::instance($course->id
);
42 require_capability('moodle/course:update', $context);
44 // Get section_info object with all availability options.
45 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
47 $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES
, 'maxbytes'=>$CFG->maxbytes
, 'trusttext'=>false, 'noclean'=>true);
48 $mform = course_get_format($course->id
)->editsection_form($PAGE->url
,
49 array('cs' => $sectioninfo, 'editoroptions' => $editoroptions));
50 // set current value, make an editable copy of section_info object
51 // this will retrieve all format-specific options as well
52 $initialdata = convert_to_array($sectioninfo);
53 if (!empty($CFG->enableavailability
)) {
54 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability
;
56 $mform->set_data($initialdata);
58 if ($mform->is_cancelled()){
59 // Form cancelled, return to course.
60 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
61 } else if ($data = $mform->get_data()) {
62 // Data submitted and validated, update and return to course.
64 // For consistency, we set the availability field to 'null' if it is empty.
65 if (!empty($CFG->enableavailability
)) {
67 $data->availability
= $data->availabilityconditionsjson
;
68 unset($data->availabilityconditionsjson
);
69 if ($data->availability
=== '') {
70 $data->availability
= null;
73 $DB->update_record('course_sections', $data);
74 rebuild_course_cache($course->id
, true);
75 if (isset($data->section
)) {
76 // Usually edit form does not change relative section number but just in case.
77 $sectionnum = $data->section
;
79 course_get_format($course->id
)->update_section_format_options($data);
81 // Set section info, as this might not be present in form_data.
82 if (!isset($data->section
)) {
83 $data->section
= $sectionnum;
85 // Trigger an event for course section update.
86 $event = \core\event\course_section_updated
::create(
88 'objectid' => $data->id
,
89 'courseid' => $course->id
,
90 'context' => $context,
91 'other' => array('sectionnum' => $data->section
)
96 $PAGE->navigation
->clear_cache();
97 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
100 // The edit form is displayed for the first time or if there was validation error on the previous step.
101 $sectionname = get_section_name($course, $sectionnum);
102 $stredit = get_string('edita', '', " $sectionname");
103 $strsummaryof = get_string('summaryof', '', " $sectionname");
105 $PAGE->set_title($stredit);
106 $PAGE->set_heading($course->fullname
);
107 $PAGE->navbar
->add($stredit);
108 echo $OUTPUT->header();
110 echo $OUTPUT->heading($strsummaryof);
113 echo $OUTPUT->footer();