Merge branch 'MDL-81065-master' of https://github.com/jleyva/moodle
[moodle.git] / course / view.php
blobe25c664945d34c155641d3345634ef56dc9a53c7
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 the course home page.
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_course
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir.'/completionlib.php');
29 redirect_if_major_upgrade_required();
31 $id = optional_param('id', 0, PARAM_INT);
32 $name = optional_param('name', '', PARAM_TEXT);
33 $edit = optional_param('edit', -1, PARAM_BOOL);
34 $hide = optional_param('hide', 0, PARAM_INT);
35 $show = optional_param('show', 0, PARAM_INT);
36 $duplicatesection = optional_param('duplicatesection', 0, PARAM_INT);
37 $idnumber = optional_param('idnumber', '', PARAM_RAW);
38 $sectionid = optional_param('sectionid', 0, PARAM_INT);
39 $section = optional_param('section', null, PARAM_INT);
40 $expandsection = optional_param('expandsection', -1, PARAM_INT);
41 $move = optional_param('move', 0, PARAM_INT);
42 $marker = optional_param('marker', -1 , PARAM_INT);
43 $switchrole = optional_param('switchrole', -1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
44 $return = optional_param('return', 0, PARAM_LOCALURL);
46 $params = [];
47 if (!empty($name)) {
48 $params = ['shortname' => $name];
49 } else if (!empty($idnumber)) {
50 $params = ['idnumber' => $idnumber];
51 } else if (!empty($id)) {
52 $params = ['id' => $id];
53 } else {
54 throw new \moodle_exception('unspecifycourseid', 'error');
57 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
59 $urlparams = ['id' => $course->id];
61 // Sectionid should get priority over section number.
62 if ($sectionid) {
63 $section = $DB->get_field('course_sections', 'section', ['id' => $sectionid, 'course' => $course->id], MUST_EXIST);
65 if (!is_null($section)) {
66 $urlparams['section'] = $section;
68 if ($expandsection !== -1) {
69 $urlparams['expandsection'] = $expandsection;
72 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc.
74 // Prevent caching of this page to stop confusion when changing page after making AJAX changes.
75 $PAGE->set_cacheable(false);
77 context_helper::preload_course($course->id);
78 $context = context_course::instance($course->id, MUST_EXIST);
80 // Remove any switched roles before checking login.
81 if ($switchrole == 0 && confirm_sesskey()) {
82 role_switch($switchrole, $context);
85 require_login($course);
87 // Switchrole - sanity check in cost-order...
88 $resetuserallowedediting = false;
89 if ($switchrole > 0 && confirm_sesskey() &&
90 has_capability('moodle/role:switchroles', $context)) {
91 // Is this role assignable in this context?
92 // Inquiring minds want to know.
93 $aroles = get_switchable_roles($context);
94 if (is_array($aroles) && isset($aroles[$switchrole])) {
95 role_switch($switchrole, $context);
96 // Double check that this role is allowed here.
97 require_login($course);
99 // Reset course page state. This prevents some weird problems.
100 $USER->activitycopy = false;
101 $USER->activitycopycourse = null;
102 unset($USER->activitycopyname);
103 unset($SESSION->modform);
104 $USER->editing = 0;
105 $resetuserallowedediting = true;
108 // If course is hosted on an external server, redirect to corresponding
109 // url with appropriate authentication attached as parameter.
110 if (file_exists($CFG->dirroot . '/course/externservercourse.php')) {
111 include($CFG->dirroot . '/course/externservercourse.php');
112 if (function_exists('extern_server_course')) {
113 if ($externurl = extern_server_course($course)) {
114 redirect($externurl);
119 require_once($CFG->dirroot.'/calendar/lib.php'); // This is after login because it needs $USER.
121 // Must set layout before gettting section info. See MDL-47555.
122 $PAGE->set_pagelayout('course');
123 $PAGE->add_body_class('limitedwidth');
125 if ($section && $section > 0) {
127 // Get section details and check it exists.
128 $modinfo = get_fast_modinfo($course);
129 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
131 // Check user is allowed to see it.
132 if (!$coursesections->uservisible) {
133 // Check if coursesection has conditions affecting availability and if
134 // so, output availability info.
135 if ($coursesections->visible && $coursesections->availableinfo) {
136 $sectionname = get_section_name($course, $coursesections);
137 $message = get_string('notavailablecourse', '', $sectionname);
138 redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
139 } else {
140 // Note: We actually already know they don't have this capability
141 // or uservisible would have been true; this is just to get the
142 // correct error message shown.
143 require_capability('moodle/course:viewhiddensections', $context);
148 // Fix course format if it is no longer installed.
149 $format = course_get_format($course);
150 $course->format = $format->get_format();
152 $PAGE->set_pagetype('course-view-' . $course->format);
153 $PAGE->set_other_editing_capability('moodle/course:update');
154 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
155 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
156 if (course_format_uses_sections($course->format)) {
157 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
158 $PAGE->set_other_editing_capability('moodle/course:movesections');
161 // Preload course format renderer before output starts.
162 // This is a little hacky but necessary since
163 // format.php is not included until after output starts.
164 $renderer = $format->get_renderer($PAGE);
166 if ($resetuserallowedediting) {
167 // Ugly hack.
168 unset($PAGE->_user_allowed_editing);
171 if (!isset($USER->editing)) {
172 $USER->editing = 0;
174 if ($PAGE->user_allowed_editing()) {
175 if (($edit == 1) && confirm_sesskey()) {
176 $USER->editing = 1;
177 // Redirect to site root if Editing is toggled on frontpage.
178 if ($course->id == SITEID) {
179 redirect($CFG->wwwroot .'/?redirect=0');
180 } else if (!empty($return)) {
181 redirect($CFG->wwwroot . $return);
182 } else {
183 $url = new moodle_url($PAGE->url, ['notifyeditingon' => 1]);
184 redirect($url);
186 } else if (($edit == 0) && confirm_sesskey()) {
187 $USER->editing = 0;
188 if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
189 $USER->activitycopy = false;
190 $USER->activitycopycourse = null;
192 // Redirect to site root if Editing is toggled on frontpage.
193 if ($course->id == SITEID) {
194 redirect($CFG->wwwroot .'/?redirect=0');
195 } else if (!empty($return)) {
196 redirect($CFG->wwwroot . $return);
197 } else {
198 redirect($PAGE->url);
202 if (has_capability('moodle/course:sectionvisibility', $context)) {
203 if ($hide && confirm_sesskey()) {
204 set_section_visible($course->id, $hide, '0');
205 if ($sectionid) {
206 redirect(course_get_url($course, $section, ['navigation' => true]));
207 } else {
208 redirect($PAGE->url);
212 if ($show && confirm_sesskey()) {
213 set_section_visible($course->id, $show, '1');
214 if ($sectionid) {
215 redirect(course_get_url($course, $section, ['navigation' => true]));
216 } else {
217 redirect($PAGE->url);
222 if ($marker >= 0 && confirm_sesskey()) {
223 course_set_marker($course->id, $marker);
224 if ($sectionid) {
225 redirect(course_get_url($course, $section, ['navigation' => true]));
226 } else {
227 redirect($PAGE->url);
231 if (
232 !empty($section) && !empty($coursesections) && !empty($duplicatesection)
233 && has_capability('moodle/course:update', $context) && confirm_sesskey()
235 $newsection = $format->duplicate_section($coursesections);
236 redirect(course_get_url($course, $newsection->section));
239 if (!empty($section) && !empty($move) &&
240 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
241 $destsection = $section + $move;
242 if (move_section_to($course, $section, $destsection)) {
243 if ($course->id == SITEID) {
244 redirect($CFG->wwwroot . '/?redirect=0');
245 } else {
246 if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE) {
247 redirect(course_get_url($course));
248 } else {
249 redirect(course_get_url($course, $destsection));
252 } else {
253 echo $OUTPUT->notification('An error occurred while moving a section');
256 } else {
257 $USER->editing = 0;
260 $SESSION->fromdiscussion = $PAGE->url->out(false);
263 if ($course->id == SITEID) {
264 // This course is not a real course.
265 redirect($CFG->wwwroot .'/?redirect=0');
268 // Determine whether the user has permission to download course content.
269 $candownloadcourse = \core\content::can_export_context($context, $USER);
271 // We are currently keeping the button here from 1.x to help new teachers figure out
272 // what to do, even though the link also appears in the course admin block. It also
273 // means you can back out of a situation where you removed the admin block.
274 if ($PAGE->user_allowed_editing()) {
275 $buttons = $OUTPUT->edit_button($PAGE->url);
276 $PAGE->set_button($buttons);
279 $editingtitle = '';
280 if ($PAGE->user_is_editing()) {
281 // Append this to the page title's lang string to get its equivalent when editing mode is turned on.
282 $editingtitle = 'editing';
285 // If viewing a section, make the title more specific.
286 if ($section && $section > 0 && course_format_uses_sections($course->format)) {
287 $sectionname = get_string('sectionname', "format_$course->format");
288 $sectiontitle = get_section_name($course, $section);
289 $PAGE->set_title(
290 get_string(
291 'coursesectiontitle' . $editingtitle,
292 'moodle',
293 ['course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
296 } else {
297 $PAGE->set_title(get_string('coursetitle' . $editingtitle, 'moodle', ['course' => $course->fullname]));
300 // Add bulk editing control.
301 $bulkbutton = $renderer->bulk_editing_button($format);
302 if (!empty($bulkbutton)) {
303 $PAGE->add_header_action($bulkbutton);
306 $PAGE->set_heading($course->fullname);
307 echo $OUTPUT->header();
309 // Show communication room status notification.
310 if (core_communication\api::is_available() && has_capability('moodle/course:update', $context)) {
311 $communication = \core_communication\api::load_by_instance(
312 $context,
313 'core_course',
314 'coursecommunication',
315 $course->id
317 $communication->show_communication_room_status_notification();
320 if ($USER->editing == 1) {
322 // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
323 require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
325 if (async_helper::is_async_pending($id, 'course', 'backup')) {
326 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
330 // Course wrapper start.
331 echo html_writer::start_tag('div', ['class' => 'course-content']);
333 // Make sure that section 0 exists (this function will create one if it is missing).
334 course_create_sections_if_missing($course, 0);
336 // Get information about course modules and existing module types.
337 // format.php in course formats may rely on presence of these variables.
338 $modinfo = get_fast_modinfo($course);
339 $modnames = get_module_types_names();
340 $modnamesplural = get_module_types_names(true);
341 $modnamesused = $modinfo->get_used_module_names();
342 $mods = $modinfo->get_cms();
343 $sections = $modinfo->get_section_info_all();
345 // CAUTION, hacky fundamental variable defintion to follow!
346 // Note that because of the way course fromats are constructed though
347 // inclusion we pass parameters around this way.
348 $displaysection = $section;
350 // Include course AJAX.
351 include_course_ajax($course, $modnamesused);
353 // Include the actual course format.
354 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
355 // Content wrapper end.
357 echo html_writer::end_tag('div');
359 // Trigger course viewed event.
360 // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
361 // anything after that point.
362 course_view(context_course::instance($course->id), $section);
364 // If available, include the JS to prepare the download course content modal.
365 if ($candownloadcourse) {
366 $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
369 // Load the view JS module if completion tracking is enabled for this course.
370 $completion = new completion_info($course);
371 if ($completion->is_enabled()) {
372 $PAGE->requires->js_call_amd('core_course/view', 'init');
375 echo $OUTPUT->footer();