Proyectos de Subversion Moodle

Rev

Rev 1221 | | 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');
1441 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);
1441 ariadna 34
$hide = optional_param('hide', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
35
$show = optional_param('show', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
1 efrain 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);
1441 ariadna 41
$move = optional_param('move', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
42
$marker = optional_param('marker', -1 , PARAM_INT); // TODO remove this param as part of MDL-83530.
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;
1441 ariadna 89
if ($switchrole > 0 && confirm_sesskey() &&
90
    has_capability('moodle/role:switchroles', $context)) {
1 efrain 91
    // Is this role assignable in this context?
92
    // Inquiring minds want to know.
93
    $aroles = get_switchable_roles($context);
94
    if (is_array($aroles) && isset($aroles[$switchrole])) {
95
        role_switch($switchrole, $context);
96
        // Double check that this role is allowed here.
97
        require_login($course);
98
    }
99
    // Reset course page state. This prevents some weird problems.
100
    $USER->activitycopy = false;
101
    $USER->activitycopycourse = null;
102
    unset($USER->activitycopyname);
103
    unset($SESSION->modform);
104
    $USER->editing = 0;
105
    $resetuserallowedediting = true;
106
}
107
 
108
// If course is hosted on an external server, redirect to corresponding
109
// url with appropriate authentication attached as parameter.
1441 ariadna 110
$hook = new \core_course\hook\before_course_viewed($course);
111
\core\hook\manager::get_instance()->dispatch($hook);
1 efrain 112
 
1441 ariadna 113
require_once($CFG->dirroot.'/calendar/lib.php'); // This is after login because it needs $USER.
1 efrain 114
 
115
// Must set layout before gettting section info. See MDL-47555.
116
$PAGE->set_pagelayout('course');
117
$PAGE->add_body_class('limitedwidth');
118
 
119
if ($section && $section > 0) {
120
 
121
    // Get section details and check it exists.
122
    $modinfo = get_fast_modinfo($course);
123
    $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
124
 
125
    // Check user is allowed to see it.
126
    if (!$coursesections->uservisible) {
127
        // Check if coursesection has conditions affecting availability and if
128
        // so, output availability info.
129
        if ($coursesections->visible && $coursesections->availableinfo) {
130
            $sectionname = get_section_name($course, $coursesections);
131
            $message = get_string('notavailablecourse', '', $sectionname);
132
            redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
133
        } else {
134
            // Note: We actually already know they don't have this capability
135
            // or uservisible would have been true; this is just to get the
136
            // correct error message shown.
137
            require_capability('moodle/course:viewhiddensections', $context);
138
        }
139
    }
140
}
141
 
142
// Fix course format if it is no longer installed.
143
$format = course_get_format($course);
144
$course->format = $format->get_format();
145
 
146
$PAGE->set_pagetype('course-view-' . $course->format);
147
$PAGE->set_other_editing_capability('moodle/course:update');
148
$PAGE->set_other_editing_capability('moodle/course:manageactivities');
149
$PAGE->set_other_editing_capability('moodle/course:activityvisibility');
150
if (course_format_uses_sections($course->format)) {
151
    $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
152
    $PAGE->set_other_editing_capability('moodle/course:movesections');
153
}
154
 
155
// Preload course format renderer before output starts.
156
// This is a little hacky but necessary since
157
// format.php is not included until after output starts.
158
$renderer = $format->get_renderer($PAGE);
159
 
160
if ($resetuserallowedediting) {
161
    // Ugly hack.
162
    unset($PAGE->_user_allowed_editing);
163
}
164
 
165
if (!isset($USER->editing)) {
166
    $USER->editing = 0;
167
}
168
if ($PAGE->user_allowed_editing()) {
169
    if (($edit == 1) && confirm_sesskey()) {
170
        $USER->editing = 1;
171
        // Redirect to site root if Editing is toggled on frontpage.
172
        if ($course->id == SITEID) {
1441 ariadna 173
            redirect($CFG->wwwroot .'/?redirect=0');
1 efrain 174
        } else if (!empty($return)) {
175
            redirect($CFG->wwwroot . $return);
176
        } else {
177
            $url = new moodle_url($PAGE->url, ['notifyeditingon' => 1]);
178
            redirect($url);
179
        }
180
    } else if (($edit == 0) && confirm_sesskey()) {
181
        $USER->editing = 0;
182
        if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
183
            $USER->activitycopy = false;
184
            $USER->activitycopycourse = null;
185
        }
186
        // Redirect to site root if Editing is toggled on frontpage.
187
        if ($course->id == SITEID) {
1441 ariadna 188
            redirect($CFG->wwwroot .'/?redirect=0');
1 efrain 189
        } else if (!empty($return)) {
190
            redirect($CFG->wwwroot . $return);
191
        } else {
192
            redirect($PAGE->url);
193
        }
194
    }
195
 
1441 ariadna 196
    // TODO remove this if as part of MDL-83530.
1 efrain 197
    if (has_capability('moodle/course:sectionvisibility', $context)) {
198
        if ($hide && confirm_sesskey()) {
1441 ariadna 199
            debugging(
200
                'The hide param in course view is deprecated. Please use course/format/update.php instead.',
201
                DEBUG_DEVELOPER
202
            );
1 efrain 203
            set_section_visible($course->id, $hide, '0');
204
            if ($sectionid) {
205
                redirect(course_get_url($course, $section, ['navigation' => true]));
206
            } else {
207
                redirect($PAGE->url);
208
            }
209
        }
210
 
211
        if ($show && confirm_sesskey()) {
1441 ariadna 212
            debugging(
213
                'The show param in course view is deprecated. Please use course/format/update.php instead.',
214
                DEBUG_DEVELOPER
215
            );
1 efrain 216
            set_section_visible($course->id, $show, '1');
217
            if ($sectionid) {
218
                redirect(course_get_url($course, $section, ['navigation' => true]));
219
            } else {
220
                redirect($PAGE->url);
221
            }
222
        }
223
    }
224
 
1441 ariadna 225
    // TODO remove this if as part of MDL-83530.
1 efrain 226
    if ($marker >= 0 && confirm_sesskey()) {
1441 ariadna 227
        debugging(
228
            'The marker param in course view is deprecated. Please use course/format/update.php instead.',
229
            DEBUG_DEVELOPER
230
        );
1 efrain 231
        course_set_marker($course->id, $marker);
232
        if ($sectionid) {
233
            redirect(course_get_url($course, $section, ['navigation' => true]));
234
        } else {
235
            redirect($PAGE->url);
236
        }
237
    }
238
 
239
    if (
240
        !empty($section) && !empty($coursesections) && !empty($duplicatesection)
241
        && has_capability('moodle/course:update', $context) && confirm_sesskey()
242
    ) {
243
        $newsection = $format->duplicate_section($coursesections);
244
        redirect(course_get_url($course, $newsection->section));
245
    }
246
 
1441 ariadna 247
    // TODO remove this if as part of MDL-83530.
1220 ariadna 248
    if (
1441 ariadna 249
        !empty($section)
250
        && !empty($move)
251
        && has_capability('moodle/course:movesections', $context) && confirm_sesskey()
1220 ariadna 252
    ) {
1441 ariadna 253
        debugging(
254
            'The move param is deprecated. Please use the standard move modal instead.',
255
            DEBUG_DEVELOPER
256
        );
1 efrain 257
        $destsection = $section + $move;
258
        if (move_section_to($course, $section, $destsection)) {
259
            if ($course->id == SITEID) {
260
                redirect($CFG->wwwroot . '/?redirect=0');
261
            } else {
262
                if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE) {
263
                    redirect(course_get_url($course));
264
                } else {
265
                    redirect(course_get_url($course, $destsection));
266
                }
267
            }
268
        } else {
269
            echo $OUTPUT->notification('An error occurred while moving a section');
270
        }
271
    }
272
} else {
273
    $USER->editing = 0;
274
}
275
 
276
$SESSION->fromdiscussion = $PAGE->url->out(false);
277
 
278
 
279
if ($course->id == SITEID) {
280
    // This course is not a real course.
1441 ariadna 281
    redirect($CFG->wwwroot .'/?redirect=0');
1 efrain 282
}
283
 
284
// Determine whether the user has permission to download course content.
285
$candownloadcourse = \core\content::can_export_context($context, $USER);
286
 
287
// We are currently keeping the button here from 1.x to help new teachers figure out
288
// what to do, even though the link also appears in the course admin block.  It also
289
// means you can back out of a situation where you removed the admin block.
290
if ($PAGE->user_allowed_editing()) {
291
    $buttons = $OUTPUT->edit_button($PAGE->url);
292
    $PAGE->set_button($buttons);
293
}
294
 
295
$editingtitle = '';
296
if ($PAGE->user_is_editing()) {
297
    // Append this to the page title's lang string to get its equivalent when editing mode is turned on.
298
    $editingtitle = 'editing';
299
}
300
 
301
// If viewing a section, make the title more specific.
302
if ($section && $section > 0 && course_format_uses_sections($course->format)) {
1441 ariadna 303
    $sectionname = $format->get_generic_section_name();
304
    $sectiontitle = $format->get_section_name($section);
1 efrain 305
    $PAGE->set_title(
306
        get_string(
307
            'coursesectiontitle' . $editingtitle,
308
            'moodle',
309
            ['course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname]
310
        )
311
    );
312
} else {
313
    $PAGE->set_title(get_string('coursetitle' . $editingtitle, 'moodle', ['course' => $course->fullname]));
314
}
315
 
316
// Add bulk editing control.
317
$bulkbutton = $renderer->bulk_editing_button($format);
318
if (!empty($bulkbutton)) {
319
    $PAGE->add_header_action($bulkbutton);
320
}
321
 
322
$PAGE->set_heading($course->fullname);
1441 ariadna 323
 
324
// Make sure that section 0 exists (this function will create one if it is missing).
325
course_create_sections_if_missing($course, 0);
326
 
327
// Get information about course modules and existing module types.
328
// format.php in course formats may rely on presence of these variables.
329
$modinfo = get_fast_modinfo($course);
330
$modnames = get_module_types_names();
331
$modnamesplural = get_module_types_names(true);
332
$modnamesused = $modinfo->get_used_module_names();
333
$mods = $modinfo->get_cms();
334
$sections = $modinfo->get_section_info_all();
335
 
336
// Include course AJAX. This should be done before starting the UI
337
// to allow page header, blocks, or drawers use the course editor.
338
include_course_ajax($course, $modnamesused);
339
 
1 efrain 340
echo $OUTPUT->header();
341
 
342
// Show communication room status notification.
343
if (has_capability('moodle/course:update', $context)) {
344
    core_communication\helper::get_course_communication_status_notification($course);
345
}
346
 
1441 ariadna 347
$containerattributes = ['class' => 'course-content'];
348
if ($PAGE->user_is_editing()) {
1 efrain 349
    // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
350
    require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
351
 
352
    if (async_helper::is_async_pending($id, 'course', 'backup')) {
353
        echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
354
    }
1441 ariadna 355
 
356
    // Allow drag and drop in the course index.
357
    $containerattributes['data-courseindexdndallowed'] = 'true';
1 efrain 358
}
359
 
360
// Course wrapper start.
1441 ariadna 361
echo html_writer::start_tag('div', $containerattributes);
1 efrain 362
 
363
// CAUTION, hacky fundamental variable defintion to follow!
364
// Note that because of the way course fromats are constructed though
365
// inclusion we pass parameters around this way.
366
$displaysection = $section;
367
 
368
// Include the actual course format.
1441 ariadna 369
require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
1 efrain 370
// Content wrapper end.
371
 
372
echo html_writer::end_tag('div');
373
 
374
// Trigger course viewed event.
375
// We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
376
// anything after that point.
377
course_view(context_course::instance($course->id), $section);
378
 
379
// If available, include the JS to prepare the download course content modal.
380
if ($candownloadcourse) {
381
    $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
382
}
383
 
384
// Load the view JS module if completion tracking is enabled for this course.
385
$completion = new completion_info($course);
386
if ($completion->is_enabled()) {
387
    $PAGE->requires->js_call_amd('core_course/view', 'init');
388
}
389
 
390
echo $OUTPUT->footer();