$CFG->tabselectedtofront is out. Part of MDL-8746
[pfb-moodle.git] / course / view.php
blob8aa8663c5d3d4831a2b1ffcc2d7f5385334037d7
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');
10 $id = optional_param('id', 0, PARAM_INT);
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);
16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
18 $marker = optional_param('marker',-1 , PARAM_INT);
19 $switchrole = optional_param('switchrole',-1, PARAM_INT);
23 if (empty($id) && empty($name) && empty($idnumber)) {
24 error("Must specify course id, short name or idnumber");
27 if (!empty($name)) {
28 if (! ($course = get_record('course', 'shortname', $name)) ) {
29 error('Invalid short course name');
31 } else if (!empty($idnumber)) {
32 if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
33 error('Invalid course idnumber');
35 } else {
36 if (! ($course = get_record('course', 'id', $id)) ) {
37 error('Invalid course id');
41 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
42 print_error('nocontext');
45 if ($switchrole == 0) { // Remove any switched roles before checking login
46 role_switch($switchrole, $context);
49 require_login($course->id);
51 if ($switchrole > 0) {
52 role_switch($switchrole, $context);
53 require_login($course->id); // Double check that this role is allowed here
56 //If course is hosted on an external server, redirect to corresponding
57 //url with appropriate authentication attached as parameter
58 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
59 include $CFG->dirroot .'/course/externservercourse.php';
60 if (function_exists('extern_server_course')) {
61 if ($extern_url = extern_server_course($course)) {
62 redirect($extern_url);
68 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
70 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
72 $course->format = clean_param($course->format, PARAM_ALPHA);
73 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
74 $course->format = 'weeks'; // Default format is weeks
77 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
78 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
81 if (!isset($USER->editing)) {
82 $USER->editing = 0;
84 if ($PAGE->user_allowed_editing()) {
85 if (($edit == 1) and confirm_sesskey()) {
86 $USER->editing = 1;
87 } else if (($edit == 0) and confirm_sesskey()) {
88 $USER->editing = 0;
89 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
90 $USER->activitycopy = false;
91 $USER->activitycopycourse = NULL;
95 if ($hide && confirm_sesskey()) {
96 set_section_visible($course->id, $hide, '0');
99 if ($show && confirm_sesskey()) {
100 set_section_visible($course->id, $show, '1');
103 if (!empty($section)) {
104 if (!empty($move) and confirm_sesskey()) {
105 if (!move_section($course, $section, $move)) {
106 notify('An error occurred while moving a section');
110 } else {
111 $USER->editing = 0;
114 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
117 if ($course->id == SITEID) {
118 // This course is not a real course.
119 redirect($CFG->wwwroot .'/');
123 // AJAX-capable course format?
124 $CFG->useajax = false;
125 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
126 $bodytags = '';
128 if (file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
130 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
131 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
133 require_once($ajaxformatfile);
135 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
136 // Course-based switches
138 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
140 require_js(array('yui_yahoo',
141 'yui_dom',
142 'yui_event',
143 'yui_dragdrop',
144 'yui_connection',
145 'ajaxcourse_blocks',
146 'ajaxcourse_sections'));
148 if (debugging('', DEBUG_DEVELOPER)) {
149 require_js(array('yui_logger'));
151 $bodytags = 'onload = "javascript:
152 show_logger = function() {
153 var logreader = new YAHOO.widget.LogReader();
154 logreader.newestOnTop = false;
155 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
157 show_logger();
161 // Okay, global variable alert. VERY UGLY. We need to create
162 // this object here before the <blockname>_print_block()
163 // function is called, since that function needs to set some
164 // stuff in the javascriptportal object.
165 $COURSE->javascriptportal = new jsportal();
166 $CFG->useajax = true;
171 $CFG->blocksdrag = $CFG->useajax; // this will add a new class to the header so we can style differently
174 $PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);
175 // Course wrapper start.
176 echo '<div class="course-content">';
179 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
181 if (! $sections = get_all_sections($course->id)) { // No sections found
182 // Double-check to be extra sure
183 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
184 $section->course = $course->id; // Create a default section.
185 $section->section = 0;
186 $section->visible = 1;
187 $section->id = insert_record('course_sections', $section);
189 if (! $sections = get_all_sections($course->id) ) { // Try again
190 error('Error finding or creating section structures for this course');
195 if (empty($course->modinfo)) {
196 // Course cache was never made.
197 rebuild_course_cache($course->id);
198 if (! $course = get_record('course', 'id', $course->id) ) {
199 error("That's an invalid course id");
204 // Include the actual course format.
205 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
206 // Content wrapper end.
207 echo "</div>\n\n";
210 // Use AJAX?
211 if ($CFG->useajax && has_capability('moodle/course:manageactivities', $context)) {
212 // At the bottom because we want to process sections and activities
213 // after the relevant html has been generated. We're forced to do this
214 // because of the way in which lib/ajax/ajaxcourse.js is written.
215 echo '<script type="text/javascript" ';
216 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
218 $COURSE->javascriptportal->print_javascript($course->id);
222 print_footer(NULL, $course);