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