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