-
Notifications
You must be signed in to change notification settings - Fork 65
/
turnitintooltwo_comms.class.php
209 lines (177 loc) · 6.97 KB
/
turnitintooltwo_comms.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?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/>.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/sdk/api.class.php');
require_once(__DIR__.'/turnitintooltwo_perflog.class.php');
class turnitintooltwo_comms {
private $tiiaccountid;
private $tiiapiurl;
private $tiisecretkey;
private $tiiintegrationid;
private $diagnostic;
private $langcode;
public function __construct($accountid = null, $accountshared = null, $url = null) {
$config = turnitintooltwo_admin_config();
$tiiapiurl = (substr($config->apiurl, -1) == '/') ? substr($config->apiurl, 0, -1) : $config->apiurl;
$this->tiiintegrationid = 12;
$this->tiiaccountid = is_null($accountid) ? $config->accountid : $accountid;
$this->tiiapiurl = is_null($url) ? $tiiapiurl : $url;
$this->tiisecretkey = is_null($accountshared) ? $config->secretkey : $accountshared;
if (empty($this->tiiaccountid) || empty($this->tiiapiurl) || empty($this->tiisecretkey)) {
turnitintooltwo_print_error( 'configureerror', 'turnitintooltwo' );
}
$this->diagnostic = $config->enablediagnostic;
$this->langcode = $this->get_lang();
}
/**
* Initialise the API object
*
* @return object \APITurnitin
*/
public function initialise_api( $testingconnection = false ) {
global $CFG, $tiipp;
$api = new TurnitinAPI($this->tiiaccountid, $this->tiiapiurl, $this->tiisecretkey,
$this->tiiintegrationid, $this->langcode);
// Enable logging if diagnostic mode is turned on.
if ($this->diagnostic) {
$api->setLogPath($CFG->tempdir.'/turnitintooltwo/logs/');
}
// Use Moodle's proxy settings if specified.
if (!empty($CFG->proxyhost)) {
$api->setProxyHost($CFG->proxyhost);
}
if (!empty($CFG->proxyport)) {
$api->setProxyPort($CFG->proxyport);
}
if (!empty($CFG->proxyuser)) {
$api->setProxyUser($CFG->proxyuser);
}
if (!empty($CFG->proxypassword)) {
$api->setProxyPassword($CFG->proxypassword);
}
if (!empty($CFG->proxytype)) {
$api->setProxyType($CFG->proxytype);
}
if (!empty($CFG->proxybypass)) {
$api->setProxyBypass($CFG->proxybypass);
}
$pluginversion = turnitintooltwo_get_version();
$api->setIntegrationVersion($CFG->version);
$api->setPluginVersion($pluginversion);
if (is_readable("$CFG->dataroot/moodleorgca.crt")) {
$certificate = realpath("$CFG->dataroot/moodleorgca.crt");
$api->setSSLCertificate($certificate);
}
// Offline mode provided by Androgogic.
if (!empty($CFG->tiioffline) && !$testingconnection && empty($tiipp->in_use)) {
turnitintooltwo_print_error('turnitintoolofflineerror', 'turnitintooltwo');
}
$api->setTestingConnection($testingconnection);
$api->setPerformanceLog(new turnitintooltwo_performancelog());
return $api;
}
/**
* Log API exceptions and print error to screen if required
*
* @param object $e
* @param string $tterrorstr
* @param boolean $toscreen
* @param boolean $embedded
*/
public static function handle_exceptions($e, $tterrorstr = "", $toscreen = true, $embedded = false) {
global $CFG;
$errorstr = "";
if (!empty($tterrorstr)) {
$errorstr = get_string($tterrorstr, 'turnitintooltwo')."<br/><br/>";
if ($embedded == true) {
$errorstr .= get_string('tii_submission_failure', 'turnitintooltwo')."<br/><br/>";
}
}
// Show full debugging information if developer mode is on.
if ($CFG->debug == DEBUG_DEVELOPER) {
if (is_callable(array($e, 'getFaultCode'))) {
$errorstr .= get_string('faultcode', 'turnitintooltwo').": ".$e->getFaultCode()." | ";
}
if (is_callable(array($e, 'getFile'))) {
$errorstr .= get_string('file').": ".$e->getFile()." | ";
}
if (is_callable(array($e, 'getLine'))) {
$errorstr .= get_string('line', 'turnitintooltwo').": ".$e->getLine()." | ";
}
if (is_callable(array($e, 'getMessage'))) {
$errorstr .= get_string('message', 'turnitintooltwo').": ".$e->getMessage()." | ";
}
if (is_callable(array($e, 'getCode'))) {
$errorstr .= get_string('code', 'turnitintooltwo').": ".$e->getCode();
}
} else {
// Only show message if full debugging is not on.
if (is_callable(array($e, 'getMessage'))) {
$errorstr .= get_string('message', 'turnitintooltwo').": ".$e->getMessage();
}
}
turnitintooltwo_activitylog($errorstr, "API_ERROR");
if ($toscreen) {
turnitintooltwo_print_error($errorstr, null);
} else if ($embedded) {
return $errorstr;
}
}
/**
* Outputs a language code to use with the Turnitin API
*
* @param string $langcode The Moodle language code
* @return string The cleaned and mapped associated Turnitin lang code
*/
private function get_lang() {
$langcode = str_replace("_utf8", "", current_language());
$langarray = array(
'en' => 'en_us',
'en_us' => 'en_us',
'fr' => 'fr',
'fr_ca' => 'fr',
'es' => 'es',
'es_mx' => 'es',
'de' => 'de',
'de_du' => 'de',
'zh_cn' => 'zh_hans',
'zh_tw' => 'zh_tw',
'pt_br' => 'pt_br',
'th' => 'th',
'ja' => 'ja',
'ko' => 'ko',
'ms' => 'ms',
'tr' => 'tr',
'ca' => 'es',
'sv' => 'sv',
'nl' => 'nl',
'fi' => 'fi',
'ar' => 'ar'
);
$langcode = (isset($langarray[$langcode])) ? $langarray[$langcode] : 'en_us';
return $langcode;
}
/**
* @param int $diagnostic Set diagnostic setting.
*/
public function set_diagnostic($diagnostic) {
$this->diagnostic = $diagnostic;
}
}