-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.php
165 lines (150 loc) · 7.96 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
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
<?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/>.
/**
* Enrolment method "SEMCO" - Settings
*
* @package enrol_semco
* @copyright 2022 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
// Require plugin library.
require_once($CFG->dirroot.'/enrol/semco/locallib.php');
// Show the webservice token from the DB.
// But only if we are on the right page to save unnecessary database queries.
// And if we are not during the initial install or if the script is called without setting the page URL
// (which will happen during the plugin installation and will show a debug warning, that's why we suppress debugging messages
// temporarily).
$settingsurl = new moodle_url('/admin/settings.php', ['section' => 'enrolsettingssemco']);
$olddebug = $CFG->debug;
$CFG->debug = 0;
$pageurl = $PAGE->url;
$CFG->debug = $olddebug;
if (!during_initial_install() && $settingsurl->compare($pageurl, URL_MATCH_PARAMS) == true) {
// Create connection information heading.
$name = 'enrol_semco/settings_connectioninfoheading';
$title = get_string('settings_connectioninfoheading', 'enrol_semco', null, true);
$description = '';
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
// Create wwwroot information widget.
$name = 'enrol_semco/settings_wwwrootinfo';
$title = get_string('settings_wwwrootinfo', 'enrol_semco', null, true);
$description = '<p>'.get_string('settings_wwwrootinfofound', 'enrol_semco', $CFG->wwwroot, true).'</p>';
$setting = new admin_setting_description($name, $title, $description);
$settings->add($setting);
// Get the webservice token.
$sql = 'SELECT et.token
FROM {external_tokens} et
JOIN {external_services} es ON et.externalserviceid = es.id
JOIN {user} u ON et.userid = u.id
WHERE u.username = :username AND es.shortname = :serviceshortname';
$sqlparams = ['serviceshortname' => ENROL_SEMCO_SERVICENAME, 'username' => ENROL_SEMCO_ROLEANDUSERNAME];
$webservicetoken = $DB->get_field_sql($sql, $sqlparams);
// If a token was found.
if ($webservicetoken != false) {
// Create token information widget.
$name = 'enrol_semco/settings_tokeninfo';
$title = get_string('settings_tokeninfo', 'enrol_semco', null, true);
$description = '<p>'.get_string('settings_tokeninfofound', 'enrol_semco', $webservicetoken, true).'</p>';
$setting = new admin_setting_description($name, $title, $description);
$settings->add($setting);
// Otherwise.
} else {
// Create token information widget.
$name = 'enrol_semco/settings_tokeninfo';
$title = get_string('settings_tokeninfo', 'enrol_semco', null, true);
$description = '<p>'.get_string('settings_tokeninfononefound', 'enrol_semco', null, true).'</p>';
$setting = new admin_setting_description($name, $title, $description);
$settings->add($setting);
}
}
// Create enrolment report heading.
$name = 'enrol_semco/settings_enrolmentreportheading';
$reporturl = new moodle_url('/enrol/semco/enrolreport.php');
$title = get_string('settings_enrolmentreportheading', 'enrol_semco', null, true);
$description = get_string('settings_enrolmentreportheading_desc', 'enrol_semco', null, true).'<br />'.
html_writer::link($reporturl,
get_string('settings_enrolmentreportbutton', 'enrol_semco', null, true),
['class' => 'btn btn-secondary my-3']);
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
// Create enrolment settings heading.
$name = 'enrol_semco/settings_enrolmentheading';
$title = get_string('settings_enrolmentheading', 'enrol_semco', null, true);
$description = '';
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
// Create role chooser widget.
$roleoptions = [];
// Get some basic data we are going to need.
$roles = get_all_roles();
$systemcontext = context_system::instance();
$rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
if (!empty($rolenames)) {
foreach ($rolenames as $key => $role) {
if (!array_key_exists($role->id, $roleoptions)) {
$roleoptions[$role->id] = $role->localname;
}
}
}
// Get first default role for 'student' archetype.
$firststudentroleid = enrol_semco_get_firststudentroleid();
// And add the widget finally.
$name = 'enrol_semco/role';
$title = get_string('settings_role', 'enrol_semco', null, true);
$description = get_string('settings_role_desc', 'enrol_semco', null, true);
$setting = new admin_setting_configselect($name, $title, $description, $firststudentroleid, $roleoptions);
$setting->set_updatedcallback('enrol_semco_roleassign_updatecallback');
$settings->add($setting);
unset($roleoptions);
// Create course completion settings heading.
$name = 'enrol_semco/settings_coursecompletionheading';
$title = get_string('settings_coursecompletionheading', 'enrol_semco', null, true);
$description = '';
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
// If local_recompletion is installed.
if (enrol_semco_check_local_recompletion() == true) {
// Create information widget.
$name = 'enrol_semco/settings_coursecompletionnotfound';
$title = '';
$localrecompletionurl = new moodle_url('/admin/settings.php', ['section' => 'local_recompletion']);
$notification = new \core\output\notification(get_string('settings_coursecompletionlrcintro', 'enrol_semco', null, true).
get_string('settings_coursecompletionlrcfound', 'enrol_semco', null, true).
get_string('settings_coursecompletionnote', 'enrol_semco', $localrecompletionurl->out(), true),
\core\output\notification::NOTIFY_SUCCESS);
$notification->set_show_closebutton(false);
$description = $OUTPUT->render($notification);
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
// Otherwise.
} else {
// Create information widget.
$name = 'enrol_semco/settings_coursecompletionnotfound';
$title = '';
$localrecompletionurl = new moodle_url('/admin/settings.php', ['section' => 'local_recompletion']);
$notification = new \core\output\notification(get_string('settings_coursecompletionlrcintro', 'enrol_semco', null, true).
get_string('settings_coursecompletionlrcnotfound', 'enrol_semco', null, true).
get_string('settings_coursecompletionnote', 'enrol_semco', $localrecompletionurl->out(), true),
\core\output\notification::NOTIFY_INFO);
$notification->set_show_closebutton(false);
$description = $OUTPUT->render($notification);
$setting = new admin_setting_heading($name, $title, $description);
$settings->add($setting);
}
}