Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
declare(strict_types=1);
18
 
19
namespace enrol_guest\form;
20
 
21
use core\context\course as context_course;
22
use core_form\dynamic_form;
23
use core_text;
24
use moodle_url;
25
 
26
/**
27
 * Form for entering password for guest enrolment
28
 *
29
 * @package    enrol_guest
30
 * @copyright  Marina Glancy
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class enrol_form extends dynamic_form {
34
    /** @var \stdClass */
35
    protected $instance;
36
 
37
    /**
38
     * Returns the instance of the enrolment method
39
     *
40
     * @throws \moodle_exception
41
     * @return \stdClass
42
     */
43
    protected function get_instance(): \stdClass {
44
        global $DB, $CFG;
45
        require_once($CFG->dirroot . '/lib/enrollib.php');
46
        if ($this->instance === null) {
47
            $courseid = $this->optional_param('id', 0, PARAM_INT);
48
            $instanceid = $this->optional_param('instance', 0, PARAM_INT);
49
            // We need enrol_get_instances() to validate that the enrolment method is enabled.
50
            $instances = enrol_get_instances($courseid, true);
51
            if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') {
52
                throw new \moodle_exception('invalidenrolinstance', 'enrol');
53
            }
54
            $this->instance = $instances[$instanceid] ?? null;
55
        }
56
        return $this->instance;
57
    }
58
 
59
    #[\Override]
60
    public function definition() {
61
        $mform = $this->_form;
62
 
63
        $mform->addElement('password', 'guestpassword', get_string('password', 'enrol_guest'));
64
 
65
        $mform->addElement('hidden', 'id');
66
        $mform->setType('id', PARAM_INT);
67
 
68
        $mform->addElement('hidden', 'instance');
69
        $mform->setType('instance', PARAM_INT);
70
    }
71
 
72
    #[\Override]
73
    public function validation($data, $files) {
74
        global $DB, $CFG;
75
 
76
        $errors = parent::validation($data, $files);
77
        $instance = $this->get_instance();
78
 
79
        if ($instance->password !== '') {
80
            if ($data['guestpassword'] !== $instance->password) {
81
                $plugin = enrol_get_plugin('guest');
82
                if ($plugin->get_config('showhint')) {
83
                    $hint = core_text::substr($instance->password, 0, 1);
84
                    $errors['guestpassword'] = get_string('passwordinvalidhint', 'enrol_guest', $hint);
85
                } else {
86
                    $errors['guestpassword'] = get_string('passwordinvalid', 'enrol_guest');
87
                }
88
            }
89
        }
90
 
91
        return $errors;
92
    }
93
 
94
    #[\Override]
95
    protected function check_access_for_dynamic_submission(): void {
96
        global $USER, $CFG;
97
        $courseid = $this->get_instance()->courseid;
98
        $course = get_course($courseid);
99
        $context = context_course::instance($this->get_instance()->courseid);
100
        if (!\core_course_category::can_view_course_info($course) && !is_enrolled($context, $USER, '', true)) {
101
            throw new \moodle_exception('coursehidden', '', $CFG->wwwroot . '/');
102
        }
103
    }
104
 
105
    #[\Override]
106
    protected function get_context_for_dynamic_submission(): \context {
107
        // This form is used for users who are not yet enrolled in the course and do not have access to the course.
108
        // For the purpose of permission checks they must be able to access the course category for this course.
109
        return context_course::instance($this->get_instance()->courseid)->get_parent_context();
110
    }
111
 
112
    #[\Override]
113
    protected function get_page_url_for_dynamic_submission(): moodle_url {
114
        $instance = $this->get_instance();
115
        return new moodle_url('/enrol/index.php', ['id' => $instance->courseid, 'instance' => $instance->id]);
116
    }
117
 
118
    /**
119
     * Process the form submission, used if form was submitted via AJAX
120
     *
121
     * Enrols the user in the course and returns the URL to redirect to
122
     *
123
     * @return string
124
     */
125
    public function process_dynamic_submission() {
126
        global $USER, $CFG, $SESSION;
127
 
128
        /** @var \enrol_guest_plugin $enrol */
129
        $enrol = enrol_get_plugin('guest');
130
        $instance = $this->get_instance();
131
 
132
        $enrol->mark_user_as_enrolled($instance, $this->get_data()->guestpassword);
133
 
134
        // Go to the originally requested page.
135
        if (!empty($SESSION->wantsurl)) {
136
            $destination = $SESSION->wantsurl;
137
            unset($SESSION->wantsurl);
138
        } else {
139
            require_once($CFG->dirroot . '/course/lib.php');
140
            $destination = course_get_url($instance->courseid);
141
        }
142
        return $destination;
143
    }
144
 
145
    #[\Override]
146
    public function set_data_for_dynamic_submission(): void {
147
        $instance = $this->get_instance();
148
        $this->set_data(['id' => $instance->courseid, 'instance' => $instance->id]);
149
    }
150
}