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
 * Self enrol plugin implementation.
19
 *
20
 * @package    enrol_self
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
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once("$CFG->libdir/formslib.php");
28
require_once($CFG->dirroot . '/enrol/locallib.php');
29
 
30
/**
31
 * Check if the given password match a group enrolment key in the specified course.
32
 *
33
 * @param  int $courseid            course id
34
 * @param  string $enrolpassword    enrolment password
35
 * @return bool                     True if match
36
 * @since  Moodle 3.0
37
 */
38
function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
39
    global $DB;
40
 
41
    $found = false;
42
    $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
43
 
44
    foreach ($groups as $group) {
45
        if (empty($group->enrolmentkey)) {
46
            continue;
47
        }
48
        if ($group->enrolmentkey === $enrolpassword) {
49
            $found = true;
50
            break;
51
        }
52
    }
53
    return $found;
54
}
55
 
1441 ariadna 56
/**
57
 * Old class for displaying the self enrolment form
58
 *
59
 * @deprecated since Moodle 5.0 - please use {@see enrol_self\form\enrol_form}
60
 */
61
#[\core\attribute\deprecated(replacement: enrol_self\form\enrol_form::class, since: '5.0', reason: 'Now a dynamic form is used')]
1 efrain 62
class enrol_self_enrol_form extends moodleform {
63
    protected $instance;
64
    protected $toomany = false;
65
 
66
    /**
1441 ariadna 67
     * Constructor
68
     *
69
     * @param mixed $action
70
     * @param mixed $customdata
71
     * @param string $method
72
     * @param string $target
73
     * @param mixed $attributes
74
     * @param bool $editable
75
     * @param array $ajaxformdata
76
     */
77
    public function __construct($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true,
78
                                $ajaxformdata=null) {
79
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
80
        parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
81
    }
82
 
83
    /**
1 efrain 84
     * Overriding this function to get unique form id for multiple self enrolments.
85
     *
86
     * @return string form identifier
87
     */
88
    protected function get_form_identifier() {
89
        $formid = $this->_customdata->id.'_'.get_class($this);
90
        return $formid;
91
    }
92
 
93
    public function definition() {
94
        global $USER, $OUTPUT, $CFG;
95
        $mform = $this->_form;
96
        $instance = $this->_customdata;
97
        $this->instance = $instance;
98
        $plugin = enrol_get_plugin('self');
99
 
100
        $heading = $plugin->get_instance_name($instance);
101
        $mform->addElement('header', 'selfheader', $heading);
102
 
103
        if ($instance->password) {
104
            // Change the id of self enrolment key input as there can be multiple self enrolment methods.
105
            $mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'),
106
                    array('id' => 'enrolpassword_'.$instance->id));
107
            $context = context_course::instance($this->instance->courseid);
108
            $userfieldsapi = \core_user\fields::for_userpic();
109
            $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
110
            $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', $ufields);
111
            $keyholdercount = 0;
112
            foreach ($keyholders as $keyholder) {
113
                $keyholdercount++;
114
                if ($keyholdercount === 1) {
115
                    $mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
116
                }
117
                $keyholdercontext = context_user::instance($keyholder->id);
118
                if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
119
                        has_coursecontact_role($keyholder->id)) {
120
                    $profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&amp;course=' .
121
                    $this->instance->courseid . '">' . fullname($keyholder) . '</a>';
122
                } else {
123
                    $profilelink = fullname($keyholder);
124
                }
125
                $profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
126
                $mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
127
            }
128
 
129
        } else {
130
            $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
131
        }
132
 
133
        $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
134
 
135
        $mform->addElement('hidden', 'id');
136
        $mform->setType('id', PARAM_INT);
137
        $mform->setDefault('id', $instance->courseid);
138
 
139
        $mform->addElement('hidden', 'instance');
140
        $mform->setType('instance', PARAM_INT);
141
        $mform->setDefault('instance', $instance->id);
142
    }
143
 
144
    public function validation($data, $files) {
145
        global $DB, $CFG;
146
 
147
        $errors = parent::validation($data, $files);
148
        $instance = $this->instance;
149
 
150
        if ($this->toomany) {
151
            $errors['notice'] = get_string('error');
152
            return $errors;
153
        }
154
 
155
        if ($instance->password) {
156
            if ($data['enrolpassword'] !== $instance->password) {
157
                if ($instance->customint1) {
158
                    // Check group enrolment key.
159
                    if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
160
                        // We can not hint because there are probably multiple passwords.
161
                        $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
162
                    }
163
 
164
                } else {
165
                    $plugin = enrol_get_plugin('self');
166
                    if ($plugin->get_config('showhint')) {
167
                        $hint = core_text::substr($instance->password, 0, 1);
168
                        $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
169
                    } else {
170
                        $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
171
                    }
172
                }
173
            }
174
        }
175
 
176
        return $errors;
177
    }
178
}