MDL-67264 core_course: Begin set up for Activity chooser
[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);
ca9cae84 32$deletesection = optional_param('delete', 0, PARAM_BOOL);
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
ca9cae84
MG
47// Deleting the section.
48if ($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();
88a7f775
MG
52 if (!$confirm && optional_param('sesskey', null, PARAM_RAW) !== null &&
53 empty($sectioninfo->summary) && empty($sectioninfo->sequence) && confirm_sesskey()) {
54 // Do not ask for confirmation if section is empty and sesskey is already provided.
55 $confirm = true;
56 }
ca9cae84 57 if ($confirm) {
3869d774 58 course_delete_section($course, $sectioninfo, true, true);
88a7f775 59 $courseurl = course_get_url($course, $sectioninfo->section - 1, array('sr' => $sectionreturn));
ca9cae84
MG
60 redirect($courseurl);
61 } else {
62 if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) {
63 $strdelete = get_string('deletesection', 'format_' . $course->format);
64 } else {
65 $strdelete = get_string('deletesection');
66 }
67 $PAGE->navbar->add($strdelete);
68 $PAGE->set_title($strdelete);
69 $PAGE->set_heading($course->fullname);
70 echo $OUTPUT->header();
71 echo $OUTPUT->box_start('noticebox');
72 $optionsyes = array('id' => $id, 'confirm' => 1, 'delete' => 1, 'sesskey' => sesskey());
73 $deleteurl = new moodle_url('/course/editsection.php', $optionsyes);
65377780 74 $formcontinue = new single_button($deleteurl, get_string('delete'));
5962f0df 75 $formcancel = new single_button($cancelurl, get_string('cancel'), 'get');
ca9cae84
MG
76 echo $OUTPUT->confirm(get_string('confirmdeletesection', '',
77 get_section_name($course, $sectioninfo)), $formcontinue, $formcancel);
78 echo $OUTPUT->box_end();
79 echo $OUTPUT->footer();
80 exit;
81 }
82 } else {
83 notice(get_string('nopermissions', 'error', get_string('deletesection')), $cancelurl);
84 }
85}
86
efe8c3c6
MG
87$editoroptions = array(
88 'context' => $context,
89 'maxfiles' => EDITOR_UNLIMITED_FILES,
90 'maxbytes' => $CFG->maxbytes,
91 'trusttext' => false,
92 'noclean' => true,
93 'subdirs' => true
94);
f1bfc897
JP
95
96$courseformat = course_get_format($course);
97$defaultsectionname = $courseformat->get_default_section_name($section);
98
99$customdata = array(
100 'cs' => $sectioninfo,
101 'editoroptions' => $editoroptions,
102 'defaultsectionname' => $defaultsectionname
103);
104$mform = $courseformat->editsection_form($PAGE->url, $customdata);
105
fc79ede5
MG
106// set current value, make an editable copy of section_info object
107// this will retrieve all format-specific options as well
400c0fd2 108$initialdata = convert_to_array($sectioninfo);
109if (!empty($CFG->enableavailability)) {
110 $initialdata['availabilityconditionsjson'] = $sectioninfo->availability;
111}
112$mform->set_data($initialdata);
a5a38c94 113
d9cb06dc 114if ($mform->is_cancelled()){
ed29bf0f 115 // Form cancelled, return to course.
fc79ede5 116 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 117} else if ($data = $mform->get_data()) {
ed29bf0f 118 // Data submitted and validated, update and return to course.
400c0fd2 119
120 // For consistency, we set the availability field to 'null' if it is empty.
121 if (!empty($CFG->enableavailability)) {
122 // Renamed field.
123 $data->availability = $data->availabilityconditionsjson;
124 unset($data->availabilityconditionsjson);
125 if ($data->availability === '') {
126 $data->availability = null;
127 }
128 }
f26481c2 129 course_update_section($course, $section, $data);
ed29bf0f 130
0a5ece3d 131 $PAGE->navigation->clear_cache();
fc79ede5 132 redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
d9cb06dc 133}
578fdd0a 134
ed29bf0f 135// The edit form is displayed for the first time or if there was validation error on the previous step.
fc79ede5 136$sectionname = get_section_name($course, $sectionnum);
7487c856
SH
137$stredit = get_string('edita', '', " $sectionname");
138$strsummaryof = get_string('summaryof', '', " $sectionname");
d9cb06dc 139
140$PAGE->set_title($stredit);
e34a326f 141$PAGE->set_heading($course->fullname);
d9cb06dc 142$PAGE->navbar->add($stredit);
d9cb06dc 143echo $OUTPUT->header();
578fdd0a 144
b54a0547 145echo $OUTPUT->heading($strsummaryof);
a19a06d0 146
d9cb06dc 147$mform->display();
0831fd78 148echo $OUTPUT->footer();