MDL-51568 self_enrol: New Web Service enrol_self_enrol_user
[moodle.git] / enrol / self / locallib.php
CommitLineData
df997f84 1<?php
df997f84
PS
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 *
d9669db9 20 * @package enrol_self
c1dfc4a8
PS
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
23 */
24
97795859 25defined('MOODLE_INTERNAL') || die();
df997f84
PS
26
27require_once("$CFG->libdir/formslib.php");
28
e8ad99ab
JL
29/**
30 * Check if the given password match a group enrolment key in the specified course.
31 *
32 * @param int $courseid course id
33 * @param string $enrolpassword enrolment password
34 * @return bool True if match
35 * @since Moodle 3.0
36 */
37function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
38 global $DB;
39
40 $found = false;
41 $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
42
43 foreach ($groups as $group) {
44 if (empty($group->enrolmentkey)) {
45 continue;
46 }
47 if ($group->enrolmentkey === $enrolpassword) {
48 $found = true;
49 break;
50 }
51 }
52 return $found;
53}
54
df997f84
PS
55class enrol_self_enrol_form extends moodleform {
56 protected $instance;
78c5042e 57 protected $toomany = false;
df997f84 58
3a70ccb8 59 /**
d9669db9 60 * Overriding this function to get unique form id for multiple self enrolments.
3a70ccb8
RT
61 *
62 * @return string form identifier
63 */
64 protected function get_form_identifier() {
cc1b5015 65 $formid = $this->_customdata->id.'_'.get_class($this);
3a70ccb8
RT
66 return $formid;
67 }
68
df997f84 69 public function definition() {
0ac6a57e 70 global $USER, $OUTPUT, $CFG;
df997f84 71 $mform = $this->_form;
cc1b5015 72 $instance = $this->_customdata;
df997f84
PS
73 $this->instance = $instance;
74 $plugin = enrol_get_plugin('self');
75
78c5042e
PS
76 $heading = $plugin->get_instance_name($instance);
77 $mform->addElement('header', 'selfheader', $heading);
78
df997f84 79 if ($instance->password) {
d9669db9 80 // Change the id of self enrolment key input as there can be multiple self enrolment methods.
3a70ccb8 81 $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
6aa3aabe 82 array('id' => 'enrolpassword_'.$instance->id));
0ac6a57e
ND
83 $context = context_course::instance($this->instance->courseid);
84 $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', user_picture::fields('u'));
85 $keyholdercount = 0;
86 foreach ($keyholders as $keyholder) {
87 $keyholdercount++;
88 if ($keyholdercount === 1) {
89 $mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
90 }
91 $keyholdercontext = context_user::instance($keyholder->id);
92 if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
93 has_coursecontact_role($keyholder->id)) {
94 $profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&amp;course=' .
95 $this->instance->courseid . '">' . fullname($keyholder) . '</a>';
96 } else {
97 $profilelink = fullname($keyholder);
98 }
99 $profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
100 $mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
101 }
102
df997f84 103 } else {
78c5042e 104 $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
df997f84
PS
105 }
106
107 $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
108
109 $mform->addElement('hidden', 'id');
110 $mform->setType('id', PARAM_INT);
111 $mform->setDefault('id', $instance->courseid);
112
113 $mform->addElement('hidden', 'instance');
114 $mform->setType('instance', PARAM_INT);
115 $mform->setDefault('instance', $instance->id);
116 }
117
118 public function validation($data, $files) {
119 global $DB, $CFG;
120
121 $errors = parent::validation($data, $files);
122 $instance = $this->instance;
123
78c5042e
PS
124 if ($this->toomany) {
125 $errors['notice'] = get_string('error');
126 return $errors;
127 }
128
df997f84
PS
129 if ($instance->password) {
130 if ($data['enrolpassword'] !== $instance->password) {
131 if ($instance->customint1) {
e8ad99ab
JL
132 // Check group enrolment key.
133 if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
d9669db9 134 // We can not hint because there are probably multiple passwords.
52d58821
PS
135 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
136 }
df997f84
PS
137
138 } else {
139 $plugin = enrol_get_plugin('self');
140 if ($plugin->get_config('showhint')) {
2f1e464a 141 $hint = core_text::substr($instance->password, 0, 1);
df997f84
PS
142 $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
143 } else {
144 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
145 }
146 }
147 }
148 }
149
150 return $errors;
151 }
6aa3aabe 152}