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");
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');
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
)) {
84 if ($PAGE->user_allowed_editing()) {
85 if (($edit == 1) and confirm_sesskey()) {
87 } else if (($edit == 0) and confirm_sesskey()) {
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');
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';
129 if (file_exists($ajaxformatfile)) {
130 require_once($ajaxformatfile);
132 if ($USER->editing
&& !empty($USER->ajax
) && !empty($CFG->enableajax
) && $CFG->ajaxcapable
) {
134 if ($meta = require_js(array('yui_yahoo',
138 'yui_connection'))) {
140 if (debugging('', DEBUG_DEVELOPER
)) {
141 $meta .= require_js(array('yui_logger'));
143 $bodytags = 'onload = "javascript:
144 show_logger = function() {
145 var logreader = new YAHOO.widget.LogReader();
146 logreader.newestOnTop = false;
147 logreader.setTitle(\'Moodle Debug: YUI Log Console\');
152 // Okay, global variable alert. VERY UGLY. We need to create
153 // this object here before the <blockname>_print_block()
154 // function is called, since that function needs to set some
155 // stuff in the javascriptportal object.
156 $COURSE->javascriptportal
= new jsportal();
157 $CFG->useajax
= true;
162 $CFG->blocksdrag
= $CFG->useajax
; // this will add a new class to the header so we can style differently
165 $PAGE->print_header(get_string('course').': %fullname%', NULL, $meta, $bodytags);
166 // Course wrapper start.
167 echo '<div class="course-content">';
170 get_all_mods($course->id
, $mods, $modnames, $modnamesplural, $modnamesused);
172 if (! $sections = get_all_sections($course->id
)) { // No sections found
173 // Double-check to be extra sure
174 if (! $section = get_record('course_sections', 'course', $course->id
, 'section', 0)) {
175 $section->course
= $course->id
; // Create a default section.
176 $section->section
= 0;
177 $section->visible
= 1;
178 $section->id
= insert_record('course_sections', $section);
180 if (! $sections = get_all_sections($course->id
) ) { // Try again
181 error('Error finding or creating section structures for this course');
186 if (empty($course->modinfo
)) {
187 // Course cache was never made.
188 rebuild_course_cache($course->id
);
189 if (! $course = get_record('course', 'id', $course->id
) ) {
190 error("That's an invalid course id");
195 // Include the actual course format.
196 require($CFG->dirroot
.'/course/format/'. $course->format
.'/format.php');
197 // Content wrapper end.
203 // At the bottom because we want to process sections and activities
204 // after the relevant html has been generated.
205 if ($jsincludes = require_js(array('ajaxcourse_blocks',
206 'ajaxcourse_sections',
209 $COURSE->javascriptportal
->print_javascript($course->id
);
214 print_footer(NULL, $course);