MDL-44070 Conditional availability enhancements (6): core changes
[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 29require_once($CFG->libdir . '/conditionlib.php');
30
fc79ede5 31$id = required_param('id', PARAM_INT); // course_sections.id
b8514b6a 32$sectionreturn = optional_param('sr', 0, PARAM_INT);
578fdd0a 33
b8514b6a 34$PAGE->set_url('/course/editsection.php', array('id'=>$id, 'sr'=> $sectionreturn));
2fdcf5e3 35
09eb2151 36$section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST);
74df2951 37$course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
fc79ede5 38$sectionnum = $section->section;
d9cb06dc 39
40require_login($course);
9a5e297b 41$context = context_course::instance($course->id);
d9cb06dc 42require_capability('moodle/course:update', $context);
43
ed29bf0f 44// Get section_info object with all availability options.
fc79ede5 45$sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
578fdd0a 46
fc79ede5
MG
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$mform->set_data(convert_to_array($sectioninfo));
a5a38c94 53
d9cb06dc 54if ($mform->is_cancelled()){
ed29bf0f 55 // Form cancelled, return to course.
fc79ede5 56 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 57} else if ($data = $mform->get_data()) {
ed29bf0f 58 // Data submitted and validated, update and return to course.
fc79ede5
MG
59 $DB->update_record('course_sections', $data);
60 rebuild_course_cache($course->id, true);
61 if (isset($data->section)) {
ed29bf0f 62 // Usually edit form does not change relative section number but just in case.
fc79ede5 63 $sectionnum = $data->section;
ce4dfd27 64 }
ce4dfd27 65 if (!empty($CFG->enableavailability)) {
ed29bf0f 66 // Update grade and completion conditions.
fc79ede5
MG
67 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
68 condition_info_section::update_section_from_form($sectioninfo, $data);
69 rebuild_course_cache($course->id, true);
ce4dfd27 70 }
fc79ede5 71 course_get_format($course->id)->update_section_format_options($data);
ce4dfd27 72
ed29bf0f
RT
73 // Set section info, as this might not be present in form_data.
74 if (!isset($data->section)) {
75 $data->section = $sectionnum;
76 }
77 // Trigger an event for course section update.
78 $event = \core\event\course_section_updated::create(
79 array(
80 'objectid' => $data->id,
81 'courseid' => $course->id,
82 'context' => $context,
83 'other' => array('sectionnum' => $data->section)
84 )
85 );
86 $event->trigger();
87
0a5ece3d 88 $PAGE->navigation->clear_cache();
fc79ede5 89 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 90}
578fdd0a 91
ed29bf0f 92// The edit form is displayed for the first time or if there was validation error on the previous step.
fc79ede5 93$sectionname = get_section_name($course, $sectionnum);
7487c856
SH
94$stredit = get_string('edita', '', " $sectionname");
95$strsummaryof = get_string('summaryof', '', " $sectionname");
d9cb06dc 96
97$PAGE->set_title($stredit);
e34a326f 98$PAGE->set_heading($course->fullname);
d9cb06dc 99$PAGE->navbar->add($stredit);
d9cb06dc 100echo $OUTPUT->header();
578fdd0a 101
b54a0547 102echo $OUTPUT->heading($strsummaryof);
a19a06d0 103
d9cb06dc 104$mform->display();
0831fd78 105echo $OUTPUT->footer();