Add debug mode

This commit is contained in:
Jane Adelmann 2022-07-05 17:04:51 +03:00
parent 8187645ee1
commit 59185593d9
No known key found for this signature in database
GPG Key ID: 4CCF39DF30B8AF72
4 changed files with 89 additions and 10 deletions

View File

@ -55,6 +55,14 @@ class plagiarism_pchkorg_setup_form extends moodleform {
$mform->addRule('pchkorg_token', null, 'required', null, 'client');
$mform->setType('pchkorg_token', PARAM_TEXT);
$mform->addElement(
'select',
'pchkorg_enable_debug',
get_string('pchkorg_enable_debug', 'plagiarism_pchkorg'),
array(get_string('no'), get_string('yes'))
);
$mform->addHelpButton('pchkorg_enable_debug', 'pchkorg_enable_debug', 'plagiarism_pchkorg');
$mform->registerRule('check_pchkorg_min_percent', 'callback', 'pchkorg_check_pchkorg_min_percent');
$label = get_string('pchkorg_min_percent', 'plagiarism_pchkorg');

View File

@ -42,8 +42,19 @@ $string['pchkorg_min_percent_range'] = 'Must be between 0 and 99';
$string['pchkorg_exclude_self_plagiarism'] = 'Exclude self-plagiarism';
$string['pchkorg_include_referenced'] = 'Include References';
$string['pchkorg_include_citation'] = 'Include Quotes';
$string['pchkorg_enable_debug'] = 'Enable debug information';
$string['pchkorg_enable_debug_help'] = 'This information can help you to understand why something not work';
$string['pchkorg_enable_quiz'] = 'Enable PlagiarismCheck in Quizzes';
$string['pchkorg_enable_forum'] = 'Enable PlagiarismCheck in Forum Activity';
$string['pchkorg_debug_mime'] = 'Mime of file is not supported';
$string['pchkorg_debug_disabled'] = 'Plugin is disabled';
$string['pchkorg_debug_empty_context'] = 'Plugin is not supported in this place';
$string['pchkorg_debug_user_has_no_permission'] = 'User has no moodle-permission to see this';
$string['pchkorg_debug_disabled_acitivity'] = 'Plugin is disabled for this activity';
$string['pchkorg_debug_not_member'] = 'User is not a member of plagiarismcheck group';
$string['pchkorg_debug_user_has_no_capability'] = 'User does not have capability';
$string['pchkorg_debug_no_check'] = 'There is no checks for this activity';
$string['pchkorg_debug_status_error'] = 'Some error for this file';
$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>

74
lib.php
View File

@ -187,6 +187,7 @@ class plagiarism_plugin_pchkorg extends plagiarism_plugin {
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model();
$apitoken = $pchkorgconfigmodel->get_system_config('pchkorg_token');
$isdebugenabled = $pchkorgconfigmodel->get_system_config('pchkorg_enable_debug') === '1';
$apiprovider = new plagiarism_pchkorg_api_provider($apitoken);
$cmid = $linkarray['cmid'];
@ -199,13 +200,22 @@ class plagiarism_plugin_pchkorg extends plagiarism_plugin {
// We can do nothing with submissions which we can not handle.
if (null !== $file && !$apiprovider->is_supported_mime($file->get_mimetype())) {
return '';
return $this->exit_message(
sprintf(
'%s (%s)',
get_string('pchkorg_debug_mime', 'plagiarism_pchkorg'),
$file->get_mimetype()),
$isdebugenabled
);
}
// SQL will be called only once, result is static.
$config = $pchkorgconfigmodel->get_system_config('pchkorg_use');
if ('1' !== $config) {
return '';
return $this->exit_message(
get_string('pchkorg_debug_disabled', 'plagiarism_pchkorg'),
$isdebugenabled
);
}
$context = null;
$component = !empty($linkarray['component']) ? $linkarray['component'] : '';
@ -223,17 +233,26 @@ class plagiarism_plugin_pchkorg extends plagiarism_plugin {
}
if (empty($context)) {
return '';
return $this->exit_message(
get_string('pchkorg_debug_empty_context', 'plagiarism_pchkorg'),
$isdebugenabled
);
}
$canview = has_capability(capability::VIEW_SIMILARITY, $context);
if (!$canview) {
return '';
return $this->exit_message(
get_string('pchkorg_debug_user_has_no_permission', 'plagiarism_pchkorg'),
$isdebugenabled
);
}
// SQL will be called only once per page. There is static result inside.
if (!$pchkorgconfigmodel->is_enabled_for_module($cmid)) {
return '';
return $this->exit_message(
get_string('pchkorg_debug_disabled_acitivity', 'plagiarism_pchkorg'),
$isdebugenabled
);
}
// Only for some type of account, method will call a remote HTTP API.
@ -242,12 +261,24 @@ class plagiarism_plugin_pchkorg extends plagiarism_plugin {
// 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)) {
return '';
return $this->exit_message(
sprintf(
'%s (%s)',
get_string('pchkorg_debug_not_member', 'plagiarism_pchkorg'),
$USER->email),
$isdebugenabled
);
}
$isgranted = !empty($context) && has_capability('mod/assign:view', $context, null);
if (!$isgranted) {
return '';
return $this->exit_message(
sprintf(
'%s (%s)',
get_string('pchkorg_debug_user_has_no_capability', 'plagiarism_pchkorg'),
'mod/assign:view'),
$isdebugenabled
);
}
$where = new \stdClass();
@ -426,9 +457,38 @@ display: inline-block;"
<img src="' . $imgsrc . '" alt="logo" width="20" />
' . $label . '
</span>';
} else {
return $this->exit_message(
sprintf(
'%s (%s)',
get_string('pchkorg_debug_status_error', 'plagiarism_pchkorg'),
$filerecord->state),
$isdebugenabled
);
}
}
return $this->exit_message(
sprintf(
'%s (%s)',
get_string('pchkorg_debug_no_check', 'plagiarism_pchkorg'),
$cmid),
$isdebugenabled
);
}
/**
* Render message with reason why do we stop plugin.
*
* @param string $message - exit message
* @param bool $debug - is debug enabled.
* @return string
*/
private function exit_message($message, $debug) {
if ($debug) {
return $message;
}
return '';
}

View File

@ -26,11 +26,11 @@ defined('MOODLE_INTERNAL') || die();
if (!isset($plugin)) {
$plugin = new stdClass();
}
$plugin->version = 2022061418;
$plugin->version = 2022071720;
$plugin->requires = 2020061501; // Requires Moodle 3.9 .
$plugin->release = 'v3.9.6';
$plugin->release = 'v3.9.7';
$plugin->component = 'plagiarism_pchkorg';
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = array(
'mod_assign' => ANY_VERSION,
'mod_assign' => ANY_VERSION,
);