MDL-29148 MNet - improved handling of a verification function return code
[moodle.git] / course / view.php
blobd38a8a5cca3a743eb2ec982106009da477396e08
1 <?php // $Id$
3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/blocklib.php');
8 require_once($CFG->libdir.'/ajax/ajaxlib.php');
9 require_once($CFG->dirroot.'/mod/forum/lib.php');
11 $id = optional_param('id', 0, PARAM_INT);
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);
17 $section = optional_param('section', 0, PARAM_INT);
18 $move = optional_param('move', 0, PARAM_INT);
19 $marker = optional_param('marker',-1 , PARAM_INT);
20 $switchrole = optional_param('switchrole',-1, PARAM_INT);
24 if (empty($id) && empty($name) && empty($idnumber)) {
25 error("Must specify course id, short name or idnumber");
28 if (!empty($name)) {
29 if (! ($course = get_record('course', 'shortname', $name)) ) {
30 error('Invalid short course name');
32 } else if (!empty($idnumber)) {
33 if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
34 error('Invalid course idnumber');
36 } else {
37 if (! ($course = get_record('course', 'id', $id)) ) {
38 error('Invalid course id');
42 preload_course_contexts($course->id);
43 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
44 print_error('nocontext');
47 // Remove any switched roles before checking login
48 if ($switchrole == 0 && confirm_sesskey()) {
49 role_switch($switchrole, $context);
52 require_login($course);
54 // Switchrole - sanity check in cost-order...
55 $reset_user_allowed_editing = false;
56 if ($switchrole > 0 && confirm_sesskey() &&
57 has_capability('moodle/role:switchroles', $context)) {
58 // is this role assignable in this context?
59 // inquiring minds want to know...
60 $aroles = get_assignable_roles_for_switchrole($context);
61 if (is_array($aroles) && isset($aroles[$switchrole])) {
62 role_switch($switchrole, $context);
63 // Double check that this role is allowed here
64 require_login($course->id);
66 // reset course page state - this prevents some weird problems ;-)
67 $USER->activitycopy = false;
68 $USER->activitycopycourse = NULL;
69 unset($USER->activitycopyname);
70 unset($SESSION->modform);
71 $USER->editing = 0;
72 $reset_user_allowed_editing = true;
75 //If course is hosted on an external server, redirect to corresponding
76 //url with appropriate authentication attached as parameter
77 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
78 include $CFG->dirroot .'/course/externservercourse.php';
79 if (function_exists('extern_server_course')) {
80 if ($extern_url = extern_server_course($course)) {
81 redirect($extern_url);
87 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
89 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
91 $course->format = clean_param($course->format, PARAM_ALPHA);
92 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
93 $course->format = 'weeks'; // Default format is weeks
96 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
97 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
99 if ($reset_user_allowed_editing) {
100 // ugly hack
101 unset($PAGE->_user_allowed_editing);
104 if (!isset($USER->editing)) {
105 $USER->editing = 0;
107 if ($PAGE->user_allowed_editing()) {
108 if (($edit == 1) and confirm_sesskey()) {
109 $USER->editing = 1;
110 } else if (($edit == 0) and confirm_sesskey()) {
111 $USER->editing = 0;
112 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
113 $USER->activitycopy = false;
114 $USER->activitycopycourse = NULL;
118 if ($hide && confirm_sesskey()) {
119 set_section_visible($course->id, $hide, '0');
122 if ($show && confirm_sesskey()) {
123 set_section_visible($course->id, $show, '1');
126 if (!empty($section)) {
127 if (!empty($move) and confirm_sesskey()) {
128 if (!move_section($course, $section, $move)) {
129 notify('An error occurred while moving a section');
133 } else {
134 $USER->editing = 0;
137 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
140 if ($course->id == SITEID) {
141 // This course is not a real course.
142 redirect($CFG->wwwroot .'/');
146 // AJAX-capable course format?
147 $useajax = false;
148 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
149 $bodytags = '';
151 if (empty($CFG->disablecourseajax) and file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
153 // TODO: stop abusing CFG global here
154 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
155 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
157 require_once($ajaxformatfile);
159 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
160 // Course-based switches
162 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
164 require_js(array('yui_yahoo',
165 'yui_dom',
166 'yui_event',
167 'yui_dragdrop',
168 'yui_connection',
169 'yui_selector',
170 'yui_element',
171 'ajaxcourse_blocks',
172 'ajaxcourse_sections'));
174 if (debugging('', DEBUG_DEVELOPER)) {
175 require_js(array('yui_logger'));
177 $bodytags = 'onload = "javascript:
178 show_logger = function() {
179 var logreader = new YAHOO.widget.LogReader();
180 logreader.newestOnTop = false;
181 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
183 show_logger();
187 // Okay, global variable alert. VERY UGLY. We need to create
188 // this object here before the <blockname>_print_block()
189 // function is called, since that function needs to set some
190 // stuff in the javascriptportal object.
191 $COURSE->javascriptportal = new jsportal();
192 $useajax = true;
197 $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
200 $PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);
201 // Course wrapper start.
202 echo '<div class="course-content">';
204 $modinfo =& get_fast_modinfo($COURSE);
205 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
206 foreach($mods as $modid=>$unused) {
207 if (!isset($modinfo->cms[$modid])) {
208 rebuild_course_cache($course->id);
209 $modinfo =& get_fast_modinfo($COURSE);
210 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
211 break;
215 if (! $sections = get_all_sections($course->id)) { // No sections found
216 // Double-check to be extra sure
217 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
218 $section->course = $course->id; // Create a default section.
219 $section->section = 0;
220 $section->visible = 1;
221 $section->id = insert_record('course_sections', $section);
223 if (! $sections = get_all_sections($course->id) ) { // Try again
224 error('Error finding or creating section structures for this course');
228 // Include the actual course format.
229 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
230 // Content wrapper end.
231 echo "</div>\n\n";
234 // Use AJAX?
235 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
236 // At the bottom because we want to process sections and activities
237 // after the relevant html has been generated. We're forced to do this
238 // because of the way in which lib/ajax/ajaxcourse.js is written.
239 echo '<script type="text/javascript" ';
240 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
242 $COURSE->javascriptportal->print_javascript($course->id);
246 print_footer(NULL, $course);