source: moodle/trunk/fuentes/course/view.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 11.9 KB
Line 
1<?php
2
3//  Display the course home page.
4
5    require_once('../config.php');
6    require_once('lib.php');
7    require_once($CFG->libdir.'/completionlib.php');
8
9    $id          = optional_param('id', 0, PARAM_INT);
10    $name        = optional_param('name', '', PARAM_RAW);
11    $edit        = optional_param('edit', -1, PARAM_BOOL);
12    $hide        = optional_param('hide', 0, PARAM_INT);
13    $show        = optional_param('show', 0, PARAM_INT);
14    $idnumber    = optional_param('idnumber', '', PARAM_RAW);
15    $sectionid   = optional_param('sectionid', 0, PARAM_INT);
16    $section     = optional_param('section', 0, PARAM_INT);
17    $move        = optional_param('move', 0, PARAM_INT);
18    $marker      = optional_param('marker',-1 , PARAM_INT);
19    $switchrole  = optional_param('switchrole',-1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
20    $modchooser  = optional_param('modchooser', -1, PARAM_BOOL);
21    $return      = optional_param('return', 0, PARAM_LOCALURL);
22
23    $params = array();
24    if (!empty($name)) {
25        $params = array('shortname' => $name);
26    } else if (!empty($idnumber)) {
27        $params = array('idnumber' => $idnumber);
28    } else if (!empty($id)) {
29        $params = array('id' => $id);
30    }else {
31        print_error('unspecifycourseid', 'error');
32    }
33
34    $course = $DB->get_record('course', $params, '*', MUST_EXIST);
35
36    $urlparams = array('id' => $course->id);
37
38    // Sectionid should get priority over section number
39    if ($sectionid) {
40        $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
41    }
42    if ($section) {
43        $urlparams['section'] = $section;
44    }
45
46    $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
47
48    // Prevent caching of this page to stop confusion when changing page after making AJAX changes
49    $PAGE->set_cacheable(false);
50
51    context_helper::preload_course($course->id);
52    $context = context_course::instance($course->id, MUST_EXIST);
53
54    // Remove any switched roles before checking login
55    if ($switchrole == 0 && confirm_sesskey()) {
56        role_switch($switchrole, $context);
57    }
58
59    require_login($course);
60
61    // Switchrole - sanity check in cost-order...
62    $reset_user_allowed_editing = false;
63    if ($switchrole > 0 && confirm_sesskey() &&
64        has_capability('moodle/role:switchroles', $context)) {
65        // is this role assignable in this context?
66        // inquiring minds want to know...
67        $aroles = get_switchable_roles($context);
68        if (is_array($aroles) && isset($aroles[$switchrole])) {
69            role_switch($switchrole, $context);
70            // Double check that this role is allowed here
71            require_login($course);
72        }
73        // reset course page state - this prevents some weird problems ;-)
74        $USER->activitycopy = false;
75        $USER->activitycopycourse = NULL;
76        unset($USER->activitycopyname);
77        unset($SESSION->modform);
78        $USER->editing = 0;
79        $reset_user_allowed_editing = true;
80    }
81
82    //If course is hosted on an external server, redirect to corresponding
83    //url with appropriate authentication attached as parameter
84    if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
85        include $CFG->dirroot .'/course/externservercourse.php';
86        if (function_exists('extern_server_course')) {
87            if ($extern_url = extern_server_course($course)) {
88                redirect($extern_url);
89            }
90        }
91    }
92
93
94    require_once($CFG->dirroot.'/calendar/lib.php');    /// This is after login because it needs $USER
95
96    // Must set layout before gettting section info. See MDL-47555.
97    $PAGE->set_pagelayout('course');
98
99    if ($section and $section > 0) {
100
101        // Get section details and check it exists.
102        $modinfo = get_fast_modinfo($course);
103        $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
104
105        // Check user is allowed to see it.
106        if (!$coursesections->uservisible) {
107            // Note: We actually already know they don't have this capability
108            // or uservisible would have been true; this is just to get the
109            // correct error message shown.
110            require_capability('moodle/course:viewhiddensections', $context);
111        }
112    }
113
114    // Fix course format if it is no longer installed
115    $course->format = course_get_format($course)->get_format();
116
117    $PAGE->set_pagetype('course-view-' . $course->format);
118    $PAGE->set_other_editing_capability('moodle/course:update');
119    $PAGE->set_other_editing_capability('moodle/course:manageactivities');
120    $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
121    if (course_format_uses_sections($course->format)) {
122        $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
123        $PAGE->set_other_editing_capability('moodle/course:movesections');
124    }
125
126    // Preload course format renderer before output starts.
127    // This is a little hacky but necessary since
128    // format.php is not included until after output starts
129    if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
130        require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
131        if (class_exists('format_'.$course->format.'_renderer')) {
132            // call get_renderer only if renderer is defined in format plugin
133            // otherwise an exception would be thrown
134            $PAGE->get_renderer('format_'. $course->format);
135        }
136    }
137
138    if ($reset_user_allowed_editing) {
139        // ugly hack
140        unset($PAGE->_user_allowed_editing);
141    }
142
143    if (!isset($USER->editing)) {
144        $USER->editing = 0;
145    }
146    if ($PAGE->user_allowed_editing()) {
147        if (($edit == 1) and confirm_sesskey()) {
148            $USER->editing = 1;
149            // Redirect to site root if Editing is toggled on frontpage
150            if ($course->id == SITEID) {
151                redirect($CFG->wwwroot .'/?redirect=0');
152            } else if (!empty($return)) {
153                redirect($CFG->wwwroot . $return);
154            } else {
155                $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
156                redirect($url);
157            }
158        } else if (($edit == 0) and confirm_sesskey()) {
159            $USER->editing = 0;
160            if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
161                $USER->activitycopy       = false;
162                $USER->activitycopycourse = NULL;
163            }
164            // Redirect to site root if Editing is toggled on frontpage
165            if ($course->id == SITEID) {
166                redirect($CFG->wwwroot .'/?redirect=0');
167            } else if (!empty($return)) {
168                redirect($CFG->wwwroot . $return);
169            } else {
170                redirect($PAGE->url);
171            }
172        }
173        if (($modchooser == 1) && confirm_sesskey()) {
174            set_user_preference('usemodchooser', $modchooser);
175        } else if (($modchooser == 0) && confirm_sesskey()) {
176            set_user_preference('usemodchooser', $modchooser);
177        }
178
179        if (has_capability('moodle/course:sectionvisibility', $context)) {
180            if ($hide && confirm_sesskey()) {
181                set_section_visible($course->id, $hide, '0');
182                redirect($PAGE->url);
183            }
184
185            if ($show && confirm_sesskey()) {
186                set_section_visible($course->id, $show, '1');
187                redirect($PAGE->url);
188            }
189        }
190
191        if (!empty($section) && !empty($move) &&
192                has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
193            $destsection = $section + $move;
194            if (move_section_to($course, $section, $destsection)) {
195                if ($course->id == SITEID) {
196                    redirect($CFG->wwwroot . '/?redirect=0');
197                } else {
198                    redirect(course_get_url($course));
199                }
200            } else {
201                echo $OUTPUT->notification('An error occurred while moving a section');
202            }
203        }
204    } else {
205        $USER->editing = 0;
206    }
207
208    $SESSION->fromdiscussion = $PAGE->url->out(false);
209
210
211    if ($course->id == SITEID) {
212        // This course is not a real course.
213        redirect($CFG->wwwroot .'/');
214    }
215
216    $completion = new completion_info($course);
217    if ($completion->is_enabled()) {
218        $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
219        $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
220        $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
221        $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
222
223        $PAGE->requires->js_init_call('M.core_completion.init');
224    }
225
226    // We are currently keeping the button here from 1.x to help new teachers figure out
227    // what to do, even though the link also appears in the course admin block.  It also
228    // means you can back out of a situation where you removed the admin block. :)
229    if ($PAGE->user_allowed_editing()) {
230        $buttons = $OUTPUT->edit_button($PAGE->url);
231        $PAGE->set_button($buttons);
232    }
233
234    // If viewing a section, make the title more specific
235    if ($section and $section > 0 and course_format_uses_sections($course->format)) {
236        $sectionname = get_string('sectionname', "format_$course->format");
237        $sectiontitle = get_section_name($course, $section);
238        $PAGE->set_title(get_string('coursesectiontitle', 'moodle', array('course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)));
239    } else {
240        $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
241    }
242
243    $PAGE->set_heading($course->fullname);
244    echo $OUTPUT->header();
245
246    if ($completion->is_enabled()) {
247        // This value tracks whether there has been a dynamic change to the page.
248        // It is used so that if a user does this - (a) set some tickmarks, (b)
249        // go to another page, (c) clicks Back button - the page will
250        // automatically reload. Otherwise it would start with the wrong tick
251        // values.
252        echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
253        echo html_writer::start_tag('div');
254        echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
255        echo html_writer::end_tag('div');
256        echo html_writer::end_tag('form');
257    }
258
259    // Course wrapper start.
260    echo html_writer::start_tag('div', array('class'=>'course-content'));
261
262    // make sure that section 0 exists (this function will create one if it is missing)
263    course_create_sections_if_missing($course, 0);
264
265    // get information about course modules and existing module types
266    // format.php in course formats may rely on presence of these variables
267    $modinfo = get_fast_modinfo($course);
268    $modnames = get_module_types_names();
269    $modnamesplural = get_module_types_names(true);
270    $modnamesused = $modinfo->get_used_module_names();
271    $mods = $modinfo->get_cms();
272    $sections = $modinfo->get_section_info_all();
273
274    // CAUTION, hacky fundamental variable defintion to follow!
275    // Note that because of the way course fromats are constructed though
276    // inclusion we pass parameters around this way..
277    $displaysection = $section;
278
279    // Include the actual course format.
280    require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
281    // Content wrapper end.
282
283    echo html_writer::end_tag('div');
284
285    // Trigger course viewed event.
286    // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
287    // anything after that point.
288    course_view(context_course::instance($course->id), $section);
289
290    // Include course AJAX
291    include_course_ajax($course, $modnamesused);
292
293    echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.