User auto registration

This commit is contained in:
Jane Adelmann 2022-11-04 17:13:24 +02:00
parent e198855544
commit 0803af58ea
No known key found for this signature in database
GPG Key ID: 4CCF39DF30B8AF72
3 changed files with 120 additions and 8 deletions

View File

@ -506,6 +506,88 @@ class plagiarism_pchkorg_api_provider {
return $resultmap[$email];
}
/**
* Check that user belongs to group when it is group account.
* And Receive auto_registration_option.
*
* @param string $email
* @return object
*/
public function get_group_member_response($email = '') {
if (!$this->is_group_token()) {
$result = new \stdClass;
$result->is_member = true;
$result->is_auto_registration_enabled = false;
return $result;
}
static $resultmap = array();
if (!array_key_exists($email, $resultmap)) {
//default result. For case when we can not receive response.
$result = new \stdClass;
$result->is_member = false;
$result->is_auto_registration_enabled = false;
$resultmap[$email] = $result;
$curl = new curl();
$response = $curl->post($this->endpoint . '/lms/moodle/is-group-member/', array(
'token' => $this->token,
'hash' => $this->user_email_to_hash($email)
), array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FOLLOWLOCATION' => true,
'CURLOPT_SSL_VERIFYHOST' => false,
'CURLOPT_SSL_VERIFYPEER' => false,
// The maximum number of seconds to allow cURL functions to execute.
'CURLOPT_TIMEOUT' => 8
));
if ($json = json_decode($response)) {
$result->is_member = $json->is_member;
$result->is_auto_registration_enabled = $json->is_auto_registration_enabled;
$resultmap[$email] = $result;
}
}
return $resultmap[$email];
}
/**
* Auto registration is enabled for this university,
* so we registrate a user and user can check submissions.
*
* @param $name
* @param $email
* @param $role
*
* @return bool
*/
public function auto_registrate_member($name, $email, $role) {
$curl = new curl();
$response = $curl->post($this->endpoint . '/lms/moodle/auto-registration/', array(
'token' => $this->token,
'name' => $name,
'email' => $email,
'role' => $role,
), array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FOLLOWLOCATION' => true,
'CURLOPT_SSL_VERIFYHOST' => false,
'CURLOPT_SSL_VERIFYPEER' => false,
// The maximum number of seconds to allow cURL functions to execute.
'CURLOPT_TIMEOUT' => 8
));
if ($json = json_decode($response)) {
return $json->success;
}
return false;
}
/**
* Check status of document.
* If document has been checked, state is 5.

42
lib.php
View File

@ -367,7 +367,7 @@ class plagiarism_plugin_pchkorg extends plagiarism_plugin {
// Only for some type of account, method will call a remote HTTP API.
// The API will be called only once, because result is static.
// Also, there is timeout 2 seconds for response.
// Also, there is timeout 8 seconds for response.
// Even if service will be unavailable, method will try call API only once.
// Also, we don't use raw user email.
if (!$apiprovider->is_group_member($USER->email)) {
@ -680,6 +680,17 @@ display: inline-block;"
public function event_handler($eventdata) {
global $USER, $DB;
// Whitelist of supported events, ignore other.
$issupportedevent = in_array($eventdata['eventtype'], array(
"forum_attachment",
"quiz_submitted",
"assessable_submitted",
"content_uploaded"
));
if (!$issupportedevent) {
return true;
}
$modulename = $eventdata['other']['modulename'];
$allowedmodules = array('assign', 'mod_assign');
// We support only assign module so just ignore all other.
@ -706,7 +717,7 @@ display: inline-block;"
$cmid = $eventdata['contextinstanceid'];
// Remove the event if the course module no longer exists.
$cm = get_coursemodule_from_id($eventdata['other']['modulename'], $cmid);
$context = context_module::instance($cm->id);
if (!$cm) {
return true;
}
@ -719,11 +730,30 @@ display: inline-block;"
// Only for some type of account, method will call a remote HTTP API.
// The API will be called only once, because result is static.
// Also, there is timeout 2 seconds for response.
// Also, there is timeout 8 seconds for response.
// Even if service is unavailable, method will try call only once.
// Also, we don't use raw users email.
if (!$apiprovider->is_group_member($USER->email)) {
return true;
$ismemberresponse = $apiprovider->get_group_member_response($USER->email);
if (!$ismemberresponse->is_member) {
if ($ismemberresponse->is_auto_registration_enabled) {
$name = $USER->firstname . ' ' . $USER->lastname;
$roleDatas = get_user_roles($context, $USER->id, true);
$roles = [];
foreach ($roleDatas as $rolesData) {
$roles[] = strtolower($rolesData->shortname);
}
// Moodle has multiple roles in courses.
$isstudent = in_array('student', $roles)
&& !in_array('teacher', $roles)
&& !in_array('editingteacher', $roles)
&& !in_array('managerteacher', $roles);
$isregistered = $apiprovider->auto_registrate_member($name, $USER->email, $isstudent ? 3 : 2);
if (!$isregistered) {
return true;
}
} else {
return true;
}
}
// Set the author and submitter.
@ -733,7 +763,7 @@ display: inline-block;"
// Related user ID will be NULL if an instructor submits on behalf of a student who is in a group.
// To get around this, we get the group ID, get the group members and set the author as the first student in the group.
if ((empty($eventdata['relateduserid'])) && ($eventdata['other']['modulename'] == 'assign')
&& has_capability('mod/assign:editothersubmission', context_module::instance($cm->id), $submitter)) {
&& has_capability('mod/assign:editothersubmission', $context, $submitter)) {
$moodlesubmission = $DB->get_record('assign_submission', array('id' => $eventdata['objectid']), 'id, groupid');
if (!empty($moodlesubmission->groupid)) {
$author = $this->get_first_group_author($cm->course, $moodlesubmission->groupid);

View File

@ -26,9 +26,9 @@ defined('MOODLE_INTERNAL') || die();
if (!isset($plugin)) {
$plugin = new stdClass();
}
$plugin->version = 2022110417;
$plugin->version = 2022110418;
$plugin->requires = 2020061501; // Requires Moodle 3.9 .
$plugin->release = 'v3.12.4';
$plugin->release = 'v3.13.1';
$plugin->component = 'plagiarism_pchkorg';
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = array(