Added LinuxChix theme
[moodle-linuxchix.git] / course / view.php
blob7319c39f2fc72660390f6b29c3cb2a3396350a99
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);
53 // Switchrole - sanity check in cost-order...
54 $reset_user_allowed_editing = false;
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...
59 $aroles = get_assignable_roles($context);
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);
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;
74 //If course is hosted on an external server, redirect to corresponding
75 //url with appropriate authentication attached as parameter
76 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
77 include $CFG->dirroot .'/course/externservercourse.php';
78 if (function_exists('extern_server_course')) {
79 if ($extern_url = extern_server_course($course)) {
80 redirect($extern_url);
86 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
88 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
90 $course->format = clean_param($course->format, PARAM_ALPHA);
91 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
92 $course->format = 'weeks'; // Default format is weeks
95 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
96 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
98 if ($reset_user_allowed_editing) {
99 // ugly hack
100 unset($PAGE->_user_allowed_editing);
103 if (!isset($USER->editing)) {
104 $USER->editing = 0;
106 if ($PAGE->user_allowed_editing()) {
107 if (($edit == 1) and confirm_sesskey()) {
108 $USER->editing = 1;
109 } else if (($edit == 0) and confirm_sesskey()) {
110 $USER->editing = 0;
111 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
112 $USER->activitycopy = false;
113 $USER->activitycopycourse = NULL;
117 if ($hide && confirm_sesskey()) {
118 set_section_visible($course->id, $hide, '0');
121 if ($show && confirm_sesskey()) {
122 set_section_visible($course->id, $show, '1');
125 if (!empty($section)) {
126 if (!empty($move) and confirm_sesskey()) {
127 if (!move_section($course, $section, $move)) {
128 notify('An error occurred while moving a section');
132 } else {
133 $USER->editing = 0;
136 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
139 if ($course->id == SITEID) {
140 // This course is not a real course.
141 redirect($CFG->wwwroot .'/');
145 // AJAX-capable course format?
146 $useajax = false;
147 $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
148 $bodytags = '';
150 if (empty($CFG->disablecourseajax) and file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
152 // TODO: stop abusing CFG global here
153 $CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
154 $CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
156 require_once($ajaxformatfile);
158 if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
159 // Course-based switches
161 if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
163 require_js(array('yui_yahoo',
164 'yui_dom',
165 'yui_event',
166 'yui_dragdrop',
167 'yui_connection',
168 'ajaxcourse_blocks',
169 'ajaxcourse_sections'));
171 if (debugging('', DEBUG_DEVELOPER)) {
172 require_js(array('yui_logger'));
174 $bodytags = 'onload = "javascript:
175 show_logger = function() {
176 var logreader = new YAHOO.widget.LogReader();
177 logreader.newestOnTop = false;
178 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
180 show_logger();
184 // Okay, global variable alert. VERY UGLY. We need to create
185 // this object here before the <blockname>_print_block()
186 // function is called, since that function needs to set some
187 // stuff in the javascriptportal object.
188 $COURSE->javascriptportal = new jsportal();
189 $useajax = true;
194 $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
197 $PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);
198 // Course wrapper start.
199 echo '<div class="course-content">';
201 $modinfo =& get_fast_modinfo($COURSE);
202 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
203 foreach($mods as $modid=>$unused) {
204 if (!isset($modinfo->cms[$modid])) {
205 rebuild_course_cache($course->id);
206 $modinfo =& get_fast_modinfo($COURSE);
207 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
208 break;
212 if (! $sections = get_all_sections($course->id)) { // No sections found
213 // Double-check to be extra sure
214 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
215 $section->course = $course->id; // Create a default section.
216 $section->section = 0;
217 $section->visible = 1;
218 $section->id = insert_record('course_sections', $section);
220 if (! $sections = get_all_sections($course->id) ) { // Try again
221 error('Error finding or creating section structures for this course');
225 // Include the actual course format.
226 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
227 // Content wrapper end.
228 echo "</div>\n\n";
231 // Use AJAX?
232 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
233 // At the bottom because we want to process sections and activities
234 // after the relevant html has been generated. We're forced to do this
235 // because of the way in which lib/ajax/ajaxcourse.js is written.
236 echo '<script type="text/javascript" ';
237 echo "src=\"{$CFG->wwwroot}/lib/ajax/ajaxcourse.js\"></script>\n";
239 $COURSE->javascriptportal->print_javascript($course->id);
243 print_footer(NULL, $course);