Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 * This page shows all course enrolment options for current user.
19
 *
20
 * @package    core_enrol
21
 * @copyright  2010 Petr Skoda {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require('../config.php');
26
require_once("$CFG->libdir/formslib.php");
27
 
28
$id = required_param('id', PARAM_INT);
1441 ariadna 29
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
1 efrain 30
 
31
if (!isloggedin()) {
32
    $referer = get_local_referer();
33
    if (empty($referer)) {
34
        // A user that is not logged in has arrived directly on this page,
35
        // they should be redirected to the course page they are trying to enrol on after logging in.
36
        $SESSION->wantsurl = "$CFG->wwwroot/course/view.php?id=$id";
37
    }
38
    // do not use require_login here because we are usually coming from it,
39
    // it would also mess up the SESSION->wantsurl
40
    redirect(get_login_url());
41
}
42
 
43
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
44
$context = context_course::instance($course->id, MUST_EXIST);
45
 
46
// Everybody is enrolled on the frontpage
47
if ($course->id == SITEID) {
48
    redirect("$CFG->wwwroot/");
49
}
50
 
51
if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
52
    throw new \moodle_exception('coursehidden');
53
}
54
 
55
$PAGE->set_course($course);
1441 ariadna 56
$PAGE->set_context($context->get_parent_context());
1 efrain 57
$PAGE->set_pagelayout('incourse');
58
$PAGE->set_url('/enrol/index.php', array('id'=>$course->id));
59
$PAGE->set_secondary_navigation(false);
60
$PAGE->add_body_class('limitedwidth');
61
 
62
// do not allow enrols when in login-as session
63
if (\core\session\manager::is_loggedinas() and $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
64
    throw new \moodle_exception('loginasnoenrol', '', $CFG->wwwroot.'/course/view.php?id='.$USER->loginascontext->instanceid);
65
}
66
 
67
// Check if user has access to the category where the course is located.
68
if (!core_course_category::can_view_course_info($course) && !is_enrolled($context, $USER, '', true)) {
69
    throw new \moodle_exception('coursehidden', '', $CFG->wwwroot . '/');
70
}
71
 
1441 ariadna 72
// Get all enrol widgets available in this course.
1 efrain 73
$enrols = enrol_get_plugins(true);
74
$enrolinstances = enrol_get_instances($course->id, true);
1441 ariadna 75
$widgets = [];
1 efrain 76
foreach($enrolinstances as $instance) {
77
    if (!isset($enrols[$instance->enrol])) {
78
        continue;
79
    }
1441 ariadna 80
    $widget = $enrols[$instance->enrol]->enrol_page_hook($instance);
81
    if ($widget) {
82
        $widgets[$instance->id] = $widget;
1 efrain 83
    }
84
}
85
 
86
// Check if user already enrolled
87
if (is_enrolled($context, $USER, '', true)) {
88
    if (!empty($SESSION->wantsurl)) {
89
        $destination = $SESSION->wantsurl;
90
        unset($SESSION->wantsurl);
91
    } else {
92
        $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
93
    }
94
    redirect($destination);   // Bye!
95
}
96
 
97
$PAGE->set_title($course->shortname);
98
$PAGE->set_heading($course->fullname);
99
$PAGE->navbar->add(get_string('enrolmentoptions','enrol'));
100
 
1441 ariadna 101
/** @var core_course_renderer $courserenderer */
102
$courserenderer = $PAGE->get_renderer('core', 'course');
103
$content = $courserenderer->enrolment_options($course, $widgets,
104
    $returnurl ? new \core\url($returnurl) : null);
1 efrain 105
echo $OUTPUT->header();
1441 ariadna 106
echo $content;
1 efrain 107
echo $OUTPUT->footer();