Merged fixes from HEAD, bug MDL-6791
[moodle.git] / course / view.php
blobdb0458cdc90565a768960edc9877757fad8c3b03
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');
9 $id = optional_param('id', 0, PARAM_INT);
10 $name = optional_param('name', '', PARAM_RAW);
11 $edit = optional_param('edit', -1, PARAM_BOOL);
12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $idnumber = optional_param('idnumber', '', PARAM_RAW);
15 $studentview = optional_param('studentview', -1, PARAM_BOOL);
16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
18 $marker = optional_param('marker',-1 , PARAM_INT);
21 if (empty($id) && empty($name) && empty($idnumber)) {
22 error("Must specify course id, short name or idnumber");
25 if (!empty($name)) {
26 if (! ($course = get_record('course', 'shortname', $name)) ) {
27 error('Invalid short course name');
29 } else if (!empty($idnumber)) {
30 if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
31 error('Invalid course idnumber');
33 } else {
34 if (! ($course = get_record('course', 'id', $id)) ) {
35 error('Invalid course id');
39 require_login($course->id);
41 //If course is hosted on an external server, redirect to corresponding
42 //url with appropriate authentication attached as parameter
43 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
44 include $CFG->dirroot .'/course/externservercourse.php';
45 if (function_exists(extern_server_course)) {
46 if ($extern_url = extern_server_course($course)) {
47 redirect($extern_url);
52 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
54 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
56 $course->format = clean_param($course->format, PARAM_ALPHA);
57 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
58 $course->format = 'weeks'; // Default format is weeks
61 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
62 $pageblocks = blocks_setup($PAGE,BLOCKS_PINNED_BOTH);
64 if (!isset($USER->editing)) {
65 $USER->editing = 0;
68 if (!isset($USER->studentview)) {
69 $USER->studentview = false;
72 // need to check this here, as studentview=on disables edit allowed (where 'on' is checked)
73 if (($studentview == 0) and confirm_sesskey()) {
74 $USER->studentview = false;
77 if ($PAGE->user_allowed_editing()) {
78 if (($edit == 1) and confirm_sesskey()) {
79 $USER->editing = 1;
80 } else if (($edit == 0) and confirm_sesskey()) {
81 $USER->editing = 0;
82 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
83 $USER->activitycopy = false;
84 $USER->activitycopycourse = NULL;
88 if (($studentview == 1) and confirm_sesskey()) {
89 $USER->studentview = true;
90 $USER->editing = 0;
93 if ($hide && confirm_sesskey()) {
94 set_section_visible($course->id, $hide, '0');
97 if ($show && confirm_sesskey()) {
98 set_section_visible($course->id, $show, '1');
101 if (!empty($section)) {
102 if (!empty($move) and confirm_sesskey()) {
103 if (!move_section($course, $section, $move)) {
104 notify('An error occurred while moving a section');
108 } else {
109 $USER->editing = 0;
112 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
114 if ($course->id == SITEID) { // This course is not a real course.
115 redirect($CFG->wwwroot .'/');
118 $PAGE->print_header(get_string('course').': %fullname%');
120 echo '<div class="course-content">'; // course wrapper start
122 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
124 if (! $sections = get_all_sections($course->id)) { // No sections found
125 // Double-check to be extra sure
126 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
127 $section->course = $course->id; // Create a default section.
128 $section->section = 0;
129 $section->visible = 1;
130 $section->id = insert_record('course_sections', $section);
132 if (! $sections = get_all_sections($course->id) ) { // Try again
133 error('Error finding or creating section structures for this course');
137 if (empty($course->modinfo)) { // Course cache was never made
138 rebuild_course_cache($course->id);
139 if (! $course = get_record('course', 'id', $course->id) ) {
140 error("That's an invalid course id");
144 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php'); // Include the actual course format
146 echo '</div>'; // content wrapper end
147 print_footer(NULL, $course);