Merge branch 'MDL-79937_fixlessonmatching' of https://github.com/catalystfd/moodle
[moodle.git] / course / section.php
blob01e1aba09ea7218c4f4125473ae19b10a9866ba2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Display a course section.
20 * @package core_course
21 * @copyright 2023 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir.'/completionlib.php');
29 redirect_if_major_upgrade_required();
31 $sectionid = required_param('id', PARAM_INT);
32 // This parameter is used by the classic theme to force editing on.
33 $edit = optional_param('edit', -1, PARAM_BOOL);
35 $section = $DB->get_record('course_sections', ['id' => $sectionid], '*', MUST_EXIST);
37 // Defined here to avoid notices on errors.
38 $PAGE->set_url('/course/section.php', ['id' => $sectionid]);
40 if ($section->course == SITEID) {
41 // The home page is not a real course.
42 redirect($CFG->wwwroot .'/?redirect=0');
45 $course = get_course($section->course);
46 // Fix course format if it is no longer installed.
47 $format = course_get_format($course);
48 $course->format = $format->get_format();
50 // When the course format doesn't support sections, redirect to course page.
51 if (!course_format_uses_sections($course->format)) {
52 redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
55 // Prevent caching of this page to stop confusion when changing page after making AJAX changes.
56 $PAGE->set_cacheable(false);
58 context_helper::preload_course($course->id);
59 $context = context_course::instance($course->id, MUST_EXIST);
61 require_login($course);
63 // Must set layout before getting section info. See MDL-47555.
64 $PAGE->set_pagelayout('course');
65 $PAGE->add_body_class('limitedwidth');
67 // Get section details and check it exists.
68 $modinfo = get_fast_modinfo($course);
69 $coursesections = $modinfo->get_section_info($section->section, MUST_EXIST);
71 // Check user is allowed to see it.
72 if (!$coursesections->uservisible) {
73 // Check if coursesection has conditions affecting availability and if
74 // so, output availability info.
75 if ($coursesections->visible && $coursesections->availableinfo) {
76 $sectionname = get_section_name($course, $coursesections);
77 $message = get_string('notavailablecourse', '', $sectionname);
78 redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
79 } else {
80 // Note: We actually already know they don't have this capability
81 // or uservisible would have been true; this is just to get the
82 // correct error message shown.
83 require_capability('moodle/course:viewhiddensections', $context);
87 $PAGE->set_pagetype('course-view-' . $course->format);
88 $PAGE->set_other_editing_capability('moodle/course:update');
89 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
90 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
91 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
92 $PAGE->set_other_editing_capability('moodle/course:movesections');
94 $renderer = $PAGE->get_renderer('format_' . $course->format);
96 // This is used by the Classic theme to change the editing mode based on the 'edit' parameter value.
97 if (!isset($USER->editing)) {
98 $USER->editing = 0;
100 if ($PAGE->user_allowed_editing()) {
101 if (($edit == 1) && confirm_sesskey()) {
102 $USER->editing = 1;
103 $url = new moodle_url($PAGE->url, ['notifyeditingon' => 1]);
104 redirect($url);
105 } else if (($edit == 0) && confirm_sesskey()) {
106 $USER->editing = 0;
107 if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
108 $USER->activitycopy = false;
109 $USER->activitycopycourse = null;
111 redirect($PAGE->url);
115 // This is used by the Classic theme, to display the Turn editing on/off button.
116 // We are currently keeping the button here from 1.x to help new teachers figure out what to do, even though the link also appears
117 // in the course admin block. It also means you can back out of a situation where you removed the admin block.
118 if ($PAGE->user_allowed_editing()) {
119 $buttons = $OUTPUT->edit_button($PAGE->url);
120 $PAGE->set_button($buttons);
123 // Make the title more specific when editing, for accessibility reasons.
124 $editingtitle = '';
125 if ($PAGE->user_is_editing()) {
126 $editingtitle = 'editing';
128 $sectionname = get_string('sectionname', "format_$course->format");
129 $sectiontitle = get_section_name($course, $section);
130 $PAGE->set_title(
131 get_string(
132 'coursesectiontitle' . $editingtitle,
133 'moodle',
134 ['course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
138 // Add bulk editing control.
139 $bulkbutton = $renderer->bulk_editing_button($format);
140 if (!empty($bulkbutton)) {
141 $PAGE->add_header_action($bulkbutton);
144 $PAGE->set_heading($course->fullname);
145 echo $OUTPUT->header();
147 // Show communication room status notification.
148 if (core_communication\api::is_available() && has_capability('moodle/course:update', $context)) {
149 $communication = \core_communication\api::load_by_instance(
150 $context,
151 'core_course',
152 'coursecommunication',
153 $course->id
155 $communication->show_communication_room_status_notification();
158 // Display a warning if asynchronous backups are pending for this course.
159 if ($PAGE->user_is_editing()) {
160 require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
161 if (async_helper::is_async_pending($course->id, 'course', 'backup')) {
162 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
166 echo $renderer->container_start('course-content');
168 // Include course AJAX.
169 include_course_ajax($course, $modinfo->get_used_module_names());
171 $format->set_section_number($section->section);
172 $outputclass = $format->get_output_classname('content');
173 $widget = new $outputclass($format);
174 echo $renderer->render($widget);
176 // Include course format javascript files.
177 $jsfiles = $format->get_required_jsfiles();
178 foreach ($jsfiles as $jsfile) {
179 $PAGE->requires->js($jsfile);
182 echo $renderer->container_end();
184 // Trigger course viewed event.
185 course_view($context, $section->section);
187 // Load the view JS module if completion tracking is enabled for this course.
188 $completion = new completion_info($course);
189 if ($completion->is_enabled()) {
190 $PAGE->requires->js_call_amd('core_course/view', 'init');
193 echo $OUTPUT->footer();