Merge branch 'MDL-72498' of git://github.com/paulholden/moodle
[moodle.git] / course / view.php
blob41cf7a80af3bc964bd8f87515d2abefd2fd3b9e5
1 <?php
3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/completionlib.php');
9 $id = optional_param('id', 0, PARAM_INT);
10 $name = optional_param('name', '', PARAM_TEXT);
11 $edit = optional_param('edit', -1, PARAM_BOOL);
12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $idnumber = optional_param('idnumber', '', PARAM_RAW);
15 $sectionid = optional_param('sectionid', 0, PARAM_INT);
16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
18 $marker = optional_param('marker',-1 , PARAM_INT);
19 $switchrole = optional_param('switchrole',-1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
20 $return = optional_param('return', 0, PARAM_LOCALURL);
22 $params = array();
23 if (!empty($name)) {
24 $params = array('shortname' => $name);
25 } else if (!empty($idnumber)) {
26 $params = array('idnumber' => $idnumber);
27 } else if (!empty($id)) {
28 $params = array('id' => $id);
29 }else {
30 print_error('unspecifycourseid', 'error');
33 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
35 $urlparams = array('id' => $course->id);
37 // Sectionid should get priority over section number
38 if ($sectionid) {
39 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
41 if ($section) {
42 $urlparams['section'] = $section;
45 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
47 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
48 $PAGE->set_cacheable(false);
50 context_helper::preload_course($course->id);
51 $context = context_course::instance($course->id, MUST_EXIST);
53 // Remove any switched roles before checking login
54 if ($switchrole == 0 && confirm_sesskey()) {
55 role_switch($switchrole, $context);
58 require_login($course);
60 // Switchrole - sanity check in cost-order...
61 $reset_user_allowed_editing = false;
62 if ($switchrole > 0 && confirm_sesskey() &&
63 has_capability('moodle/role:switchroles', $context)) {
64 // is this role assignable in this context?
65 // inquiring minds want to know...
66 $aroles = get_switchable_roles($context);
67 if (is_array($aroles) && isset($aroles[$switchrole])) {
68 role_switch($switchrole, $context);
69 // Double check that this role is allowed here
70 require_login($course);
72 // reset course page state - this prevents some weird problems ;-)
73 $USER->activitycopy = false;
74 $USER->activitycopycourse = NULL;
75 unset($USER->activitycopyname);
76 unset($SESSION->modform);
77 $USER->editing = 0;
78 $reset_user_allowed_editing = true;
81 //If course is hosted on an external server, redirect to corresponding
82 //url with appropriate authentication attached as parameter
83 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
84 include $CFG->dirroot .'/course/externservercourse.php';
85 if (function_exists('extern_server_course')) {
86 if ($extern_url = extern_server_course($course)) {
87 redirect($extern_url);
93 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
95 // Must set layout before gettting section info. See MDL-47555.
96 $PAGE->set_pagelayout('course');
98 if ($section and $section > 0) {
100 // Get section details and check it exists.
101 $modinfo = get_fast_modinfo($course);
102 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
104 // Check user is allowed to see it.
105 if (!$coursesections->uservisible) {
106 // Check if coursesection has conditions affecting availability and if
107 // so, output availability info.
108 if ($coursesections->visible && $coursesections->availableinfo) {
109 $sectionname = get_section_name($course, $coursesections);
110 $message = get_string('notavailablecourse', '', $sectionname);
111 redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
112 } else {
113 // Note: We actually already know they don't have this capability
114 // or uservisible would have been true; this is just to get the
115 // correct error message shown.
116 require_capability('moodle/course:viewhiddensections', $context);
121 // Fix course format if it is no longer installed
122 $format = course_get_format($course);
123 $course->format = $format->get_format();
125 $PAGE->set_pagetype('course-view-' . $course->format);
126 $PAGE->set_other_editing_capability('moodle/course:update');
127 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
128 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
129 if (course_format_uses_sections($course->format)) {
130 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
131 $PAGE->set_other_editing_capability('moodle/course:movesections');
134 // Preload course format renderer before output starts.
135 // This is a little hacky but necessary since
136 // format.php is not included until after output starts
137 $format->get_renderer($PAGE);
139 if ($reset_user_allowed_editing) {
140 // ugly hack
141 unset($PAGE->_user_allowed_editing);
144 if (!isset($USER->editing)) {
145 $USER->editing = 0;
147 if ($PAGE->user_allowed_editing()) {
148 if (($edit == 1) and confirm_sesskey()) {
149 $USER->editing = 1;
150 // Redirect to site root if Editing is toggled on frontpage
151 if ($course->id == SITEID) {
152 redirect($CFG->wwwroot .'/?redirect=0');
153 } else if (!empty($return)) {
154 redirect($CFG->wwwroot . $return);
155 } else {
156 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
157 redirect($url);
159 } else if (($edit == 0) and confirm_sesskey()) {
160 $USER->editing = 0;
161 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
162 $USER->activitycopy = false;
163 $USER->activitycopycourse = NULL;
165 // Redirect to site root if Editing is toggled on frontpage
166 if ($course->id == SITEID) {
167 redirect($CFG->wwwroot .'/?redirect=0');
168 } else if (!empty($return)) {
169 redirect($CFG->wwwroot . $return);
170 } else {
171 redirect($PAGE->url);
175 if (has_capability('moodle/course:sectionvisibility', $context)) {
176 if ($hide && confirm_sesskey()) {
177 set_section_visible($course->id, $hide, '0');
178 redirect($PAGE->url);
181 if ($show && confirm_sesskey()) {
182 set_section_visible($course->id, $show, '1');
183 redirect($PAGE->url);
187 if (!empty($section) && !empty($move) &&
188 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
189 $destsection = $section + $move;
190 if (move_section_to($course, $section, $destsection)) {
191 if ($course->id == SITEID) {
192 redirect($CFG->wwwroot . '/?redirect=0');
193 } else {
194 redirect(course_get_url($course));
196 } else {
197 echo $OUTPUT->notification('An error occurred while moving a section');
200 } else {
201 $USER->editing = 0;
204 $SESSION->fromdiscussion = $PAGE->url->out(false);
207 if ($course->id == SITEID) {
208 // This course is not a real course.
209 redirect($CFG->wwwroot .'/');
212 // Determine whether the user has permission to download course content.
213 $candownloadcourse = \core\content::can_export_context($context, $USER);
215 // We are currently keeping the button here from 1.x to help new teachers figure out
216 // what to do, even though the link also appears in the course admin block. It also
217 // means you can back out of a situation where you removed the admin block. :)
218 if ($PAGE->user_allowed_editing()) {
219 $buttons = $OUTPUT->edit_button($PAGE->url);
220 $PAGE->set_button($buttons);
221 } else if ($candownloadcourse) {
222 // Show the download course content button if user has permission to access it.
223 // Only showing this if user doesn't have edit rights, since those who do will access it via the actions menu.
224 $buttonattr = \core_course\output\content_export_link::get_attributes($context);
225 $button = new single_button($buttonattr->url, $buttonattr->displaystring, 'post', false, $buttonattr->elementattributes);
226 $PAGE->set_button($OUTPUT->render($button));
229 // If viewing a section, make the title more specific
230 if ($section and $section > 0 and course_format_uses_sections($course->format)) {
231 $sectionname = get_string('sectionname', "format_$course->format");
232 $sectiontitle = get_section_name($course, $section);
233 $PAGE->set_title(get_string('coursesectiontitle', 'moodle', array('course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)));
234 } else {
235 $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
238 $PAGE->set_heading($course->fullname);
239 echo $OUTPUT->header();
241 if ($USER->editing == 1) {
243 // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
244 require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
246 if (async_helper::is_async_pending($id, 'course', 'backup')) {
247 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
251 // Course wrapper start.
252 echo html_writer::start_tag('div', array('class'=>'course-content'));
254 // make sure that section 0 exists (this function will create one if it is missing)
255 course_create_sections_if_missing($course, 0);
257 // get information about course modules and existing module types
258 // format.php in course formats may rely on presence of these variables
259 $modinfo = get_fast_modinfo($course);
260 $modnames = get_module_types_names();
261 $modnamesplural = get_module_types_names(true);
262 $modnamesused = $modinfo->get_used_module_names();
263 $mods = $modinfo->get_cms();
264 $sections = $modinfo->get_section_info_all();
266 // CAUTION, hacky fundamental variable defintion to follow!
267 // Note that because of the way course fromats are constructed though
268 // inclusion we pass parameters around this way..
269 $displaysection = $section;
271 // Include the actual course format.
272 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
273 // Content wrapper end.
275 echo html_writer::end_tag('div');
277 // Trigger course viewed event.
278 // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
279 // anything after that point.
280 course_view(context_course::instance($course->id), $section);
282 // Include course AJAX
283 include_course_ajax($course, $modnamesused);
285 // If available, include the JS to prepare the download course content modal.
286 if ($candownloadcourse) {
287 $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
290 // Load the view JS module if completion tracking is enabled for this course.
291 $completion = new completion_info($course);
292 if ($completion->is_enabled()) {
293 $PAGE->requires->js_call_amd('core_course/view', 'init');
296 echo $OUTPUT->footer();