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