diff --git a/PlagScan/API.php b/PlagScan/API.php new file mode 100644 index 0000000000000000000000000000000000000000..b7518bff24da7b126d3902dc3c2a3b951c0e28d2 --- /dev/null +++ b/PlagScan/API.php @@ -0,0 +1,97 @@ + + * @since 20.08.19 + */ +class API +{ + + const HOST = 'https://api.plagscan.com/'; + const VERSION = 'v3'; + + /** + * @var string Base URL + */ + protected $connection; + + /** + * @var API + */ + protected static $instance; + + /** + * @return API + */ + public static function getInstance() : API + { + if (self::$instance === null) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * PlagScanApi constructor. + */ + public function __construct() + { + $this->connection = self::HOST . self::VERSION; + } + + /** + * Make a HTTP request to the API + * + * @param string $endPoint + * @param string $requestType + * @param string $accessToken + * @param array $data + * + * @return array|string + */ + public function request(string $endPoint, string $requestType, string $accessToken = '', array $data = []) + { + + $ch = curl_init(); + + $url = $this->connection . '/' . $endPoint; + + if ($accessToken) { + $url .= '?access_token=' . $accessToken; + } + + if ($requestType === 'GET') { + foreach ($data as $key => $value) { + $url .= '&' . $key . '=' . $value; + } + if (strpos($url, '?') === false) { + $url = preg_replace('/\&/', '?', $url, 1); + } + $data = []; + } + + $curlopt = [ + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => $requestType, + ]; + + if (!empty($data)) { + $curlopt = [CURLOPT_POSTFIELDS => $data] + $curlopt; + } + curl_setopt_array($ch, $curlopt); + + $response = curl_exec($ch); + curl_close($ch); + + $response = json_decode($response, true) ?? $response; + if (isset($response['data'])) { + return $response['data']; + } + if (isset( $response['error'])) { + return $response['error']['message']; + } + return $response; + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Base.php b/PlagScan/Helper/Base.php new file mode 100644 index 0000000000000000000000000000000000000000..851736b8a243cff3e5a438d6eb1857b30f3fc21d --- /dev/null +++ b/PlagScan/Helper/Base.php @@ -0,0 +1,69 @@ + + * @since 20.08.19 + */ +class Base +{ + /** + * @var \API + */ + protected $handler; + + /** + * @var array + */ + protected $accessToken; + + /** + * Ping constructor. + */ + public function __construct() + { + require_once __DIR__ . '/../API.php'; + $this->handler = \API::getInstance(); + $this->createAccessToken(); + } + + /** + * @return array|string + */ + public function ping() + { + return $this->handler->request('ping', 'GET'); + } + + /** + * @return array|string + */ + public function checkCallbackConfiguration() + { + return $this->handler->request('checkcallback', 'PUT', $this->accessToken); + } + + /** + * @param string $client_id + * @param string $client_secret + * @return array|string + */ + public function createAccessToken(string $client_id = '', string $client_secret = '') + { + global $DIC; + $client_id = $client_id ?? $DIC->settings()->get('psx_clientid'); + $client_secret = $client_secret ?? $DIC->settings()->get('psx_apikey'); + + $response = $this->handler->request('token', 'POST', '', [ + 'client_id' => $client_id, + 'client_secret' => $client_secret, + ]); + + if (isset($response['access_token'])) { + $this->accessToken = $response['access_token']; + } + + return $response; + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Documents.php b/PlagScan/Helper/Documents.php new file mode 100644 index 0000000000000000000000000000000000000000..df09dadb99f15dadc906770f581a48ece8fc865f --- /dev/null +++ b/PlagScan/Helper/Documents.php @@ -0,0 +1,157 @@ + + * @since 20.08.19 + */ +class Documents extends Base +{ + /** + * @param int $docID + * @return array|string + */ + public function get(int $docID) + { + return $this->handler->request('documents/' . $docID, 'GET', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function getStatus(int $docID) + { + return $this->handler->request('documents/' . $docID . '/status', 'GET', $this->accessToken); + } + + /** + * @param string $data + * @param bool $plain + * @return array|string + */ + public function add(string $fileData, bool $plain = false) + { + if ($plain) { + $data['textdata'] = $fileData; + } else { + $data['fileUpload'] = $fileData; + } + return $this->handler->request('documents', 'POST', $this->accessToken, $data); + } + + /** + * @param int $docID + * @return array|string + */ + public function delete(int $docID) + { + return $this->handler->request('documents/' . $docID, 'DELETE', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function archive(int $docID) + { + return $this->handler->request('documents/' . $docID . '/archive', 'PUT', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function check(int $docID) + { + return $this->handler->request('documents/' . $docID . '/check', 'PUT', $this->accessToken); + } + + /** + * @param int $docID + * @param int $mode + * @param int $userID + * @return array|string + */ + public function retrieveReport(int $docID, int $mode = 0, int $userID = 0) + { + $data['mode'] = $mode; + if ($userID) { + $data['userID'] = $userID; + } + return $this->handler->request('documents/' . $docID . '/retrieve', 'GET', $this->accessToken); + } + + /** + * @param int $docID + * @param string $format + * @return array|string + */ + public function retrieveAllSources(int $docID, string $format = 'old_html') + { + $data['format'] = $format; + return $this->handler->request('documents/' . $docID . '/sources', 'GET', $this->accessToken, $data); + } + + /** + * @param int $docID + * @param string $source + * @param string $format + * @return array|string + */ + public function retrieveHighlightedSources(int $docID, string $source, string $format = 'old_html') + { + $data['source'] = $source; + $data['format'] = $format; + return $this->handler->request('documents/' . $docID . '/highlight', 'GET', $this->accessToken, $data); + } + + /** + * @param int $docID + * @return array|string + */ + public function privatize(int $docID) + { + return $this->handler->request('documents/' . $docID . '/private', 'PUT', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function publish(int $docID) + { + return $this->handler->request('documents/' . $docID . '/private', 'DELETE', $this->accessToken); + } + + /** + * @param int $docID + * @param array $config + * @return array|string + */ + public function share(int $docID, array $config = []) + { + return $this->handler->request('documents/' . $docID . '/share', 'PUT', $this->accessToken, $config); + } + + /** + * @param int $docID + * @return array|string + */ + public function unshare(int $docID) + { + return $this->handler->request('documents/' . $docID . '/share', 'DELETE', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function getShareLink(int $docID) + { + return $this->handler->request('documents/' . $docID . '/share', 'GET', $this->accessToken); + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Groups.php b/PlagScan/Helper/Groups.php new file mode 100644 index 0000000000000000000000000000000000000000..da9d609b3457aea04d9ec72b953c369d1abf9ec0 --- /dev/null +++ b/PlagScan/Helper/Groups.php @@ -0,0 +1,57 @@ + + * @since 20.08.19 + */ +class Groups extends Base +{ + + /** + * @param int $groupID + * @return array|string + */ + public function get(int $groupID) + { + return $this->handler->request('groups/' . $groupID, 'GET', $this->accessToken); + } + + /** + * @param string $groupName + * @param int $groupAdminID + * @return array|string + */ + public function add(string $groupName, int $groupAdminID = 0) + { + $data['deptName'] = $groupName; + + if ($groupAdminID) { + $data['deptAdmin'] = $groupAdminID; + } + + return $this->handler->request('groups', 'POST', $this->accessToken, $data); + } + + /** + * @param int $groupID + * @param array $changes + * @return array|string + */ + public function change(int $groupID, array $changes) + { + return $this->handler->request('groups/' . $groupID, 'PATCH', $this->accessToken, $changes); + } + + /** + * @param int $groupID + * @return array|string + */ + public function delete(int $groupID) + { + return $this->handler->request('groups/' . $groupID, 'DELETE', $this->accessToken); + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Repository.php b/PlagScan/Helper/Repository.php new file mode 100644 index 0000000000000000000000000000000000000000..fb5f4a1a56f4a91bc3d7cdf20f4d7113dc62f5c0 --- /dev/null +++ b/PlagScan/Helper/Repository.php @@ -0,0 +1,30 @@ + + * @since 20.08.19 + */ +class Repository extends Base +{ + + /** + * @return array|string + */ + public function getAll() + { + return $this->handler->request('repositories/documents', 'GET', $this->accessToken); + } + + /** + * @param int $docID + * @return array|string + */ + public function delete(int $docID) + { + return $this->handler->request('submissions/documents/' . $docID, 'DELETE', $this->accessToken); + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Submissions.php b/PlagScan/Helper/Submissions.php new file mode 100644 index 0000000000000000000000000000000000000000..5a8a7499a53f317be7d94d63dd37230b8f6e0ffe --- /dev/null +++ b/PlagScan/Helper/Submissions.php @@ -0,0 +1,142 @@ + + * @since 20.08.19 + */ +class Submissions extends Base +{ + + /** + * @param int $userID + * @return array|string + */ + public function getAll(int $userID = 0) + { + $data = []; + if ($userID) { + $data['ownerID'] = $userID; + } + + return $this->handler->request('submissions', 'GET', $this->accessToken, $data); + } + + /** + * @param string $title + * @param int $maxUploads + * @param int $share + * @param int $checkDeadline + * @param int $notifyDeadline + * @param int $enableResubmission + * @param int $checkOnUpload + * @param int $type + * @param array $configuration + * @return array|string + */ + public function add( + string $title, + int $maxUploads, + int $share, + int $checkDeadline, + int $notifyDeadline, + int $enableResubmission, + int $checkOnUpload, + int $type, + array $configuration = [] + ) + { + $data['title'] = $title; + $data['maxUploads'] = $maxUploads; + $data['share'] = $share; + $data['checkDeadline'] = $checkDeadline; + $data['notifyDeadline'] = $notifyDeadline; + $data['enableResubmission'] = $enableResubmission; + $data['checkOnUpload'] = $checkOnUpload; + $data['type'] = $type; + + $data = $data + $configuration; + + return $this->handler->request('submissions', 'POST', $this->accessToken, $data); + } + + /** + * @param int $submissionID + * @param array $changes + * @return array|string + */ + public function change(int $submissionID, array $changes) + { + return $this->handler->request('submission/' . $submissionID, 'PATCH', $this->accessToken, $changes); + } + + /** + * @param int $submissionID + * @return array|string + */ + public function delete(int $submissionID) + { + return $this->handler->request('submissions/' . $submissionID, 'DELETE', $this->accessToken); + } + + /** + * @param int $submissionID + * @param int $userID + * @return array|string + */ + public function getDocuments(int $submissionID, int $userID) + { + $data['ownerID'] = $userID; + return $this->handler->request('submissions/' . $submissionID . '/documents', 'GET', $this->accessToken, $data); + } + + /** + * @param int $submissionID + * @param string $fileUpload + * @param string $title + * @param string $firstname + * @param string $lastname + * @param string $email + * @param int $sendResults + * @param int $saveOrigin + * @return array|string + */ + public function addDocument( + int $submissionID, + string $fileUpload, + string $title, + string $firstname, + string $lastname, + string $email, + int $sendResults = 0, + int $saveOrigin = 0 + ) + { + $data['fileUpload'] = $fileUpload; + $data['title'] = $title; + $data['firstname'] = $firstname; + $data['lastname'] = $lastname; + $data['email'] = $email; + $data['sendResults'] = $sendResults; + $data['saveOrig'] = $saveOrigin; + return $this->handler->request('submissions/' . $submissionID, 'POST', $this->accessToken, $data); + } + + /** + * @param int $submissionID + * @param int $docID + * @param string $fileUpload + * @param int $saveOrigin + * @return array|string + */ + public function replaceDocument(int $submissionID, int $docID, string $fileUpload, int $saveOrigin = 0) + { + $data['docID'] = $docID; + $data['fileUpload'] = $fileUpload; + $data['saveOrig'] = $saveOrigin; + return $this->handler->request('submissions/' . $submissionID, 'POST', $this->accessToken, $data); + } +} \ No newline at end of file diff --git a/PlagScan/Helper/Users.php b/PlagScan/Helper/Users.php new file mode 100644 index 0000000000000000000000000000000000000000..b1bfd3825b706bea60c21eb2a8c74bf772190ecc --- /dev/null +++ b/PlagScan/Helper/Users.php @@ -0,0 +1,131 @@ + + * @since 20.08.19 + */ +class Users extends Base +{ + + /** + * @param int $start + * @param int $limit + * @return array|string + */ + public function getAll(int $start = 0, int $limit = 0) + { + $data = []; + if ($start) { + $data['start'] = $start; + } + if ($limit) { + $data['limit'] = $limit; + } + + return $this->handler->request('users', 'GET', $this->accessToken, $data); + } + + /** + * Return user for the give Plagscan ID + * + * @param int $userID + * @return array|string + */ + public function get(int $userID) + { + return $this->handler->request('users/' . $userID, 'GET', $this->accessToken); + } + + /** + * @param string $username + * @param string $firstname + * @param string $lastname + * @param string $email + * @param int $group + * @return array|string + */ + public function add(string $username, string $firstname, string $lastname, string $email, int $group = 0) + { + $data = [ + 'username' => $username, + 'firstname' => $firstname, + 'lastname' => $lastname, + 'email' => $email + ]; + + if ($group !== 0) { + $data['deptID'] = $group; + } + + return $this->handler->request('users', 'POST', $this->accessToken, $data); + } + + /** + * @param int $userID + * @param array $changes + * @return array|string + */ + public function change(int $userID, array $changes) + { + return $this->handler->request('users/' . $userID, 'PATCH', $this->accessToken, $changes); + } + + /** + * @param int $userID + * @return array|string + */ + public function delete(int $userID) + { + return $this->handler->request('users/' . $userID, 'DELETE', $this->accessToken); + } + + /** + * @param int $userID + * @param string $shared + * @param int $start + * @param int $limit + * @param int $mode + * @return array|string + */ + public function getDocuments(int $userID, string $shared = 'withMe', int $start = 0, int $limit = 0, int $mode = 0) + { + $data['shared'] = $shared; + if ($start) { + $data['start'] = $start; + } + if ($limit) { + $data['limit'] = $limit; + } + if ($mode) { + $data['mode'] = $mode; + } + return $this->handler->request('users/' . $userID . '/documents', 'GET', $this->accessToken, $data); + } + + public function archiveDocuments(int $userID) { + return $this->handler->request('users/' . $userID . '/documents/archive', 'PUT', $this->accessToken); + } + + /** + * @param int $userID + * @param int $start + * @param int $limit + * @return array|string + */ + public function getRepositoryDocuments(int $userID, int $start = 0, int $limit = 0) + { + $data = []; + if ($start) { + $data['start'] = $start; + } + if ($limit) { + $data['limit'] = $limit; + } + + return $this->handler->request('users/' . $userID . '/repository/documents', 'GET', $this->accessToken, $data); + } +} \ No newline at end of file diff --git a/PlagScanAPI/plagscan_api.php b/PlagScanAPI/plagscan_api.php deleted file mode 100644 index 4f0f08dc74bca8e80676343c93e7b3d66efdbd80..0000000000000000000000000000000000000000 --- a/PlagScanAPI/plagscan_api.php +++ /dev/null @@ -1,182 +0,0 @@ -. - -/** - * plagscan_api.php - Class to help working with the PlagScan API. - * - * @package plagiarism_plagscan - * @subpackage plagiarism - * @author Jesús Prieto - * @copyright 2018 PlagScan GmbH {@link https://www.plagscan.com/} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace plagiarism_plagscan\classes; - -class plagscan_api { - - const API_FILES = "/documents"; - const API_USERS = "/users"; - const API_SUBMISSIONS = "/submissions"; - const API_TOKEN = "/token"; - const API_PING = "/ping"; - const API_CHECK_CALLBACK = "/checkcallback"; - const API_DEFAULT_URL = "https://api.plagscan.com/v3"; - const API_INTEGRATION_CONSUMER_KEY = "APIconsumer"; - const API_INTEGRATION_CONSUMER_VALUE = "ILIAS"; - - protected $plagscan_server; - - public function __construct($plagscan_server) - { - $this->plagscan_server = filter_var($plagscan_server, FILTER_SANITIZE_URL);; - } - - /** - * Make a HTTP request to the API - * - * @param string $endPoint - * @param string $requestType - * @param array $data - * @param array $filedata - * @param bool $urlencodeddata - * - * @return array - */ - public function request($endPoint, $requestType, $data, $filedata = null, $urlencodeddata = false) { - - $ch = curl_init(); - - $url = $this->plagscan_server . $endPoint; - - if ($endPoint != self::API_TOKEN && $endPoint != self::API_PING) { - $url .= "&" . self::API_INTEGRATION_CONSUMER_KEY . "=" . self::API_INTEGRATION_CONSUMER_VALUE; - } - - if ($urlencodeddata) { - foreach ($data as $param => $value) { - $url .="&$param=" . urlencode($value); - } - } - - if ($requestType == "POST" && $filedata != null) { - $boundary = uniqid(); - $delimiter = '-------------' . $boundary; - - $data = $this->build_data_files($boundary, $data, $filedata); - - $curlopt = array( - CURLOPT_URL => $url, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_CUSTOMREQUEST => $requestType, - CURLOPT_POST => 1, - CURLOPT_POSTFIELDS => $data, - CURLOPT_HTTPHEADER => array( - //"Authorization: Bearer $TOKEN", - "Content-Type: multipart/form-data; boundary=" . $delimiter, - "Content-Length: " . strlen($data) - ), - ); - } else if ($requestType == "PATCH") { - $curlopt = array( - CURLOPT_URL => $url, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_CUSTOMREQUEST => $requestType, - CURLOPT_HTTPHEADER => array( - //"Authorization: Bearer $TOKEN", - "Content-Type: application/json-patch+json" - ), - ); - } else { - $curlopt = array( - CURLOPT_URL => $url, - CURLOPT_CUSTOMREQUEST => $requestType, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_POSTFIELDS => $data, - ); - } - curl_setopt_array($ch, $curlopt); - - if(curl_errno($ch)){ - $pslog = array( - 'other' => [ - 'errormsg' => curl_error($ch) - ] - ); - // TODO: REWORK THIS: error_happened::create($pslog)->trigger(); - } - - $response = curl_exec($ch); - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - return $this->handle_response($response, $httpcode); - } - - /** - * Returns the response within in array with response json decoded and http code - * - * @param string $response - * @param int $httpcode - * @return array - */ - private function handle_response($response, $httpcode) { - $response = json_decode($response, true); - - return array("response" => $response, "httpcode" => $httpcode); - } - - /** - * Helps to build a HTTP content with a given files data - * - * @param string $boundary - * @param array $fields - * @param array $files - * @return string - */ - private function build_data_files($boundary, $fields, $files) { - $data = ''; - $eol = "\r\n"; - - $delimiter = '-------------' . $boundary; - - foreach ($fields as $name => $content) { - $data .= "--" . $delimiter . $eol - . 'Content-Disposition: form-data; name="' . $name . "\"" . $eol . $eol - . $content . $eol; - } - - - foreach ($files as $file) { - $data .= "--" . $delimiter . $eol - . 'Content-Disposition: form-data; name="fileUpload"; filename="' . $file->get_filename() . '"' . $eol - . 'Content-Type: ' . $file->get_mimetype() . '' . $eol - . 'Content-Transfer-Encoding: binary' . $eol - ; - - $data .= $eol; - $data .= $file->get_content() . $eol; - } - $data .= "--" . $delimiter . "--" . $eol; - - - return $data; - } - -} \ No newline at end of file diff --git a/PlagScanAPI/plagscan_connection.php b/PlagScanAPI/plagscan_connection.php deleted file mode 100644 index 593cad0a06846cca01cbc028f60fee5923b2039f..0000000000000000000000000000000000000000 --- a/PlagScanAPI/plagscan_connection.php +++ /dev/null @@ -1,849 +0,0 @@ -. - -namespace plagiarism_plagscan\classes; - -require_once'./Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/PlagScanExercise/PlagScanAPI/plagscan_api.php'; - -/** - * plagscan_connection.php - Class that defines some of the functionalities of the PlagScan Plagiarism plugin - * - * @package plagiarism_plagscan - * @subpackage plagiarism - * @author Jesús Prieto (Based on the work of Ruben Olmedo ) - * @copyright 2018 PlagScan GmbH {@link https://www.plagscan.com/} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class plagscan_connection { - - const SUBMIT_OK = 0; - const SUBMIT_UNSUPPORTED = 1; - const SUBMIT_OPTOUT = 2; - const SUBMIT_FAILED_BY_CONNECTION = 3; - - /** - * Defines the configuration of the module or assignment - * - * @var array $config - */ - protected $config; -/* -$this->config->plagscan_admin_email $this->config['plagscan_admin_email'] -$this->config->plagscan_multipleaccounts $this->config['plagscan_multipleaccounts'] - */ - /** - * Username of the user using the plugin - * - * @var string $username - */ - protected $username = -1; // TODO: Clean type - - /**@var bool $nondisclosure */ - protected $nondisclosure = false; - - /** - * Constructor of the plagscan_connection class - * - * @param bool $notinstance - */ - public function __construct($config, $notinstance = false ) { - $this->config = $config; - if ($notinstance) { - $this->username = false; - } - } - - /** - * Returns an array with the mapped user settings for PlagScan - * - * @return array - */ - public function get_user_settings_mapping() { - return array('plagscan_language' => 'language', - 'plagscan_email_policy' => 'emailPolicy', - 'plagscan_web' => 'checkWeb', - 'plagscan_own_workspace' => 'checkOwnWorkspace', - 'plagscan_own_repository' => 'checkOwnRepository', - 'plagscan_orga_repository' => 'checkOrgaRepository', - 'plagscan_plagiarism_prevention_pool' => 'checkPS', - 'plagscan_autodel' => 'cleanupPolicy', - 'plagscan_docx' => 'docxPolicy', - 'plagscan_ssty' => 'detailPolicy', - 'plagscan_red' => 'redLevel', - 'plagscan_yellow' => 'yellowLevel'); - } - - /** - * Enables nondisclosure - */ - public function enable_nondisclosure() { - $this->nondisclosure = true; - } - - /** - * Set username - * - * @param string $username - */ - public function set_username($username) { - $this->username = $username; - } - - /** - * Get username - * - * @param bool $adminuser - * @param string $originaluser - * @param bool $tryfallback - * @return string - */ - protected function get_username($adminuser = false, $originaluser = '', $tryfallback = false) { - if ($adminuser) { - return $this->config['plagscan_admin_email']; // Admin request - always send the admin user. - } - - if (!empty($originaluser)) { - return $originaluser; // File already has a user associated with it - use this. - } - - if (empty($this->config['plagscan_multipleaccounts']) && !$this->nondisclosure) { - // Using the global user account. - if (!$tryfallback) { // Fallback = try local account instead. - return $this->config['plagscan_admin_email']; - } - } - - // Check the local username is OK - /* if ($this->username === -1) { - throw new coding_exception("Must call 'set_username' if associated with a particular instance"); - } */ - - if (empty($this->username)) { - return $this->config['plagscan_admin_email']; // No local username - just return the global account. - } - - if (!$tryfallback || empty($this->config['plagscan_multipleaccounts'])) { - return $this->username; // Default for 'use local username' + fallback for 'use global account' - } else { - return $this->config['plagscan_admin_email']; // Fallback for 'use local username' - } - } - - /** - * Update the module config and status - * - * @param int $cmid - */ - public function update_module_status($cmid) { - global $DB, $CFG; - - $files = $DB->get_records_select('plagiarism_plagscan', 'cmid = ? AND (status != ? OR pstatus IS null)', array($cmid, plagscan_file::STATUS_FINISHED)); - foreach ($files as $file) { - plagscan_file::update_status($file); - } - } - - /** - * Deletes the document already submitted and overwrites it for the new one. - * - * @param int $pid - * @return boolean - */ - public function delete_submitted_file($pid) { - - $access_token = $this->get_access_token(); - $url = plagscan_api::API_FILES . "/$pid?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "DELETE", null); - - if ($res["httpcode"] != 204) { - return false; - } - - return true; - } - - /** - * Returns the PlagScan settings from the user - * - * @param \stdClass $user - * @return array - */ - public function get_user_settings($user) { - - $psuserid = $this->find_user($user); - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_USERS . "/$psuserid?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "PATCH", null); - - return $res["response"]["data"]; - } - - /** - * Sets the PlagScan settings from the user - * - * @param int $user - * @param array $settings - * @return boolean - */ - public function set_user_settings($user, $settings) { - // Send the setting to the plagscan server - - $psuserid = $this->find_user($user); - - if ($psuserid == NULL) { - $psuserid = $connection->add_new_user($user); // This never worked. - } - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_USERS . "/$psuserid?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "PATCH", $settings, null, true); - - if ($res["httpcode"] != 200 || !isset($res["response"]["data"])) { - return false; - } - - - return true; - } - - /** - * Get an access token to make request to the API - * - * @param int $psclient - * @param string $pskey - * @return string - */ - function get_access_token($psclient = null, $pskey = null) { - if ($psclient == null) { - $psclient = get_config('plagiarism_plagscan', 'plagscan_id'); // TODO: REWORK - } - - if ($pskey == null) { - $pskey = get_config('plagiarism_plagscan', 'plagscan_key'); // TODO: REWORK - } - - $data = ["client_id" => $psclient, "client_secret" => $pskey]; - $token = NULL; - - $res = plagscan_api::instance()->request(plagscan_api::API_TOKEN, "POST", $data); - if (isset($res["response"]["access_token"])) { - $token = $res["response"]["access_token"]; - } - - return $token; - } - - /** - * Submits a document into a PlagScan submission - * - * @param array $filedata - * @return int - */ - public function submit_into_submission($filedata) { - $docid = -1; - - $access_token = $this->get_access_token(); - - $submissionid = $filedata["submissionid"]; - - $data = ["submissionID" => $submissionid, - "ownerID" => $filedata["ownerid"], - "submitterID" => $filedata["submitterid"], - "title" => $filedata["filename"], - "firstname" => $filedata["firstname"], - "lastname" => $filedata["lastname"], - "email" => $filedata["email"], - "sendResults" => "0", - "toRepository" => false]; - - $files = array($filedata["file"]); - - - $url = plagscan_api::API_SUBMISSIONS . "/" . $submissionid . "?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "POST", $data, $files); - - if ($res["httpcode"] == 201) { - $docid = $res["response"]["data"]["docID"]; - } - - return $docid; - } - - /** - * Submits a single document into PlagScan - * - * @param array $filedata - * @return int - */ - public function submit_single_file($filedata) { - $docid = -1; - - $access_token = $this->get_access_token(); - - $data = ["userID" => $filedata["ownerid"], - "toRepository" => false]; - - - $files = array($filedata["file"]); - - - $url = plagscan_api::API_FILES . "?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "POST", $data, $files); - - if ($res["httpcode"] == 201) { - $docid = $res["response"]["data"]["docID"]; - } - - return $docid; - } - - /** - * Analyzes the document checking for plagiarism and generates the report - * - * @param string $access_token - * @param int $pid - * @return string - */ - public function analyze($pid) { - global $DB; // TODO: REWORK - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_FILES . "/" . $pid . "/check?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "PUT", null); - - if ($res["httpcode"] == 204) { - $current = $DB->get_record('plagiarism_plagscan', array('pid' => $pid)); - $current->status = 1; - $DB->update_record('plagiarism_plagscan', $current); - return null; - } else { - if (isset($res["response"]["error"])) { - return $res["response"]["error"]["message"]; - } else { - return $res["response"]; - } - } - } - - /** - * Retrieves the plagiarism percentage result from the report - * - * @param int $pid - * @return int - */ - public function plaglevel_retrieve($pid) { - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_FILES . "/$pid/retrieve?access_token=$access_token&mode=0"; - - $res = plagscan_api::instance()->request($url, "GET", null); - - $httpcode = $res["httpcode"]; - - if ($httpcode != 200) { - $plaglevel = -1; - } else { - $plaglevel = $res["response"]["data"]["plagLevel"]; - } - - return $plaglevel; - } - - /** - * Retrieves the link to access to the report - * - * @param int $pid - * @param \stdClass $user - * @param int $mode - * @return array - */ - public function report_retrieve($pid, $user, $mode) { - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_FILES . "/$pid/retrieve?mode=" . $mode . "&access_token=$access_token"; - - if ($mode == 10) { - $url .= "&userID=" . $user->psuserid; - } - - $res = plagscan_api::instance()->request($url, "GET", null); - - if ($res["httpcode"] != 200) { - if (isset($res["response"]["error"])) { - return $res["response"]["error"]; - } else { - return null; - } - } else { - return $res["response"]["data"]; - } - } - - /** - * Creates the submission on PlagScan - * - * @param int $cmid - * @param \stdClass $module - * @param \stdClass $config - * @param \stdClass $user - * @return int - */ - function create_submissionid($cmid, $module, $config, $user) { - global $DB, $CFG; - //733-745lines are repeated from function get_links() - $modulesql = 'SELECT m.id, m.name, cm.instance' . - ' FROM {course_modules} cm' . - ' INNER JOIN {modules} m on cm.module = m.id ' . - 'WHERE cm.id = ?'; - $moduledetail = $DB->get_record_sql($modulesql, array($cmid)); - if (!empty($moduledetail)) { - $sql = "SELECT * FROM " . $CFG->prefix . $moduledetail->name . " WHERE id= ?"; - $module = $DB->get_record_sql($sql, array($moduledetail->instance)); - // print_r($module); - - $data = $DB->get_record('plagiarism_plagscan_config', array('cm' => $cmid)); - // print_r($data); - } - - $psuserid = $this->find_user($user); - if ($psuserid == null) { - $psuserid = $this->add_new_user($user); - } - - $title = $module->name; - $maxuplaods = '100'; - $share = 1; - $checkdeadline = 0; - $enableresub = 1; - $checkonupload = 0; - $type = 1; - - //start manually - if ($config->upload == 1) { - $checkdeadline = 0; - $checkonupload = 0; - } - //start immediately - else if ($config->upload == 3) { - $checkonupload = 1; - } - - - $endtime = null; - //start after due date - if ($config->upload == 2 || $config->upload == 4) { - $checkdeadline = 1; - //check date - if ($config->upload == 2 && $module->duedate > 0) { - $endtime = date('d-m-Y H:i:s', $module->duedate); - } else if ($module->cutoffdate > 0) { - $endtime = date('d-m-Y H:i:s', $module->cutoffdate); - } else if ($module->duedate > 0) { - $endtime = date('d-m-Y H:i:s', $module->duedate); - } - } - - $data = ["ownerID" => $psuserid, - "title" => $title, - "endTime" => "" . $endtime, - "maxUploads" => $maxuplaods, - "share" => $share, - "checkDeadline" => $checkdeadline, - "enableResubmission" => $enableresub, - "checkOnUpload" => $checkonupload, - "type" => $type]; - - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_SUBMISSIONS . "?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "POST", $data); - - return $res["response"]["data"]["submissionID"]; - } - - /** - * Updates the submission in PlagScan - * - * @param int $cmid - * @param \stdClass $module - * @param \stdClass $config - * @param int $assign_id - * @param \stdClass $user - * @return bool - */ - function update_submission($cmid, $module, $config, $assign_id, $user) { - global $DB, $CFG; - //733-745lines are repeated from function get_links() - $modulesql = 'SELECT m.id, m.name, cm.instance' . - ' FROM {course_modules} cm' . - ' INNER JOIN {modules} m on cm.module = m.id ' . - 'WHERE cm.id = ?'; - $moduledetail = $DB->get_record_sql($modulesql, array($cmid)); - if (!empty($moduledetail)) { - $sql = "SELECT * FROM " . $CFG->prefix . $moduledetail->name . " WHERE id= ?"; - $module = $DB->get_record_sql($sql, array($moduledetail->instance)); - // print_r($module); - - $data = $DB->get_record('plagiarism_plagscan_config', array('cm' => $cmid)); - // print_r($data); - } - - $psuserid = $this->find_user($user); - - if ($psuserid == null) { - $psuserid = $this->add_new_user($user); - } - - $title = $module->name; - $checkdeadline = 0; - $enableresub = 1; - $checkonupload = 0; - - //start manually - if ($config->upload == 1) { - $checkdeadline = 0; - $checkonupload = 0; - } - //start immediately - else if ($config->upload == 3) { - $checkonupload = 1; - } - - - $endtime = null; - //start after due date - if ($config->upload == 2 || $config->upload == 4) { - $checkdeadline = 1; - //check date - if ($config->upload == 2 && $module->duedate > 0) { - $endtime = date('d-m-Y H:i:s', $module->duedate); - } else if ($module->cutoffdate > 0) { - $endtime = date('d-m-Y H:i:s', $module->cutoffdate); - } else if ($module->duedate > 0) { - $endtime = date('d-m-Y H:i:s', $module->duedate); - } - } - - $data = ["ownerID" => $psuserid, - "title" => $title, - "endTime" => "" . $endtime, - "checkDeadline" => $checkdeadline, - "enableResubmission" => $enableresub, - "checkOnUpload" => $checkonupload]; - - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_SUBMISSIONS . "/$assign_id?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "PATCH", $data, null, true); - - return true; - } - - /** - * Creates the user in PlagScan - * - * @param \stdClass $user - * @return int - */ - function add_new_user($user) { - global $DB; - - $access_token = $this->get_access_token(); - - $userlog = array( - 'context' => \context_system::instance(), - 'userid' => $user->id - ); - - user_creation_started::create($userlog)->trigger(); - - $data = ["email" => $user->email, "username" => $user->email, "firstname" => $user->firstname, "lastname" => $user->lastname]; - - $url = plagscan_api::API_USERS . "?access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "POST", $data); - - $psid = 0; - if (isset($res["response"]["data"]["userID"])) { - $psid = intval($res["response"]["data"]["userID"]); - } - - if ($psid <= 0) { - throw new moodle_exception('error_user_creation', 'plagiarism_plagscan'); - } - - $insert = new \stdClass(); - $insert->userid = $user->id; - $insert->psuserid = $psid; - - $DB->insert_record('plagiarism_plagscan_user', $insert); - $userlog['other']['psuserid'] = $psid; - user_creation_completed::create($userlog)->trigger(); - return $psid; - } - - /** - * Checks if the user exist already in PlagScan - * - * @param \stdClass $user - * @return int - */ - function find_user($user) { - global $DB; - //First check if the user is registered in the Moodle DB with the PS id - $psuser = $DB->get_record("plagiarism_plagscan_user", array("userid" => $user->id)); - $psuserid = null; - - if ($psuser) { - $psuserid = $psuser->psuserid; - } else { - $access_token = $this->get_access_token(); - - $userlog = array( - 'context' => \context_system::instance(), - 'userid' => $user->id - ); - - user_search_started::create($userlog)->trigger(); - - $url = plagscan_api::API_USERS . "?access_token=$access_token&searchByEmail=$user->email"; - - $res = plagscan_api::instance()->request($url, "GET", null); - - $psuserid = 0; - if (isset($res["response"]["data"]["userID"])) { - $psuserid = $res["response"]["data"]["userID"]; - } - - if (intval($psuserid) > 0) { - $insert = new \stdClass(); - $insert->userid = $user->id; - $insert->psuserid = $psuserid; - $DB->insert_record('plagiarism_plagscan_user', $insert); - - $userlog['other']['psuserid'] = $psuserid; - user_search_completed::create($userlog)->trigger(); - } - } - - return $psuserid; - } - - /** - * Check if the user is involved in the PlagScan submission - * - * @param \stdClass $context - * @param \stdClass $assignment - * @param \stdClass $user - * @return boolean - */ - public function is_assistant($context, $assignment, $user) { - $is_assistant = false; - - if (has_capability('plagiarism/plagscan:control', $context) && $user->id != $assignment->ownerid) { - $is_assistant = true; - } - - return $is_assistant; - } - - /** - * Involves the user in the PlagScan submission - * - * @param \stdClass $assign - * @param int $assign_psownerid - * @param \stdClass $involved_user - * @return boolean - * @throws moodle_exception - */ - public function involve_assistant($assign, $assign_psownerid, $involved_user) { - $access_token = $this->get_access_token(); - - $involved_psuserid = $this->find_user($involved_user); - - if ($involved_psuserid == null) { - $involved_psuserid = $this->add_new_user($involved_user); - } - - $url = plagscan_api::API_SUBMISSIONS . "/$assign->submissionid/involve?userID=$involved_psuserid&ownerID=$assign_psownerid&shareMode=4&access_token=" . $access_token; - - $res = plagscan_api::instance()->request($url, "GET", null); - - $is_involved = $res["response"]["data"]["involved"]; - - if (!$is_involved) { - $res = plagscan_api::instance()->request($url, "PUT", null); - - if ($res["httpcode"] == 400) { - throw new moodle_exception('errorinvolvingassistant', 'plagiarism_plagscan'); - return false; - } - } - - return true; - } - - /** - * Send a request to check the callback configuration from the PlagScan admin account - */ - public function check_callback_config() { - $access_token = $this->get_access_token(); - - $url = plagscan_api::API_CHECK_CALLBACK . "?access_token=$access_token"; - - return plagscan_api::instance()->request($url, "PUT", null); - } - - /** - * Send a request to check the connection is with the PlagScan API - */ - public function check_connection_to_plagscan_server(){ - $url = "/ping"; - - return plagscan_api::instance()->request($url, "GET", null); - } - - /** - * Check the status of the files from an array of PlagScan file ids. It returns an array with one pair, file id and message/content - * - * @param array $psfiles - * @param \stdClass - * @return array - */ - public function check_report_status($psfiles, $context, $viewlinks, $showlinks, $viewreport, $ps_yellow_level, $ps_red_level) { - - $psfiles = plagscan_file::find_by_psids($psfiles); - - $results = array(); - - foreach ($psfiles as $psfile) { - $message = $this->get_message_view_from_report_status($psfile, $context, $viewlinks, $showlinks, $viewreport, $ps_yellow_level, $ps_red_level); - if ($message != "") { - array_push($results, array("id" => $psfile->id, "content" => $message)); - } - } - return $results; - } - - public function get_message_view_from_report_status($psfile, $context, $viewlinks, $showlinks, $viewreport, $ps_yellow_level, $ps_red_level) { - global $PAGE; - - $message = ""; - - //create $message - if (!$psfile) { - $message = get_string('notsubmitted', 'plagiarism_plagscan'); - } else { - - $message .= "
"; - - if ($psfile->status >= plagscan_file::STATUS_FAILED) { - if ($psfile->status == plagscan_file::STATUS_FAILED_FILETYPE) { - $message .= get_string('unsupportedfiletype', 'plagiarism_plagscan'); - } elseif ($psfile->status == plagscan_file::STATUS_FAILED_OPTOUT) { - $message .= get_string('wasoptedout', 'plagiarism_plagscan'); - } else if ($psfile->status == plagscan_file::STATUS_FAILED_CONNECTION) { - $message .= get_string('serverconnectionproblem', 'plagiarism_plagscan'); - } else { // STATUS_FAILED_UNKNOWN - $message .= get_string('serverrejected', 'plagiarism_plagscan'); - } - } else if ($psfile->status != plagscan_file::STATUS_FINISHED) { - - if ($psfile->status == plagscan_file::STATUS_SUBMITTING || $psfile->status == plagscan_file::STATUS_CHECKING || $psfile->status == plagscan_file::STATUS_ONGOING || $psfile->status == plagscan_file::STATUS_QUEUED) { - if ($viewreport || $viewlinks) { - $message .= ""; - if ($psfile->status == plagscan_file::STATUS_SUBMITTING) { - $message .= get_string('process_uploading', 'plagiarism_plagscan'); - } else { - $message .= get_string('process_checking', 'plagiarism_plagscan'); - } - $message .= ""; - $message .= html_writer::empty_tag('br'); - $message .= ""; - } - } else { - $message .= get_string('notprocessed', 'plagiarism_plagscan'); - - - if ($viewlinks) { - // $message .= html_writer::empty_tag('br'); - //$message .= ' '.html_writer::link($checkurl, get_string('checkstatus', 'plagiarism_plagscan')); if($psfile->pstatus == -2) - if($psfile->pstatus == -2) - $message.=html_writer::tag('i', '', array('title' => get_string('report_check_error_cred','plagiarism_plagscan'), - 'class' => 'fa fa-exclamation-triangle', 'style' => 'color:#f0ad4e')); - $message .= html_writer::empty_tag('br'); - $submiturl = new moodle_url('/plagiarism/plagscan/reports/analyze.php', array('pid' => s($psfile->pid), - 'return' => urlencode($PAGE->url))); - $message .= html_writer::link($submiturl, get_string('check', 'plagiarism_plagscan')); - - $message .= html_writer::empty_tag('br'); - } - } - - if ($psfile->pid > 0) { - $checkurl = new moodle_url('/plagiarism/plagscan/reports/check_status.php', array('pid' => s($psfile->pid), 'return' => urlencode($PAGE->url))); - if ($viewlinks) { - $message .= ' ' . html_writer::link($checkurl, get_string('checkstatus', 'plagiarism_plagscan')); - } - } - } else { - $percent = ''; - if (!is_null($psfile->pstatus)) { - $percentclass = 'plagscan_good'; - if ($psfile->pstatus > ($ps_red_level / 10)) { - $percentclass = 'plagscan_bad'; - } else if ($psfile->pstatus > ($ps_yellow_level / 10)) { - $percentclass = 'plagscan_warning'; - } - // $percent = html_writer::tag('span', sprintf('%0.1f%%', $psfile->pstatus), array('class' => $percentclass)); - } - $psurl = new moodle_url('/plagiarism/plagscan/reports/view.php', array('pid' => s($psfile->pid), 'return' => urlencode($PAGE->url))); - $pslink = html_writer::link($psurl, html_writer::tag('span', sprintf('%0.1f%%', $psfile->pstatus), array('id' => s($psfile->pid), 'class' => $percentclass)), array('target' => '_blank')); - $pslink .= "
"; - - if (($viewlinks || has_capability('plagiarism/plagscan:viewfullreport', $context))) { - $message .= $pslink; - } else { - if (!$showlinks) { - $message .= html_writer::tag('span', sprintf('%0.1f%%', s($psfile->pstatus)), array('class' => $percentclass)); - } else { - $message .= $pslink; - } - } - $message .= html_writer::empty_tag('br'); - } - $message .="
"; - } - - return $message; - } - -} \ No newline at end of file diff --git a/PlagScanAPI/plagscan_file.php b/PlagScanAPI/plagscan_file.php deleted file mode 100644 index f4d2075b93d5b3ca022dd5e49d6168fadc5d3383..0000000000000000000000000000000000000000 --- a/PlagScanAPI/plagscan_file.php +++ /dev/null @@ -1,423 +0,0 @@ -. - -/** - * plagscan_file.php - This class help to manage the documents submitted to PlagScan through the plugin - * - * @package plagiarism_plagscan - * @subpackage plagiarism - * @author Jesús Prieto - * @copyright 2018 PlagScan GmbH {@link https://www.plagscan.com/} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace plagiarism_plagscan\classes; - -use plagiarism_plagscan\classes\plagscan_connection; -use moodle_exception; - -require_once($CFG->dirroot . '/plagiarism/plagscan/lib.php'); - - -class plagscan_file { - - /** - * STATUS_NOT_STARTED - */ - const STATUS_NOT_STARTED = 0; - - /** - * STATUS_CHECKING - */ - const STATUS_CHECKING = 1; - - /** - * STATUS_ONGOING - */ - const STATUS_ONGOING = 2; - - /** - * STATUS_FINISHED - */ - const STATUS_FINISHED = 3; - - /** - * STATUS_QUEUED - */ - const STATUS_QUEUED = 4; - - /** - * STATUS_SUBMITTING - */ - const STATUS_SUBMITTING = 100; - - /** - * STATUS_FAILED - */ - const STATUS_FAILED = 1000; - - /** - * STATUS_FAILED_FILETYPE - */ - const STATUS_FAILED_FILETYPE = 1001; - - /** - * STATUS_FAILED_UNKNOWN - */ - const STATUS_FAILED_UNKNOWN = 1002; - - /** - * STATUS_FAILED_OPTOUT - */ - const STATUS_FAILED_OPTOUT = 1003; - - /** - * STATUS_FAILED_CONNECTION - */ - const STATUS_FAILED_CONNECTION = 1004; - - /** - * REPORT_STATS - */ - const REPORT_STATS = 0; - - /** - * REPORT_LINKS - */ - const REPORT_LINKS = 1; - - /** - * REPORT_SOURCES - */ - const REPORT_SOURCES = 2; - - /** - * REPORT_DOCX - */ - const REPORT_DOCX = 3; - - /** - * REPORT_HTML - */ - const REPORT_HTML = 4; - - /** - * REPORT_MATCHES - */ - const REPORT_MATCHES = 5; - - /** - * REPORT_PS - */ - const REPORT_PS = 6; - - /** - * NEW_REPORT - */ - const NEW_REPORT = 7; - //const REPORT_RESERVED = 7; - /** - * REPORT_PDFHTML - */ - const REPORT_PDFHTML = 8; - - /** - * REPORT_PDFREPORT - */ - const REPORT_PDFREPORT = 9; - - /** - * REPORT_HIGHLIGHT - */ - const REPORT_HIGHLIGHT = 25; - - /** - * REPORT_GETSOURCE - */ - const REPORT_GETSOURCE = 26; - - /** - * Check if the file/online text content exist on the PlagScan table - * - * @param int $cmid - * @param int $userid - * @param string $filehash - * @return \stdClass - */ - public static function find($cmid, $userid, $filehash) { - global $DB; - - return $DB->get_record('plagiarism_plagscan', array('cmid' => $cmid, - 'userid' => $userid, - 'filehash' => $filehash)); - } - - public static function find_by_psids($ids) { - global $DB; - - return $DB->get_records_list('plagiarism_plagscan', 'id', $ids); - } - - /** - * Saves the file data on the PlagScan table - * - * @param \stdClass $file - * @return int - */ - public static function save($file) { - global $DB; - - return $DB->insert_record('plagiarism_plagscan', $file); - } - - /** - * Updates the file data on the PlagScan table - * - * @param \stdClass $file - * @return int - */ - public static function update($file) { - global $DB; - - return $DB->update_record('plagiarism_plagscan', $file); - } - - /** - * Deltes the file data on the PlagScan table - * - * @param \stdClass $file - * @return int - */ - public static function delete($file){ - global $DB; - - return $DB->delete_records('plagiarism_plagscan', array('id' => $file->id)); - } - - /** - * Returns an array with the supported filetypes in PlagScan - * - * @param string $filename - * @return bool - */ - public static function plagscan_supported_filetype($filename) { - $allowedtypes = array('docx', 'doc', 'pdf', 'txt', 'html', 'wps', 'wpd', - 'odt', 'ott', 'rtf', 'sdw', 'sxw', 'xml', 'pdb', 'ltx', 'pages', 'key', 'numbers'); - $extn = pathinfo($filename, PATHINFO_EXTENSION); - - return in_array($extn, $allowedtypes); - } - - /** - * Submit a file/online text content to PlagScan - * - * @param \stdClass $psfile - * @param array $filedata - * @return int - * @throws moodle_exception - */ - public static function submit($psfile, $filedata) { - global $DB, $CFG; - - $connection = new plagscan_connection(); - - - if (!self::plagscan_supported_filetype($filedata['filename'])) { - self::set_status($psfile, self::STATUS_FAILED_FILETYPE); - return plagscan_connection::SUBMIT_UNSUPPORTED; // Unsupported file type. - } - - if (plagscan_user_opted_out($psfile->userid)) { - self::set_status($psfile, self::STATUS_FAILED_OPTOUT); - return plagscan_connection::SUBMIT_OPTOUT; // User has opted-out of PlagScan uploads. - } - - /* // Delete any existing reports / records for this file - // mostly works with re_submit - $oldrecords = $DB->get_records('plagiarism_plagscan', array('cmid' => $psfile->cmid, - 'userid' => $psfile->userid, - 'filehash' => $psfile->filehash, 'submissiontype' => $psfile->submissiontype), 'id'); - - foreach ($oldrecords as $oldrecord) { - self::delete_reports($psfile->cmid, $oldrecord->id); - - if (!$connection->delete_submitted_file($oldrecord->pid)) { - throw new moodle_exception('oldsubmission_notdeleted', 'plagiarism_plagscan'); - } - $DB->delete_records('plagiarism_plagscan', array('id' => $oldrecord->id)); - } */ - - try { - //Check if the assignment was created from a previous versions without creating it on PS too - if ($filedata['submissionid'] == null) { - $result = $connection->submit_single_file($filedata); - } else { - $result = $connection->submit_into_submission($filedata); - } - } catch (moodle_exception $e) { - self::set_status($psfile, self::STATUS_FAILED_CONNECTION); - throw($e); - } - - - if ($result <= 0) { - self::set_status($psfile, self::STATUS_FAILED_UNKNOWN); - return plagscan_connection::SUBMIT_FAILED_BY_CONNECTION; - } - - $psfile->pid = intval($result); - self::update($psfile); - - return plagscan_connection::SUBMIT_OK; - } - - - /** - * Deletes previously saved reports for the file - * - * @global \stdClass $CFG - * @param int $cmid - * @param int $pid - */ - protected static function delete_reports($cmid, $pid = false) { - global $CFG; - - if ($CFG->version < 2011120100) { - $context = get_context_instance(CONTEXT_MODULE, $cmid); - } else { - $context = \context_module::instance($cmid); - } - - $fs = get_file_storage(); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_DOCX), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_HTML), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_MATCHES), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_PS), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_PDFHTML), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_PDFREPORT), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_HIGHLIGHT), $pid); - $fs->delete_area_files($context->id, 'plagiarism_plagscan', self::file_area_from_type(self::REPORT_GETSOURCE), $pid); - } - - /** - * Returns an array with the file area mapping from type of report - * - * @param int $reporttype - * @return string - */ - protected static function file_area_from_type($reporttype) { - $mapping = array(self::REPORT_DOCX => 'reportdocx', - self::REPORT_HTML => 'reporthtml', - self::REPORT_MATCHES => 'reportmatches', - self::REPORT_PS => 'reportps', - self::REPORT_PDFHTML => 'reportpdfhtml', - self::REPORT_PDFREPORT => 'reportpdfreport', - self::REPORT_HIGHLIGHT => 'reporthighlight', - self::REPORT_GETSOURCE => 'reportgetsource', - self::NEW_REPORT => 'new_report'); - - return $mapping[$reporttype]; - } - - /** - * Set the status of a file - * - * @param \stdClass $psfile - * @param \stdClass $status - */ - protected static function set_status($psfile, $status) { - global $DB; - - $current = $DB->get_record('plagiarism_plagscan', array('userid' => $psfile->userid, - 'cmid' => $psfile->cmid, - 'filehash' => $psfile->filehash)); - if ($current) { - if ($status != $current->status) { - $upd = new \stdClass(); - $upd->id = $current->id; - $upd->status = $status; - $DB->update_record('plagiarism_plagscan', $upd); - } - } else { - $psfile->status = $status; - $psfile->pid = 0; - $psfile->id = $DB->insert_record('plagiarism_plagscan', $psfile); - } - } - - /** - * Updates the status of a file - * - * @param \stdClass $psfile - * @return boolean - */ - public static function update_status($psfile) { - global $DB; - if ($psfile->status == self::STATUS_FINISHED && !is_null($psfile->pstatus) && !$psfile->updatestatus) { - // Don't retrieve status if already finished - return false; - } - - $pid = $psfile->pid; - if ($pid <= 0) { - // File was not submitted properly in the first place - skip it. - return false; - } - - $connection = new plagscan_connection(); - $result = $connection->plaglevel_retrieve($pid); - - $pstatus = null; - if (intval($result) >= 0) { - $status = self::STATUS_FINISHED; - if (empty($result)) { - $pstatus = null; - } else { - $pstatus = floatval($result) / 10; - } - } else - return false; - - if ($status == $psfile->status && $pstatus == $psfile->pstatus && !$psfile->updatestatus) { - return false; // Nothing has changed - } - - $update = new \stdClass(); - $update->status = $status; - $update->pstatus = $result; - $update->pid = $psfile->pid; - $update->updatestatus = 0; - - if (isset($psfile->id)) { - $update->id = $psfile->id; - self::update($update); - - // Ready to be returned - $update->userid = $psfile->userid; - $update->cmid = $psfile->cmid; - $update->filehash = $psfile->filehash; - } else { - $update->userid = $psfile->userid; - $update->cmid = $psfile->cmid; - $update->filehash = $psfile->filehash; - $update->id = self::save($update); - } - - return true; - } - -} \ No newline at end of file