Merged from 1.7
[moodle.git] / course / view.php
CommitLineData
24f2b65a 1<?php // $Id$
f9903ed0 2
3// Display the course home page.
4
08cebf19 5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/blocklib.php');
f9903ed0 8
2335781f 9 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 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);
0cdae0dc 15 $section = optional_param('section', 0, PARAM_INT);
16 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 17 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 18 $switchrole = optional_param('switchrole',-1, PARAM_INT);
19
7ea8ea7f 20
f9903ed0 21
533229c4 22 if (empty($id) && empty($name) && empty($idnumber)) {
23 error("Must specify course id, short name or idnumber");
388f8911 24 }
f9903ed0 25
2335781f 26 if (!empty($name)) {
27 if (! ($course = get_record('course', 'shortname', $name)) ) {
28 error('Invalid short course name');
388f8911 29 }
816acb46 30 } else if (!empty($idnumber)) {
31 if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
32 error('Invalid course idnumber');
33 }
388f8911 34 } else {
2335781f 35 if (! ($course = get_record('course', 'id', $id)) ) {
36 error('Invalid course id');
388f8911 37 }
f9903ed0 38 }
39
3a52e764 40 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
41 print_error('nocontext');
42 }
43
ac4c51be 44 if ($switchrole == 0) { // Remove any switched roles before checking login
45 role_switch($switchrole, $context);
46 }
47
783da262 48 require_login($course->id);
388f8911 49
ac4c51be 50 if ($switchrole > 0) {
3a52e764 51 role_switch($switchrole, $context);
ac4c51be 52 require_login($course->id); // Double check that this role is allowed here
3a52e764 53 }
54
542a0435 55 //If course is hosted on an external server, redirect to corresponding
56 //url with appropriate authentication attached as parameter
892c0825 57 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
58 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 59 if (function_exists('extern_server_course')) {
542a0435 60 if ($extern_url = extern_server_course($course)) {
61 redirect($extern_url);
62 }
63 }
64 }
65
3a52e764 66
08cebf19 67 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 68
892c0825 69 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
f9903ed0 70
e069b3e2 71 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 72 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
73 $course->format = 'weeks'; // Default format is weeks
74 }
75
7542a4e5 76 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
12177083 77 $pageblocks = blocks_setup($PAGE,BLOCKS_PINNED_BOTH);
daa27ce4 78
0f3fe4b6 79 if (!isset($USER->editing)) {
d2b23346 80 $USER->editing = 0;
0f3fe4b6 81 }
82
a3f24f7c 83 if ($PAGE->user_allowed_editing()) {
0cdae0dc 84 if (($edit == 1) and confirm_sesskey()) {
d2b23346 85 $USER->editing = 1;
0cdae0dc 86 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 87 $USER->editing = 0;
a3f24f7c 88 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
89 $USER->activitycopy = false;
90 $USER->activitycopycourse = NULL;
9c9f7d77 91 }
f9903ed0 92 }
8223d271 93
0cdae0dc 94 if ($hide && confirm_sesskey()) {
82b181f2 95 set_section_visible($course->id, $hide, '0');
73fafc38 96 }
7d99d695 97
0cdae0dc 98 if ($show && confirm_sesskey()) {
82b181f2 99 set_section_visible($course->id, $show, '1');
73fafc38 100 }
12905134 101
102 if (!empty($section)) {
5b224948 103 if (!empty($move) and confirm_sesskey()) {
56e34ee4 104 if (!move_section($course, $section, $move)) {
892c0825 105 notify('An error occurred while moving a section');
56e34ee4 106 }
12905134 107 }
108 }
48d72fa7 109 } else {
d2b23346 110 $USER->editing = 0;
7d99d695 111 }
112
892c0825 113 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
4c701e6f 114
83f3c62d 115 if ($course->id == SITEID) { // This course is not a real course.
892c0825 116 redirect($CFG->wwwroot .'/');
83f3c62d 117 }
118
f032aa7a 119 $PAGE->print_header(get_string('course').': %fullname%');
f9903ed0 120
ee4dc51e 121 echo '<div class="course-content">'; // course wrapper start
daa27ce4 122
9c9f7d77 123 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
19801b2b 124
dfec7b01 125 if (! $sections = get_all_sections($course->id)) { // No sections found
126 // Double-check to be extra sure
892c0825 127 if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
dfec7b01 128 $section->course = $course->id; // Create a default section.
129 $section->section = 0;
130 $section->visible = 1;
892c0825 131 $section->id = insert_record('course_sections', $section);
dfec7b01 132 }
133 if (! $sections = get_all_sections($course->id) ) { // Try again
892c0825 134 error('Error finding or creating section structures for this course');
19801b2b 135 }
136 }
7468bf01 137
51955364 138 if (empty($course->modinfo)) { // Course cache was never made
139 rebuild_course_cache($course->id);
892c0825 140 if (! $course = get_record('course', 'id', $course->id) ) {
db34bb59 141 error("That's an invalid course id");
142 }
51955364 143 }
144
892c0825 145 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php'); // Include the actual course format
f9903ed0 146
daa27ce4 147 echo '</div>'; // content wrapper end
093a4a24 148 print_footer(NULL, $course);
f9903ed0 149
15ffee79 150?>