MDL-40611 fix context cache size expectation
[moodle.git] / course / view.php
bloba1c8fd3f305a8751033517b72e4873179f3aa4c8
1 <?php
3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->dirroot.'/mod/forum/lib.php');
8 require_once($CFG->libdir.'/conditionlib.php');
9 require_once($CFG->libdir.'/completionlib.php');
11 $id = optional_param('id', 0, PARAM_INT);
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);
17 $sectionid = optional_param('sectionid', 0, PARAM_INT);
18 $section = optional_param('section', 0, PARAM_INT);
19 $move = optional_param('move', 0, PARAM_INT);
20 $marker = optional_param('marker',-1 , PARAM_INT);
21 $switchrole = optional_param('switchrole',-1, PARAM_INT);
22 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
23 $return = optional_param('return', 0, PARAM_LOCALURL);
25 $params = array();
26 if (!empty($name)) {
27 $params = array('shortname' => $name);
28 } else if (!empty($idnumber)) {
29 $params = array('idnumber' => $idnumber);
30 } else if (!empty($id)) {
31 $params = array('id' => $id);
32 }else {
33 print_error('unspecifycourseid', 'error');
36 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
38 $urlparams = array('id' => $course->id);
40 // Sectionid should get priority over section number
41 if ($sectionid) {
42 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
44 if ($section) {
45 $urlparams['section'] = $section;
48 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
50 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
51 $PAGE->set_cacheable(false);
53 preload_course_contexts($course->id);
54 $context = context_course::instance($course->id, MUST_EXIST);
56 // Remove any switched roles before checking login
57 if ($switchrole == 0 && confirm_sesskey()) {
58 role_switch($switchrole, $context);
61 require_login($course);
63 // Switchrole - sanity check in cost-order...
64 $reset_user_allowed_editing = false;
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...
69 $aroles = get_switchable_roles($context);
70 if (is_array($aroles) && isset($aroles[$switchrole])) {
71 role_switch($switchrole, $context);
72 // Double check that this role is allowed here
73 require_login($course);
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;
84 //If course is hosted on an external server, redirect to corresponding
85 //url with appropriate authentication attached as parameter
86 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
87 include $CFG->dirroot .'/course/externservercourse.php';
88 if (function_exists('extern_server_course')) {
89 if ($extern_url = extern_server_course($course)) {
90 redirect($extern_url);
96 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
98 $logparam = 'id='. $course->id;
99 $loglabel = 'view';
100 $infoid = $course->id;
101 if ($section and $section > 0) {
102 $loglabel = 'view section';
104 // Get section details and check it exists.
105 $modinfo = get_fast_modinfo($course);
106 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
108 // Check user is allowed to see it.
109 if (!$coursesections->uservisible) {
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);
115 $infoid = $coursesections->id;
116 $logparam .= '&sectionid='. $infoid;
118 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
120 // Fix course format if it is no longer installed
121 $course->format = course_get_format($course)->get_format();
123 $PAGE->set_pagelayout('course');
124 $PAGE->set_pagetype('course-view-' . $course->format);
125 $PAGE->set_other_editing_capability('moodle/course:update');
126 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
127 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
128 if (course_format_uses_sections($course->format)) {
129 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
130 $PAGE->set_other_editing_capability('moodle/course:movesections');
133 // Preload course format renderer before output starts.
134 // This is a little hacky but necessary since
135 // format.php is not included until after output starts
136 if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
137 require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
138 if (class_exists('format_'.$course->format.'_renderer')) {
139 // call get_renderer only if renderer is defined in format plugin
140 // otherwise an exception would be thrown
141 $PAGE->get_renderer('format_'. $course->format);
145 if ($reset_user_allowed_editing) {
146 // ugly hack
147 unset($PAGE->_user_allowed_editing);
150 if (!isset($USER->editing)) {
151 $USER->editing = 0;
153 if ($PAGE->user_allowed_editing()) {
154 if (($edit == 1) and confirm_sesskey()) {
155 $USER->editing = 1;
156 // Redirect to site root if Editing is toggled on frontpage
157 if ($course->id == SITEID) {
158 redirect($CFG->wwwroot .'/?redirect=0');
159 } else if (!empty($return)) {
160 redirect($CFG->wwwroot . $return);
161 } else {
162 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
163 redirect($url);
165 } else if (($edit == 0) and confirm_sesskey()) {
166 $USER->editing = 0;
167 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
168 $USER->activitycopy = false;
169 $USER->activitycopycourse = NULL;
171 // Redirect to site root if Editing is toggled on frontpage
172 if ($course->id == SITEID) {
173 redirect($CFG->wwwroot .'/?redirect=0');
174 } else if (!empty($return)) {
175 redirect($CFG->wwwroot . $return);
176 } else {
177 redirect($PAGE->url);
180 if (($modchooser == 1) && confirm_sesskey()) {
181 set_user_preference('usemodchooser', $modchooser);
182 } else if (($modchooser == 0) && confirm_sesskey()) {
183 set_user_preference('usemodchooser', $modchooser);
186 if (has_capability('moodle/course:sectionvisibility', $context)) {
187 if ($hide && confirm_sesskey()) {
188 set_section_visible($course->id, $hide, '0');
189 redirect($PAGE->url);
192 if ($show && confirm_sesskey()) {
193 set_section_visible($course->id, $show, '1');
194 redirect($PAGE->url);
198 if (!empty($section) && !empty($move) &&
199 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
200 $destsection = $section + $move;
201 if (move_section_to($course, $section, $destsection)) {
202 if ($course->id == SITEID) {
203 redirect($CFG->wwwroot . '/?redirect=0');
204 } else {
205 redirect(course_get_url($course));
207 } else {
208 echo $OUTPUT->notification('An error occurred while moving a section');
211 } else {
212 $USER->editing = 0;
215 $SESSION->fromdiscussion = $PAGE->url->out(false);
218 if ($course->id == SITEID) {
219 // This course is not a real course.
220 redirect($CFG->wwwroot .'/');
223 $completion = new completion_info($course);
224 if ($completion->is_enabled() && ajaxenabled()) {
225 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
226 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
227 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
228 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
230 $PAGE->requires->js_init_call('M.core_completion.init');
233 // We are currently keeping the button here from 1.x to help new teachers figure out
234 // what to do, even though the link also appears in the course admin block. It also
235 // means you can back out of a situation where you removed the admin block. :)
236 if ($PAGE->user_allowed_editing()) {
237 $buttons = $OUTPUT->edit_button($PAGE->url);
238 $PAGE->set_button($buttons);
241 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
242 $PAGE->set_heading($course->fullname);
243 echo $OUTPUT->header();
245 if ($completion->is_enabled() && ajaxenabled()) {
246 // This value tracks whether there has been a dynamic change to the page.
247 // It is used so that if a user does this - (a) set some tickmarks, (b)
248 // go to another page, (c) clicks Back button - the page will
249 // automatically reload. Otherwise it would start with the wrong tick
250 // values.
251 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
252 echo html_writer::start_tag('div');
253 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
254 echo html_writer::end_tag('div');
255 echo html_writer::end_tag('form');
258 // Course wrapper start.
259 echo html_writer::start_tag('div', array('class'=>'course-content'));
261 // make sure that section 0 exists (this function will create one if it is missing)
262 course_create_sections_if_missing($course, 0);
264 // get information about course modules and existing module types
265 // format.php in course formats may rely on presence of these variables
266 $modinfo = get_fast_modinfo($course);
267 $modnames = get_module_types_names();
268 $modnamesplural = get_module_types_names(true);
269 $modnamesused = $modinfo->get_used_module_names();
270 $mods = $modinfo->get_cms();
271 $sections = $modinfo->get_section_info_all();
273 // CAUTION, hacky fundamental variable defintion to follow!
274 // Note that because of the way course fromats are constructed though
275 // inclusion we pass parameters around this way..
276 $displaysection = $section;
278 // Include the actual course format.
279 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
280 // Content wrapper end.
282 echo html_writer::end_tag('div');
284 // Include course AJAX
285 if (include_course_ajax($course, $modnamesused)) {
286 // Add the module chooser
287 $renderer = $PAGE->get_renderer('core', 'course');
288 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
291 echo $OUTPUT->footer();