weekly release 3.0.6+
[moodle.git] / course / editsection.php
blob1aff7314de520934be3d846852e401393ba2b7f5
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);
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 // Deleting the section.
48 if ($deletesection) {
49 $cancelurl = course_get_url($course, $sectioninfo, array('sr' => $sectionreturn));
50 if (course_can_delete_section($course, $sectioninfo)) {
51 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
52 if ($confirm) {
53 course_delete_section($course, $sectioninfo, true);
54 $courseurl = course_get_url($course, 0, array('sr' => $sectionreturn));
55 redirect($courseurl);
56 } else {
57 if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) {
58 $strdelete = get_string('deletesection', 'format_' . $course->format);
59 } else {
60 $strdelete = get_string('deletesection');
62 $PAGE->navbar->add($strdelete);
63 $PAGE->set_title($strdelete);
64 $PAGE->set_heading($course->fullname);
65 echo $OUTPUT->header();
66 echo $OUTPUT->box_start('noticebox');
67 $optionsyes = array('id' => $id, 'confirm' => 1, 'delete' => 1, 'sesskey' => sesskey());
68 $deleteurl = new moodle_url('/course/editsection.php', $optionsyes);
69 $formcontinue = new single_button($deleteurl, get_string('delete'));
70 $formcancel = new single_button($cancelurl, get_string('cancel'), 'get');
71 echo $OUTPUT->confirm(get_string('confirmdeletesection', '',
72 get_section_name($course, $sectioninfo)), $formcontinue, $formcancel);
73 echo $OUTPUT->box_end();
74 echo $OUTPUT->footer();
75 exit;
77 } else {
78 notice(get_string('nopermissions', 'error', get_string('deletesection')), $cancelurl);
82 $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
84 $courseformat = course_get_format($course);
85 $defaultsectionname = $courseformat->get_default_section_name($section);
87 $customdata = array(
88 'cs' => $sectioninfo,
89 'editoroptions' => $editoroptions,
90 'defaultsectionname' => $defaultsectionname
92 $mform = $courseformat->editsection_form($PAGE->url, $customdata);
94 // set current value, make an editable copy of section_info object
95 // this will retrieve all format-specific options as well
96 $initialdata = convert_to_array($sectioninfo);
97 if (!empty($CFG->enableavailability)) {
98 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability;
100 $mform->set_data($initialdata);
102 if ($mform->is_cancelled()){
103 // Form cancelled, return to course.
104 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
105 } else if ($data = $mform->get_data()) {
106 // Data submitted and validated, update and return to course.
108 // For consistency, we set the availability field to 'null' if it is empty.
109 if (!empty($CFG->enableavailability)) {
110 // Renamed field.
111 $data->availability = $data->availabilityconditionsjson;
112 unset($data->availabilityconditionsjson);
113 if ($data->availability === '') {
114 $data->availability = null;
117 $DB->update_record('course_sections', $data);
118 rebuild_course_cache($course->id, true);
119 if (isset($data->section)) {
120 // Usually edit form does not change relative section number but just in case.
121 $sectionnum = $data->section;
123 course_get_format($course->id)->update_section_format_options($data);
125 // Set section info, as this might not be present in form_data.
126 if (!isset($data->section)) {
127 $data->section = $sectionnum;
129 // Trigger an event for course section update.
130 $event = \core\event\course_section_updated::create(
131 array(
132 'objectid' => $data->id,
133 'courseid' => $course->id,
134 'context' => $context,
135 'other' => array('sectionnum' => $data->section)
138 $event->trigger();
140 $PAGE->navigation->clear_cache();
141 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
144 // The edit form is displayed for the first time or if there was validation error on the previous step.
145 $sectionname = get_section_name($course, $sectionnum);
146 $stredit = get_string('edita', '', " $sectionname");
147 $strsummaryof = get_string('summaryof', '', " $sectionname");
149 $PAGE->set_title($stredit);
150 $PAGE->set_heading($course->fullname);
151 $PAGE->navbar->add($stredit);
152 echo $OUTPUT->header();
154 echo $OUTPUT->heading($strsummaryof);
156 $mform->display();
157 echo $OUTPUT->footer();