Detected a display bug when looking for possible causes of
[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
1eb25080 50 //We *always* need to get active blocks (perhaps this should be cached
51 //in a session object and updated in when editing = true ????
52 $recblocks = get_records('blocks','visible','1');
53
0f3fe4b6 54 // Doing this now so we can pass the results to block_action()
55 // and dodge the overhead of doing the same work twice.
56
57 $blocks = $course->blockinfo;
58 $delimpos = strpos($blocks, ':');
59
60 if($delimpos === false) {
61 // No ':' found, we have all left blocks
62 $leftblocks = explode(',', $blocks);
63 $rightblocks = array();
64 }
65 else if($delimpos === 0) {
66 // ':' at start of string, we have all right blocks
67 $blocks = substr($blocks, 1);
68 $leftblocks = array();
69 $rightblocks = explode(',', $blocks);
70 }
71 else {
72 // Both left and right blocks
73 $leftpart = substr($blocks, 0, $delimpos);
74 $rightpart = substr($blocks, $delimpos + 1);
75 $leftblocks = explode(',', $leftpart);
76 $rightblocks = explode(',', $rightpart);
77 }
78
79 if (!isset($USER->editing)) {
80 $USER->editing = false;
81 }
82
83 $editing = false;
84
73047f2f 85 if (isteacheredit($course->id)) {
0f3fe4b6 86 if (isset($edit)) {
9c9f7d77 87 if ($edit == "on") {
88 $USER->editing = true;
89 } else if ($edit == "off") {
90 $USER->editing = false;
91 }
f9903ed0 92 }
8223d271 93
0f3fe4b6 94 $editing = $USER->editing;
95
73fafc38 96 if (isset($hide)) {
97 set_section_visible($course->id, $hide, "0");
98 }
7d99d695 99
73fafc38 100 if (isset($show)) {
101 set_section_visible($course->id, $show, "1");
102 }
12905134 103
0f3fe4b6 104 if (isset($_GET['blockaction'])) {
105 if (isset($_GET['blockid'])) {
106 block_action($course, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
107 }
108 }
109
110 // This has to happen after block_action() has possibly updated the two arrays
111 $allblocks = array_merge($leftblocks, $rightblocks);
112
113 $missingblocks = array();
0f3fe4b6 114 if($editing && $recblocks) {
115 foreach($recblocks as $recblock) {
116 // If it's not hidden or displayed right now...
117 if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
118 // And if it's applicable for display in this format...
119 if(block_method_result($recblock->name, 'applicable_formats') & $courseformat) {
120 // Add it to the missing blocks
121 $missingblocks[] = $recblock->id;
122 }
123 }
124 }
125 }
126
0f3fe4b6 127
12905134 128 if (!empty($section)) {
129 if (!empty($move)) {
56e34ee4 130 if (!move_section($course, $section, $move)) {
131 notify("An error occurred while moving a section");
132 }
12905134 133 }
134 }
48d72fa7 135 } else {
136 $USER->editing = false;
7d99d695 137 }
138
4c701e6f 139 $SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
4c701e6f 140
b8391635 141 if (! $course->category) { // This course is not a real course.
142 redirect("$CFG->wwwroot/");
143 }
144
43acb21f 145 $strcourse = get_string("course");
fa0626c6 146
b328523c 147 $loggedinas = "<p class=\"logininfo\">".user_login_string($course, $USER)."</p>";
a282d0ff 148
0f3fe4b6 149 print_header("$strcourse: $course->fullname", "$course->fullname", "$course->shortname",
43acb21f 150 "", "", true, update_course_icon($course->id), $loggedinas);
f9903ed0 151
9c9f7d77 152 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
19801b2b 153
dfec7b01 154 if (! $sections = get_all_sections($course->id)) { // No sections found
155 // Double-check to be extra sure
156 if (! $section = get_record("course_sections", "course", $course->id, "section", 0)) {
157 $section->course = $course->id; // Create a default section.
158 $section->section = 0;
159 $section->visible = 1;
160 $section->id = insert_record("course_sections", $section);
161 }
162 if (! $sections = get_all_sections($course->id) ) { // Try again
19801b2b 163 error("Error finding or creating section structures for this course");
164 }
165 }
7468bf01 166
51955364 167 if (empty($course->modinfo)) { // Course cache was never made
168 rebuild_course_cache($course->id);
db34bb59 169 if (! $course = get_record("course", "id", $course->id) ) {
170 error("That's an invalid course id");
171 }
51955364 172 }
173
1eb25080 174 //Calculate left and right width. This has to be calculated *always*. Perhaps
175 //we could cache it in a session variable and update it only when editing=true ????
176 //This caused some display problems (100 width) in student mode.
177 $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
178 $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
179
6049fb78 180 require("$CFG->dirroot/course/format/$course->format/format.php"); // Include the actual course format
f9903ed0 181
2c26c985 182 print_footer();
f9903ed0 183
f9903ed0 184?>