NOBUG: Bumping version to test auto-packaging
[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
e069b3e2 120 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 121 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
122 $course->format = 'weeks'; // Default format is weeks
123 }
124
78946b9b 125 $PAGE->set_pagelayout('course');
66b10689 126 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 127 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 128
46bd37bf 129 if ($reset_user_allowed_editing) {
130 // ugly hack
131 unset($PAGE->_user_allowed_editing);
132 }
daa27ce4 133
0f3fe4b6 134 if (!isset($USER->editing)) {
d2b23346 135 $USER->editing = 0;
0f3fe4b6 136 }
a3f24f7c 137 if ($PAGE->user_allowed_editing()) {
0cdae0dc 138 if (($edit == 1) and confirm_sesskey()) {
d2b23346 139 $USER->editing = 1;
ad96cc52
AKA
140 // Redirect to site root if Editing is toggled on frontpage
141 if ($course->id == SITEID) {
142 redirect($CFG->wwwroot .'/?redirect=0');
52fc56a7
JF
143 } else if (!empty($return)) {
144 redirect($CFG->wwwroot . $return);
ad96cc52 145 } else {
3970a471 146 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
6c0ae99b 147 redirect($url);
ad96cc52 148 }
0cdae0dc 149 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 150 $USER->editing = 0;
a3f24f7c 151 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
152 $USER->activitycopy = false;
153 $USER->activitycopycourse = NULL;
9c9f7d77 154 }
ad96cc52
AKA
155 // Redirect to site root if Editing is toggled on frontpage
156 if ($course->id == SITEID) {
157 redirect($CFG->wwwroot .'/?redirect=0');
52fc56a7
JF
158 } else if (!empty($return)) {
159 redirect($CFG->wwwroot . $return);
ad96cc52
AKA
160 } else {
161 redirect($PAGE->url);
162 }
f9903ed0 163 }
01e0e704
ARN
164 if (($modchooser == 1) && confirm_sesskey()) {
165 set_user_preference('usemodchooser', $modchooser);
166 } else if (($modchooser == 0) && confirm_sesskey()) {
167 set_user_preference('usemodchooser', $modchooser);
168 }
8223d271 169
643b1de8 170 if (has_capability('moodle/course:sectionvisibility', $context)) {
af189935
PS
171 if ($hide && confirm_sesskey()) {
172 set_section_visible($course->id, $hide, '0');
e3c171e1 173 redirect($PAGE->url);
af189935 174 }
7d99d695 175
af189935
PS
176 if ($show && confirm_sesskey()) {
177 set_section_visible($course->id, $show, '1');
e3c171e1 178 redirect($PAGE->url);
af189935 179 }
643b1de8 180 }
12905134 181
643b1de8 182 if (has_capability('moodle/course:update', $context)) {
af189935 183 if (!empty($section)) {
ad778c84 184 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
ac5f6414
RT
185 $destsection = $section + $move;
186 if (move_section_to($course, $section, $destsection)) {
93d46f48
RK
187 if ($course->id == SITEID) {
188 redirect($CFG->wwwroot . '/?redirect=0');
189 } else {
e3c171e1 190 redirect(course_get_url($course));
93d46f48
RK
191 }
192 } else {
af189935
PS
193 echo $OUTPUT->notification('An error occurred while moving a section');
194 }
56e34ee4 195 }
12905134 196 }
197 }
48d72fa7 198 } else {
d2b23346 199 $USER->editing = 0;
7d99d695 200 }
201
45774bdd 202 $SESSION->fromdiscussion = $PAGE->url->out(false);
4c701e6f 203
f45cc76f 204
205 if ($course->id == SITEID) {
206 // This course is not a real course.
892c0825 207 redirect($CFG->wwwroot .'/');
83f3c62d 208 }
0a127169 209
cf615522 210 $completion = new completion_info($course);
211 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
212 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
213 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
214 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
215 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
216
217 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 218 }
219
8a7934f7
PS
220 // We are currently keeping the button here from 1.x to help new teachers figure out
221 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 222 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 223 if ($PAGE->user_allowed_editing()) {
d628215d 224 $buttons = $OUTPUT->edit_button($PAGE->url);
2b3d1ac7 225 $PAGE->set_button($buttons);
5d3e9d9f 226 }
227
2b9e3bac 228 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 229 $PAGE->set_heading($course->fullname);
0a122046 230 echo $OUTPUT->header();
f45cc76f 231
cf615522 232 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 233 // This value tracks whether there has been a dynamic change to the page.
234 // It is used so that if a user does this - (a) set some tickmarks, (b)
235 // go to another page, (c) clicks Back button - the page will
236 // automatically reload. Otherwise it would start with the wrong tick
237 // values.
47e75d04
SH
238 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
239 echo html_writer::start_tag('div');
240 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
241 echo html_writer::end_tag('div');
242 echo html_writer::end_tag('form');
4e781c7b 243 }
244
f45cc76f 245 // Course wrapper start.
47e75d04 246 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 247
d57aa283 248 // make sure that section 0 exists (this function will create one if it is missing)
4ede27b2 249 course_create_sections_if_missing($course, 0);
d57aa283
MG
250
251 // get information about course modules and existing module types
252 // format.php in course formats may rely on presence of these variables
253 $modinfo = get_fast_modinfo($course);
254 $modnames = get_module_types_names();
255 $modnamesplural = get_module_types_names(true);
256 $modnamesused = $modinfo->get_used_module_names();
257 $mods = $modinfo->get_cms();
258 $sections = $modinfo->get_section_info_all();
7468bf01 259
45774bdd
DP
260 // CAUTION, hacky fundamental variable defintion to follow!
261 // Note that because of the way course fromats are constructed though
262 // inclusion we pass parameters around this way..
263 $displaysection = $section;
264
f45cc76f 265 // Include the actual course format.
266 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
267 // Content wrapper end.
47e75d04
SH
268
269 echo html_writer::end_tag('div');
f45cc76f 270
01e0e704
ARN
271 // Include course AJAX
272 if (include_course_ajax($course, $modnamesused)) {
273 // Add the module chooser
274 $renderer = $PAGE->get_renderer('core', 'course');
a41b1d96 275 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
01e0e704 276 }
f45cc76f 277
d60c1124 278 echo $OUTPUT->footer();