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 |
|
|
|
56 |
class enrol_self_enrol_form extends moodleform {
|
|
|
57 |
protected $instance;
|
|
|
58 |
protected $toomany = false;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Overriding this function to get unique form id for multiple self enrolments.
|
|
|
62 |
*
|
|
|
63 |
* @return string form identifier
|
|
|
64 |
*/
|
|
|
65 |
protected function get_form_identifier() {
|
|
|
66 |
$formid = $this->_customdata->id.'_'.get_class($this);
|
|
|
67 |
return $formid;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function definition() {
|
|
|
71 |
global $USER, $OUTPUT, $CFG;
|
|
|
72 |
$mform = $this->_form;
|
|
|
73 |
$instance = $this->_customdata;
|
|
|
74 |
$this->instance = $instance;
|
|
|
75 |
$plugin = enrol_get_plugin('self');
|
|
|
76 |
|
|
|
77 |
$heading = $plugin->get_instance_name($instance);
|
|
|
78 |
$mform->addElement('header', 'selfheader', $heading);
|
|
|
79 |
|
|
|
80 |
if ($instance->password) {
|
|
|
81 |
// Change the id of self enrolment key input as there can be multiple self enrolment methods.
|
|
|
82 |
$mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'),
|
|
|
83 |
array('id' => 'enrolpassword_'.$instance->id));
|
|
|
84 |
$context = context_course::instance($this->instance->courseid);
|
|
|
85 |
$userfieldsapi = \core_user\fields::for_userpic();
|
|
|
86 |
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
|
|
87 |
$keyholders = get_users_by_capability($context, 'enrol/self:holdkey', $ufields);
|
|
|
88 |
$keyholdercount = 0;
|
|
|
89 |
foreach ($keyholders as $keyholder) {
|
|
|
90 |
$keyholdercount++;
|
|
|
91 |
if ($keyholdercount === 1) {
|
|
|
92 |
$mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
|
|
|
93 |
}
|
|
|
94 |
$keyholdercontext = context_user::instance($keyholder->id);
|
|
|
95 |
if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
|
|
|
96 |
has_coursecontact_role($keyholder->id)) {
|
|
|
97 |
$profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&course=' .
|
|
|
98 |
$this->instance->courseid . '">' . fullname($keyholder) . '</a>';
|
|
|
99 |
} else {
|
|
|
100 |
$profilelink = fullname($keyholder);
|
|
|
101 |
}
|
|
|
102 |
$profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
|
|
|
103 |
$mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
} else {
|
|
|
107 |
$mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
|
|
|
111 |
|
|
|
112 |
$mform->addElement('hidden', 'id');
|
|
|
113 |
$mform->setType('id', PARAM_INT);
|
|
|
114 |
$mform->setDefault('id', $instance->courseid);
|
|
|
115 |
|
|
|
116 |
$mform->addElement('hidden', 'instance');
|
|
|
117 |
$mform->setType('instance', PARAM_INT);
|
|
|
118 |
$mform->setDefault('instance', $instance->id);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public function validation($data, $files) {
|
|
|
122 |
global $DB, $CFG;
|
|
|
123 |
|
|
|
124 |
$errors = parent::validation($data, $files);
|
|
|
125 |
$instance = $this->instance;
|
|
|
126 |
|
|
|
127 |
if ($this->toomany) {
|
|
|
128 |
$errors['notice'] = get_string('error');
|
|
|
129 |
return $errors;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if ($instance->password) {
|
|
|
133 |
if ($data['enrolpassword'] !== $instance->password) {
|
|
|
134 |
if ($instance->customint1) {
|
|
|
135 |
// Check group enrolment key.
|
|
|
136 |
if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
|
|
|
137 |
// We can not hint because there are probably multiple passwords.
|
|
|
138 |
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
} else {
|
|
|
142 |
$plugin = enrol_get_plugin('self');
|
|
|
143 |
if ($plugin->get_config('showhint')) {
|
|
|
144 |
$hint = core_text::substr($instance->password, 0, 1);
|
|
|
145 |
$errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
|
|
|
146 |
} else {
|
|
|
147 |
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
return $errors;
|
|
|
154 |
}
|
|
|
155 |
}
|