MDL-76897 quiz: quiz_update_all_attempt_sumgrades -> grade_calculator
[moodle.git] / course / view.php
blob594e5ffd4890a43d7b803ab61c7cb18210a80233
1 <?php
3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/completionlib.php');
9 redirect_if_major_upgrade_required();
11 $id = optional_param('id', 0, PARAM_INT);
12 $name = optional_param('name', '', PARAM_TEXT);
13 $edit = optional_param('edit', -1, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $idnumber = optional_param('idnumber', '', PARAM_RAW);
17 $sectionid = optional_param('sectionid', 0, PARAM_INT);
18 $section = optional_param('section', 0, PARAM_INT);
19 $move = optional_param('move', 0, PARAM_INT);
20 $marker = optional_param('marker',-1 , PARAM_INT);
21 $switchrole = optional_param('switchrole',-1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
22 $return = optional_param('return', 0, PARAM_LOCALURL);
24 $params = array();
25 if (!empty($name)) {
26 $params = array('shortname' => $name);
27 } else if (!empty($idnumber)) {
28 $params = array('idnumber' => $idnumber);
29 } else if (!empty($id)) {
30 $params = array('id' => $id);
31 }else {
32 throw new \moodle_exception('unspecifycourseid', 'error');
35 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
37 $urlparams = array('id' => $course->id);
39 // Sectionid should get priority over section number
40 if ($sectionid) {
41 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
43 if ($section) {
44 $urlparams['section'] = $section;
47 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
49 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
50 $PAGE->set_cacheable(false);
52 context_helper::preload_course($course->id);
53 $context = context_course::instance($course->id, MUST_EXIST);
55 // Remove any switched roles before checking login
56 if ($switchrole == 0 && confirm_sesskey()) {
57 role_switch($switchrole, $context);
60 require_login($course);
62 // Switchrole - sanity check in cost-order...
63 $reset_user_allowed_editing = false;
64 if ($switchrole > 0 && confirm_sesskey() &&
65 has_capability('moodle/role:switchroles', $context)) {
66 // is this role assignable in this context?
67 // inquiring minds want to know...
68 $aroles = get_switchable_roles($context);
69 if (is_array($aroles) && isset($aroles[$switchrole])) {
70 role_switch($switchrole, $context);
71 // Double check that this role is allowed here
72 require_login($course);
74 // reset course page state - this prevents some weird problems ;-)
75 $USER->activitycopy = false;
76 $USER->activitycopycourse = NULL;
77 unset($USER->activitycopyname);
78 unset($SESSION->modform);
79 $USER->editing = 0;
80 $reset_user_allowed_editing = true;
83 //If course is hosted on an external server, redirect to corresponding
84 //url with appropriate authentication attached as parameter
85 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
86 include $CFG->dirroot .'/course/externservercourse.php';
87 if (function_exists('extern_server_course')) {
88 if ($extern_url = extern_server_course($course)) {
89 redirect($extern_url);
95 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
97 // Must set layout before gettting section info. See MDL-47555.
98 $PAGE->set_pagelayout('course');
99 $PAGE->add_body_class('limitedwidth');
101 if ($section and $section > 0) {
103 // Get section details and check it exists.
104 $modinfo = get_fast_modinfo($course);
105 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
107 // Check user is allowed to see it.
108 if (!$coursesections->uservisible) {
109 // Check if coursesection has conditions affecting availability and if
110 // so, output availability info.
111 if ($coursesections->visible && $coursesections->availableinfo) {
112 $sectionname = get_section_name($course, $coursesections);
113 $message = get_string('notavailablecourse', '', $sectionname);
114 redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
115 } else {
116 // Note: We actually already know they don't have this capability
117 // or uservisible would have been true; this is just to get the
118 // correct error message shown.
119 require_capability('moodle/course:viewhiddensections', $context);
124 // Fix course format if it is no longer installed
125 $format = course_get_format($course);
126 $course->format = $format->get_format();
128 $PAGE->set_pagetype('course-view-' . $course->format);
129 $PAGE->set_other_editing_capability('moodle/course:update');
130 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
131 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
132 if (course_format_uses_sections($course->format)) {
133 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
134 $PAGE->set_other_editing_capability('moodle/course:movesections');
137 // Preload course format renderer before output starts.
138 // This is a little hacky but necessary since
139 // format.php is not included until after output starts
140 $renderer = $format->get_renderer($PAGE);
142 if ($reset_user_allowed_editing) {
143 // ugly hack
144 unset($PAGE->_user_allowed_editing);
147 if (!isset($USER->editing)) {
148 $USER->editing = 0;
150 if ($PAGE->user_allowed_editing()) {
151 if (($edit == 1) and confirm_sesskey()) {
152 $USER->editing = 1;
153 // Redirect to site root if Editing is toggled on frontpage
154 if ($course->id == SITEID) {
155 redirect($CFG->wwwroot .'/?redirect=0');
156 } else if (!empty($return)) {
157 redirect($CFG->wwwroot . $return);
158 } else {
159 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
160 redirect($url);
162 } else if (($edit == 0) and confirm_sesskey()) {
163 $USER->editing = 0;
164 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
165 $USER->activitycopy = false;
166 $USER->activitycopycourse = NULL;
168 // Redirect to site root if Editing is toggled on frontpage
169 if ($course->id == SITEID) {
170 redirect($CFG->wwwroot .'/?redirect=0');
171 } else if (!empty($return)) {
172 redirect($CFG->wwwroot . $return);
173 } else {
174 redirect($PAGE->url);
178 if (has_capability('moodle/course:sectionvisibility', $context)) {
179 if ($hide && confirm_sesskey()) {
180 set_section_visible($course->id, $hide, '0');
181 redirect($PAGE->url);
184 if ($show && confirm_sesskey()) {
185 set_section_visible($course->id, $show, '1');
186 redirect($PAGE->url);
190 if (!empty($section) && !empty($move) &&
191 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
192 $destsection = $section + $move;
193 if (move_section_to($course, $section, $destsection)) {
194 if ($course->id == SITEID) {
195 redirect($CFG->wwwroot . '/?redirect=0');
196 } else {
197 if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE) {
198 redirect(course_get_url($course));
199 } else {
200 redirect(course_get_url($course, $destsection));
203 } else {
204 echo $OUTPUT->notification('An error occurred while moving a section');
207 } else {
208 $USER->editing = 0;
211 $SESSION->fromdiscussion = $PAGE->url->out(false);
214 if ($course->id == SITEID) {
215 // This course is not a real course.
216 redirect($CFG->wwwroot .'/?redirect=0');
219 // Determine whether the user has permission to download course content.
220 $candownloadcourse = \core\content::can_export_context($context, $USER);
222 // We are currently keeping the button here from 1.x to help new teachers figure out
223 // what to do, even though the link also appears in the course admin block. It also
224 // means you can back out of a situation where you removed the admin block. :)
225 if ($PAGE->user_allowed_editing()) {
226 $buttons = $OUTPUT->edit_button($PAGE->url);
227 $PAGE->set_button($buttons);
230 // If viewing a section, make the title more specific
231 if ($section and $section > 0 and course_format_uses_sections($course->format)) {
232 $sectionname = get_string('sectionname', "format_$course->format");
233 $sectiontitle = get_section_name($course, $section);
234 $PAGE->set_title(get_string('coursesectiontitle', 'moodle', array('course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)));
235 } else {
236 $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
239 // Add bulk editing control.
240 $bulkbutton = $renderer->bulk_editing_button($format);
241 if (!empty($bulkbutton)) {
242 $PAGE->add_header_action($bulkbutton);
245 $PAGE->set_heading($course->fullname);
246 echo $OUTPUT->header();
248 if ($USER->editing == 1) {
250 // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
251 require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
253 if (async_helper::is_async_pending($id, 'course', 'backup')) {
254 echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
258 // Course wrapper start.
259 echo html_writer::start_tag('div', array('class'=>'course-content'));
261 // make sure that section 0 exists (this function will create one if it is missing)
262 course_create_sections_if_missing($course, 0);
264 // get information about course modules and existing module types
265 // format.php in course formats may rely on presence of these variables
266 $modinfo = get_fast_modinfo($course);
267 $modnames = get_module_types_names();
268 $modnamesplural = get_module_types_names(true);
269 $modnamesused = $modinfo->get_used_module_names();
270 $mods = $modinfo->get_cms();
271 $sections = $modinfo->get_section_info_all();
273 // CAUTION, hacky fundamental variable defintion to follow!
274 // Note that because of the way course fromats are constructed though
275 // inclusion we pass parameters around this way..
276 $displaysection = $section;
278 // Include course AJAX
279 include_course_ajax($course, $modnamesused);
281 // Include the actual course format.
282 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
283 // Content wrapper end.
285 echo html_writer::end_tag('div');
287 // Trigger course viewed event.
288 // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
289 // anything after that point.
290 course_view(context_course::instance($course->id), $section);
292 // If available, include the JS to prepare the download course content modal.
293 if ($candownloadcourse) {
294 $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
297 // Load the view JS module if completion tracking is enabled for this course.
298 $completion = new completion_info($course);
299 if ($completion->is_enabled()) {
300 $PAGE->requires->js_call_amd('core_course/view', 'init');
303 echo $OUTPUT->footer();