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

Add filters for API Provider

This commit is contained in:
Jane Adelmann 2022-04-28 15:02:50 +03:00
parent 8d2814a419
commit 4f1c16bf57
No known key found for this signature in database
GPG Key ID: 4CCF39DF30B8AF72
3 changed files with 116 additions and 28 deletions

View File

@ -92,8 +92,17 @@ class plagiarism_pchkorg_api_provider {
* *
* @return |null * @return |null
*/ */
public function send_group_text($authorhash, $cousereid, $assignmentid, $submissionid, $attachmentid, $content, $mime, public function send_group_text(
$filename) { $authorhash,
$cousereid,
$assignmentid,
$submissionid,
$attachmentid,
$content,
$mime,
$filename,
$filters = array()
) {
$boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time())); $boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time()));
@ -109,7 +118,9 @@ class plagiarism_pchkorg_api_provider {
$attachmentid, $attachmentid,
$content, $content,
$mime, $mime,
$filename), $filename,
$filters,
),
array( array(
'CURLOPT_RETURNTRANSFER' => true, 'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_FOLLOWLOCATION' => true,
@ -149,15 +160,18 @@ class plagiarism_pchkorg_api_provider {
* @param $filename * @param $filename
* @return string * @return string
*/ */
private function get_body_for_group($boundary, private function get_body_for_group(
$authorhash, $boundary,
$cousereid, $authorhash,
$assignmentid, $cousereid,
$submissionid, $assignmentid,
$attachmentid, $submissionid,
$content, $attachmentid,
$mime, $content,
$filename) { $mime,
$filename,
$filters = array()
) {
$eol = "\r\n"; $eol = "\r\n";
$body = ''; $body = '';
@ -171,6 +185,9 @@ class plagiarism_pchkorg_api_provider {
$body .= $this->get_part('language', 'en', $boundary); $body .= $this->get_part('language', 'en', $boundary);
$body .= $this->get_part('skip_english_words_validation', '1', $boundary); $body .= $this->get_part('skip_english_words_validation', '1', $boundary);
$body .= $this->get_part('skip_percentage_words_validation', '1', $boundary); $body .= $this->get_part('skip_percentage_words_validation', '1', $boundary);
foreach ($filters as $filtername => $filtervalue) {
$body .= $this->get_part($filtername, $filtervalue, $boundary);
}
$body .= $this->get_file_part('content', $content, $mime, $filename, $boundary); $body .= $this->get_file_part('content', $content, $mime, $filename, $boundary);
$body .= '--' . $boundary . '--' . $eol; $body .= '--' . $boundary . '--' . $eol;
@ -185,14 +202,14 @@ class plagiarism_pchkorg_api_provider {
* @param $filename * @param $filename
* @return |null * @return |null
*/ */
public function send_text($content, $mime, $filename) { public function send_text($content, $mime, $filename, $filters = array()) {
$boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time())); $boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time()));
$curl = new curl(); $curl = new curl();
$response = $curl->post( $response = $curl->post(
$this->endpoint . '/api/v1/text', $this->endpoint . '/api/v1/text',
$this->get_body($boundary, $content, $mime, $filename), $this->get_body($boundary, $content, $mime, $filename, $filters),
array( array(
'CURLOPT_RETURNTRANSFER' => true, 'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_FOLLOWLOCATION' => true,
@ -299,13 +316,16 @@ class plagiarism_pchkorg_api_provider {
* @param $filename * @param $filename
* @return string * @return string
*/ */
private function get_body($boundary, $content, $mime, $filename) { private function get_body($boundary, $content, $mime, $filename, $filters = array()) {
$eol = "\r\n"; $eol = "\r\n";
$body = ''; $body = '';
$body .= $this->get_part('language', 'en', $boundary); $body .= $this->get_part('language', 'en', $boundary);
$body .= $this->get_part('skip_english_words_validation', '1', $boundary); $body .= $this->get_part('skip_english_words_validation', '1', $boundary);
$body .= $this->get_part('skip_percentage_words_validation', '1', $boundary); $body .= $this->get_part('skip_percentage_words_validation', '1', $boundary);
foreach ($filters as $filtername => $filtervalue) {
$body .= $this->get_part($filtername, $filtervalue, $boundary);
}
$body .= $this->get_file_part('text', $content, $mime, $filename, $boundary); $body .= $this->get_file_part('text', $content, $mime, $filename, $boundary);
$body .= '--' . $boundary . '--' . $eol; $body .= '--' . $boundary . '--' . $eol;

View File

@ -44,6 +44,7 @@ class plagiarism_pchkorg_config_model {
if (!array_key_exists($module, $resultmap)) { if (!array_key_exists($module, $resultmap)) {
$configs = $DB->get_records('plagiarism_pchkorg_config', array( $configs = $DB->get_records('plagiarism_pchkorg_config', array(
'cm' => $module, 'cm' => $module,
'name' => 'pchkorg_module_use'
)); ));
$enabled = false; $enabled = false;
@ -63,6 +64,42 @@ class plagiarism_pchkorg_config_model {
return $resultmap[$module]; return $resultmap[$module];
} }
/**
*
* Check if plugin is enable for some specific module.
* Result is static.
*
* @param $module
* @return bool
*/
public function get_min_percent_for_module($module) {
global $DB;
static $resultmap = array();
// This will be called only once per module.
if (!array_key_exists($module, $resultmap)) {
$configs = $DB->get_records('plagiarism_pchkorg_config', array(
'cm' => $module,
'name' => 'pchkorg_min_percent'
));
$minpercent = null;
foreach ($configs as $record) {
switch ($record->name) {
case 'pchkorg_min_percent':
$minpercent = $record->value;
break;
default:
break;
}
}
$resultmap[$module] = $minpercent;
}
return $resultmap[$module];
}
/** /**
* *
* Save plugin settings. * Save plugin settings.
@ -100,7 +137,7 @@ class plagiarism_pchkorg_config_model {
'cm' => 0, 'cm' => 0,
'name' => $name, 'name' => $name,
)); ));
$resultsmap[$name] = null;
foreach ($records as $record) { foreach ($records as $record) {
$resultsmap[$name] = $record->value; $resultsmap[$name] = $record->value;
break; break;

55
lib.php
View File

@ -221,18 +221,34 @@ display: inline-block;"
'cm' => $data->coursemodule 'cm' => $data->coursemodule
)); ));
$context = context_module::instance($data->coursemodule);
$canchangeminpercent = has_capability(capability::CHANGE_MIN_PERCENT_FILTER, $context);
foreach ($fields as $field) { foreach ($fields as $field) {
$isfounded = false; $isfounded = false;
foreach ($records as $record) { foreach ($records as $record) {
if ($record->name === $field) { if ($record->name === $field) {
$isfounded = true; $isfounded = true;
if ($field === 'pchkorg_min_percent' && !$canchangeminpercent) {
$DB->delete_records('plagiarism_pchkorg_config', array('id' => $record->id));
break;
}
if ($field === 'pchkorg_min_percent' && 0 == $data->{$record->name}) {
$DB->delete_records('plagiarism_pchkorg_config', array('id' => $record->id));
break;
}
$record->value = $data->{$record->name}; $record->value = $data->{$record->name};
$DB->update_record('plagiarism_pchkorg_config', $record); $DB->update_record('plagiarism_pchkorg_config', $record);
break; break;
} }
} }
if (!$isfounded && isset($data->{$field})) { if (!$isfounded && isset($data->{$field})) {
if ($field === 'pchkorg_min_percent' && !$canchangeminpercent) {
continue;
}
if ($field === 'pchkorg_min_percent' && 0 == $data->{$field}) {
continue;
}
$insert = new \stdClass(); $insert = new \stdClass();
$insert->cm = $data->coursemodule; $insert->cm = $data->coursemodule;
$insert->name = $field; $insert->name = $field;
@ -274,10 +290,10 @@ display: inline-block;"
|| is_null($mform->exportValues()['pchkorg_module_use'])) { || is_null($mform->exportValues()['pchkorg_module_use'])) {
$mform->setDefault('pchkorg_module_use', '1'); $mform->setDefault('pchkorg_module_use', '1');
} }
if (!isset($mform->exportValues()['pchkorg_min_percent']) // if (!isset($mform->exportValues()['pchkorg_min_percent'])
|| is_null($mform->exportValues()['pchkorg_min_percent'])) { // || is_null($mform->exportValues()['pchkorg_min_percent'])) {
$mform->setDefault('pchkorg_min_percent', $minpercent); // $mform->setDefault('pchkorg_min_percent', $minpercent);
} // }
} else { } else {
$records = $DB->get_records('plagiarism_pchkorg_config', array( $records = $DB->get_records('plagiarism_pchkorg_config', array(
'cm' => $cm, 'cm' => $cm,
@ -325,7 +341,7 @@ display: inline-block;"
$dissabledattribute $dissabledattribute
); );
$mform->addHelpButton('pchkorg_min_percent', 'pchkorg_min_percent', 'plagiarism_pchkorg'); $mform->addHelpButton('pchkorg_min_percent', 'pchkorg_min_percent', 'plagiarism_pchkorg');
$mform->addRule('pchkorg_min_percent', null, 'numeric', null, 'client'); $mform->addRule('pchkorg_min_percent', null, 'text', null, 'client');
$mform->addRule( $mform->addRule(
'pchkorg_min_percent', 'pchkorg_min_percent',
get_string('pchkorg_min_percent_range', 'plagiarism_pchkorg'), get_string('pchkorg_min_percent_range', 'plagiarism_pchkorg'),
@ -400,6 +416,7 @@ display: inline-block;"
} }
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model(); $pchkorgconfigmodel = new plagiarism_pchkorg_config_model();
// Token is needed for API auth.
$apitoken = $pchkorgconfigmodel->get_system_config('pchkorg_token'); $apitoken = $pchkorgconfigmodel->get_system_config('pchkorg_token');
$apiprovider = new plagiarism_pchkorg_api_provider($apitoken); $apiprovider = new plagiarism_pchkorg_api_provider($apitoken);
@ -588,23 +605,33 @@ display: inline-block;"
} }
$filesconditions = array('state' => 10); $filesconditions = array('state' => 10);
$moodlefiles = $DB->get_records('plagiarism_pchkorg_files', $filesconditions, $moodlefiles = $DB->get_records('plagiarism_pchkorg_files', $filesconditions,
'id', '*', 0, 20); 'id', '*', 0, 20);
if ($moodlefiles) { if ($moodlefiles) {
$fs = get_file_storage(); $fs = get_file_storage();
foreach ($moodlefiles as $filedb) { foreach ($moodlefiles as $filedb) {
$textid = null; $textid = null;
$user = $DB->get_record('user', array('id' => $filedb->userid)); $user = $DB->get_record('user', array('id' => $filedb->userid));
// This is attached file. // This is attached file.
$cm = get_coursemodule_from_id('', $filedb->cm); $cm = get_coursemodule_from_id('', $filedb->cm);
// Filter for future search.
$systemminpercent = $pchkorgconfigmodel->get_system_config('pchkorg_min_percent');
// Module filter value has a bigger priority then system config value.
$moduleminpercent = $pchkorgconfigmodel->get_min_percent_for_module($cm->id);
if ($moduleminpercent) {
$minpercent = $moduleminpercent;
} else {
$minpercent = $systemminpercent;
}
$filters = [];
if ($minpercent) {
$filters['source_min_percent'] = $minpercent;
}
if ($filedb->fileid === null) { if ($filedb->fileid === null) {
$moodletextsubmission = $DB->get_record('assignsubmission_onlinetext', $moodletextsubmission = $DB->get_record('assignsubmission_onlinetext',
array('submission' => $filedb->itemid), '*'); array('submission' => $filedb->itemid), '*');
if ($moodletextsubmission) { if ($moodletextsubmission) {
$content = $moodletextsubmission->onlinetext; $content = $moodletextsubmission->onlinetext;
if ($apiprovider->is_group_token()) { if ($apiprovider->is_group_token()) {
$textid = $apiprovider->send_group_text( $textid = $apiprovider->send_group_text(
$apiprovider->user_email_to_hash($user->email), $apiprovider->user_email_to_hash($user->email),
@ -614,13 +641,15 @@ display: inline-block;"
$moodletextsubmission->id, $moodletextsubmission->id,
html_to_text($content, 75, false), html_to_text($content, 75, false),
'plain/text', 'plain/text',
sprintf('%s-submussion.txt', $moodletextsubmission->id) sprintf('%s-submussion.txt', $moodletextsubmission->id),
$filters,
); );
} else { } else {
$textid = $apiprovider->send_text( $textid = $apiprovider->send_text(
html_to_text($content, 75, false), html_to_text($content, 75, false),
'plain/text', 'plain/text',
sprintf('%s-submussion.txt', $moodletextsubmission->id) sprintf('%s-submussion.txt', $moodletextsubmission->id),
$filters
); );
} }
} }
@ -652,7 +681,8 @@ display: inline-block;"
$file->get_id(), $file->get_id(),
$file->get_content(), $file->get_content(),
$file->get_mimetype(), $file->get_mimetype(),
$file->get_filename() $file->get_filename(),
$filters
); );
} else { } else {
$agreementwhere = array( $agreementwhere = array(
@ -669,7 +699,8 @@ display: inline-block;"
$textid = $apiprovider->send_text( $textid = $apiprovider->send_text(
$file->get_content(), $file->get_content(),
$file->get_mimetype(), $file->get_mimetype(),
$file->get_filename() $file->get_filename(),
$filters
); );
} }
} }