MDL-37206 preload course format renderer in course/view.php
[moodle.git] / course / view.php
CommitLineData
aa6c1ced 1<?php
f9903ed0 2
3// Display the course home page.
4
08cebf19 5 require_once('../config.php');
6 require_once('lib.php');
afc58a28 7 require_once($CFG->dirroot.'/mod/forum/lib.php');
ce4dfd27 8 require_once($CFG->libdir.'/conditionlib.php');
516c5eca 9 require_once($CFG->libdir.'/completionlib.php');
f9903ed0 10
2335781f 11 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 12 $name = optional_param('name', '', PARAM_RAW);
13 $edit = optional_param('edit', -1, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $idnumber = optional_param('idnumber', '', PARAM_RAW);
5249ba74 17 $sectionid = optional_param('sectionid', 0, PARAM_INT);
0cdae0dc 18 $section = optional_param('section', 0, PARAM_INT);
19 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 20 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 21 $switchrole = optional_param('switchrole',-1, PARAM_INT);
01e0e704 22 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
52fc56a7 23 $return = optional_param('return', 0, PARAM_LOCALURL);
3a52e764 24
599a52fe 25 $params = array();
2335781f 26 if (!empty($name)) {
599a52fe 27 $params = array('shortname' => $name);
816acb46 28 } else if (!empty($idnumber)) {
599a52fe
DP
29 $params = array('idnumber' => $idnumber);
30 } else if (!empty($id)) {
31 $params = array('id' => $id);
32 }else {
33 print_error('unspecifycourseid', 'error');
f9903ed0 34 }
35
599a52fe
DP
36 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
37
45774bdd 38 $urlparams = array('id' => $course->id);
5249ba74 39
2c7a72df 40 // Sectionid should get priority over section number
5249ba74
AA
41 if ($sectionid) {
42 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
43 }
45774bdd
DP
44 if ($section) {
45 $urlparams['section'] = $section;
46 }
47
48 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
088e3363 49
fbb4c959
SH
50 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
51 $PAGE->set_cacheable(false);
52
00653161 53 preload_course_contexts($course->id);
599a52fe 54 $context = context_course::instance($course->id, MUST_EXIST);
3a52e764 55
d7d4b0e5 56 // Remove any switched roles before checking login
57 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 58 role_switch($switchrole, $context);
59 }
60
a6af674a 61 require_login($course);
388f8911 62
d7d4b0e5 63 // Switchrole - sanity check in cost-order...
46bd37bf 64 $reset_user_allowed_editing = false;
d7d4b0e5 65 if ($switchrole > 0 && confirm_sesskey() &&
66 has_capability('moodle/role:switchroles', $context)) {
67 // is this role assignable in this context?
68 // inquiring minds want to know...
82701e24 69 $aroles = get_switchable_roles($context);
d7d4b0e5 70 if (is_array($aroles) && isset($aroles[$switchrole])) {
71 role_switch($switchrole, $context);
72 // Double check that this role is allowed here
cdbea7ee 73 require_login($course);
d7d4b0e5 74 }
46bd37bf 75 // reset course page state - this prevents some weird problems ;-)
76 $USER->activitycopy = false;
77 $USER->activitycopycourse = NULL;
78 unset($USER->activitycopyname);
79 unset($SESSION->modform);
80 $USER->editing = 0;
81 $reset_user_allowed_editing = true;
3a52e764 82 }
83
542a0435 84 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 85 //url with appropriate authentication attached as parameter
892c0825 86 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
87 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 88 if (function_exists('extern_server_course')) {
542a0435 89 if ($extern_url = extern_server_course($course)) {
90 redirect($extern_url);
91 }
92 }
93 }
94
3a52e764 95
08cebf19 96 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 97
15a848ba
RW
98 $logparam = 'id='. $course->id;
99 $loglabel = 'view';
34b84d45 100 $infoid = $course->id;
9e586967 101 if ($section and $section > 0) {
15a848ba 102 $loglabel = 'view section';
c069dacf 103
104 // Get section details and check it exists.
105 $modinfo = get_fast_modinfo($course);
106 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
107
108 // Check user is allowed to see it.
134e28b7 109 if (!$coursesections->uservisible) {
c069dacf 110 // Note: We actually already know they don't have this capability
111 // or uservisible would have been true; this is just to get the
112 // correct error message shown.
113 require_capability('moodle/course:viewhiddensections', $context);
114 }
336d8c49
AA
115 $infoid = $coursesections->id;
116 $logparam .= '&sectionid='. $infoid;
34b84d45
RW
117 }
118 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
f9903ed0 119
8afb1cc8
MG
120 // Fix course format if it is no longer installed
121 $course->format = course_get_format($course)->get_format();
0f3fe4b6 122
78946b9b 123 $PAGE->set_pagelayout('course');
66b10689 124 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 125 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 126
8afb1cc8
MG
127 // Preload course format renderer before output starts.
128 // This is a little hacky but necessary since
129 // format.php is not included until after output starts
130 if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
131 require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
132 if (class_exists('format_'.$course->format.'_renderer')) {
133 // call get_renderer only if renderer is defined in format plugin
134 // otherwise an exception would be thrown
135 $PAGE->get_renderer('format_'. $course->format);
136 }
137 }
138
46bd37bf 139 if ($reset_user_allowed_editing) {
140 // ugly hack
141 unset($PAGE->_user_allowed_editing);
142 }
daa27ce4 143
0f3fe4b6 144 if (!isset($USER->editing)) {
d2b23346 145 $USER->editing = 0;
0f3fe4b6 146 }
a3f24f7c 147 if ($PAGE->user_allowed_editing()) {
0cdae0dc 148 if (($edit == 1) and confirm_sesskey()) {
d2b23346 149 $USER->editing = 1;
ad96cc52
AKA
150 // Redirect to site root if Editing is toggled on frontpage
151 if ($course->id == SITEID) {
152 redirect($CFG->wwwroot .'/?redirect=0');
52fc56a7
JF
153 } else if (!empty($return)) {
154 redirect($CFG->wwwroot . $return);
ad96cc52 155 } else {
3970a471 156 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
6c0ae99b 157 redirect($url);
ad96cc52 158 }
0cdae0dc 159 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 160 $USER->editing = 0;
a3f24f7c 161 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
162 $USER->activitycopy = false;
163 $USER->activitycopycourse = NULL;
9c9f7d77 164 }
ad96cc52
AKA
165 // Redirect to site root if Editing is toggled on frontpage
166 if ($course->id == SITEID) {
167 redirect($CFG->wwwroot .'/?redirect=0');
52fc56a7
JF
168 } else if (!empty($return)) {
169 redirect($CFG->wwwroot . $return);
ad96cc52
AKA
170 } else {
171 redirect($PAGE->url);
172 }
f9903ed0 173 }
01e0e704
ARN
174 if (($modchooser == 1) && confirm_sesskey()) {
175 set_user_preference('usemodchooser', $modchooser);
176 } else if (($modchooser == 0) && confirm_sesskey()) {
177 set_user_preference('usemodchooser', $modchooser);
178 }
8223d271 179
643b1de8 180 if (has_capability('moodle/course:sectionvisibility', $context)) {
af189935
PS
181 if ($hide && confirm_sesskey()) {
182 set_section_visible($course->id, $hide, '0');
e3c171e1 183 redirect($PAGE->url);
af189935 184 }
7d99d695 185
af189935
PS
186 if ($show && confirm_sesskey()) {
187 set_section_visible($course->id, $show, '1');
e3c171e1 188 redirect($PAGE->url);
af189935 189 }
643b1de8 190 }
12905134 191
643b1de8 192 if (has_capability('moodle/course:update', $context)) {
af189935 193 if (!empty($section)) {
ad778c84 194 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
ac5f6414
RT
195 $destsection = $section + $move;
196 if (move_section_to($course, $section, $destsection)) {
93d46f48
RK
197 if ($course->id == SITEID) {
198 redirect($CFG->wwwroot . '/?redirect=0');
199 } else {
e3c171e1 200 redirect(course_get_url($course));
93d46f48
RK
201 }
202 } else {
af189935
PS
203 echo $OUTPUT->notification('An error occurred while moving a section');
204 }
56e34ee4 205 }
12905134 206 }
207 }
48d72fa7 208 } else {
d2b23346 209 $USER->editing = 0;
7d99d695 210 }
211
45774bdd 212 $SESSION->fromdiscussion = $PAGE->url->out(false);
4c701e6f 213
f45cc76f 214
215 if ($course->id == SITEID) {
216 // This course is not a real course.
892c0825 217 redirect($CFG->wwwroot .'/');
83f3c62d 218 }
0a127169 219
cf615522 220 $completion = new completion_info($course);
221 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
222 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
223 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
224 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
225 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
226
227 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 228 }
229
8a7934f7
PS
230 // We are currently keeping the button here from 1.x to help new teachers figure out
231 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 232 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 233 if ($PAGE->user_allowed_editing()) {
d628215d 234 $buttons = $OUTPUT->edit_button($PAGE->url);
2b3d1ac7 235 $PAGE->set_button($buttons);
5d3e9d9f 236 }
237
2b9e3bac 238 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 239 $PAGE->set_heading($course->fullname);
0a122046 240 echo $OUTPUT->header();
f45cc76f 241
cf615522 242 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 243 // This value tracks whether there has been a dynamic change to the page.
244 // It is used so that if a user does this - (a) set some tickmarks, (b)
245 // go to another page, (c) clicks Back button - the page will
246 // automatically reload. Otherwise it would start with the wrong tick
247 // values.
47e75d04
SH
248 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
249 echo html_writer::start_tag('div');
250 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
251 echo html_writer::end_tag('div');
252 echo html_writer::end_tag('form');
4e781c7b 253 }
254
f45cc76f 255 // Course wrapper start.
47e75d04 256 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 257
d57aa283 258 // make sure that section 0 exists (this function will create one if it is missing)
4ede27b2 259 course_create_sections_if_missing($course, 0);
d57aa283
MG
260
261 // get information about course modules and existing module types
262 // format.php in course formats may rely on presence of these variables
263 $modinfo = get_fast_modinfo($course);
264 $modnames = get_module_types_names();
265 $modnamesplural = get_module_types_names(true);
266 $modnamesused = $modinfo->get_used_module_names();
267 $mods = $modinfo->get_cms();
268 $sections = $modinfo->get_section_info_all();
7468bf01 269
45774bdd
DP
270 // CAUTION, hacky fundamental variable defintion to follow!
271 // Note that because of the way course fromats are constructed though
272 // inclusion we pass parameters around this way..
273 $displaysection = $section;
274
f45cc76f 275 // Include the actual course format.
276 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
277 // Content wrapper end.
47e75d04
SH
278
279 echo html_writer::end_tag('div');
f45cc76f 280
01e0e704
ARN
281 // Include course AJAX
282 if (include_course_ajax($course, $modnamesused)) {
283 // Add the module chooser
284 $renderer = $PAGE->get_renderer('core', 'course');
a41b1d96 285 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
01e0e704 286 }
f45cc76f 287
d60c1124 288 echo $OUTPUT->footer();