weekly release 2.9dev
[moodle.git] / course / editsection.php
CommitLineData
d9cb06dc 1<?php
578fdd0a 2
d9cb06dc 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.
14//
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/>.
578fdd0a 17
d9cb06dc 18/**
fc79ede5 19 * Edit the section basic information and availability
d9cb06dc 20 *
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
24 */
578fdd0a 25
d9cb06dc 26require_once("../config.php");
27require_once("lib.php");
30905b5b 28require_once($CFG->libdir . '/formslib.php');
ce4dfd27 29
fc79ede5 30$id = required_param('id', PARAM_INT); // course_sections.id
b8514b6a 31$sectionreturn = optional_param('sr', 0, PARAM_INT);
578fdd0a 32
b8514b6a 33$PAGE->set_url('/course/editsection.php', array('id'=>$id, 'sr'=> $sectionreturn));
2fdcf5e3 34
09eb2151 35$section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST);
74df2951 36$course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
fc79ede5 37$sectionnum = $section->section;
d9cb06dc 38
39require_login($course);
9a5e297b 40$context = context_course::instance($course->id);
d9cb06dc 41require_capability('moodle/course:update', $context);
42
ed29bf0f 43// Get section_info object with all availability options.
fc79ede5 44$sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
578fdd0a 45
fc79ede5
MG
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
400c0fd2 51$initialdata = convert_to_array($sectioninfo);
52if (!empty($CFG->enableavailability)) {
53 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability;
54}
55$mform->set_data($initialdata);
a5a38c94 56
d9cb06dc 57if ($mform->is_cancelled()){
ed29bf0f 58 // Form cancelled, return to course.
fc79ede5 59 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 60} else if ($data = $mform->get_data()) {
ed29bf0f 61 // Data submitted and validated, update and return to course.
400c0fd2 62
63 // For consistency, we set the availability field to 'null' if it is empty.
64 if (!empty($CFG->enableavailability)) {
65 // Renamed field.
66 $data->availability = $data->availabilityconditionsjson;
67 unset($data->availabilityconditionsjson);
68 if ($data->availability === '') {
69 $data->availability = null;
70 }
71 }
fc79ede5
MG
72 $DB->update_record('course_sections', $data);
73 rebuild_course_cache($course->id, true);
74 if (isset($data->section)) {
ed29bf0f 75 // Usually edit form does not change relative section number but just in case.
fc79ede5 76 $sectionnum = $data->section;
ce4dfd27 77 }
fc79ede5 78 course_get_format($course->id)->update_section_format_options($data);
ce4dfd27 79
ed29bf0f
RT
80 // Set section info, as this might not be present in form_data.
81 if (!isset($data->section)) {
82 $data->section = $sectionnum;
83 }
84 // Trigger an event for course section update.
85 $event = \core\event\course_section_updated::create(
86 array(
87 'objectid' => $data->id,
88 'courseid' => $course->id,
89 'context' => $context,
90 'other' => array('sectionnum' => $data->section)
91 )
92 );
93 $event->trigger();
94
0a5ece3d 95 $PAGE->navigation->clear_cache();
fc79ede5 96 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 97}
578fdd0a 98
ed29bf0f 99// The edit form is displayed for the first time or if there was validation error on the previous step.
fc79ede5 100$sectionname = get_section_name($course, $sectionnum);
7487c856
SH
101$stredit = get_string('edita', '', " $sectionname");
102$strsummaryof = get_string('summaryof', '', " $sectionname");
d9cb06dc 103
104$PAGE->set_title($stredit);
e34a326f 105$PAGE->set_heading($course->fullname);
d9cb06dc 106$PAGE->navbar->add($stredit);
d9cb06dc 107echo $OUTPUT->header();
578fdd0a 108
b54a0547 109echo $OUTPUT->heading($strsummaryof);
a19a06d0 110
d9cb06dc 111$mform->display();
0831fd78 112echo $OUTPUT->footer();