Reputation: 436
I am creating a moodle website. I already setup my courses with their specific enrolment keys, etc. But I want to know in what .php file and where in that file (in the moodle files), does Moodle check if the enrollment key entered by the user matches what I set as the enrolment key for the course...
Thanks for your help!
UPDATE-------- I did as Russell England suggested, but when I go to the page where I type my enrolment key, the page isn't loading or the page is redirected to my moodle homepage. My table that I store the enrolment keys is user_enrolment_keys.
Here is the updated validation function:
public function validation($data, $files) {
global $DB, $CFG, $USER;
$errors = parent::validation($data, $files);
$instance = $this->instance;
if ($this->toomany) {
$errors['notice'] = get_string('error');
return $errors;
}
//--------Russell's suggestion--------------
if ($instance->password) {
$params = array('user_email' => $USER->email, 'course_id' => $instance->courseid, 'enrolment_key' => $data['enrolpassword']);
if (!$DB->record_exists('user_enrolment_keys', $params)) {
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
return $errors;
}
}
//What I tried last (did not work either)...
/*$uemail = $USER->email;
$userscoursekey = 'testing';
$connecty = mysqli_connect("localhost", "...", "...", "...");
mysql_select_db('user_enrolment_keys', $connecty);
$var2 = $instance->courseid;
$resulty = mysqli_query($connecty, "SELECT * FROM user_enrolment_keys WHERE user_email='$uemail' AND course_id='$var2'");
$numrows = $resulty->num_rows;
if($numrows > 0)
{
while($row = mysqli_fetch_assoc($resulty))
{
$userscoursekey = $row['enrolment_key'];
}
}
$instance->password = $userscoursekey;
my_sqli_close($connecty); //Close the database connection.*/
if ($instance->password) {
if ($data['enrolpassword'] !== $instance->password) {
if ($instance->customint1) {
$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_self');
}
} else {
$plugin = enrol_get_plugin('self');
if ($plugin->get_config('showhint')) {
$hint = core_text::substr($instance->password, 0, 1);
$errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
} else {
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
}
}
}
}
return $errors;
// END DEFAULT BLOCK
}
Upvotes: 0
Views: 1025
Reputation: 10221
Its validated in the custom self enrolment form
In the function validation() in the file /enrol/self/locallib.php it checks if the password matches and displays an error if not.
UPDATE:
I would probably do this in the validation function
Add the $USER variable at the top
global $DB, $CFG, $USER;
Then check if the user, course and password combination exists in your newly created licence table ;)
$params = array('userid' => $USER->id, 'courseid' => $instance->courseid, 'password' => $data['enrolpassword']);
if (!$DB->record_exists('local_licence_table', $params)) {
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
return $errors;
}
Upvotes: 0