-
Notifications
You must be signed in to change notification settings - Fork 6
/
enrol_form.php
112 lines (93 loc) · 3.89 KB
/
enrol_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package enrol_profilefield
* @category enrol
* @author Valery Fremaux <[email protected]>
* @copyright 2010 Valery Fremaux ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
class enrol_profilefield_enrol_form extends moodleform {
protected $instance;
protected $toomany = false;
/**
* Overriding this function to get unique form id for multiple self enrolments
*
* @return string form identifier
*/
protected function get_form_identifier() {
$formid = $this->_customdata->id.'_'.get_class($this);
return $formid;
}
public function definition() {
global $DB;
$mform = $this->_form;
$instance = $this->_customdata;
$this->instance = $instance;
$plugin = enrol_get_plugin('self');
$heading = $plugin->get_instance_name($instance);
$mform->addElement('header', 'profilefieldheader', $heading);
if ($instance->customint3 > 0) {
// Max enrol limit specified.
$count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($count >= $instance->customint3) {
// Bad luck, no more self enrolments here.
$this->toomany = true;
$mform->addElement('static', 'notice', '', get_string('maxenrolledreached', 'enrol_profilefield'));
return;
}
}
// Change the id of self enrolment key input as there can be multiple self enrolment methods.
$params = array('id' => $instance->id.'_enrolpassword');
$mform->addElement('passwordunmask', 'enrolpassword', get_string('grouppassword', 'enrol_profilefield'), $params);
$this->add_action_buttons(false, get_string('enrolme', 'enrol_profilefield'));
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', $instance->courseid);
$mform->addElement('hidden', 'instance');
$mform->setType('instance', PARAM_INT);
$mform->setDefault('instance', $instance->id);
}
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
$instance = $this->instance;
if ($this->toomany) {
$errors['notice'] = get_string('error');
return $errors;
}
if (!empty($data['enrolpassword'])) {
$groups = $DB->get_records('groups', array('courseid' => $instance->courseid), 'id ASC', 'id, enrolmentkey');
$found = false;
foreach ($groups as $group) {
if (empty($group->enrolmentkey)) {
continue;
}
if ($group->enrolmentkey === $data['enrolpassword']) {
$found = true;
break;
}
}
if (!$found) {
// We can not hint because there are probably multiple passwords.
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_profilefield');
}
}
return $errors;
}
}