MDL-24419 (3): Add new parameter to format_float for shorter display
[moodle.git] / course / view.php
CommitLineData
aa6c1ced 1<?php
f9903ed0 2
3// Display the course home page.
4
08cebf19 5 require_once('../config.php');
6 require_once('lib.php');
afc58a28 7 require_once($CFG->dirroot.'/mod/forum/lib.php');
516c5eca 8 require_once($CFG->libdir.'/completionlib.php');
f9903ed0 9
2335781f 10 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 11 $name = optional_param('name', '', PARAM_RAW);
12 $edit = optional_param('edit', -1, PARAM_BOOL);
13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
15 $idnumber = optional_param('idnumber', '', PARAM_RAW);
0cdae0dc 16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 18 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 19 $switchrole = optional_param('switchrole',-1, PARAM_INT);
20
599a52fe 21 $params = array();
2335781f 22 if (!empty($name)) {
599a52fe 23 $params = array('shortname' => $name);
816acb46 24 } else if (!empty($idnumber)) {
599a52fe
DP
25 $params = array('idnumber' => $idnumber);
26 } else if (!empty($id)) {
27 $params = array('id' => $id);
28 }else {
29 print_error('unspecifycourseid', 'error');
f9903ed0 30 }
31
599a52fe
DP
32 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
33
45774bdd
DP
34 $urlparams = array('id' => $course->id);
35 if ($section) {
36 $urlparams['section'] = $section;
37 }
38
39 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
088e3363 40
00653161 41 preload_course_contexts($course->id);
599a52fe 42 $context = context_course::instance($course->id, MUST_EXIST);
3a52e764 43
d7d4b0e5 44 // Remove any switched roles before checking login
45 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 46 role_switch($switchrole, $context);
47 }
48
a6af674a 49 require_login($course);
388f8911 50
d7d4b0e5 51 // Switchrole - sanity check in cost-order...
46bd37bf 52 $reset_user_allowed_editing = false;
d7d4b0e5 53 if ($switchrole > 0 && confirm_sesskey() &&
54 has_capability('moodle/role:switchroles', $context)) {
55 // is this role assignable in this context?
56 // inquiring minds want to know...
82701e24 57 $aroles = get_switchable_roles($context);
d7d4b0e5 58 if (is_array($aroles) && isset($aroles[$switchrole])) {
59 role_switch($switchrole, $context);
60 // Double check that this role is allowed here
cdbea7ee 61 require_login($course);
d7d4b0e5 62 }
46bd37bf 63 // reset course page state - this prevents some weird problems ;-)
64 $USER->activitycopy = false;
65 $USER->activitycopycourse = NULL;
66 unset($USER->activitycopyname);
67 unset($SESSION->modform);
68 $USER->editing = 0;
69 $reset_user_allowed_editing = true;
3a52e764 70 }
71
542a0435 72 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 73 //url with appropriate authentication attached as parameter
892c0825 74 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
75 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 76 if (function_exists('extern_server_course')) {
542a0435 77 if ($extern_url = extern_server_course($course)) {
78 redirect($extern_url);
79 }
80 }
81 }
82
3a52e764 83
08cebf19 84 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 85
45774bdd 86 //TODO: danp do we need different urls?
892c0825 87 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
f9903ed0 88
e069b3e2 89 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 90 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
91 $course->format = 'weeks'; // Default format is weeks
92 }
93
78946b9b 94 $PAGE->set_pagelayout('course');
66b10689 95 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 96 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 97
46bd37bf 98 if ($reset_user_allowed_editing) {
99 // ugly hack
100 unset($PAGE->_user_allowed_editing);
101 }
daa27ce4 102
0f3fe4b6 103 if (!isset($USER->editing)) {
d2b23346 104 $USER->editing = 0;
0f3fe4b6 105 }
a3f24f7c 106 if ($PAGE->user_allowed_editing()) {
0cdae0dc 107 if (($edit == 1) and confirm_sesskey()) {
d2b23346 108 $USER->editing = 1;
ad96cc52
AKA
109 // Redirect to site root if Editing is toggled on frontpage
110 if ($course->id == SITEID) {
111 redirect($CFG->wwwroot .'/?redirect=0');
112 } else {
113 redirect($PAGE->url);
114 }
0cdae0dc 115 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 116 $USER->editing = 0;
a3f24f7c 117 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
118 $USER->activitycopy = false;
119 $USER->activitycopycourse = NULL;
9c9f7d77 120 }
ad96cc52
AKA
121 // Redirect to site root if Editing is toggled on frontpage
122 if ($course->id == SITEID) {
123 redirect($CFG->wwwroot .'/?redirect=0');
124 } else {
125 redirect($PAGE->url);
126 }
f9903ed0 127 }
8223d271 128
af189935
PS
129 if (has_capability('moodle/course:update', $context)) {
130 if ($hide && confirm_sesskey()) {
131 set_section_visible($course->id, $hide, '0');
e3c171e1 132 redirect($PAGE->url);
af189935 133 }
7d99d695 134
af189935
PS
135 if ($show && confirm_sesskey()) {
136 set_section_visible($course->id, $show, '1');
e3c171e1 137 redirect($PAGE->url);
af189935 138 }
12905134 139
af189935
PS
140 if (!empty($section)) {
141 if (!empty($move) and confirm_sesskey()) {
93d46f48
RK
142 if (move_section($course, $section, $move)) {
143 if ($course->id == SITEID) {
144 redirect($CFG->wwwroot . '/?redirect=0');
145 } else {
e3c171e1 146 redirect(course_get_url($course));
93d46f48
RK
147 }
148 } else {
af189935
PS
149 echo $OUTPUT->notification('An error occurred while moving a section');
150 }
56e34ee4 151 }
12905134 152 }
153 }
48d72fa7 154 } else {
d2b23346 155 $USER->editing = 0;
7d99d695 156 }
157
45774bdd 158 $SESSION->fromdiscussion = $PAGE->url->out(false);
4c701e6f 159
f45cc76f 160
161 if ($course->id == SITEID) {
162 // This course is not a real course.
892c0825 163 redirect($CFG->wwwroot .'/');
83f3c62d 164 }
0a127169 165
cf615522 166 $completion = new completion_info($course);
167 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
168 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
169 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
170 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
171 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
172
173 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 174 }
175
8a7934f7
PS
176 // We are currently keeping the button here from 1.x to help new teachers figure out
177 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 178 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 179 if ($PAGE->user_allowed_editing()) {
d628215d 180 $buttons = $OUTPUT->edit_button($PAGE->url);
2b3d1ac7 181 $PAGE->set_button($buttons);
5d3e9d9f 182 }
183
2b9e3bac 184 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 185 $PAGE->set_heading($course->fullname);
0a122046 186 echo $OUTPUT->header();
f45cc76f 187
cf615522 188 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 189 // This value tracks whether there has been a dynamic change to the page.
190 // It is used so that if a user does this - (a) set some tickmarks, (b)
191 // go to another page, (c) clicks Back button - the page will
192 // automatically reload. Otherwise it would start with the wrong tick
193 // values.
47e75d04
SH
194 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
195 echo html_writer::start_tag('div');
196 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
197 echo html_writer::end_tag('div');
198 echo html_writer::end_tag('form');
4e781c7b 199 }
200
f45cc76f 201 // Course wrapper start.
47e75d04 202 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 203
f20edd52 204 $modinfo = get_fast_modinfo($COURSE);
9c9f7d77 205 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 206 foreach($mods as $modid=>$unused) {
207 if (!isset($modinfo->cms[$modid])) {
208 rebuild_course_cache($course->id);
f20edd52 209 $modinfo = get_fast_modinfo($COURSE);
a6af674a 210 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
211 break;
212 }
213 }
19801b2b 214
dfec7b01 215 if (! $sections = get_all_sections($course->id)) { // No sections found
216 // Double-check to be extra sure
6bb08163 217 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
dfec7b01 218 $section->course = $course->id; // Create a default section.
219 $section->section = 0;
220 $section->visible = 1;
09eb2151 221 $section->summaryformat = FORMAT_HTML;
6bb08163 222 $section->id = $DB->insert_record('course_sections', $section);
dfec7b01 223 }
224 if (! $sections = get_all_sections($course->id) ) { // Try again
dbf91d9d 225 print_error('cannotcreateorfindstructs', 'error');
19801b2b 226 }
227 }
7468bf01 228
45774bdd
DP
229 // CAUTION, hacky fundamental variable defintion to follow!
230 // Note that because of the way course fromats are constructed though
231 // inclusion we pass parameters around this way..
232 $displaysection = $section;
233
f45cc76f 234 // Include the actual course format.
235 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
236 // Content wrapper end.
47e75d04
SH
237
238 echo html_writer::end_tag('div');
f45cc76f 239
ebaa29d1
ARN
240 // Include the command toolbox YUI module
241 include_course_ajax($course, $modnamesused);
f45cc76f 242
d60c1124 243 echo $OUTPUT->footer();