MDL-11515:
[moodle-linuxchix.git] / course / view.php
blob6fa94bc1a7d8219c6dd1a99fd4ec885e1b43a748
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 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
43 print_error('nocontext');
46 // Remove any switched roles before checking login
47 if ($switchrole == 0 && confirm_sesskey()) {
48 role_switch($switchrole, $context);
51 require_login($course->id);
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);
66 //If course is hosted on an external server, redirect to corresponding
67 //url with appropriate authentication attached as parameter
68 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
69 include $CFG->dirroot .'/course/externservercourse.php';
70 if (function_exists('extern_server_course')) {
71 if ($extern_url = extern_server_course($course)) {
72 redirect($extern_url);
78 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
80 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
82 $course->format = clean_param($course->format, PARAM_ALPHA);
83 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
84 $course->format = 'weeks'; // Default format is weeks
87 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
88 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
91 if (!isset($USER->editing)) {
92 $USER->editing = 0;
94 if ($PAGE->user_allowed_editing()) {
95 if (($edit == 1) and confirm_sesskey()) {
96 $USER->editing = 1;
97 } else if (($edit == 0) and confirm_sesskey()) {
98 $USER->editing = 0;
99 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
100 $USER->activitycopy = false;
101 $USER->activitycopycourse = NULL;
105 if ($hide && confirm_sesskey()) {
106 set_section_visible($course->id, $hide, '0');
109 if ($show && confirm_sesskey()) {
110 set_section_visible($course->id, $show, '1');
113 if (!empty($section)) {
114 if (!empty($move) and confirm_sesskey()) {
115 if (!move_section($course, $section, $move)) {
116 notify('An error occurred while moving a section');
120 } else {
121 $USER->editing = 0;
124 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
127 if ($course->id == SITEID) {
128 // This course is not a real course.
129 redirect($CFG->wwwroot .'/');
133 // AJAX-capable course format?
134 $CFG->useajax = false;
135 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
136 $bodytags = '';
138 if (file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
140 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
141 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
143 require_once($ajaxformatfile);
145 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
146 // Course-based switches
148 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
150 require_js(array('yui_yahoo',
151 'yui_dom',
152 'yui_event',
153 'yui_dragdrop',
154 'yui_connection',
155 'ajaxcourse_blocks',
156 'ajaxcourse_sections'));
158 if (debugging('', DEBUG_DEVELOPER)) {
159 require_js(array('yui_logger'));
161 $bodytags = 'onload = "javascript:
162 show_logger = function() {
163 var logreader = new YAHOO.widget.LogReader();
164 logreader.newestOnTop = false;
165 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
167 show_logger();
171 // Okay, global variable alert. VERY UGLY. We need to create
172 // this object here before the <blockname>_print_block()
173 // function is called, since that function needs to set some
174 // stuff in the javascriptportal object.
175 $COURSE->javascriptportal = new jsportal();
176 $CFG->useajax = true;
181 $CFG->blocksdrag = $CFG->useajax; // this will add a new class to the header so we can style differently
184 $PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);
185 // Course wrapper start.
186 echo '<div class="course-content">';
189 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
191 if (! $sections = get_all_sections($course->id)) { // No sections found
192 // Double-check to be extra sure
193 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
194 $section->course = $course->id; // Create a default section.
195 $section->section = 0;
196 $section->visible = 1;
197 $section->id = insert_record('course_sections', $section);
199 if (! $sections = get_all_sections($course->id) ) { // Try again
200 error('Error finding or creating section structures for this course');
205 if (empty($course->modinfo)) {
206 // Course cache was never made.
207 rebuild_course_cache($course->id);
208 if (! $course = get_record('course', 'id', $course->id) ) {
209 error("That's an invalid course id");
214 // Include the actual course format.
215 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
216 // Content wrapper end.
217 echo "</div>\n\n";
220 // Use AJAX?
221 if ($CFG->useajax && has_capability('moodle/course:manageactivities', $context)) {
222 // At the bottom because we want to process sections and activities
223 // after the relevant html has been generated. We're forced to do this
224 // because of the way in which lib/ajax/ajaxcourse.js is written.
225 echo '<script type="text/javascript" ';
226 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
228 $COURSE->javascriptportal->print_javascript($course->id);
232 print_footer(NULL, $course);