2018-11-01 14:21:14 +00:00
|
|
|
<?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/>.
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
|
|
|
* @package plagiarism_pchkorg
|
|
|
|
* @category plagiarism
|
|
|
|
* @copyright PlagiarismCheck.org, https://plagiarismcheck.org/
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2018-11-01 14:21:14 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
require_once($CFG->dirroot . '/plagiarism/lib.php');
|
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
|
|
|
require_once($CFG->libdir . '/accesslib.php');
|
|
|
|
require_once(__DIR__ . '/classes/plagiarism_pchkorg_config_model.php');
|
|
|
|
require_once(__DIR__ . '/classes/plagiarism_pchkorg_url_generator.php');
|
|
|
|
require_once(__DIR__ . '/classes/plagiarism_pchkorg_api_provider.php');
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
|
|
|
* Class plagiarism_plugin_pchkorg
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
class plagiarism_plugin_pchkorg extends plagiarism_plugin {
|
2018-11-01 14:21:14 +00:00
|
|
|
/**
|
|
|
|
* hook to allow plagiarism specific information to be displayed beside a submission
|
2019-01-17 16:51:22 +00:00
|
|
|
*
|
2018-11-01 14:21:14 +00:00
|
|
|
* @param array $linkarraycontains all relevant information for the plugin to generate a link
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function get_links($linkarray) {
|
|
|
|
global $DB, $USER;
|
2019-02-28 13:17:56 +00:00
|
|
|
|
|
|
|
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model();
|
2019-01-17 16:51:22 +00:00
|
|
|
$urlgenerator = new plagiarism_pchkorg_url_generator();
|
2019-02-28 13:17:56 +00:00
|
|
|
$apitoken = $pchkorgconfigmodel->get_system_config('pchkorg_token');
|
|
|
|
$apiprovider = new plagiarism_pchkorg_api_provider($apitoken);
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
$cmid = $linkarray['cmid'];
|
|
|
|
$file = $linkarray['file'];
|
|
|
|
|
|
|
|
// We can do nothing with submissions which we can not handle.
|
|
|
|
if (!$apiprovider->is_supported_mime($file->get_mimetype())) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQL will be called only once, result is static.
|
2019-01-17 16:51:22 +00:00
|
|
|
$config = $pchkorgconfigmodel->get_system_config('pchkorg_use');
|
2018-11-01 14:21:14 +00:00
|
|
|
if ('1' !== $config) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
$context = null;
|
|
|
|
if (!empty($cmid)) {
|
|
|
|
$context = context_module::instance($cmid);// Get context of course.
|
|
|
|
}
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
// SQL will be called only once per page. There is static result inside.
|
2019-01-17 16:51:22 +00:00
|
|
|
if (!$pchkorgconfigmodel->is_enabled_for_module($cmid)) {
|
2018-11-01 14:21:14 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
// 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.
|
|
|
|
// Even if service is unavailable, method will try call only once.
|
|
|
|
// Also, we don't use use raw user email.
|
2019-01-30 15:36:04 +00:00
|
|
|
if (!$apiprovider->is_group_member($USER->email)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
$isgranted = !empty($context) && has_capability('mod/assign:view', $context, null);
|
|
|
|
if (!$isgranted) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$where = new \stdClass();
|
|
|
|
$where->cm = $cmid;
|
|
|
|
$where->fileid = $file->get_id();
|
|
|
|
|
|
|
|
$filerecord = $DB->get_record('plagiarism_pchkorg_files', (array) $where);
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$checkurl = $urlgenerator->get_check_url($cmid, $file->get_id());
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
if ($filerecord) {
|
|
|
|
$label = sprintf('%.2f', $filerecord->score) . '%';
|
2019-01-30 17:39:47 +00:00
|
|
|
$link = sprintf(' <a href="%s" target="_blank">( %s )</a> ', $checkurl->__toString(), $label);
|
2018-11-01 14:21:14 +00:00
|
|
|
} else {
|
2019-01-17 16:51:22 +00:00
|
|
|
$label = get_string('pchkorg_check_for_plagiarism', 'plagiarism_pchkorg');
|
2019-01-30 17:39:47 +00:00
|
|
|
$link = sprintf(' <a href="%s">( %s )</a> ', $checkurl->__toString(), $label);
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 17:39:47 +00:00
|
|
|
return $link;
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* hook to save plagiarism specific settings on a module settings page
|
|
|
|
* @param object $data - data from an mform submission.
|
|
|
|
*/
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @throws dml_exception
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function save_form_elements($data) {
|
2018-11-01 14:21:14 +00:00
|
|
|
global $DB;
|
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model();
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$config = $pchkorgconfigmodel->get_system_config('pchkorg_use');
|
2018-11-01 14:21:14 +00:00
|
|
|
if ('1' != $config) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isset($data->pchkorg_module_use)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$records = $DB->get_records('plagiarism_pchkorg_config', array(
|
2019-01-17 16:51:22 +00:00
|
|
|
'cm' => $data->coursemodule
|
2018-11-01 14:21:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if (empty($records)) {
|
2019-01-17 16:51:22 +00:00
|
|
|
$insert = new \stdClass();
|
|
|
|
$insert->cm = $data->coursemodule;
|
|
|
|
$insert->name = 'pchkorg_module_use';
|
2018-11-01 14:21:14 +00:00
|
|
|
$insert->value = $data->pchkorg_module_use;
|
|
|
|
$DB->insert_record('plagiarism_pchkorg_config', $insert);
|
|
|
|
} else {
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$record->value = $data->{$record->name};
|
|
|
|
$DB->update_record('plagiarism_pchkorg_config', $record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
|
|
|
* @param object $mform
|
|
|
|
* @param object $context
|
|
|
|
* @param string $modulename
|
|
|
|
* @throws coding_exception
|
|
|
|
* @throws dml_exception
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function get_form_elements_module($mform, $context, $modulename = '') {
|
2019-02-28 13:17:56 +00:00
|
|
|
if (!$context || !isset($modulename) || 'mod_assign' !== $modulename) {
|
2019-01-17 16:51:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
global $DB;
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model();
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$config = $pchkorgconfigmodel->get_system_config('pchkorg_use');
|
2018-11-01 14:21:14 +00:00
|
|
|
if ('1' == $config) {
|
|
|
|
$defaultcmid = null;
|
2019-01-17 16:51:22 +00:00
|
|
|
$cm = optional_param('update', $defaultcmid, PARAM_INT);
|
2018-11-01 14:21:14 +00:00
|
|
|
if (null !== $cm) {
|
|
|
|
$records = $DB->get_records('plagiarism_pchkorg_config', array(
|
2019-01-17 16:51:22 +00:00
|
|
|
'cm' => $cm,
|
2018-11-01 14:21:14 +00:00
|
|
|
));
|
|
|
|
if (!empty($records)) {
|
2019-02-28 13:17:56 +00:00
|
|
|
$mform->setDefault($records[0]->name, $records[0]->value);
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
$mform->addElement('header', 'plagiarism_pchkorg', get_string('pluginname', 'plagiarism_pchkorg'));
|
2018-11-01 14:21:14 +00:00
|
|
|
$mform->addElement(
|
2019-01-17 16:51:22 +00:00
|
|
|
'select',
|
|
|
|
$setting = 'pchkorg_module_use',
|
2019-02-28 13:17:56 +00:00
|
|
|
get_string('pchkorg_module_use', 'plagiarism_pchkorg'),
|
2019-01-17 16:51:22 +00:00
|
|
|
array(get_string('no'), get_string('yes'))
|
2018-11-01 14:21:14 +00:00
|
|
|
);
|
|
|
|
$mform->addHelpButton('pchkorg_module_use', 'pchkorg_module_use', 'plagiarism_pchkorg');
|
|
|
|
|
|
|
|
if (!isset($mform->exportValues()[$setting]) || is_null($mform->exportValues()[$setting])) {
|
|
|
|
$mform->setDefault($setting, '1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hook to allow a disclosure to be printed notifying users what will happen with their submission
|
2019-01-17 16:51:22 +00:00
|
|
|
*
|
2018-11-01 14:21:14 +00:00
|
|
|
* @param int $cmid - course module id
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function print_disclosure($cmid) {
|
2019-02-28 13:17:56 +00:00
|
|
|
global $OUTPUT;
|
|
|
|
|
|
|
|
if (empty($cmid)) {
|
|
|
|
return '';
|
|
|
|
}
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
// Get course details.
|
|
|
|
$cm = get_coursemodule_from_id('', $cmid);
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
if ($cm->modname != 'assign') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$configmodel = new plagiarism_pchkorg_config_model();
|
|
|
|
|
|
|
|
$enabled = $configmodel->get_system_config('pchkorg_use');
|
|
|
|
if ($enabled !== '1') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($configmodel->is_enabled_for_module($cmid) != '1') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = '';
|
|
|
|
|
|
|
|
$result .= $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$formatoptions = new stdClass;
|
2018-11-01 14:21:14 +00:00
|
|
|
$formatoptions->noclean = true;
|
2019-01-17 16:51:22 +00:00
|
|
|
$formatoptions->cmid = $cmid;
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
$result .= format_text(get_string('pchkorg_disclosure', 'plagiarism_pchkorg'), FORMAT_MOODLE, $formatoptions);
|
|
|
|
$result .= $OUTPUT->box_end();
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
return $result;
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
}
|