Proyectos de Subversion Moodle

Rev

Rev 1220 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Display the course home page.
19
 *
20
 * @copyright 1999 Martin Dougiamas  http://dougiamas.com
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @package core_course
23
 */
24
 
25
require_once('../config.php');
26
require_once('lib.php');
1220 ariadna 27
require_once($CFG->libdir . '/completionlib.php');
1 efrain 28
 
29
redirect_if_major_upgrade_required();
30
 
31
$id = optional_param('id', 0, PARAM_INT);
32
$name = optional_param('name', '', PARAM_TEXT);
33
$edit = optional_param('edit', -1, PARAM_BOOL);
34
$hide = optional_param('hide', 0, PARAM_INT);
35
$show = optional_param('show', 0, PARAM_INT);
36
$duplicatesection = optional_param('duplicatesection', 0, PARAM_INT);
37
$idnumber = optional_param('idnumber', '', PARAM_RAW);
38
$sectionid = optional_param('sectionid', 0, PARAM_INT);
39
$section = optional_param('section', null, PARAM_INT);
40
$expandsection = optional_param('expandsection', -1, PARAM_INT);
41
$move = optional_param('move', 0, PARAM_INT);
1220 ariadna 42
$marker = optional_param('marker', -1, PARAM_INT);
1 efrain 43
$switchrole = optional_param('switchrole', -1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
44
$return = optional_param('return', 0, PARAM_LOCALURL);
45
 
46
$params = [];
47
if (!empty($name)) {
48
    $params = ['shortname' => $name];
49
} else if (!empty($idnumber)) {
50
    $params = ['idnumber' => $idnumber];
51
} else if (!empty($id)) {
52
    $params = ['id' => $id];
53
} else {
54
    throw new \moodle_exception('unspecifycourseid', 'error');
55
}
56
 
57
$course = $DB->get_record('course', $params, '*', MUST_EXIST);
58
 
59
$urlparams = ['id' => $course->id];
60
 
61
// Sectionid should get priority over section number.
62
if ($sectionid) {
63
    $section = $DB->get_field('course_sections', 'section', ['id' => $sectionid, 'course' => $course->id], MUST_EXIST);
64
}
65
if (!is_null($section)) {
66
    $urlparams['section'] = $section;
67
}
68
if ($expandsection !== -1) {
69
    $urlparams['expandsection'] = $expandsection;
70
}
71
 
72
$PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc.
73
 
74
// Prevent caching of this page to stop confusion when changing page after making AJAX changes.
75
$PAGE->set_cacheable(false);
76
 
77
context_helper::preload_course($course->id);
78
$context = context_course::instance($course->id, MUST_EXIST);
79
 
80
// Remove any switched roles before checking login.
81
if ($switchrole == 0 && confirm_sesskey()) {
82
    role_switch($switchrole, $context);
83
}
84
 
85
require_login($course);
86
 
87
// Switchrole - sanity check in cost-order...
88
$resetuserallowedediting = false;
1220 ariadna 89
if (
90
    $switchrole > 0 && confirm_sesskey() &&
91
    has_capability('moodle/role:switchroles', $context)
92
) {
1 efrain 93
    // Is this role assignable in this context?
94
    // Inquiring minds want to know.
95
    $aroles = get_switchable_roles($context);
96
    if (is_array($aroles) && isset($aroles[$switchrole])) {
97
        role_switch($switchrole, $context);
98
        // Double check that this role is allowed here.
99
        require_login($course);
100
    }
101
    // Reset course page state. This prevents some weird problems.
102
    $USER->activitycopy = false;
103
    $USER->activitycopycourse = null;
104
    unset($USER->activitycopyname);
105
    unset($SESSION->modform);
106
    $USER->editing = 0;
107
    $resetuserallowedediting = true;
108
}
109
 
110
// If course is hosted on an external server, redirect to corresponding
111
// url with appropriate authentication attached as parameter.
112
if (file_exists($CFG->dirroot . '/course/externservercourse.php')) {
113
    include($CFG->dirroot . '/course/externservercourse.php');
114
    if (function_exists('extern_server_course')) {
115
        if ($externurl = extern_server_course($course)) {
116
            redirect($externurl);
117
        }
118
    }
119
}
120
 
1220 ariadna 121
require_once($CFG->dirroot . '/calendar/lib.php'); // This is after login because it needs $USER.
1 efrain 122
 
123
// Must set layout before gettting section info. See MDL-47555.
124
$PAGE->set_pagelayout('course');
125
$PAGE->add_body_class('limitedwidth');
126
 
127
if ($section && $section > 0) {
128
 
129
    // Get section details and check it exists.
130
    $modinfo = get_fast_modinfo($course);
131
    $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
132
 
133
    // Check user is allowed to see it.
134
    if (!$coursesections->uservisible) {
135
        // Check if coursesection has conditions affecting availability and if
136
        // so, output availability info.
137
        if ($coursesections->visible && $coursesections->availableinfo) {
138
            $sectionname = get_section_name($course, $coursesections);
139
            $message = get_string('notavailablecourse', '', $sectionname);
140
            redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
141
        } else {
142
            // Note: We actually already know they don't have this capability
143
            // or uservisible would have been true; this is just to get the
144
            // correct error message shown.
145
            require_capability('moodle/course:viewhiddensections', $context);
146
        }
147
    }
148
}
149
 
150
// Fix course format if it is no longer installed.
151
$format = course_get_format($course);
152
$course->format = $format->get_format();
153
 
154
$PAGE->set_pagetype('course-view-' . $course->format);
155
$PAGE->set_other_editing_capability('moodle/course:update');
156
$PAGE->set_other_editing_capability('moodle/course:manageactivities');
157
$PAGE->set_other_editing_capability('moodle/course:activityvisibility');
158
if (course_format_uses_sections($course->format)) {
159
    $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
160
    $PAGE->set_other_editing_capability('moodle/course:movesections');
161
}
162
 
163
// Preload course format renderer before output starts.
164
// This is a little hacky but necessary since
165
// format.php is not included until after output starts.
166
$renderer = $format->get_renderer($PAGE);
167
 
168
if ($resetuserallowedediting) {
169
    // Ugly hack.
170
    unset($PAGE->_user_allowed_editing);
171
}
172
 
173
if (!isset($USER->editing)) {
174
    $USER->editing = 0;
175
}
176
if ($PAGE->user_allowed_editing()) {
177
    if (($edit == 1) && confirm_sesskey()) {
178
        $USER->editing = 1;
179
        // Redirect to site root if Editing is toggled on frontpage.
180
        if ($course->id == SITEID) {
1221 ariadna 181
            redirect($CFG->wwwroot . '/?redirect=0');
1 efrain 182
        } else if (!empty($return)) {
183
            redirect($CFG->wwwroot . $return);
184
        } else {
185
            $url = new moodle_url($PAGE->url, ['notifyeditingon' => 1]);
186
            redirect($url);
187
        }
188
    } else if (($edit == 0) && confirm_sesskey()) {
189
        $USER->editing = 0;
190
        if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
191
            $USER->activitycopy = false;
192
            $USER->activitycopycourse = null;
193
        }
194
        // Redirect to site root if Editing is toggled on frontpage.
195
        if ($course->id == SITEID) {
1220 ariadna 196
            redirect($CFG->wwwroot . '/?redirect=0');
1 efrain 197
        } else if (!empty($return)) {
198
            redirect($CFG->wwwroot . $return);
199
        } else {
200
            redirect($PAGE->url);
201
        }
202
    }
203
 
204
    if (has_capability('moodle/course:sectionvisibility', $context)) {
205
        if ($hide && confirm_sesskey()) {
206
            set_section_visible($course->id, $hide, '0');
207
            if ($sectionid) {
208
                redirect(course_get_url($course, $section, ['navigation' => true]));
209
            } else {
210
                redirect($PAGE->url);
211
            }
212
        }
213
 
214
        if ($show && confirm_sesskey()) {
215
            set_section_visible($course->id, $show, '1');
216
            if ($sectionid) {
217
                redirect(course_get_url($course, $section, ['navigation' => true]));
218
            } else {
219
                redirect($PAGE->url);
220
            }
221
        }
222
    }
223
 
224
    if ($marker >= 0 && confirm_sesskey()) {
225
        course_set_marker($course->id, $marker);
226
        if ($sectionid) {
227
            redirect(course_get_url($course, $section, ['navigation' => true]));
228
        } else {
229
            redirect($PAGE->url);
230
        }
231
    }
232
 
233
    if (
234
        !empty($section) && !empty($coursesections) && !empty($duplicatesection)
235
        && has_capability('moodle/course:update', $context) && confirm_sesskey()
236
    ) {
237
        $newsection = $format->duplicate_section($coursesections);
238
        redirect(course_get_url($course, $newsection->section));
239
    }
240
 
1220 ariadna 241
    if (
242
        !empty($section) && !empty($move) &&
243
        has_capability('moodle/course:movesections', $context) && confirm_sesskey()
244
    ) {
1 efrain 245
        $destsection = $section + $move;
246
        if (move_section_to($course, $section, $destsection)) {
247
            if ($course->id == SITEID) {
248
                redirect($CFG->wwwroot . '/?redirect=0');
249
            } else {
250
                if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE) {
251
                    redirect(course_get_url($course));
252
                } else {
253
                    redirect(course_get_url($course, $destsection));
254
                }
255
            }
256
        } else {
257
            echo $OUTPUT->notification('An error occurred while moving a section');
258
        }
259
    }
260
} else {
261
    $USER->editing = 0;
262
}
263
 
264
$SESSION->fromdiscussion = $PAGE->url->out(false);
265
 
266
 
267
if ($course->id == SITEID) {
268
    // This course is not a real course.
1220 ariadna 269
    redirect($CFG->wwwroot . '/?redirect=0');
1 efrain 270
}
271
 
272
// Determine whether the user has permission to download course content.
273
$candownloadcourse = \core\content::can_export_context($context, $USER);
274
 
275
// We are currently keeping the button here from 1.x to help new teachers figure out
276
// what to do, even though the link also appears in the course admin block.  It also
277
// means you can back out of a situation where you removed the admin block.
278
if ($PAGE->user_allowed_editing()) {
279
    $buttons = $OUTPUT->edit_button($PAGE->url);
280
    $PAGE->set_button($buttons);
281
}
282
 
283
$editingtitle = '';
284
if ($PAGE->user_is_editing()) {
285
    // Append this to the page title's lang string to get its equivalent when editing mode is turned on.
286
    $editingtitle = 'editing';
287
}
288
 
289
// If viewing a section, make the title more specific.
290
if ($section && $section > 0 && course_format_uses_sections($course->format)) {
291
    $sectionname = get_string('sectionname', "format_$course->format");
292
    $sectiontitle = get_section_name($course, $section);
293
    $PAGE->set_title(
294
        get_string(
295
            'coursesectiontitle' . $editingtitle,
296
            'moodle',
297
            ['course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
298
        )
299
    );
300
} else {
301
    $PAGE->set_title(get_string('coursetitle' . $editingtitle, 'moodle', ['course' => $course->fullname]));
302
}
303
 
304
// Add bulk editing control.
305
$bulkbutton = $renderer->bulk_editing_button($format);
306
if (!empty($bulkbutton)) {
307
    $PAGE->add_header_action($bulkbutton);
308
}
309
 
310
$PAGE->set_heading($course->fullname);
311
echo $OUTPUT->header();
312
 
313
// Show communication room status notification.
314
if (has_capability('moodle/course:update', $context)) {
315
    core_communication\helper::get_course_communication_status_notification($course);
316
}
317
 
318
if ($USER->editing == 1) {
319
 
320
    // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
321
    require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
322
 
323
    if (async_helper::is_async_pending($id, 'course', 'backup')) {
324
        echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
325
    }
326
}
327
 
328
// Course wrapper start.
329
echo html_writer::start_tag('div', ['class' => 'course-content']);
330
 
331
// Make sure that section 0 exists (this function will create one if it is missing).
332
course_create_sections_if_missing($course, 0);
333
 
334
// Get information about course modules and existing module types.
335
// format.php in course formats may rely on presence of these variables.
336
$modinfo = get_fast_modinfo($course);
337
$modnames = get_module_types_names();
338
$modnamesplural = get_module_types_names(true);
339
$modnamesused = $modinfo->get_used_module_names();
340
$mods = $modinfo->get_cms();
341
$sections = $modinfo->get_section_info_all();
342
 
343
// CAUTION, hacky fundamental variable defintion to follow!
344
// Note that because of the way course fromats are constructed though
345
// inclusion we pass parameters around this way.
346
$displaysection = $section;
347
 
348
// Include course AJAX.
349
include_course_ajax($course, $modnamesused);
350
 
351
// Include the actual course format.
1220 ariadna 352
require($CFG->dirroot . '/course/format/' . $course->format . '/format.php');
1 efrain 353
// Content wrapper end.
354
 
355
echo html_writer::end_tag('div');
356
 
357
// Trigger course viewed event.
358
// We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
359
// anything after that point.
360
course_view(context_course::instance($course->id), $section);
361
 
362
// If available, include the JS to prepare the download course content modal.
363
if ($candownloadcourse) {
364
    $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
365
}
366
 
367
// Load the view JS module if completion tracking is enabled for this course.
368
$completion = new completion_info($course);
369
if ($completion->is_enabled()) {
370
    $PAGE->requires->js_call_amd('core_course/view', 'init');
371
}
372
 
373
echo $OUTPUT->footer();