-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.php
126 lines (104 loc) · 4.69 KB
/
settings.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
<?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/>.
/**
* plagiarism_originality settings.
*
* @package plagiarism_originality
* @copyright 2023 mattandor <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->libdir . '/plagiarismlib.php');
require_once($CFG->dirroot . '/plagiarism/originality/lib.php');
require_once($CFG->dirroot . '/plagiarism/originality/plagiarism_form.php');
require_once($CFG->dirroot . '/plagiarism/originality/version.php');
require_once($CFG->dirroot . '/user/lib.php');
require_login();
admin_externalpage_setup('manageplagiarismplugins');
$context = context_system::instance();
require_capability('moodle/site:config', $context, $USER->id, true, 'nopermissions');
$form = new plagiarism_setup_form();
$settingspage = new moodle_url('/plagiarism/originality/settings.php');
$config = get_config('plagiarism_originality');
if (($data = $form->get_data()) && confirm_sesskey()) {
if (!isset($data->enabled)) {
$data->enabled = 0;
}
if (!isset($data->default_use)) {
$data->default_use = 0;
}
if (!isset($data->check_ghostwriter)) {
$data->check_ghostwriter = 0;
}
foreach ($data as $key => $value) {
set_config($key, $value, 'plagiarism_originality');
}
// Create external token.
$service = $DB->get_record('external_services', ['shortname' => 'plagiarism_originality_service']);
if ($service) {
if (!$user = $DB->get_record('user', ['idnumber' => 'originalityuser'])) {
$user = new stdClass();
$user->firstname = 'originality';
$user->lastname = 'user';
$user->idnumber = 'originalityuser';
$user->username = 'originalityuser';
$user->email = '[email protected]';
$user->confirmed = true;
$user->mnethostid = $CFG->mnet_localhost_id;
$user->id = user_create_user($user, false, false);
}
$role = $DB->get_record('role', ['shortname' => 'originality']);
if (empty($role)) {
$roleid = create_role('Originality', 'originality', get_string('pluginname', 'plagiarism_originality'), 'originality');
} else {
$roleid = $role->id;
}
set_role_contextlevels($roleid, [CONTEXT_SYSTEM]);
assign_capability('plagiarism/originality:manage', CAP_ALLOW, $roleid, $context->id, true);
assign_capability('webservice/rest:use', CAP_ALLOW, $roleid, $context->id, true);
accesslib_clear_role_cache($roleid);
// Role assign.
role_assign($roleid, $user->id, $context->id);
// Check if a token has already been created for this user and this service.
$conditions = [
'userid' => $user->id,
'externalserviceid' => $service->id,
'tokentype' => EXTERNAL_TOKEN_PERMANENT,
];
// Check existing tokens.
$tokens = $DB->get_record('external_tokens', $conditions);
if (!$tokens && has_capability('moodle/webservice:createtoken', context_system::instance())) {
$token = external_generate_token(EXTERNAL_TOKEN_PERMANENT, $service->id, $user->id, \context_system::instance(), 0);
set_config('wstoken', $token, 'plagiarism_originality');
} else {
set_config('wstoken', $tokens->token, 'plagiarism_originality');
}
}
$originality = new plagiarism_plugin_originality();
if (!$originality->utils->subscription() || !$originality->utils->webserver_token()) {
redirect($settingspage, get_string('saved_failed', 'plagiarism_originality'), null,
\core\output\notification::NOTIFY_ERROR);
} else {
redirect($settingspage, get_string('plugin_connected', 'plagiarism_originality'), null,
\core\output\notification::NOTIFY_SUCCESS);
}
}
echo $OUTPUT->header();
$form->set_data($config);
$form->display();
echo $OUTPUT->footer();