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_self\form;
20
 
21
use core\context\course as context_course;
22
use core\context\system as context_system;
23
use core_form\dynamic_form;
24
use core_text;
25
use html_writer;
26
use moodle_url;
27
 
28
/**
29
 * Form for entering password for self enrolment
30
 *
31
 * @package    enrol_self
32
 * @copyright  Marina Glancy
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class enrol_form extends dynamic_form {
36
    /** @var \stdClass */
37
    protected $instance;
38
    /** @var \enrol_self_plugin */
39
    protected $plugin = null;
40
 
41
    /**
42
     * Returns the enrolment method
43
     *
44
     * @return \enrol_self_plugin
45
     */
46
    protected function get_plugin(): \enrol_self_plugin {
47
        global $CFG;
48
        require_once($CFG->dirroot . '/lib/enrollib.php');
49
        if ($this->plugin === null) {
50
            $this->plugin = enrol_get_plugin('self');
51
        }
52
        return $this->plugin;
53
    }
54
 
55
    /**
56
     * Returns the instance of the enrolment method
57
     *
58
     * @return \stdClass
59
     */
60
    protected function get_instance(): \stdClass {
61
        global $DB, $CFG;
62
        require_once($CFG->dirroot . '/lib/enrollib.php');
63
        if ($this->instance === null) {
64
            // Method enrol_get_instances() will also validate that the enrolment method and the instance are enabled.
65
            $courseid = $this->optional_param('id', 0, PARAM_INT);
66
            $instanceid = $this->optional_param('instance', 0, PARAM_INT);
67
            $instances = enrol_get_instances($courseid, true);
68
            $this->instance = $instances[$instanceid] ?? null;
69
            if (empty($this->instance) || $this->instance->enrol !== 'self') {
70
                throw new \moodle_exception('invalidenrolinstance', 'enrol');
71
            }
72
        }
73
        return $this->instance;
74
    }
75
 
76
    #[\Override]
77
    public function definition() {
78
        global $USER, $OUTPUT, $CFG;
79
 
80
        $mform = $this->_form;
81
 
82
        $mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'));
83
 
84
        // Display keyholders - list of users who have 'enrol/self:holdkey' capability.
85
        $context = context_course::instance($this->instance->courseid);
86
        $userfieldsapi = \core_user\fields::for_userpic();
87
        $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
88
        $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', $ufields);
89
        $keyholdercount = 0;
90
        foreach ($keyholders as $keyholder) {
91
            $keyholdercount++;
92
            if ($keyholdercount === 1) {
93
                $mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
94
            }
95
            if ($USER->id == $keyholder->id
96
                    || has_capability('moodle/user:viewdetails', context_system::instance())
97
                    || has_coursecontact_role($keyholder->id)) {
98
                $profileurl = new moodle_url('/user/profile.php', ['id' => $keyholder->id, 'course' => $this->instance->courseid]);
99
                $profilelink = html_writer::link($profileurl, fullname($keyholder));
100
            } else {
101
                $profilelink = fullname($keyholder);
102
            }
103
            $profilepic = $OUTPUT->user_picture($keyholder, ['size' => 35, 'courseid' => $this->instance->courseid]);
104
            $mform->addElement('static', 'keyholder' . $keyholdercount, '', $profilepic . $profilelink);
105
        }
106
 
107
        $mform->addElement('hidden', 'id');
108
        $mform->setType('id', PARAM_INT);
109
 
110
        $mform->addElement('hidden', 'instance');
111
        $mform->setType('instance', PARAM_INT);
112
    }
113
 
114
    #[\Override]
115
    public function validation($data, $files) {
116
        global $DB, $CFG;
117
        require_once($CFG->dirroot.'/enrol/self/locallib.php');
118
 
119
        $errors = parent::validation($data, $files);
120
        $instance = $this->get_instance();
121
        if ($data['enrolpassword'] !== $instance->password) {
122
            if ($instance->customint1) {
123
                // Check group enrolment key.
124
                if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
125
                    // We can not hint because there are probably multiple passwords.
126
                    $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
127
                }
128
            } else {
129
                $plugin = enrol_get_plugin('self');
130
                if ($plugin->get_config('showhint')) {
131
                    $hint = core_text::substr($instance->password, 0, 1);
132
                    $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
133
                } else {
134
                    $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
135
                }
136
            }
137
        }
138
 
139
        return $errors;
140
    }
141
 
142
    #[\Override]
143
    protected function check_access_for_dynamic_submission(): void {
144
        global $USER, $CFG;
145
        $instance = $this->get_instance();
146
        $courseid = $instance->courseid;
147
        $course = get_course($courseid);
148
        $context = context_course::instance($instance->courseid);
149
        if (!\core_course_category::can_view_course_info($course) && !is_enrolled($context, $USER, '', true)) {
150
            throw new \moodle_exception('coursehidden', '', $CFG->wwwroot . '/');
151
        }
152
        if (isguestuser()) {
153
            throw new \moodle_exception('noguestaccess', 'enrol');
154
        }
155
        $canselfenrol = $this->get_plugin()->can_self_enrol($instance);
156
        if ($canselfenrol !== true) {
157
            throw new \moodle_exception($canselfenrol);
158
        }
159
        if (!$instance->password) {
160
            throw new \moodle_exception('nopassword', 'enrol_self');
161
        }
162
    }
163
 
164
    #[\Override]
165
    protected function get_context_for_dynamic_submission(): \context {
166
        // This form is used for users who are not yet enrolled in the course and do not have access to the course.
167
        // For the purpose of permission checks they must be able to access the course category for this course.
168
        return context_course::instance($this->get_instance()->courseid)->get_parent_context();
169
    }
170
 
171
    #[\Override]
172
    protected function get_page_url_for_dynamic_submission(): moodle_url {
173
        $instance = $this->get_instance();
174
        return new moodle_url('/enrol/index.php', ['id' => $instance->courseid, 'instance' => $instance->id]);
175
    }
176
 
177
    /**
178
     * Process the form submission, used if form was submitted via AJAX
179
     *
180
     * Enrols the user in the course and returns the URL to redirect to
181
     *
182
     * @return string
183
     */
184
    public function process_dynamic_submission() {
185
        global $CFG, $SESSION;
186
        $this->get_plugin()->enrol_self($this->get_instance(), $this->get_data());
187
 
188
        // Go to the originally requested page.
189
        if (!empty($SESSION->wantsurl)) {
190
            $destination = $SESSION->wantsurl;
191
            unset($SESSION->wantsurl);
192
        } else {
193
            require_once($CFG->dirroot . '/course/lib.php');
194
            $destination = course_get_url($this->get_instance()->courseid);
195
        }
196
        return $destination;
197
    }
198
 
199
    #[\Override]
200
    public function set_data_for_dynamic_submission(): void {
201
        $instance = $this->get_instance();
202
        $this->set_data(['id' => $instance->courseid, 'instance' => $instance->id]);
203
    }
204
}