Small addition for the future: blocks will be able to define their own
[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");
0d250ac1 8 require_once('../calendar/lib.php');
f9903ed0 9
388f8911 10 optional_variable($id);
11 optional_variable($name);
f9903ed0 12
388f8911 13 if (!$id and !$name) {
14 error("Must specify course id or short name");
15 }
f9903ed0 16
c7609872 17 if (!empty($_GET['name'])) {
0f3fe4b6 18 if (! ($course = get_record("course", "shortname", $name)) ) {
388f8911 19 error("That's an invalid short course name");
20 }
21 } else {
0f3fe4b6 22 if (! ($course = get_record("course", "id", $id)) ) {
388f8911 23 error("That's an invalid course id");
24 }
f9903ed0 25 }
26
783da262 27 require_login($course->id);
388f8911 28
600149be 29 add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
f9903ed0 30
0f3fe4b6 31 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
32 $course->format = 'weeks'; // Default format is weeks
33 }
34
35 // Can't avoid this... :(
36 switch($course->format) {
37 case 'weeks':
38 $courseformat = COURSE_FORMAT_WEEKS;
39 break;
40 case 'topics':
41 $courseformat = COURSE_FORMAT_TOPICS;
42 break;
43 case 'social':
44 $courseformat = COURSE_FORMAT_SOCIAL;
45 break;
46 default:
47 $courseformat = 0;
48 }
49
50 // Doing this now so we can pass the results to block_action()
51 // and dodge the overhead of doing the same work twice.
52
53 $blocks = $course->blockinfo;
54 $delimpos = strpos($blocks, ':');
55
56 if($delimpos === false) {
57 // No ':' found, we have all left blocks
58 $leftblocks = explode(',', $blocks);
59 $rightblocks = array();
60 }
61 else if($delimpos === 0) {
62 // ':' at start of string, we have all right blocks
63 $blocks = substr($blocks, 1);
64 $leftblocks = array();
65 $rightblocks = explode(',', $blocks);
66 }
67 else {
68 // Both left and right blocks
69 $leftpart = substr($blocks, 0, $delimpos);
70 $rightpart = substr($blocks, $delimpos + 1);
71 $leftblocks = explode(',', $leftpart);
72 $rightblocks = explode(',', $rightpart);
73 }
82b181f2 74
0f3fe4b6 75 if (!isset($USER->editing)) {
76 $USER->editing = false;
77 }
78
79 $editing = false;
80
73047f2f 81 if (isteacheredit($course->id)) {
0f3fe4b6 82 if (isset($edit)) {
9c9f7d77 83 if ($edit == "on") {
84 $USER->editing = true;
85 } else if ($edit == "off") {
86 $USER->editing = false;
87 }
f9903ed0 88 }
8223d271 89
0f3fe4b6 90 $editing = $USER->editing;
91
73fafc38 92 if (isset($hide)) {
82b181f2 93 set_section_visible($course->id, $hide, '0');
73fafc38 94 }
7d99d695 95
73fafc38 96 if (isset($show)) {
82b181f2 97 set_section_visible($course->id, $show, '1');
73fafc38 98 }
12905134 99
0f3fe4b6 100 if (isset($_GET['blockaction'])) {
101 if (isset($_GET['blockid'])) {
102 block_action($course, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
103 }
104 }
105
106 // This has to happen after block_action() has possibly updated the two arrays
107 $allblocks = array_merge($leftblocks, $rightblocks);
108
109 $missingblocks = array();
82b181f2 110 $recblocks = get_records('blocks','visible','1');
111
0f3fe4b6 112 if($editing && $recblocks) {
113 foreach($recblocks as $recblock) {
114 // If it's not hidden or displayed right now...
115 if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
116 // And if it's applicable for display in this format...
117 if(block_method_result($recblock->name, 'applicable_formats') & $courseformat) {
118 // Add it to the missing blocks
119 $missingblocks[] = $recblock->id;
120 }
121 }
122 }
123 }
124
12905134 125 if (!empty($section)) {
126 if (!empty($move)) {
56e34ee4 127 if (!move_section($course, $section, $move)) {
128 notify("An error occurred while moving a section");
129 }
12905134 130 }
131 }
48d72fa7 132 } else {
133 $USER->editing = false;
7d99d695 134 }
135
4c701e6f 136 $SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
4c701e6f 137
b8391635 138 if (! $course->category) { // This course is not a real course.
139 redirect("$CFG->wwwroot/");
140 }
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
2c26c985 193 print_footer();
f9903ed0 194
f9903ed0 195?>