-
Notifications
You must be signed in to change notification settings - Fork 32
/
PlagiarismSettingsForm.php
178 lines (152 loc) · 5.93 KB
/
PlagiarismSettingsForm.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
<?php
/**
* @file plugins/generic/plagiarism/PlagiarismSettingsForm.php
*
* Copyright (c) 2013-2024 Simon Fraser University
* Copyright (c) 2013-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PlagiarismSettingsForm
*
* @brief plagiarism plugin settings form class
*/
namespace APP\plugins\generic\plagiarism;
use APP\core\Application;
use APP\plugins\generic\plagiarism\classes\form\validation\FormValidatorIthenticateAccess;
use APP\plugins\generic\plagiarism\PlagiarismPlugin;
use APP\template\TemplateManager;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorUrl;
use PKP\context\Context;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorPost;
class PlagiarismSettingsForm extends Form
{
/**
* The context
*/
protected Context $_context;
/**
* The PlagiarismPlugin instance
*/
protected PlagiarismPlugin $_plugin;
/**
* Constructor
*/
public function __construct(PlagiarismPlugin $plugin, Context $context)
{
$this->_plugin = $plugin;
$this->_context = $context;
$request = Application::get()->getRequest();
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
if (!empty(array_filter([$request->getUserVar('ithenticateApiUrl'), $request->getUserVar('ithenticateApiKey')]))) {
$this->addCheck(new FormValidator($this, 'ithenticateApiUrl', 'required', 'plugins.generic.plagiarism.manager.settings.apiUrlRequired'));
$this->addCheck(new FormValidator($this, 'ithenticateApiKey', 'required', 'plugins.generic.plagiarism.manager.settings.apiKeyRequired'));
$this->addCheck(new FormValidatorUrl($this, 'ithenticateApiUrl', 'required', 'plugins.generic.plagiarism.manager.settings.apiUrlInvalid'));
$this->addCheck(
new FormValidatorIthenticateAccess(
$this,
'',
'required',
'plugins.generic.plagiarism.manager.settings.serviceAccessInvalid',
$this->_plugin->initIthenticate(
$request->getUserVar('ithenticateApiUrl'),
$request->getUserVar('ithenticateApiKey')
)
)
);
}
$this->addCheck(
new FormValidatorCustom(
$this,
'excludeSmallMatches',
'required',
'plugins.generic.plagiarism.similarityCheck.settings.field.excludeSmallMatches.validation.min',
function($excludeSmallMatches) {
return (int) $excludeSmallMatches >= IThenticate::EXCLUDE_SAMLL_MATCHES_MIN;
}
)
);
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* Initialize form data.
*/
public function initData()
{
$this->_data = [
'ithenticateForced' => $this->_plugin->hasForcedCredentials($this->_context),
'ithenticateApiUrl' => $this->_plugin->getSetting($this->_context->getId(), 'ithenticateApiUrl'),
'ithenticateApiKey' => $this->_plugin->getSetting($this->_context->getId(), 'ithenticateApiKey'),
'disableAutoSubmission' => $this->_plugin->getSetting($this->_context->getId(), 'disableAutoSubmission'),
];
foreach(array_keys($this->_plugin->similaritySettings) as $settingOption) {
$this->_data[$settingOption] = $this->_plugin->getSetting($this->_context->getId(), $settingOption);
}
// set the default value `8` for `excludeSmallMatches` as per iThenticate guide
if ((int) $this->_data['excludeSmallMatches'] < IThenticate::EXCLUDE_SAMLL_MATCHES_MIN) {
$this->_data['excludeSmallMatches'] = IThenticate::EXCLUDE_SAMLL_MATCHES_MIN;
}
}
/**
* Assign form data to user-submitted data.
*/
public function readInputData()
{
$this->readUserVars(
array_merge([
'ithenticateApiUrl',
'ithenticateApiKey',
'disableAutoSubmission',
], array_keys($this->_plugin->similaritySettings))
);
}
/**
* @copydoc Form::fetch()
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->_plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* @copydoc Form::execute()
*/
public function execute(...$functionArgs)
{
$ithenticateApiUrl = trim($this->getData('ithenticateApiUrl'), "\"\';");
$ithenticateApiKey = trim($this->getData('ithenticateApiKey'), "\"\';");
// if proper api url and api key provided and if there is no forced credentails defined in
// `config.inc.php` at global or for this context
if (!empty(array_filter([$ithenticateApiUrl, $ithenticateApiKey])) &&
!$this->_plugin->hasForcedCredentials($this->_context)) {
// access updated or new access entered, need to update webhook registration
if ($this->_plugin->getSetting($this->_context->getId(), 'ithenticateApiUrl') !== $ithenticateApiUrl ||
$this->_plugin->getSetting($this->_context->getId(), 'ithenticateApiKey') !== $ithenticateApiKey) {
$ithenticate = $this->_plugin->initIthenticate($ithenticateApiUrl, $ithenticateApiKey);
// If there is a already registered webhook for this context, need to delete it first
// before creating a new one as webhook URL remains same which will return 409(HTTP_CONFLICT)
if ($this->_context->getData('ithenticateWebhookId')) {
$ithenticate->deleteWebhook($this->_context->getData('ithenticateWebhookId'));
}
$this->_plugin->registerIthenticateWebhook($ithenticate);
}
$this->_plugin->updateSetting($this->_context->getId(), 'ithenticateApiUrl', $ithenticateApiUrl, 'string');
$this->_plugin->updateSetting($this->_context->getId(), 'ithenticateApiKey', $ithenticateApiKey, 'string');
}
$this->_plugin->updateSetting($this->_context->getId(), 'disableAutoSubmission', $this->getData('disableAutoSubmission'), 'bool');
foreach($this->_plugin->similaritySettings as $settingName => $settingValueType) {
$this->_plugin->updateSetting(
$this->_context->getId(),
$settingName,
$this->getData($settingName),
$settingValueType
);
}
parent::execute(...$functionArgs);
}
}