MDL-38852 repository_merloy: fix missing setType calls
[moodle.git] / course / view.php
blob02ae98252314440123fa4aaebf7390fc6c0c1d1a
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); // Deprecated, use course/switchrole.php instead.
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:manageactivities');
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);
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);
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);
180 if (has_capability('moodle/course:sectionvisibility', $context)) {
181 if ($hide && confirm_sesskey()) {
182 set_section_visible($course->id, $hide, '0');
183 redirect($PAGE->url);
186 if ($show && confirm_sesskey()) {
187 set_section_visible($course->id, $show, '1');
188 redirect($PAGE->url);
192 if (has_capability('moodle/course:update', $context)) {
193 if (!empty($section)) {
194 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
195 $destsection = $section + $move;
196 if (move_section_to($course, $section, $destsection)) {
197 if ($course->id == SITEID) {
198 redirect($CFG->wwwroot . '/?redirect=0');
199 } else {
200 redirect(course_get_url($course));
202 } else {
203 echo $OUTPUT->notification('An error occurred while moving a section');
208 } else {
209 $USER->editing = 0;
212 $SESSION->fromdiscussion = $PAGE->url->out(false);
215 if ($course->id == SITEID) {
216 // This course is not a real course.
217 redirect($CFG->wwwroot .'/');
220 $completion = new completion_info($course);
221 if ($completion->is_enabled() && ajaxenabled()) {
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');
227 $PAGE->requires->js_init_call('M.core_completion.init');
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
232 // means you can back out of a situation where you removed the admin block. :)
233 if ($PAGE->user_allowed_editing()) {
234 $buttons = $OUTPUT->edit_button($PAGE->url);
235 $PAGE->set_button($buttons);
238 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
239 $PAGE->set_heading($course->fullname);
240 echo $OUTPUT->header();
242 if ($completion->is_enabled() && ajaxenabled()) {
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.
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');
255 // Course wrapper start.
256 echo html_writer::start_tag('div', array('class'=>'course-content'));
258 // make sure that section 0 exists (this function will create one if it is missing)
259 course_create_sections_if_missing($course, 0);
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();
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;
275 // Include the actual course format.
276 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
277 // Content wrapper end.
279 echo html_writer::end_tag('div');
281 // Include course AJAX
282 include_course_ajax($course, $modnamesused);
284 echo $OUTPUT->footer();