Updated the HEAD build version to 20100830
[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');
f9903ed0 8
2335781f 9 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 10 $name = optional_param('name', '', PARAM_RAW);
11 $edit = optional_param('edit', -1, PARAM_BOOL);
12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $idnumber = optional_param('idnumber', '', PARAM_RAW);
0cdae0dc 15 $section = optional_param('section', 0, PARAM_INT);
16 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 17 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 18 $switchrole = optional_param('switchrole',-1, PARAM_INT);
19
533229c4 20 if (empty($id) && empty($name) && empty($idnumber)) {
dbf91d9d 21 print_error('unspecifycourseid', 'error');
388f8911 22 }
f9903ed0 23
2335781f 24 if (!empty($name)) {
6bb08163 25 if (! ($course = $DB->get_record('course', array('shortname'=>$name)))) {
dbf91d9d 26 print_error('invalidcoursenameshort', 'error');
388f8911 27 }
816acb46 28 } else if (!empty($idnumber)) {
6bb08163 29 if (! ($course = $DB->get_record('course', array('idnumber'=>$idnumber)))) {
dbf91d9d 30 print_error('invalidcourseid', 'error');
816acb46 31 }
388f8911 32 } else {
6bb08163 33 if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
dbf91d9d 34 print_error('invalidcourseid', 'error');
388f8911 35 }
f9903ed0 36 }
37
088e3363
MD
38 $PAGE->set_url('/course/view.php', array('id' => $course->id)); // Defined here to avoid notices on errors etc
39
00653161 40 preload_course_contexts($course->id);
3a52e764 41 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
42 print_error('nocontext');
43 }
44
d7d4b0e5 45 // Remove any switched roles before checking login
46 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 47 role_switch($switchrole, $context);
48 }
49
a6af674a 50 require_login($course);
388f8911 51
d7d4b0e5 52 // Switchrole - sanity check in cost-order...
46bd37bf 53 $reset_user_allowed_editing = false;
d7d4b0e5 54 if ($switchrole > 0 && confirm_sesskey() &&
55 has_capability('moodle/role:switchroles', $context)) {
56 // is this role assignable in this context?
57 // inquiring minds want to know...
82701e24 58 $aroles = get_switchable_roles($context);
d7d4b0e5 59 if (is_array($aroles) && isset($aroles[$switchrole])) {
60 role_switch($switchrole, $context);
61 // Double check that this role is allowed here
62 require_login($course->id);
63 }
46bd37bf 64 // reset course page state - this prevents some weird problems ;-)
65 $USER->activitycopy = false;
66 $USER->activitycopycourse = NULL;
67 unset($USER->activitycopyname);
68 unset($SESSION->modform);
69 $USER->editing = 0;
70 $reset_user_allowed_editing = true;
3a52e764 71 }
72
542a0435 73 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 74 //url with appropriate authentication attached as parameter
892c0825 75 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
76 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 77 if (function_exists('extern_server_course')) {
542a0435 78 if ($extern_url = extern_server_course($course)) {
79 redirect($extern_url);
80 }
81 }
82 }
83
3a52e764 84
08cebf19 85 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 86
892c0825 87 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
f9903ed0 88
e069b3e2 89 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 90 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
91 $course->format = 'weeks'; // Default format is weeks
92 }
93
78946b9b 94 $PAGE->set_pagelayout('course');
66b10689 95 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 96 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 97
46bd37bf 98 if ($reset_user_allowed_editing) {
99 // ugly hack
100 unset($PAGE->_user_allowed_editing);
101 }
daa27ce4 102
0f3fe4b6 103 if (!isset($USER->editing)) {
d2b23346 104 $USER->editing = 0;
0f3fe4b6 105 }
a3f24f7c 106 if ($PAGE->user_allowed_editing()) {
0cdae0dc 107 if (($edit == 1) and confirm_sesskey()) {
d2b23346 108 $USER->editing = 1;
8a7934f7 109 redirect($PAGE->url);
0cdae0dc 110 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 111 $USER->editing = 0;
a3f24f7c 112 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
113 $USER->activitycopy = false;
114 $USER->activitycopycourse = NULL;
9c9f7d77 115 }
8a7934f7 116 redirect($PAGE->url);
f9903ed0 117 }
8223d271 118
0cdae0dc 119 if ($hide && confirm_sesskey()) {
82b181f2 120 set_section_visible($course->id, $hide, '0');
73fafc38 121 }
7d99d695 122
0cdae0dc 123 if ($show && confirm_sesskey()) {
82b181f2 124 set_section_visible($course->id, $show, '1');
73fafc38 125 }
12905134 126
127 if (!empty($section)) {
5b224948 128 if (!empty($move) and confirm_sesskey()) {
56e34ee4 129 if (!move_section($course, $section, $move)) {
e6db3026 130 echo $OUTPUT->notification('An error occurred while moving a section');
56e34ee4 131 }
12905134 132 }
133 }
48d72fa7 134 } else {
d2b23346 135 $USER->editing = 0;
7d99d695 136 }
137
892c0825 138 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
4c701e6f 139
f45cc76f 140
141 if ($course->id == SITEID) {
142 // This course is not a real course.
892c0825 143 redirect($CFG->wwwroot .'/');
83f3c62d 144 }
0a127169 145
f9903ed0 146
f45cc76f 147 // AJAX-capable course format?
aa6c1ced 148 $useajax = false;
f45cc76f 149 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
4e86c6d7 150 $bodytags = '';
f45cc76f 151
6d77b23c 152 if (empty($CFG->disablecourseajax) and file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
61c1b4b5 153
6d77b23c 154 // TODO: stop abusing CFG global here
61c1b4b5 155 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
156 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
157
f45cc76f 158 require_once($ajaxformatfile);
4e86c6d7 159
61c1b4b5 160 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
161 // Course-based switches
7b9c9979 162
61c1b4b5 163 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
985d1d1d
PS
164 $PAGE->requires->yui2_lib('dragdrop');
165 $PAGE->requires->yui2_lib('connection');
166 $PAGE->requires->yui2_lib('selector');
f868c945
SH
167 $PAGE->requires->js('/lib/ajax/block_classes.js', true);
168 $PAGE->requires->js('/lib/ajax/section_classes.js', true);
f47e1c17 169
4e86c6d7 170 // Okay, global variable alert. VERY UGLY. We need to create
171 // this object here before the <blockname>_print_block()
172 // function is called, since that function needs to set some
173 // stuff in the javascriptportal object.
174 $COURSE->javascriptportal = new jsportal();
6d77b23c 175 $useajax = true;
4e86c6d7 176 }
f45cc76f 177 }
f45cc76f 178 }
179
6d77b23c 180 $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
75c3849a 181
cf615522 182 $completion = new completion_info($course);
183 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
184 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
185 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
186 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
187 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
188
189 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 190 }
191
8a7934f7
PS
192 // We are currently keeping the button here from 1.x to help new teachers figure out
193 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 194 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 195 if ($PAGE->user_allowed_editing()) {
2b3d1ac7
MD
196 $buttons = $OUTPUT->edit_button(new moodle_url('/course/view.php', array('id' => $course->id)));
197 $PAGE->set_button($buttons);
5d3e9d9f 198 }
199
2b9e3bac 200 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 201 $PAGE->set_heading($course->fullname);
0a122046 202 echo $OUTPUT->header();
f45cc76f 203
cf615522 204 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 205 // This value tracks whether there has been a dynamic change to the page.
206 // It is used so that if a user does this - (a) set some tickmarks, (b)
207 // go to another page, (c) clicks Back button - the page will
208 // automatically reload. Otherwise it would start with the wrong tick
209 // values.
210 print '<form action="."><div><input type="hidden" id="completion_dynamic_change"
211 name="completion_dynamic_change" value="0" /></div></form>';
4e781c7b 212 }
213
f45cc76f 214 // Course wrapper start.
215 echo '<div class="course-content">';
216
a6af674a 217 $modinfo =& get_fast_modinfo($COURSE);
9c9f7d77 218 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 219 foreach($mods as $modid=>$unused) {
220 if (!isset($modinfo->cms[$modid])) {
221 rebuild_course_cache($course->id);
222 $modinfo =& get_fast_modinfo($COURSE);
223 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
224 break;
225 }
226 }
19801b2b 227
dfec7b01 228 if (! $sections = get_all_sections($course->id)) { // No sections found
229 // Double-check to be extra sure
6bb08163 230 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
dfec7b01 231 $section->course = $course->id; // Create a default section.
232 $section->section = 0;
233 $section->visible = 1;
09eb2151 234 $section->summaryformat = FORMAT_HTML;
6bb08163 235 $section->id = $DB->insert_record('course_sections', $section);
dfec7b01 236 }
237 if (! $sections = get_all_sections($course->id) ) { // Try again
dbf91d9d 238 print_error('cannotcreateorfindstructs', 'error');
19801b2b 239 }
240 }
7468bf01 241
f45cc76f 242 // Include the actual course format.
243 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
244 // Content wrapper end.
f47e1c17 245 echo "</div>\n\n";
f45cc76f 246
aa6c1ced 247 // Use AJAX?
6d77b23c 248 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
f45cc76f 249 // At the bottom because we want to process sections and activities
f47e1c17 250 // after the relevant html has been generated. We're forced to do this
aa6c1ced
PS
251 // because of the way in which lib/ajax/ajaxcourse.js is written.
252
ddc66060 253 echo '<script type="text/javascript" ';
254 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
f47e1c17 255 $COURSE->javascriptportal->print_javascript($course->id);
f45cc76f 256 }
257
258
d60c1124 259 echo $OUTPUT->footer();
f9903ed0 260
aa6c1ced 261