MDL-14475 initialised $assign
[moodle.git] / course / view.php
CommitLineData
24f2b65a 1<?php // $Id$
f9903ed0 2
3// Display the course home page.
4
08cebf19 5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/blocklib.php');
f45cc76f 8 require_once($CFG->libdir.'/ajax/ajaxlib.php');
afc58a28 9 require_once($CFG->dirroot.'/mod/forum/lib.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);
21
7ea8ea7f 22
f9903ed0 23
533229c4 24 if (empty($id) && empty($name) && empty($idnumber)) {
5a2a5331 25 print_error("Must specify course id, short name or idnumber");
388f8911 26 }
f9903ed0 27
2335781f 28 if (!empty($name)) {
29 if (! ($course = get_record('course', 'shortname', $name)) ) {
5a2a5331 30 print_error('Invalid short course name');
388f8911 31 }
816acb46 32 } else if (!empty($idnumber)) {
33 if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
5a2a5331 34 print_error('Invalid course idnumber');
816acb46 35 }
388f8911 36 } else {
2335781f 37 if (! ($course = get_record('course', 'id', $id)) ) {
5a2a5331 38 print_error('Invalid course id');
388f8911 39 }
f9903ed0 40 }
41
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...
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...
58 $aroles = get_assignable_roles($context);
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 }
3a52e764 64 }
65
542a0435 66 //If course is hosted on an external server, redirect to corresponding
67 //url with appropriate authentication attached as parameter
892c0825 68 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
69 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 70 if (function_exists('extern_server_course')) {
542a0435 71 if ($extern_url = extern_server_course($course)) {
72 redirect($extern_url);
73 }
74 }
75 }
76
3a52e764 77
08cebf19 78 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 79
892c0825 80 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
f9903ed0 81
e069b3e2 82 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 83 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
84 $course->format = 'weeks'; // Default format is weeks
85 }
86
7542a4e5 87 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
f45cc76f 88 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
89
daa27ce4 90
0f3fe4b6 91 if (!isset($USER->editing)) {
d2b23346 92 $USER->editing = 0;
0f3fe4b6 93 }
a3f24f7c 94 if ($PAGE->user_allowed_editing()) {
0cdae0dc 95 if (($edit == 1) and confirm_sesskey()) {
d2b23346 96 $USER->editing = 1;
0cdae0dc 97 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 98 $USER->editing = 0;
a3f24f7c 99 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
100 $USER->activitycopy = false;
101 $USER->activitycopycourse = NULL;
9c9f7d77 102 }
f9903ed0 103 }
8223d271 104
0cdae0dc 105 if ($hide && confirm_sesskey()) {
82b181f2 106 set_section_visible($course->id, $hide, '0');
73fafc38 107 }
7d99d695 108
0cdae0dc 109 if ($show && confirm_sesskey()) {
82b181f2 110 set_section_visible($course->id, $show, '1');
73fafc38 111 }
12905134 112
113 if (!empty($section)) {
5b224948 114 if (!empty($move) and confirm_sesskey()) {
56e34ee4 115 if (!move_section($course, $section, $move)) {
892c0825 116 notify('An error occurred while moving a section');
56e34ee4 117 }
12905134 118 }
119 }
48d72fa7 120 } else {
d2b23346 121 $USER->editing = 0;
7d99d695 122 }
123
892c0825 124 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
4c701e6f 125
f45cc76f 126
127 if ($course->id == SITEID) {
128 // This course is not a real course.
892c0825 129 redirect($CFG->wwwroot .'/');
83f3c62d 130 }
131
f9903ed0 132
f45cc76f 133 // AJAX-capable course format?
6d77b23c 134 $useajax = false;
f45cc76f 135 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
4e86c6d7 136 $bodytags = '';
f45cc76f 137
6d77b23c 138 if (empty($CFG->disablecourseajax) and file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
61c1b4b5 139
6d77b23c 140 // TODO: stop abusing CFG global here
61c1b4b5 141 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
142 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
143
f45cc76f 144 require_once($ajaxformatfile);
4e86c6d7 145
61c1b4b5 146 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
147 // Course-based switches
7b9c9979 148
61c1b4b5 149 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
f47e1c17 150
151 require_js(array('yui_yahoo',
152 'yui_dom',
153 'yui_event',
154 'yui_dragdrop',
155 'yui_connection',
156 'ajaxcourse_blocks',
157 'ajaxcourse_sections'));
158
4e86c6d7 159 if (debugging('', DEBUG_DEVELOPER)) {
f47e1c17 160 require_js(array('yui_logger'));
4e86c6d7 161
a202dfa0 162 $bodytags = 'onload = "javascript:
4e86c6d7 163 show_logger = function() {
164 var logreader = new YAHOO.widget.LogReader();
165 logreader.newestOnTop = false;
166 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
167 };
168 show_logger();
169 "';
170 }
f47e1c17 171
4e86c6d7 172 // Okay, global variable alert. VERY UGLY. We need to create
173 // this object here before the <blockname>_print_block()
174 // function is called, since that function needs to set some
175 // stuff in the javascriptportal object.
176 $COURSE->javascriptportal = new jsportal();
6d77b23c 177 $useajax = true;
4e86c6d7 178 }
f45cc76f 179 }
f45cc76f 180 }
181
6d77b23c 182 $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
75c3849a 183
f45cc76f 184
f47e1c17 185 $PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);
f45cc76f 186 // Course wrapper start.
187 echo '<div class="course-content">';
188
a6af674a 189 $modinfo =& get_fast_modinfo($COURSE);
9c9f7d77 190 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 191 foreach($mods as $modid=>$unused) {
192 if (!isset($modinfo->cms[$modid])) {
193 rebuild_course_cache($course->id);
194 $modinfo =& get_fast_modinfo($COURSE);
195 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
196 break;
197 }
198 }
19801b2b 199
dfec7b01 200 if (! $sections = get_all_sections($course->id)) { // No sections found
201 // Double-check to be extra sure
892c0825 202 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
dfec7b01 203 $section->course = $course->id; // Create a default section.
204 $section->section = 0;
205 $section->visible = 1;
892c0825 206 $section->id = insert_record('course_sections', $section);
dfec7b01 207 }
208 if (! $sections = get_all_sections($course->id) ) { // Try again
5a2a5331 209 print_error('Error finding or creating section structures for this course');
19801b2b 210 }
211 }
7468bf01 212
f45cc76f 213 // Include the actual course format.
214 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
215 // Content wrapper end.
f47e1c17 216 echo "</div>\n\n";
f45cc76f 217
218
219 // Use AJAX?
6d77b23c 220 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
f45cc76f 221 // At the bottom because we want to process sections and activities
f47e1c17 222 // after the relevant html has been generated. We're forced to do this
223 // because of the way in which lib/ajax/ajaxcourse.js is written.
224 echo '<script type="text/javascript" ';
225 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
226
227 $COURSE->javascriptportal->print_javascript($course->id);
f45cc76f 228 }
229
230
093a4a24 231 print_footer(NULL, $course);
f9903ed0 232
61c1b4b5 233?>