MDL-79920 tool_mfa: Minor updates
[moodle.git] / course / section.php
blobfbcd0dd1394f6c60990907de13daf5f92850eb92
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();
49 $format->set_sectionid($section->id);
51 // When the course format doesn't support sections, redirect to course page.
52 if (!course_format_uses_sections($course->format)) {
53 redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
56 // Prevent caching of this page to stop confusion when changing page after making AJAX changes.
57 $PAGE->set_cacheable(false);
59 context_helper::preload_course($course->id);
60 $context = context_course::instance($course->id, MUST_EXIST);
62 require_login($course);
64 // Must set layout before getting section info. See MDL-47555.
65 $PAGE->set_pagelayout('course');
66 $PAGE->add_body_classes(['limitedwidth', 'single-section-page']);
68 // Get section details and check it exists.
69 $modinfo = get_fast_modinfo($course);
70 $sectioninfo = $modinfo->get_section_info($section->section, MUST_EXIST);
72 // Check user is allowed to see it.
73 if (!$sectioninfo->uservisible) {
74 // Check if coursesection has conditions affecting availability and if
75 // so, output availability info.
76 if ($sectioninfo->visible && $sectioninfo->availableinfo) {
77 $sectionname = get_section_name($course, $sectioninfo);
78 $message = get_string('notavailablecourse', '', $sectionname);
79 redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
80 } else {
81 // Note: We actually already know they don't have this capability
82 // or uservisible would have been true; this is just to get the
83 // correct error message shown.
84 require_capability('moodle/course:viewhiddensections', $context);
88 $PAGE->set_pagetype('section-view-' . $course->format);
89 $PAGE->set_other_editing_capability('moodle/course:update');
90 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
91 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
92 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
93 $PAGE->set_other_editing_capability('moodle/course:movesections');
95 $renderer = $PAGE->get_renderer('format_' . $course->format);
97 // This is used by the Classic theme to change the editing mode based on the 'edit' parameter value.
98 if (!isset($USER->editing)) {
99 $USER->editing = 0;
101 if ($PAGE->user_allowed_editing()) {
102 if (($edit == 1) && confirm_sesskey()) {
103 $USER->editing = 1;
104 $url = new moodle_url($PAGE->url, ['notifyeditingon' => 1]);
105 redirect($url);
106 } else if (($edit == 0) && confirm_sesskey()) {
107 $USER->editing = 0;
108 if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
109 $USER->activitycopy = false;
110 $USER->activitycopycourse = null;
112 redirect($PAGE->url);
116 // This is used by the Classic theme, to display the Turn editing on/off button.
117 // 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
118 // in the course admin block. It also means you can back out of a situation where you removed the admin block.
119 if ($PAGE->user_allowed_editing()) {
120 $buttons = $OUTPUT->edit_button($PAGE->url);
121 $PAGE->set_button($buttons);
124 // Make the title more specific when editing, for accessibility reasons.
125 $editingtitle = '';
126 if ($PAGE->user_is_editing()) {
127 $editingtitle = 'editing';
129 $sectionname = get_string('sectionname', "format_$course->format");
130 $sectiontitle = get_section_name($course, $section);
131 $PAGE->set_title(
132 get_string(
133 'coursesectiontitle' . $editingtitle,
134 'moodle',
135 ['course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
139 // Add bulk editing control.
140 $bulkbutton = $renderer->bulk_editing_button($format);
141 if (!empty($bulkbutton)) {
142 $PAGE->add_header_action($bulkbutton);
145 // Add to the header the control menu for the section.
146 if ($format->show_editor()) {
147 $sectionclass = new \core_courseformat\output\local\content\section($format, $sectioninfo);
148 $renderable = $sectionclass->export_for_template($renderer);
149 if (property_exists($renderable->controlmenu, 'menu')) {
150 $controlmenuhtml = $renderable->controlmenu->menu;
151 $PAGE->add_header_action($controlmenuhtml);
153 $sectionheading = $OUTPUT->container(
154 $OUTPUT->render($format->inplace_editable_render_section_name($sectioninfo, false)),
155 attributes: ['data-for' => 'section_title'],
157 $PAGE->set_heading($sectionheading, false, false);
158 } else {
159 $PAGE->set_heading($sectiontitle);
162 $PAGE->set_secondary_navigation(false);
164 echo $OUTPUT->header();
166 // Show communication room status notification.
167 if (core_communication\api::is_available() && has_capability('moodle/course:update', $context)) {
168 $communication = \core_communication\api::load_by_instance(
169 $context,
170 'core_course',
171 'coursecommunication',
172 $course->id
174 $communication->show_communication_room_status_notification();
177 // Display a warning if asynchronous backups are pending for this course.
178 if ($PAGE->user_is_editing()) {
179 require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
180 if (async_helper::is_async_pending($course->id, 'course', 'backup')) {
181 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
185 echo $renderer->container_start('course-content');
187 // Include course AJAX.
188 include_course_ajax($course, $modinfo->get_used_module_names());
190 $outputclass = $format->get_output_classname('content');
191 $widget = new $outputclass($format);
192 echo $renderer->render($widget);
194 // Include course format javascript files.
195 $jsfiles = $format->get_required_jsfiles();
196 foreach ($jsfiles as $jsfile) {
197 $PAGE->requires->js($jsfile);
200 echo $renderer->container_end();
202 // Trigger section viewed event.
203 course_section_view($context, $sectionid);
205 // Load the view JS module if completion tracking is enabled for this course.
206 $completion = new completion_info($course);
207 if ($completion->is_enabled()) {
208 $PAGE->requires->js_call_amd('core_course/view', 'init');
211 echo $OUTPUT->footer();