1
0
mirror of https://github.com/PlagiarismCheck/moodle-plagiarism_pchkorg.git synced 2024-12-22 04:10:07 +00:00

Add teacher auto registration

This commit is contained in:
Jane Adelmann 2024-07-31 14:19:43 +03:00
parent faaca3e8ac
commit 917a886c88
No known key found for this signature in database
GPG Key ID: 4CCF39DF30B8AF72
8 changed files with 214 additions and 5 deletions

View File

@ -0,0 +1,57 @@
<?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 plagiarism_pchkorg
* @category plagiarism
* @copyright PlagiarismCheck.org, https://plagiarismcheck.org/
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace plagiarism_pchkorg\task;
defined('MOODLE_INTERNAL') || die();
/**
* Teacher auto registration.
*/
class auto_registrate_teachers extends \core\task\scheduled_task {
/**
* Name of the task.
*
* @return string
* @throws \coding_exception
*/
public function get_name() {
return get_string('autoregistrateteachers', 'plagiarism_pchkorg');
}
/**
* Task execution.
*
* @throws \coding_exception
* @throws \dml_exception
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot.'/plagiarism/pchkorg/lib.php');
$plugin = new \plagiarism_plugin_pchkorg();
$plugin->cron_auto_registrate_teachers();
}
}

View File

@ -38,5 +38,15 @@
<KEY NAME="cm" TYPE="foreign" FIELDS="cm" REFTABLE="course_modules" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="plagiarism_pchkorg_users" COMMENT="imported users">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="email" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="plagiarism_pchkorg_users_email" TYPE="unique" FIELDS="email"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

View File

@ -42,4 +42,13 @@ $tasks = array(
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'plagiarism_pchkorg\task\auto_registrate_teachers',
'blocking' => 0,
'minute' => '*',
'hour' => '*/1',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
);

View File

@ -68,5 +68,25 @@ function xmldb_plagiarism_pchkorg_upgrade($oldversion) {
}
}
if ($oldversion < 2024072918) {
// Define table plagiarism_pchkorg_users to be created.
$table = new xmldb_table('plagiarism_pchkorg_users');
// Adding fields to table plagiarism_pchkorg_users.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('email', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
// Adding keys to table plagiarism_pchkorg_users.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('plagiarism_pchkorg_users_email', XMLDB_KEY_UNIQUE, ['email']);
// Conditionally launch create table for plagiarism_pchkorg_users.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
upgrade_plugin_savepoint(true, 2024072918, 'plagiarism', 'pchkorg');
}
return true;
}

View File

@ -94,6 +94,14 @@ class plagiarism_pchkorg_setup_form extends moodleform {
);
$mform->setDefault('pchkorg_enabled_by_default', '1');
$mform->addElement(
'select',
'pchkorg_teacher_auto_registration',
get_string('pchkorg:teacherautoregistration', 'plagiarism_pchkorg'),
array(get_string('no'), get_string('yes'))
);
$mform->setDefault('pchkorg_teacher_auto_registration', '0');
$this->add_action_buttons(true);
}

View File

@ -59,6 +59,7 @@ $string['pchkorg_debug_student_not_allowed_see_widget'] = 'Students can not see
$string['pchkorg_student_can_see_widget'] = 'Students can see a similarity score';
$string['pchkorg_student_can_see_report'] = 'Students can access a similarity report';
$string['pchkorg_check_ai'] = 'Enable AI Detector';
$string['pchkorg:teacherautoregistration'] = 'Enable Teacher auto-registration';
$string['pchkorg_disclosure'] = 'Submission will be sent to <a target="_blank" href="https://plagiarismcheck.org/">PlagiarismCheck.org</a> for check.
<br />
By submitting assignment I agree with <a target="_blank" href="https://plagiarismcheck.org/terms-of-service/">Terms &amp; Conditions</a>
@ -84,10 +85,12 @@ $string['privacy:metadata:plagiarism_pchkorg_config:value'] = 'Value of option';
$string['privacy:metadata:plagiarism_pchkorg'] = 'Service for originality check plagiarismcheck.org';
$string['privacy:metadata:plagiarism_pchkorg:file'] =
'Submission attachment for originality checkprivacy:metadata:plagiarism_pchkorg';
$string['privacy:metadata:plagiarism_pchkorg_users:email'] = 'Emails of auto-registered teachers';
$string['pchkorg:enable'] = 'Enable or Disable plugin';
$string['privacy:metadata:core_files'] = 'We need a content of submission, for originality check';
$string['sendqueuedsubmissions'] = '';
$string['updatereportscores'] = '';
$string['sendqueuedsubmissions'] = 'Send texts to check';
$string['updatereportscores'] = 'Update check result';
$string['autoregistrateteachers'] = 'Auto registration for teachers';
$string['pchkorg_label_title'] = 'PlagiarismCheck.org ID: %s; Similarity Score: %s%%';
$string['pchkorg_label_result'] = 'ID: %s Similarity: %s%%';
$string['pchkorg_label_title_ai'] = 'PlagiarismCheck.org ID: %s; Similarity Score: %s%% AI: %s%%';

102
lib.php
View File

@ -1065,6 +1065,108 @@ display: inline-block;"
return true;
}
public function cron_auto_registrate_teachers() {
global $DB;
$configmodel = new plagiarism_pchkorg_config_model();
$enabled = $configmodel->get_system_config('pchkorg_use');
$isdebugenabled = $configmodel->get_system_config('pchkorg_enable_debug') === '1';
$apitoken = $configmodel->get_system_config('pchkorg_token');
// Plugin is disabled.
if ($enabled !== '1') {
if ($isdebugenabled) {
echo 'cron_auto_registrate_teachers: Plugin is disabled';
}
return true;
}
$isfeatureenabled = $configmodel->get_system_config('pchkorg_teacher_auto_registration');
// This feature is disabled.
if ($isfeatureenabled !== '1') {
if ($isdebugenabled) {
echo 'cron_auto_registrate_teachers: This feature is disabled.';
}
return true;
}
// Api token is empty.
if (empty($apitoken)) {
if ($isdebugenabled) {
echo 'cron_auto_registrate_teachers: Api token is empty.';
}
return true;
}
$apiprovider = new plagiarism_pchkorg_api_provider($apitoken);
// This SQL fetches all teachers in courses where plugin is enabled.
// And teachers not already imported.
$sql = 'SELECT u.email as email,
CONCAT(u.firstname, \' \', u.lastname) as name
FROM {user} u
INNER JOIN {user_enrolments} ue ON ue.userid = u.id
INNER JOIN {enrol} e ON e.id = ue.enrolid
INNER JOIN {context} ctx_user ON ctx_user.instanceid = u.id AND ctx_user.contextlevel = 30 -- CONTEXT_USER
INNER JOIN {role_assignments} ON {role_assignments}.userid = u.id
INNER JOIN {role} ON {role_assignments}.roleid = {role}.id
INNER JOIN {context} ctx_course ON ctx_course.id = {role_assignments}.contextid AND ctx_course.contextlevel = 50 -- CONTEXT_COURSE
WHERE u.deleted = 0
AND {role}.shortname IN (
\'manager\',
\'coursecreator\',
\'editingteacher\',
\'teacher\'
)
AND e.courseid IN (
SELECT DISTINCT {course}.id as emabled_in_course
FROM {assign}
INNER JOIN {course_modules} ON {course_modules}.instance = {assign}.id
INNER JOIN {plagiarism_pchkorg_config} ON {plagiarism_pchkorg_config}.cm = {course_modules}.id
INNER JOIN {course} ON {course}.id = {course_modules}.course AND {assign}.course = {course}.id
INNER JOIN {modules} ON {modules}.id = {course_modules}.module
WHERE {plagiarism_pchkorg_config}.value = 1
AND {plagiarism_pchkorg_config}.name = \'pchkorg_module_use\'
AND {modules}.name = \'assign\'
) AND NOT EXISTS (SELECT 1 FROM {plagiarism_pchkorg_users} WHERE {plagiarism_pchkorg_users}.email = u.email LIMIT 1)
GROUP BY u.id
ORDER BY e.courseid DESC
LIMIT 50;';
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
// Small email validation.
if (\strpos($record->email, '@') === false) {
if ($isdebugenabled) {
echo 'cron_auto_registrate_teachers: Email format is invalid.';
}
continue;
}
$member = $apiprovider->get_group_member_response($record->email);
// This feature is not available for this user. Stop here and disable this feature.
if (!$member->is_auto_registration_enabled) {
set_config('pchkorg_teacher_auto_registration', '0', 'plagiarism_pchkorg');
$configmodel->set_system_config('pchkorg_teacher_auto_registration', '0');
return false;
}
// User is already registered.
if ($member->is_member) {
$insertdata = new \stdClass;
$insertdata->email = $record->email;
$DB->insert_record('plagiarism_pchkorg_users', $insertdata);
} else {
// Send API request for registration. Number 2 mean role teacher.
$success = $apiprovider->auto_registrate_member($record->name, $record->email, 2);
// Operation is successful.
if ($success) {
$insertdata = new \stdClass;
$insertdata->email = $record->email;
$DB->insert_record('plagiarism_pchkorg_users', $insertdata);
}
}
}
return true;
}
/**
*
* Will find the first user in group assignment.

View File

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