Latest updates
[moodle.git] / course / view.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Display the course home page.
4
b0e3a925 5 require_once("../config.php");
6 require_once("lib.php");
0f3fe4b6 7 require_once("$CFG->libdir/blocklib.php");
f9903ed0 8
388f8911 9 optional_variable($id);
10 optional_variable($name);
f9903ed0 11
388f8911 12 if (!$id and !$name) {
13 error("Must specify course id or short name");
14 }
f9903ed0 15
c7609872 16 if (!empty($_GET['name'])) {
0f3fe4b6 17 if (! ($course = get_record("course", "shortname", $name)) ) {
388f8911 18 error("That's an invalid short course name");
19 }
20 } else {
0f3fe4b6 21 if (! ($course = get_record("course", "id", $id)) ) {
388f8911 22 error("That's an invalid course id");
23 }
f9903ed0 24 }
25
783da262 26 require_login($course->id);
388f8911 27
af62781b 28 require_once('../calendar/lib.php'); /// This is after login because it needs $USER
29
600149be 30 add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
f9903ed0 31
0f3fe4b6 32 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
33 $course->format = 'weeks'; // Default format is weeks
34 }
35
36 // Can't avoid this... :(
37 switch($course->format) {
38 case 'weeks':
39 $courseformat = COURSE_FORMAT_WEEKS;
40 break;
41 case 'topics':
42 $courseformat = COURSE_FORMAT_TOPICS;
43 break;
44 case 'social':
45 $courseformat = COURSE_FORMAT_SOCIAL;
46 break;
47 default:
48 $courseformat = 0;
49 }
50
51 // Doing this now so we can pass the results to block_action()
52 // and dodge the overhead of doing the same work twice.
53
54 $blocks = $course->blockinfo;
55 $delimpos = strpos($blocks, ':');
56
57 if($delimpos === false) {
58 // No ':' found, we have all left blocks
59 $leftblocks = explode(',', $blocks);
60 $rightblocks = array();
61 }
62 else if($delimpos === 0) {
63 // ':' at start of string, we have all right blocks
64 $blocks = substr($blocks, 1);
65 $leftblocks = array();
66 $rightblocks = explode(',', $blocks);
67 }
68 else {
69 // Both left and right blocks
70 $leftpart = substr($blocks, 0, $delimpos);
71 $rightpart = substr($blocks, $delimpos + 1);
72 $leftblocks = explode(',', $leftpart);
73 $rightblocks = explode(',', $rightpart);
74 }
82b181f2 75
0f3fe4b6 76 if (!isset($USER->editing)) {
77 $USER->editing = false;
78 }
79
80 $editing = false;
81
73047f2f 82 if (isteacheredit($course->id)) {
0f3fe4b6 83 if (isset($edit)) {
9c9f7d77 84 if ($edit == "on") {
85 $USER->editing = true;
86 } else if ($edit == "off") {
87 $USER->editing = false;
88 }
f9903ed0 89 }
8223d271 90
0f3fe4b6 91 $editing = $USER->editing;
92
73fafc38 93 if (isset($hide)) {
82b181f2 94 set_section_visible($course->id, $hide, '0');
73fafc38 95 }
7d99d695 96
73fafc38 97 if (isset($show)) {
82b181f2 98 set_section_visible($course->id, $show, '1');
73fafc38 99 }
12905134 100
0f3fe4b6 101 if (isset($_GET['blockaction'])) {
102 if (isset($_GET['blockid'])) {
103 block_action($course, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
104 }
105 }
106
107 // This has to happen after block_action() has possibly updated the two arrays
108 $allblocks = array_merge($leftblocks, $rightblocks);
109
110 $missingblocks = array();
82b181f2 111 $recblocks = get_records('blocks','visible','1');
112
0784eb7e 113 // Note down which blocks are going to get displayed
114 blocks_used($allblocks, $recblocks);
115
0f3fe4b6 116 if($editing && $recblocks) {
117 foreach($recblocks as $recblock) {
118 // If it's not hidden or displayed right now...
119 if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
120 // And if it's applicable for display in this format...
121 if(block_method_result($recblock->name, 'applicable_formats') & $courseformat) {
122 // Add it to the missing blocks
123 $missingblocks[] = $recblock->id;
124 }
125 }
126 }
127 }
128
12905134 129 if (!empty($section)) {
130 if (!empty($move)) {
56e34ee4 131 if (!move_section($course, $section, $move)) {
132 notify("An error occurred while moving a section");
133 }
12905134 134 }
135 }
48d72fa7 136 } else {
137 $USER->editing = false;
0784eb7e 138
139 // Note down which blocks are going to get displayed
140 $allblocks = array_merge($leftblocks, $rightblocks);
141 $recblocks = get_records('blocks','visible','1');
142 blocks_used($allblocks, $recblocks);
7d99d695 143 }
144
4c701e6f 145 $SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
4c701e6f 146
b8391635 147 if (! $course->category) { // This course is not a real course.
148 redirect("$CFG->wwwroot/");
149 }
150
43acb21f 151 $strcourse = get_string("course");
fa0626c6 152
b328523c 153 $loggedinas = "<p class=\"logininfo\">".user_login_string($course, $USER)."</p>";
a282d0ff 154
0f3fe4b6 155 print_header("$strcourse: $course->fullname", "$course->fullname", "$course->shortname",
43acb21f 156 "", "", true, update_course_icon($course->id), $loggedinas);
f9903ed0 157
9c9f7d77 158 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
19801b2b 159
dfec7b01 160 if (! $sections = get_all_sections($course->id)) { // No sections found
161 // Double-check to be extra sure
162 if (! $section = get_record("course_sections", "course", $course->id, "section", 0)) {
163 $section->course = $course->id; // Create a default section.
164 $section->section = 0;
165 $section->visible = 1;
166 $section->id = insert_record("course_sections", $section);
167 }
168 if (! $sections = get_all_sections($course->id) ) { // Try again
19801b2b 169 error("Error finding or creating section structures for this course");
170 }
171 }
7468bf01 172
51955364 173 if (empty($course->modinfo)) { // Course cache was never made
174 rebuild_course_cache($course->id);
db34bb59 175 if (! $course = get_record("course", "id", $course->id) ) {
176 error("That's an invalid course id");
177 }
51955364 178 }
179
82b181f2 180 // If the block width cache is not set, set it
181 if(!isset($SESSION->blockcache->width->{$course->id}) || $editing) {
182
183 // This query might be optimized away if we 're in editing mode
184 if(!isset($recblocks)) {
185 $recblocks = get_records('blocks','visible','1');
186 }
187 $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
188 $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
189
190 // This may be kind of organizational overkill, granted...
191 // But is there any real need to simplify the structure?
192 $SESSION->blockcache->width->{$course->id}->left = $preferred_width_left;
193 $SESSION->blockcache->width->{$course->id}->right = $preferred_width_right;
194 }
195 else {
196 $preferred_width_left = $SESSION->blockcache->width->{$course->id}->left;
197 $preferred_width_right = $SESSION->blockcache->width->{$course->id}->right;
198 }
1eb25080 199
6049fb78 200 require("$CFG->dirroot/course/format/$course->format/format.php"); // Include the actual course format
f9903ed0 201
2c26c985 202 print_footer();
f9903ed0 203
f9903ed0 204?>