on-demand release 2.3dev
[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);
0cdae0dc 17 $section = optional_param('section', 0, PARAM_INT);
18 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 19 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 20 $switchrole = optional_param('switchrole',-1, PARAM_INT);
01e0e704 21 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
3a52e764 22
599a52fe 23 $params = array();
2335781f 24 if (!empty($name)) {
599a52fe 25 $params = array('shortname' => $name);
816acb46 26 } else if (!empty($idnumber)) {
599a52fe
DP
27 $params = array('idnumber' => $idnumber);
28 } else if (!empty($id)) {
29 $params = array('id' => $id);
30 }else {
31 print_error('unspecifycourseid', 'error');
f9903ed0 32 }
33
599a52fe
DP
34 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
35
45774bdd
DP
36 $urlparams = array('id' => $course->id);
37 if ($section) {
38 $urlparams['section'] = $section;
39 }
40
41 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
088e3363 42
fbb4c959
SH
43 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
44 $PAGE->set_cacheable(false);
45
00653161 46 preload_course_contexts($course->id);
599a52fe 47 $context = context_course::instance($course->id, MUST_EXIST);
3a52e764 48
d7d4b0e5 49 // Remove any switched roles before checking login
50 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 51 role_switch($switchrole, $context);
52 }
53
a6af674a 54 require_login($course);
388f8911 55
d7d4b0e5 56 // Switchrole - sanity check in cost-order...
46bd37bf 57 $reset_user_allowed_editing = false;
d7d4b0e5 58 if ($switchrole > 0 && confirm_sesskey() &&
59 has_capability('moodle/role:switchroles', $context)) {
60 // is this role assignable in this context?
61 // inquiring minds want to know...
82701e24 62 $aroles = get_switchable_roles($context);
d7d4b0e5 63 if (is_array($aroles) && isset($aroles[$switchrole])) {
64 role_switch($switchrole, $context);
65 // Double check that this role is allowed here
cdbea7ee 66 require_login($course);
d7d4b0e5 67 }
46bd37bf 68 // reset course page state - this prevents some weird problems ;-)
69 $USER->activitycopy = false;
70 $USER->activitycopycourse = NULL;
71 unset($USER->activitycopyname);
72 unset($SESSION->modform);
73 $USER->editing = 0;
74 $reset_user_allowed_editing = true;
3a52e764 75 }
76
542a0435 77 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 78 //url with appropriate authentication attached as parameter
892c0825 79 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
80 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 81 if (function_exists('extern_server_course')) {
542a0435 82 if ($extern_url = extern_server_course($course)) {
83 redirect($extern_url);
84 }
85 }
86 }
87
3a52e764 88
08cebf19 89 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 90
15a848ba
RW
91 $logparam = 'id='. $course->id;
92 $loglabel = 'view';
34b84d45 93 $infoid = $course->id;
15a848ba
RW
94 if(!empty($section)) {
95 $logparam .= '&section='. $section;
96 $loglabel = 'view section';
34b84d45
RW
97 $sectionparams = array('course' => $course->id, 'section' => $section);
98 if ($coursesections = $DB->get_record('course_sections', $sectionparams, 'id', MUST_EXIST)) {
99 $infoid = $coursesections->id;
15a848ba 100 }
34b84d45
RW
101 }
102 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
f9903ed0 103
e069b3e2 104 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 105 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
106 $course->format = 'weeks'; // Default format is weeks
107 }
108
78946b9b 109 $PAGE->set_pagelayout('course');
66b10689 110 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 111 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 112
46bd37bf 113 if ($reset_user_allowed_editing) {
114 // ugly hack
115 unset($PAGE->_user_allowed_editing);
116 }
daa27ce4 117
0f3fe4b6 118 if (!isset($USER->editing)) {
d2b23346 119 $USER->editing = 0;
0f3fe4b6 120 }
a3f24f7c 121 if ($PAGE->user_allowed_editing()) {
0cdae0dc 122 if (($edit == 1) and confirm_sesskey()) {
d2b23346 123 $USER->editing = 1;
ad96cc52
AKA
124 // Redirect to site root if Editing is toggled on frontpage
125 if ($course->id == SITEID) {
126 redirect($CFG->wwwroot .'/?redirect=0');
127 } else {
128 redirect($PAGE->url);
129 }
0cdae0dc 130 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 131 $USER->editing = 0;
a3f24f7c 132 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
133 $USER->activitycopy = false;
134 $USER->activitycopycourse = NULL;
9c9f7d77 135 }
ad96cc52
AKA
136 // Redirect to site root if Editing is toggled on frontpage
137 if ($course->id == SITEID) {
138 redirect($CFG->wwwroot .'/?redirect=0');
139 } else {
140 redirect($PAGE->url);
141 }
f9903ed0 142 }
01e0e704
ARN
143 if (($modchooser == 1) && confirm_sesskey()) {
144 set_user_preference('usemodchooser', $modchooser);
145 } else if (($modchooser == 0) && confirm_sesskey()) {
146 set_user_preference('usemodchooser', $modchooser);
147 }
8223d271 148
af189935
PS
149 if (has_capability('moodle/course:update', $context)) {
150 if ($hide && confirm_sesskey()) {
151 set_section_visible($course->id, $hide, '0');
e3c171e1 152 redirect($PAGE->url);
af189935 153 }
7d99d695 154
af189935
PS
155 if ($show && confirm_sesskey()) {
156 set_section_visible($course->id, $show, '1');
e3c171e1 157 redirect($PAGE->url);
af189935 158 }
12905134 159
af189935
PS
160 if (!empty($section)) {
161 if (!empty($move) and confirm_sesskey()) {
93d46f48
RK
162 if (move_section($course, $section, $move)) {
163 if ($course->id == SITEID) {
164 redirect($CFG->wwwroot . '/?redirect=0');
165 } else {
e3c171e1 166 redirect(course_get_url($course));
93d46f48
RK
167 }
168 } else {
af189935
PS
169 echo $OUTPUT->notification('An error occurred while moving a section');
170 }
56e34ee4 171 }
12905134 172 }
173 }
48d72fa7 174 } else {
d2b23346 175 $USER->editing = 0;
7d99d695 176 }
177
45774bdd 178 $SESSION->fromdiscussion = $PAGE->url->out(false);
4c701e6f 179
f45cc76f 180
181 if ($course->id == SITEID) {
182 // This course is not a real course.
892c0825 183 redirect($CFG->wwwroot .'/');
83f3c62d 184 }
0a127169 185
cf615522 186 $completion = new completion_info($course);
187 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
188 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
189 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
190 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
191 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
192
193 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 194 }
195
8a7934f7
PS
196 // We are currently keeping the button here from 1.x to help new teachers figure out
197 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 198 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 199 if ($PAGE->user_allowed_editing()) {
d628215d 200 $buttons = $OUTPUT->edit_button($PAGE->url);
2b3d1ac7 201 $PAGE->set_button($buttons);
5d3e9d9f 202 }
203
2b9e3bac 204 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 205 $PAGE->set_heading($course->fullname);
0a122046 206 echo $OUTPUT->header();
f45cc76f 207
cf615522 208 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 209 // This value tracks whether there has been a dynamic change to the page.
210 // It is used so that if a user does this - (a) set some tickmarks, (b)
211 // go to another page, (c) clicks Back button - the page will
212 // automatically reload. Otherwise it would start with the wrong tick
213 // values.
47e75d04
SH
214 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
215 echo html_writer::start_tag('div');
216 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
217 echo html_writer::end_tag('div');
218 echo html_writer::end_tag('form');
4e781c7b 219 }
220
f45cc76f 221 // Course wrapper start.
47e75d04 222 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 223
f20edd52 224 $modinfo = get_fast_modinfo($COURSE);
9c9f7d77 225 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 226 foreach($mods as $modid=>$unused) {
227 if (!isset($modinfo->cms[$modid])) {
228 rebuild_course_cache($course->id);
f20edd52 229 $modinfo = get_fast_modinfo($COURSE);
a6af674a 230 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
231 break;
232 }
233 }
19801b2b 234
94dc3c7d 235 if (!$sections = $modinfo->get_section_info_all()) { // No sections found
ce4dfd27 236 $section = new stdClass;
237 $section->course = $course->id; // Create a default section.
238 $section->section = 0;
239 $section->visible = 1;
240 $section->summaryformat = FORMAT_HTML;
241 $section->id = $DB->insert_record('course_sections', $section);
242 rebuild_course_cache($course->id);
243 $modinfo = get_fast_modinfo($COURSE);
94dc3c7d 244 if (!$sections = $modinfo->get_section_info_all()) { // Try again
dbf91d9d 245 print_error('cannotcreateorfindstructs', 'error');
19801b2b 246 }
247 }
7468bf01 248
45774bdd
DP
249 // CAUTION, hacky fundamental variable defintion to follow!
250 // Note that because of the way course fromats are constructed though
251 // inclusion we pass parameters around this way..
252 $displaysection = $section;
253
f45cc76f 254 // Include the actual course format.
255 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
256 // Content wrapper end.
47e75d04
SH
257
258 echo html_writer::end_tag('div');
f45cc76f 259
01e0e704
ARN
260 // Include course AJAX
261 if (include_course_ajax($course, $modnamesused)) {
262 // Add the module chooser
263 $renderer = $PAGE->get_renderer('core', 'course');
264 echo $renderer->course_modchooser(get_module_metadata($course, $modnames), $course);
265 }
f45cc76f 266
d60c1124 267 echo $OUTPUT->footer();