2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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', 0, 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
);
48 $params = ['shortname' => $name];
49 } else if (!empty($idnumber)) {
50 $params = ['idnumber' => $idnumber];
51 } else if (!empty($id)) {
52 $params = ['id' => $id];
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.
63 $section = $DB->get_field('course_sections', 'section', ['id' => $sectionid, 'course' => $course->id
], MUST_EXIST
);
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
);
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
);
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) {
168 unset($PAGE->_user_allowed_editing
);
171 if (!isset($USER->editing
)) {
174 if ($PAGE->user_allowed_editing()) {
175 if (($edit == 1) && confirm_sesskey()) {
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);
183 $url = new moodle_url($PAGE->url
, ['notifyeditingon' => 1]);
186 } else if (($edit == 0) && confirm_sesskey()) {
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);
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 redirect($PAGE->url
);
208 if ($show && confirm_sesskey()) {
209 set_section_visible($course->id
, $show, '1');
210 redirect($PAGE->url
);
215 !empty($section) && !empty($coursesections) && !empty($duplicatesection)
216 && has_capability('moodle/course:update', $context) && confirm_sesskey()
218 $newsection = $format->duplicate_section($coursesections);
219 redirect(course_get_url($course, $newsection->section
));
222 if (!empty($section) && !empty($move) &&
223 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
224 $destsection = $section +
$move;
225 if (move_section_to($course, $section, $destsection)) {
226 if ($course->id
== SITEID
) {
227 redirect($CFG->wwwroot
. '/?redirect=0');
229 if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE
) {
230 redirect(course_get_url($course));
232 redirect(course_get_url($course, $destsection));
236 echo $OUTPUT->notification('An error occurred while moving a section');
243 $SESSION->fromdiscussion
= $PAGE->url
->out(false);
246 if ($course->id
== SITEID
) {
247 // This course is not a real course.
248 redirect($CFG->wwwroot
.'/?redirect=0');
251 // Determine whether the user has permission to download course content.
252 $candownloadcourse = \core\content
::can_export_context($context, $USER);
254 // We are currently keeping the button here from 1.x to help new teachers figure out
255 // what to do, even though the link also appears in the course admin block. It also
256 // means you can back out of a situation where you removed the admin block.
257 if ($PAGE->user_allowed_editing()) {
258 $buttons = $OUTPUT->edit_button($PAGE->url
);
259 $PAGE->set_button($buttons);
262 // If viewing a section, make the title more specific.
263 if ($section && $section > 0 && course_format_uses_sections($course->format
)) {
264 $sectionname = get_string('sectionname', "format_$course->format");
265 $sectiontitle = get_section_name($course, $section);
268 'coursesectiontitle',
270 ['course' => $course->fullname
, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
274 $PAGE->set_title(get_string('coursetitle', 'moodle', ['course' => $course->fullname
]));
277 // Add bulk editing control.
278 $bulkbutton = $renderer->bulk_editing_button($format);
279 if (!empty($bulkbutton)) {
280 $PAGE->add_header_action($bulkbutton);
283 $PAGE->set_heading($course->fullname
);
284 echo $OUTPUT->header();
286 // Show communication room status notification.
287 if (core_communication\api
::is_available() && has_capability('moodle/course:update', $context)) {
288 $communication = \core_communication\api
::load_by_instance(
290 'coursecommunication',
293 $communication->show_communication_room_status_notification();
296 if ($USER->editing
== 1) {
298 // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
299 require_once($CFG->dirroot
. '/backup/util/helper/async_helper.class.php');
301 if (async_helper
::is_async_pending($id, 'course', 'backup')) {
302 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
306 // Course wrapper start.
307 echo html_writer
::start_tag('div', ['class' => 'course-content']);
309 // Make sure that section 0 exists (this function will create one if it is missing).
310 course_create_sections_if_missing($course, 0);
312 // Get information about course modules and existing module types.
313 // format.php in course formats may rely on presence of these variables.
314 $modinfo = get_fast_modinfo($course);
315 $modnames = get_module_types_names();
316 $modnamesplural = get_module_types_names(true);
317 $modnamesused = $modinfo->get_used_module_names();
318 $mods = $modinfo->get_cms();
319 $sections = $modinfo->get_section_info_all();
321 // CAUTION, hacky fundamental variable defintion to follow!
322 // Note that because of the way course fromats are constructed though
323 // inclusion we pass parameters around this way.
324 $displaysection = $section;
326 // Include course AJAX.
327 include_course_ajax($course, $modnamesused);
329 // Include the actual course format.
330 require($CFG->dirroot
.'/course/format/'. $course->format
.'/format.php');
331 // Content wrapper end.
333 echo html_writer
::end_tag('div');
335 // Trigger course viewed event.
336 // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
337 // anything after that point.
338 course_view(context_course
::instance($course->id
), $section);
340 // If available, include the JS to prepare the download course content modal.
341 if ($candownloadcourse) {
342 $PAGE->requires
->js_call_amd('core_course/downloadcontent', 'init');
345 // Load the view JS module if completion tracking is enabled for this course.
346 $completion = new completion_info($course);
347 if ($completion->is_enabled()) {
348 $PAGE->requires
->js_call_amd('core_course/view', 'init');
351 echo $OUTPUT->footer();