2019-01-17 16:51:22 +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
|
|
|
|
*/
|
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Class provider HTTP-API methods.
|
2019-02-06 13:38:23 +00:00
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
class plagiarism_pchkorg_api_provider {
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Auth token.
|
|
|
|
*
|
|
|
|
* @var string
|
2019-02-06 13:38:23 +00:00
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
private $token;
|
2019-04-05 16:25:06 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Url of api.
|
|
|
|
*
|
2019-01-17 16:51:22 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $endpoint;
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Last api error.
|
|
|
|
*
|
|
|
|
* @var string|null
|
2019-02-06 13:38:23 +00:00
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
private $lasterror;
|
|
|
|
|
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Fetch last api error.
|
|
|
|
*
|
2019-01-17 16:51:22 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get_last_error() {
|
|
|
|
return $this->lasterror;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Setup last api error.
|
|
|
|
*
|
2019-01-17 16:51:22 +00:00
|
|
|
* @param mixed $lasterror
|
|
|
|
*/
|
|
|
|
public function set_last_error($lasterror) {
|
|
|
|
$this->lasterror = $lasterror;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Constructor for api provider.
|
2019-02-06 13:38:23 +00:00
|
|
|
*
|
|
|
|
* @param $token
|
|
|
|
* @param string $endpoint
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function __construct($token, $endpoint = 'https://plagiarismcheck.org') {
|
|
|
|
$this->token = $token;
|
|
|
|
$this->endpoint = $endpoint;
|
|
|
|
}
|
|
|
|
|
2022-05-12 17:17:28 +00:00
|
|
|
/**
|
|
|
|
* Send text for originality check.
|
|
|
|
*
|
|
|
|
* @param $authorhash
|
|
|
|
* @param $cousereid
|
|
|
|
* @param $assignmentid
|
|
|
|
* @param $assignmentname
|
|
|
|
* @param $submissionid
|
|
|
|
* @param $attachmentid
|
|
|
|
* @param $content
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
|
|
|
*
|
|
|
|
* @return |null
|
|
|
|
*/
|
|
|
|
public function general_send_check(
|
|
|
|
$authorhash,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
|
|
|
$assignmentname,
|
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters = array()
|
|
|
|
) {
|
|
|
|
if ($this->is_group_token()) {
|
|
|
|
return $this->send_group_text(
|
|
|
|
$authorhash,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
|
|
|
$assignmentname,
|
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->send_text(
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
|
|
|
$assignmentname,
|
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Send text for originality check.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $authorhash
|
|
|
|
* @param $cousereid
|
|
|
|
* @param $assignmentid
|
2022-05-04 17:20:37 +00:00
|
|
|
* @param $assignmentname
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $submissionid
|
|
|
|
* @param $attachmentid
|
|
|
|
* @param $content
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
2019-04-05 16:25:06 +00:00
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @return |null
|
|
|
|
*/
|
2022-04-28 12:02:50 +00:00
|
|
|
public function send_group_text(
|
|
|
|
$authorhash,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2022-04-28 12:02:50 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters = array()
|
|
|
|
) {
|
2019-01-17 16:51:22 +00:00
|
|
|
|
|
|
|
$boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time()));
|
|
|
|
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->post(
|
|
|
|
$this->endpoint . '/lms/moodle/check-text/',
|
|
|
|
$this->get_body_for_group(
|
|
|
|
$boundary,
|
|
|
|
$authorhash,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2019-01-17 16:51:22 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
2022-04-28 12:02:50 +00:00
|
|
|
$filename,
|
2022-11-16 14:31:42 +00:00
|
|
|
$filters
|
2022-04-28 12:02:50 +00:00
|
|
|
),
|
2019-01-17 16:51:22 +00:00
|
|
|
array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
'CURLOPT_HTTPHEADER' => array(
|
|
|
|
'X-API-TOKEN: ' . $this->generate_api_token(),
|
|
|
|
'Content-Type: multipart/form-data; boundary=' . $boundary
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$id = null;
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
if (isset($json->message)) {
|
|
|
|
$this->set_last_error($json->message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (isset($json->success) && $json->success) {
|
|
|
|
$id = $json->data->text->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Build HTTP body of request.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $boundary
|
|
|
|
* @param $authorhash
|
|
|
|
* @param $cousereid
|
|
|
|
* @param $assignmentid
|
2022-05-04 17:20:37 +00:00
|
|
|
* @param $assignmentname
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $submissionid
|
|
|
|
* @param $attachmentid
|
|
|
|
* @param $content
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 12:02:50 +00:00
|
|
|
private function get_body_for_group(
|
|
|
|
$boundary,
|
|
|
|
$authorhash,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2022-04-28 12:02:50 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters = array()
|
|
|
|
) {
|
2019-01-17 16:51:22 +00:00
|
|
|
$eol = "\r\n";
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
$body .= $this->get_part('token', $this->token, $boundary);
|
|
|
|
$body .= $this->get_part('hash', $authorhash, $boundary);
|
|
|
|
$body .= $this->get_part('course_id', $cousereid, $boundary);
|
|
|
|
$body .= $this->get_part('assignment_id', $assignmentid, $boundary);
|
2022-05-04 17:20:37 +00:00
|
|
|
$body .= $this->get_part('assignment_name', $assignmentname, $boundary);
|
2019-01-17 16:51:22 +00:00
|
|
|
$body .= $this->get_part('submission_id', $submissionid, $boundary);
|
|
|
|
$body .= $this->get_part('attachment_id', $attachmentid, $boundary);
|
|
|
|
$body .= $this->get_part('filename', $filename, $boundary);
|
|
|
|
$body .= $this->get_part('language', 'en', $boundary);
|
2019-04-05 16:25:06 +00:00
|
|
|
$body .= $this->get_part('skip_english_words_validation', '1', $boundary);
|
|
|
|
$body .= $this->get_part('skip_percentage_words_validation', '1', $boundary);
|
2022-05-03 10:59:11 +00:00
|
|
|
$body .= $this->get_part('lms', 'moodle', $boundary);
|
2022-04-28 12:02:50 +00:00
|
|
|
foreach ($filters as $filtername => $filtervalue) {
|
2022-05-04 18:03:11 +00:00
|
|
|
if ($filtervalue !== null) {
|
|
|
|
$body .= $this->get_part($filtername, $filtervalue, $boundary);
|
|
|
|
}
|
2022-04-28 12:02:50 +00:00
|
|
|
}
|
2019-01-17 16:51:22 +00:00
|
|
|
$body .= $this->get_file_part('content', $content, $mime, $filename, $boundary);
|
|
|
|
$body .= '--' . $boundary . '--' . $eol;
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2022-05-04 17:20:37 +00:00
|
|
|
* Send text for originality check.
|
2019-04-05 16:25:06 +00:00
|
|
|
*
|
2022-05-04 17:20:37 +00:00
|
|
|
* @param $authorhash
|
|
|
|
* @param $cousereid
|
|
|
|
* @param $assignmentid
|
|
|
|
* @param $assignmentname
|
|
|
|
* @param $submissionid
|
|
|
|
* @param $attachmentid
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $content
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
2022-05-04 17:20:37 +00:00
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @return |null
|
|
|
|
*/
|
2022-05-03 10:59:11 +00:00
|
|
|
public function send_text(
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2022-05-03 10:59:11 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters = array()) {
|
2019-04-05 16:25:06 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$boundary = sprintf('PLAGCHECKBOUNDARY-%s', uniqid(time()));
|
|
|
|
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->post(
|
|
|
|
$this->endpoint . '/api/v1/text',
|
2022-05-03 10:59:11 +00:00
|
|
|
$this->get_body(
|
|
|
|
$boundary,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2022-05-03 10:59:11 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
2022-11-16 14:31:42 +00:00
|
|
|
$filters
|
2022-05-03 10:59:11 +00:00
|
|
|
),
|
2019-01-17 16:51:22 +00:00
|
|
|
array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
'CURLOPT_POST' => true,
|
|
|
|
'CURLOPT_HTTPHEADER' => array(
|
|
|
|
'X-API-TOKEN: ' . $this->generate_api_token(),
|
|
|
|
'Content-Type: multipart/form-data; boundary=' . $boundary
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$id = null;
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
if (isset($json->message)) {
|
|
|
|
$this->set_last_error($json->message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (isset($json->success) && $json->success) {
|
|
|
|
$id = $json->data->text->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
*
|
|
|
|
* Method send information to service thar agreement had been accepted.
|
|
|
|
* Method will be called only for personal account type.
|
|
|
|
*
|
2019-04-26 12:14:20 +00:00
|
|
|
* @param string $email User email
|
|
|
|
* @return void
|
2019-04-05 16:25:06 +00:00
|
|
|
*/
|
2019-04-26 12:14:20 +00:00
|
|
|
public function save_accepted_agreement($email) {
|
|
|
|
$token = $this->token;
|
|
|
|
if ($this->is_group_token()) {
|
|
|
|
$token = $this->token . '::' . hash('sha256', $this->token . $email);
|
|
|
|
}
|
|
|
|
|
2019-04-05 16:25:06 +00:00
|
|
|
$curl = new curl();
|
|
|
|
$curl->post(
|
|
|
|
$this->endpoint . '/api/v1/agreement/create/moodle-plugin/2019-04-11/',
|
|
|
|
'',
|
|
|
|
array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
'CURLOPT_POST' => true,
|
|
|
|
'CURLOPT_HTTPHEADER' => array(
|
2019-04-26 12:14:20 +00:00
|
|
|
'X-API-TOKEN: ' . $token,
|
2019-04-05 16:25:06 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build part of HTTP body.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
* @param $boundary
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
private function get_part($name, $value, $boundary) {
|
|
|
|
$eol = "\r\n";
|
|
|
|
|
|
|
|
$part = '--' . $boundary . $eol;
|
|
|
|
$part .= 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol;
|
|
|
|
$part .= $value . $eol;
|
|
|
|
|
|
|
|
return $part;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Build part of HTTP body. This part contains file.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
|
|
|
* @param $boundary
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
private function get_file_part($name, $value, $mime, $filename, $boundary) {
|
|
|
|
$eol = "\r\n";
|
|
|
|
|
|
|
|
$part = '--' . $boundary . $eol;
|
|
|
|
$part .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $filename . '";' . $eol;
|
|
|
|
$part .= 'Content-Type: ' . $mime . $eol;
|
|
|
|
$part .= 'Content-Length: ' . strlen($value) . $eol . $eol;
|
|
|
|
$part .= $value . $eol;
|
|
|
|
|
|
|
|
return $part;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2022-05-04 17:20:37 +00:00
|
|
|
* Build HTTP body of request.
|
2019-04-05 16:25:06 +00:00
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $boundary
|
2022-05-04 17:20:37 +00:00
|
|
|
* @param $cousereid
|
|
|
|
* @param $assignmentid
|
|
|
|
* @param $assignmentname
|
|
|
|
* @param $submissionid
|
|
|
|
* @param $attachmentid
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $content
|
|
|
|
* @param $mime
|
|
|
|
* @param $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-03 10:59:11 +00:00
|
|
|
private function get_body(
|
|
|
|
$boundary,
|
|
|
|
$cousereid,
|
|
|
|
$assignmentid,
|
2022-05-04 17:20:37 +00:00
|
|
|
$assignmentname,
|
2022-05-03 10:59:11 +00:00
|
|
|
$submissionid,
|
|
|
|
$attachmentid,
|
|
|
|
$content,
|
|
|
|
$mime,
|
|
|
|
$filename,
|
|
|
|
$filters = array()
|
|
|
|
) {
|
2019-01-17 16:51:22 +00:00
|
|
|
$eol = "\r\n";
|
|
|
|
|
|
|
|
$body = '';
|
|
|
|
$body .= $this->get_part('language', 'en', $boundary);
|
2019-04-05 16:25:06 +00:00
|
|
|
$body .= $this->get_part('skip_english_words_validation', '1', $boundary);
|
|
|
|
$body .= $this->get_part('skip_percentage_words_validation', '1', $boundary);
|
2022-05-03 10:59:11 +00:00
|
|
|
$body .= $this->get_part('course_id', $cousereid, $boundary);
|
|
|
|
$body .= $this->get_part('assignment_id', $assignmentid, $boundary);
|
2022-05-04 17:20:37 +00:00
|
|
|
$body .= $this->get_part('assignment_name', $assignmentname, $boundary);
|
2022-05-03 10:59:11 +00:00
|
|
|
$body .= $this->get_part('submission_id', $submissionid, $boundary);
|
|
|
|
$body .= $this->get_part('attachment_id', $attachmentid, $boundary);
|
|
|
|
$body .= $this->get_part('lms', 'moodle', $boundary);
|
2022-04-28 12:02:50 +00:00
|
|
|
foreach ($filters as $filtername => $filtervalue) {
|
2022-05-04 18:03:11 +00:00
|
|
|
if ($filtervalue !== null) {
|
|
|
|
$body .= $this->get_part($filtername, $filtervalue, $boundary);
|
|
|
|
}
|
2022-04-28 12:02:50 +00:00
|
|
|
}
|
2019-01-17 16:51:22 +00:00
|
|
|
$body .= $this->get_file_part('text', $content, $mime, $filename, $boundary);
|
|
|
|
$body .= '--' . $boundary . '--' . $eol;
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Convert user email to sha256 salted hash.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $email
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function user_email_to_hash($email) {
|
2019-04-05 16:25:06 +00:00
|
|
|
// We don't send raw user email to the service.
|
2022-11-04 15:21:10 +00:00
|
|
|
return hash('sha256', $this->token . strtolower($email));
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Check type of service account.
|
|
|
|
* There are two types of accounts: personal and group.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function is_group_token() {
|
|
|
|
return 'G-' === \substr($this->token, 0, 2);
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Check that user belongs to group when it is group account.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param string $email
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function is_group_member($email = '') {
|
|
|
|
if (!$this->is_group_token()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
static $resultmap = array();
|
|
|
|
|
|
|
|
if (!array_key_exists($email, $resultmap)) {
|
|
|
|
$resultmap[$email] = false;
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->post($this->endpoint . '/lms/moodle/is-group-member/', array(
|
|
|
|
'token' => $this->token,
|
|
|
|
'hash' => $this->user_email_to_hash($email)
|
|
|
|
), array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
// The maximum number of seconds to allow cURL functions to execute.
|
2022-10-31 11:00:16 +00:00
|
|
|
'CURLOPT_TIMEOUT' => 8
|
2019-02-28 13:17:56 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
if (true == $json->is_member) {
|
|
|
|
$resultmap[$email] = true;
|
|
|
|
}
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:17:56 +00:00
|
|
|
return $resultmap[$email];
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 15:13:24 +00:00
|
|
|
/**
|
|
|
|
* Check that user belongs to group when it is group account.
|
|
|
|
* And Receive auto_registration_option.
|
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_group_member_response($email = '') {
|
|
|
|
if (!$this->is_group_token()) {
|
|
|
|
$result = new \stdClass;
|
|
|
|
$result->is_member = true;
|
|
|
|
$result->is_auto_registration_enabled = false;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static $resultmap = array();
|
|
|
|
|
|
|
|
if (!array_key_exists($email, $resultmap)) {
|
|
|
|
//default result. For case when we can not receive response.
|
|
|
|
$result = new \stdClass;
|
|
|
|
$result->is_member = false;
|
|
|
|
$result->is_auto_registration_enabled = false;
|
|
|
|
$resultmap[$email] = $result;
|
|
|
|
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->post($this->endpoint . '/lms/moodle/is-group-member/', array(
|
|
|
|
'token' => $this->token,
|
|
|
|
'hash' => $this->user_email_to_hash($email)
|
|
|
|
), array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
// The maximum number of seconds to allow cURL functions to execute.
|
|
|
|
'CURLOPT_TIMEOUT' => 8
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
$result->is_member = $json->is_member;
|
|
|
|
$result->is_auto_registration_enabled = $json->is_auto_registration_enabled;
|
|
|
|
$resultmap[$email] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resultmap[$email];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto registration is enabled for this university,
|
|
|
|
* so we registrate a user and user can check submissions.
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @param $email
|
|
|
|
* @param $role
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function auto_registrate_member($name, $email, $role) {
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->post($this->endpoint . '/lms/moodle/auto-registration/', array(
|
|
|
|
'token' => $this->token,
|
|
|
|
'name' => $name,
|
|
|
|
'email' => $email,
|
|
|
|
'role' => $role,
|
|
|
|
), array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
// The maximum number of seconds to allow cURL functions to execute.
|
|
|
|
'CURLOPT_TIMEOUT' => 8
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
return $json->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Check status of document.
|
|
|
|
* If document has been checked, state is 5.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $textid
|
2019-04-05 16:25:06 +00:00
|
|
|
* @return object|null
|
2019-02-06 13:38:23 +00:00
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function check_text($textid) {
|
2019-04-25 16:03:19 +00:00
|
|
|
if ($this->is_group_token()) {
|
|
|
|
// The same method but for group users.
|
|
|
|
// It uses different auth.
|
|
|
|
return $this->group_check_text($textid);
|
|
|
|
}
|
2023-06-06 08:38:38 +00:00
|
|
|
|
2019-01-17 16:51:22 +00:00
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->get($this->endpoint . '/api/v1/text/' . $textid, array(), array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
'CURLOPT_POST' => false,
|
|
|
|
'CURLOPT_HTTPHEADER' => array(
|
|
|
|
'X-API-TOKEN: ' . $this->generate_api_token(),
|
|
|
|
'Content-Type: application/x-www-form-urlencoded'
|
|
|
|
),
|
|
|
|
));
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
if (isset($json->data) && 5 == $json->data->state) {
|
2023-06-06 08:38:38 +00:00
|
|
|
$result = new stdClass;
|
|
|
|
$result->id = $json->data->report->id;
|
|
|
|
$result->percent = $json->data->report->percent;
|
|
|
|
$result->percent_ai = $json->data->ai_report->processed_percent;
|
|
|
|
|
|
|
|
return $result;
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:22:49 +00:00
|
|
|
return null;
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 16:03:19 +00:00
|
|
|
/**
|
|
|
|
* Check status of document for Group User
|
|
|
|
* If document has been checked, state is 5.
|
|
|
|
*
|
|
|
|
* @param $textid
|
|
|
|
* @return object|null
|
|
|
|
*/
|
|
|
|
public function group_check_text($textid) {
|
|
|
|
$curl = new curl();
|
|
|
|
$response = $curl->get("{$this->endpoint}/lms/check-report/{$textid}/", array(
|
|
|
|
'token' => $this->token
|
|
|
|
), array(
|
|
|
|
'CURLOPT_RETURNTRANSFER' => true,
|
|
|
|
'CURLOPT_POST' => false,
|
|
|
|
'CURLOPT_HTTPHEADER' => array(
|
|
|
|
'Content-Type: application/x-www-form-urlencoded'
|
|
|
|
),
|
|
|
|
));
|
|
|
|
if ($json = json_decode($response)) {
|
|
|
|
if (isset($json->data) && 5 == $json->data->state) {
|
2023-06-06 08:38:38 +00:00
|
|
|
$result = new stdClass;
|
|
|
|
$result->id = $json->data->report->id;
|
|
|
|
$result->percent = $json->data->report->percent;
|
|
|
|
$result->percent_ai = $json->data->ai_report->processed_percent;
|
|
|
|
|
|
|
|
return $result;
|
2019-04-25 16:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Build url for the api.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $id
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-30 17:39:47 +00:00
|
|
|
public function get_report_action($id) {
|
|
|
|
return "{$this->endpoint}/lms/public-report/{$id}/";
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* Generate token for API auth.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-30 17:39:47 +00:00
|
|
|
public function generate_api_token() {
|
2019-01-17 16:51:22 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if ($this->is_group_token()) {
|
2023-07-04 12:31:09 +00:00
|
|
|
return $this->token . '::' . hash('sha256', $this->token . strtolower($USER->email));
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:38:23 +00:00
|
|
|
/**
|
2019-04-05 16:25:06 +00:00
|
|
|
* List of supported mime.
|
|
|
|
*
|
2019-02-06 13:38:23 +00:00
|
|
|
* @param $mime
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-17 16:51:22 +00:00
|
|
|
public function is_supported_mime($mime) {
|
|
|
|
return in_array($mime, array(
|
2022-09-26 10:40:25 +00:00
|
|
|
'application/msword',
|
|
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
|
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
|
|
'application/rtf',
|
|
|
|
'application/vnd.oasis.opendocument.text',
|
|
|
|
'text/plain',
|
|
|
|
'application/pdf'
|
2019-01-17 16:51:22 +00:00
|
|
|
), true);
|
|
|
|
}
|
2019-04-05 16:25:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return maximum size of document.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_max_filesize() {
|
|
|
|
return 20 * 1048576;
|
|
|
|
}
|
2019-01-17 16:51:22 +00:00
|
|
|
}
|