-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.php
More file actions
440 lines (411 loc) · 18.5 KB
/
request.php
File metadata and controls
440 lines (411 loc) · 18.5 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Main page of lsf_unification.
*
* @package local_lsf_unification
* @copyright 2025 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_lsf_unification\output\first_overview;
require_once(dirname(__FILE__) . '/../../config.php');
global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;
require_once($CFG->dirroot . '/local/lsf_unification/lib_features.php');
require_once($CFG->dirroot . '/local/lsf_unification/request_form.php');
$veranstid = optional_param('veranstid', null, PARAM_INT);
$questionsanswered = optional_param('questionsanswered', null, PARAM_INT);
$courseid = optional_param('courseid', 1, PARAM_INT);
$teachername = optional_param('teachername', "", PARAM_ALPHANUMEXT);
$accept = optional_param('accept', null, PARAM_INT);
$answer = optional_param('answer', null, PARAM_INT);
$requestid = optional_param('requestid', null, PARAM_INT);
if (!empty($answer) && !empty($requestid)) {
$PAGE->set_url('/local/lsf_unification/request.php', ["answer" => $answer, "requestid" => $requestid]);
} else if (!empty($answer) && !empty($veranstid)) {
$PAGE->set_url('/local/lsf_unification/request.php', ["answer" => $answer, "veranstid" => $veranstid]);
} else {
$PAGE->set_url('/local/lsf_unification/request.php');
}
// Where we came from. Used in a number of redirects.
$returnurl = $CFG->wwwroot . '/course/index.php';
// Check permissions.
require_login();
if (isguestuser()) {
throw new moodle_exception('guestsarenotallowed', '', $returnurl);
}
if (empty($CFG->enablecourserequests)) {
throw new moodle_exception('courserequestdisabled', '', $returnurl);
}
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_secondary_navigation(false);
require_capability('moodle/course:request', $context);
$strtitle = get_string('courserequest');
$PAGE->set_title($strtitle);
$PAGE->set_heading($strtitle);
$PAGE->navbar->add($strtitle);
echo $OUTPUT->header();
if (!empty($requestid)) {
if (($request = $DB->get_record("local_lsf_unification_course", ["id" => $requestid])) && ($request->requeststate == 1)) {
$veranstid = $request->veranstid;
}
}
/**
* Print first overview that user sees when interacting with the plugin.
* @return void
* @throws \core\exception\coding_exception
*/
function print_first_overview(): void {
global $PAGE;
global $USER;
$courselist = array_filter(
get_teachers_course_list($USER->username, true),
function ($course) {
return !course_exists($course->veranstid);
}
);
$output = $PAGE->get_renderer('core');
echo $output->render(new first_overview($courselist));
}
/**
* Print helptext.
* @param string $t
* @param string|null $s
* @return void
* @throws coding_exception
*/
function print_helptext(string $t, string|null $s = null): void {
$answerstr = get_string('answer_' . $t, 'local_lsf_unification');
$infostr = get_string('info_' . $t, 'local_lsf_unification', $s, true);
echo "<u>" . $answerstr . "</u><br>" . $infostr;
echo "<br><a href='request.php'>" . get_string('back', 'local_lsf_unification') . "</a>";
}
/**
* Print the courses the user can see.
* @return void
* @throws coding_exception
*/
function print_courseselection(): void {
global $USER, $answer;
echo "<form name='input' action='request.php' method='post'><input type='hidden' name='answer' value='" . $answer . "'>";
print_coursetable($USER->username);
echo "<input type='submit' value='" . get_string('select', 'local_lsf_unification') . "'/></form>";
echo "<br><a href='request.php'>" . get_string('back', 'local_lsf_unification') . "</a>";
}
/**
* Print the course table for a teacher.
* @param string $teacher
* @param string $appendix
* @return void
* @throws coding_exception
*/
function print_coursetable(string $teacher, string $appendix = ""): void {
echo "<table><tr><td colspan='2'><b>" . get_string('choose_course', 'local_lsf_unification') . "</b></td></tr>";
foreach (get_teachers_course_list($teacher, true) as $course) {
if (!course_exists($course->veranstid)) {
$veranstid = $course->veranstid;
echo "<tr>
<td>
<input type='radio' name='veranstid' id='veranstid_" . ($veranstid) . "' value='" . ($veranstid) . "'>
</td>
<td>
<label for='veranstid_" . ($veranstid) . "'>" . ($course->info) . "</label>
</td>
</tr>";
}
}
echo $appendix . "</table>";
}
/**
* Final print.
* @return void
* @throws coding_exception
*/
function print_final() {
global $OUTPUT, $CFG, $courseid;
$nextsteps = get_string('next_steps', 'local_lsf_unification');
$linktext = [
'users' => get_string('linktext_users', 'local_lsf_unification'),
'content' => get_string('linktext_content', 'local_lsf_unification'),
'course' => get_string('linktext_course', 'local_lsf_unification'),
];
$href = [
0 => $CFG->wwwroot . "/user/index.php?id=" . ($courseid),
1 => $CFG->wwwroot . "/backup/import.php?id=" . ($courseid),
2 => $CFG->wwwroot . "/course/view.php?id=" . ($courseid),
];
echo $OUTPUT->box("<b>" . $nextsteps . ":</b><br><a href='" . $href[0] . "'>" . $linktext['users'] . "</a><br>
<a href='" . $href[1] . "'>" . $linktext['content'] . "</a><br>
<a href='" . $href[2] . "'>" . $linktext['course'] . "</a><br> ");
}
/**
* Print result selection.
* @return void
* @throws coding_exception
* @throws dml_exception
*/
function print_res_selection() {
global $CFG, $OUTPUT, $courseid;
$acceptorid = get_course_acceptor($courseid);
if (get_config('local_lsf_unification', 'restore_old_courses')) {
$backupfiles = get_backup_files($acceptorid);
}
if (get_config('local_lsf_unification', 'restore_templates')) {
$templatefiles = get_template_files();
}
if (empty($backupfiles) && empty($templatefiles)) {
print_final();
} else if (!empty($templatefiles)) {
$alternativecounter = 1;
// Continue with the course template.
if (get_config('local_lsf_unification', 'restore_templates') && !empty($templatefiles)) {
$cats = [];
$i = 0;
foreach ($templatefiles as $id => $fileinfo) {
$cats[$fileinfo->category][$id] = $fileinfo;
}
// If there are items without a category move them to the end.
$catkeys = array_keys($cats);
if (!empty($cats) && array_pop($catkeys) == "") {
array_unshift($cats, array_pop($cats));
}
// Render.
$pretemplate = get_string('pre_template', 'local_lsf_unification', $alternativecounter++);
echo "<b>" . $pretemplate . '</b><ul style="list-style-type:none">';
foreach ($cats as $name => $catfiles) {
if (!empty($name)) {
// LEARNWEB-TODO: move the two js-lines to a new file.
$jscheck = "(document.getElementById('reslist" . $i . "').style.display == 'none') ? 'block' : 'none'";
$js = "document.getElementById('reslist" . ($i) . "').style.display=(" . $jscheck . ");";
$exp = "'url(" . $OUTPUT->image_url("t/expanded")->out() . ")'";
$col = "'url(" . $OUTPUT->image_url("t/collapsed")->out() . ")'";
$js2check = "(document.getElementById('reslist" . $i . "').style.display == 'none') ? " . $col . ":" . $exp;
$js2 = "document.getElementById('reslistselector" . ($i) . "').style.listStyleImage=(" . $js2check . ");";
$alement = '<a onclick="' . $js . $js2 . '" style="cursor:default">[<b>' . $name . '</b>]</a>';
$reslist = '</b><ul id="reslist' . $i . '" style="display:none">';
echo '<li style="list-style-image: url(' . $col . ')" id="reslistselector' . ++$i . '">' . $alement . $reslist;
}
foreach ($catfiles as $id => $fileinfo) {
$lines = explode("\n", trim($fileinfo->info, " \t\r\n"), 2);
$text = mb_convert_encoding($lines[0], 'UTF-8', 'ISO-8859-1');
$imgnavitem = $OUTPUT->image_url("i/navigationitem")->out();
$href = "duplicate_course.php?courseid=" . $courseid . "&filetype=t&fileid=" . $id;
echo '<li style="list-style-image: url(' . $imgnavitem . ')"><a href="' . $href . '">' . $text . "</a>";
if (count($lines) == 2) {
echo "<br/>" . mb_convert_encoding($lines[1], 'UTF-8', 'ISO-8859-1');
}
echo "</li>";
}
if (!empty($name)) {
echo "</ul></li>";
}
}
echo "</ul>";
}
// Continue with a blank course.
$notemplate = get_string('no_template', 'local_lsf_unification', $alternativecounter++);
$emptycourse = get_string('continue_with_empty_course', 'local_lsf_unification');
$href = "request.php?courseid=" . $courseid . "&answer=7";
echo "<b>" . $notemplate . "</b><ul><li><a href=" . $href . ">" . $emptycourse . "</a></li></ul>";
// Duplicate course from the course .
if (get_config('local_lsf_unification', 'restore_old_courses') && !empty($backupfiles)) {
echo "<b>" . get_string('template_from_course', 'local_lsf_unification', $alternativecounter++) . "</b><ul>";
// Sortiere die Backups alphabetisch.
uasort($backupfiles, function ($a, $b) {
return strcmp($a->course->fullname, $b->course->fullname);
});
foreach ($backupfiles as $id => $fileinfo) {
$href = "duplicate_course.php?courseid=" . $courseid . "&filetype=b&fileid=" . $id;
echo "<li><a href='" . $href . "'>" . $fileinfo->course->fullname . " (" . $fileinfo->datetime . ")</a></li>";
}
echo "</ul>";
}
}
}
/**
* Prints the remote course creation process.
* @return void
* @throws coding_exception
* @throws dml_exception
*/
function print_remote_creation() {
global $USER, $answer, $teachername, $veranstid;
if (!get_config('local_lsf_unification', 'remote_creation')) {
return;
}
if (empty($veranstid)) {
echo "<form name='input' action='request.php' method='post'><input type='hidden' name='answer' value='" . $answer . "'>";
if (empty($teachername)) {
$chooseteacher = get_string('choose_teacher', 'local_lsf_unification');
echo "<b>" . $chooseteacher . "</b><input type='text' name='teachername' size='20' value='" . $teachername . "'>";
} else {
echo "<input type='hidden' name='teachername' value='" . $teachername . "'>";
$answercourselsf = get_string('answer_course_in_lsf_but_invisible', 'local_lsf_unification', $teachername);
$appendix = "
<tr>
<td>
<input type='radio' name='veranstid' id='veranstid_' value='" . (-1) . "'>
</td>
<td>
<label for='veranstid_'>" . $answercourselsf . "</label>
</td>
</tr>";
print_coursetable($teachername, $appendix);
}
$select = get_string('select', 'local_lsf_unification');
$back = get_string('back', 'local_lsf_unification');
echo "<input type='submit' value='" . $select . "'/></form><br><a href='request.php'>" . $back . "</a>";
} else {
if ($veranstid < 0) {
echo get_string('his_info', 'local_lsf_unification');
} else {
$requestid = set_course_requested($veranstid);
if (!empty($requestid)) {
if (send_course_request_mail($teachername, get_course_by_veranstid($veranstid), $requestid)) {
echo get_string('request_sent', 'local_lsf_unification');
} else {
echo "unkown error";
}
} else {
echo get_string('already_requested', 'local_lsf_unification');
}
}
}
}
/**
* Prints the course creation process.
* @return void
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function print_coursecreation() {
global $CFG, $USER, $DB, $OUTPUT, $answer, $teachername, $veranstid, $courseid;
$editform = new lsf_course_request_form(null, ['veranstid' => $veranstid]);
if (!($editform->is_cancelled()) && ($data = $editform->get_data())) {
// Dbenrolment enrolment can only be enabled if it is globally enabled.
$extenabledglobal = get_config('local_lsf_unification', 'enable_enrol_ext_db') == true;
$enabledbenrolment = $extenabledglobal ? $data->dbenrolment == 1 : false;
// Enable self enrolment if dbenrolment is disabled globally.
$enableselfenrolment = !$extenabledglobal ? true : ($data->selfenrolment == 1);
$result = create_lsf_course(
$veranstid,
$data->fullname,
$data->shortname,
$data->summary,
$data->startdate,
$enabledbenrolment,
$enableselfenrolment,
empty($data->enrolment_key) ? "" : ($data->enrolment_key),
$data->category
);
$courseid = $result['course']->id;
$event = \local_lsf_unification\event\course_imported::create([
'objectid' => $courseid,
'context' => context_system::instance(0, IGNORE_MISSING),
'other' => $veranstid,
]);
$event->trigger();
if (!empty($data->category_wish)) {
if (!empty($CFG->supportemail)) {
$emailsucc = "\n" . get_string('email_success', 'local_lsf_unification');
$emailerr = "\n" . get_string('email_error', 'local_lsf_unification');
$result['warnings'] .= (send_support_mail($result['course'], $data->category_wish) ? ($emailsucc) : ($emailerr));
}
}
// Update customfield.
$coursecontext = \context_course::instance($courseid);
if ($field = $DB->get_record('customfield_field', ['shortname' => 'semester', 'type' => 'semester'])) {
$fieldcontroller = \core_customfield\field_controller::create($field->id);
$datacontroller = \core_customfield\data_controller::create(0, null, $fieldcontroller);
$datacontroller->set('instanceid', $courseid);
$datacontroller->set('contextid', $coursecontext->id);
$datacontroller->instance_form_save($data);
}
$warnings = get_string('warnings', 'local_lsf_unification');
$out = "<p>" . $OUTPUT->box("<b>" . $warnings . "</b><br>" . "<pre>" . $result["warnings"] . "<pre>") . "</p>";
echo (!empty($result["warnings"])) ? $out : "";
print_final($result["course"]->id);
} else {
$editform->display();
}
}
/**
* Shows the request handler.
* @return void
* @throws coding_exception
* @throws dml_exception
*/
function print_request_handler() {
global $CFG, $DB, $answer, $request, $veranstid, $accept;
$course = get_course_by_veranstid($veranstid);
$requester = $DB->get_record("user", ["id" => $request->requesterid]);
if (empty($accept)) {
echo get_string('remote_request_select_alternative', 'local_lsf_unification');
$params = new stdClass();
$params->a = $requester->firstname . " " . $requester->lastname;
$params->b = mb_convert_encoding($course->titel, 'UTF-8', 'ISO-8859-1');
$href = $CFG->wwwroot . "/local/lsf_unification/request.php?answer=" . $answer . "&requestid=" . $request->id;
$remoteaccept = get_string('remote_request_accept', 'local_lsf_unification', $params);
$remotedeclined = get_string('remote_request_decline', 'local_lsf_unification', $params);
echo '<p>' .
"<a href='" . $href . "&accept=1'>" . $remoteaccept . "</a>" . "<br>";
echo "<a href='" . $href . "&accept=2'>" . $remotedeclined . "</a>" . "<br>";
} else {
if ($accept == 1) {
set_course_accepted($veranstid);
send_course_creation_mail($requester, $course);
} else {
set_course_declined($veranstid);
send_sorry_mail($requester, $course);
}
echo get_string('answer_sent', 'local_lsf_unification');
}
}
// Handle Course-Request.
if (establish_secondary_DB_connection() === true) {
if (empty($answer)) {
print_first_overview(); // Task Selection.
} else if ($answer == 1) {
if (empty($veranstid)) {
print_courseselection(); // Extern Course Selection.
} else {
if (has_course_import_rights($veranstid, $USER)) { // Validate veranstid, user.
print_coursecreation(); // Request neccessary details and create course.
}
}
} else if ($answer == 2) {
print_helptext('course_not_created_yet');
} else if ($answer == 3) {
print_helptext('course_in_lsf_and_visible', $USER->username);
} else if ($answer == 6) {
print_helptext('goto_old_requestform');
} else if ($answer == 7) {
print_final(); // Goto Course.
} else if ($answer == 11) {
print_remote_creation(); // Remote Course Creation Starter.
} else if ($answer == 12) {
if (!is_course_of_teacher($veranstid, $USER->username)) { // Validate veranstid, user.
// The user isn't a teacher of this course, so he shouldn't get here.
die("Course request not existing, already handled or none of your business");
}
print_request_handler(); // Remote Course Creation Request Handler.
}
close_secondary_DB_connection();
} else {
echo get_string('db_not_available', 'local_lsf_unification');
}
echo $OUTPUT->footer();