2018-11-01 14:21:14 +00:00
|
|
|
<?php
|
2019-01-17 16:51:22 +00:00
|
|
|
// 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/>.
|
2018-11-01 16:50:10 +00:00
|
|
|
|
2018-11-01 14:21:14 +00:00
|
|
|
require_once(__DIR__ . '/../../../config.php');
|
|
|
|
require_once(__DIR__ . '/../lib.php');
|
|
|
|
require_once(__DIR__ . '/../form/send_text_form.php');
|
2019-01-17 16:51:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
global $PAGE, $CFG, $OUTPUT, $DB, $USER;
|
|
|
|
|
|
|
|
require_login();
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$pchkorgconfigmodel = new plagiarism_pchkorg_config_model($DB);
|
|
|
|
$urlgenerator = new plagiarism_pchkorg_url_generator();
|
|
|
|
$apiprovider = new plagiarism_pchkorg_api_provider(
|
|
|
|
$pchkorgconfigmodel->get_system_config('pchkorg_token')
|
|
|
|
);
|
2018-11-01 14:21:14 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$cmid = (int) required_param('cmid', PARAM_INT); // Course Module ID
|
|
|
|
$fileid = (int) required_param('file', PARAM_INT); // plagiarism file id.
|
|
|
|
$cm = get_coursemodule_from_id('', $cmid);
|
2018-11-01 14:21:14 +00:00
|
|
|
require_login($cm->course, true, $cm);
|
2019-01-17 16:51:22 +00:00
|
|
|
$context = context_module::instance($cm->id);
|
2018-11-01 14:21:14 +00:00
|
|
|
header('Content-Type: application/json');
|
2019-01-17 16:51:22 +00:00
|
|
|
$isgranted = is_viewing($context, null, 'plagiarism/pchkorg:check');
|
|
|
|
if (!$isgranted) {
|
|
|
|
die('{error: "access denied"}');
|
|
|
|
}
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$file = $fs->get_file_by_id($fileid);
|
|
|
|
|
|
|
|
if (!$file) {
|
|
|
|
die('{error: "file not exists"}');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('submission_files' !== $file->get_filearea()
|
|
|
|
|| $file->get_contextid() != $context->id) {
|
|
|
|
die('{error: "access denied"}');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent JS caching.
|
2018-11-01 14:21:14 +00:00
|
|
|
$CFG->cachejs = false;
|
2019-01-17 16:51:22 +00:00
|
|
|
$PAGE->set_url($urlgenerator->get_status_url());
|
|
|
|
$where = new \stdClass();
|
|
|
|
$where->cm = $cmid;
|
|
|
|
$where->fileid = $fileid;
|
|
|
|
|
|
|
|
$filerecord = $DB->get_record('plagiarism_pchkorg_files', (array) $where);
|
|
|
|
|
|
|
|
if (!$filerecord) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'success' => false,
|
|
|
|
'message' => '404 can not find text'
|
|
|
|
));
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$reportid = $apiprovider->check_text($filerecord->textid);
|
2018-11-01 14:21:14 +00:00
|
|
|
if ($checked = (null !== $reportid)) {
|
2019-01-17 16:51:22 +00:00
|
|
|
$report = $apiprovider->get_report($filerecord->textid);
|
2018-11-01 14:21:14 +00:00
|
|
|
$json = json_decode($report);
|
|
|
|
$score = 0;
|
|
|
|
if (isset($json->data)
|
2019-01-17 16:51:22 +00:00
|
|
|
&& isset($json->data->report)
|
|
|
|
&& isset($json->data->report->percent)
|
2018-11-01 14:21:14 +00:00
|
|
|
) {
|
|
|
|
$score = $json->data->report->percent;
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$filerecord->reportid = $reportid;
|
|
|
|
$filerecord->score = $score;
|
|
|
|
$DB->update_record('plagiarism_pchkorg_files', $filerecord);
|
2018-11-01 14:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$location = new moodle_url(sprintf(
|
2018-11-01 14:21:14 +00:00
|
|
|
'/plagiarism/pchkorg/page/report.php?cmid=%s&file=%s',
|
|
|
|
intval($cmid),
|
|
|
|
intval($fileid)
|
2019-01-17 16:51:22 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
echo json_encode(array(
|
|
|
|
'success' => true,
|
|
|
|
'checked' => $checked,
|
|
|
|
'location' => $location->out(false)
|
|
|
|
));
|