-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlsnimport.php
354 lines (329 loc) · 14.9 KB
/
lsnimport.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
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
<?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/>.
/**
* This file is used to import new lessons and keyboard layouts.
*
* Can be called from the MooTyper admin block anytime it is visible.
* The file scans the lesson and layout folders and checks the files
* found there against the ones already in the database.
* Duplicates are skipped while new ones are added with the results
* listed for the user to see. Continue at the end will take the
* user to exercises.php for possible editing.
*
* @package mod_mootyper
* @copyright 2016 AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
use mod_mootyper\event\invalid_access_attempt;
use mod_mootyper\event\lesson_imported;
use mod_mootyper\event\layout_imported;
require(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
/**
* Define the lesson import function.
* @param string $dafile
* @param int $authoridarg
* @param int $visiblearg
* @param int $editablearg
* @param int $coursearg
*/
function read_lessons_file($dafile, $authoridarg, $visiblearg, $editablearg, $coursearg=null) {
global $DB, $CFG, $USER;
// Scan the mootyper lessons folder for lessonname.txt files.
$thefile = $CFG->dirroot."/mod/mootyper/lessons/".$dafile;
// Extract the lesson name from the file name.
$record = new stdClass();
$periodpos = strrpos($dafile, '.');
$lessonname = substr($dafile, 0, $periodpos);
// Create new record for the mootyper_lessons table for this lessonname.
$record->lessonname = $lessonname; // Add the lesson name.
$record->authorid = $authoridarg; // Add the author id.
$record->visible = $visiblearg; // Add the visibility setting.
$record->editable = $editablearg; // Add the edit-ability setting.
if (!is_null($coursearg)) {
$record->courseid = $coursearg; // If we have a course id use it, otherwise set to null.
}
// Create entry in the mootyper_lessons table based on new data.
$lessonid = $DB->insert_record('mootyper_lessons', $record, true);
// Now read the whole file so we can split it into exercises.
$fh = fopen($thefile, 'r');
$thedata = fread($fh, filesize($thefile));
fclose($fh);
$haha = "";
for ($i = 0; $i < strlen($thedata); $i++) {
$haha .= $thedata[$i];
}
$haha = trim($haha);
// Break lesson into an array of separate exercises.
$splitted = explode ('/**/' , $haha);
// 20210328 Changed for loop to count by two so we can get exercise name along with the exercise text.
for ($j = 0; $j < count($splitted); $j += 2) {
// Remove whitespace from both sides of $splitted.
$exercise = trim($splitted[$j]);
// 20210328 Added same cleanup for exercisename.
$exercisename = trim($splitted[$j + 1]);
$allowed = ['ё', 'ë', '¸', 'á', 'é',
'í', 'ï', 'ó', 'ú', '\\',
'~', '!', '@', '#', '$',
'%', '^', '&', '(', ')',
'*', '_', '+', ':', ';',
'"', '{', '}', '>', '<',
'?', '\'', '-', '/', '=',
'.', ',', ' ', '|', '¡',
// phpcs:ignore
'`', 'ç', 'ñ', 'º', '¿',
'ª', '·', '\n', '\r', '\r\n',
'\n\r', ']', '[', '¬', '´',
'§', '°', '€', '¦',
'¢', '£', '?', '¹', '²',
'³', '¨', '?', 'ù', 'µ',
'û', '÷', '×', 'ł', 'Ł',
'ß', '¤', '«', '»',
];
// Create a number to use as the exercise name. Start with 1 and increment for each exercise detected.
// 20210328 We now get an actual exercise name from the lessonname.txt file, so $nm not needed now.
$texttotype = "";
// Place each character of an exercise into $texttotype.
for ($k = 0; $k < strlen($exercise); $k++) {
// Need to check on this.
// * If it is not a letter
// * and if it is not a number
// * compare against $allowed array.
// * If not included die
// * or something.
$ch = $exercise[$k];
if ($ch == "\n") {
$texttotype .= '\n';
} else {
$texttotype .= $ch;
}
}
// Create new entry in the mootyper_exercises.
$erecord = new stdClass();
$erecord->texttotype = $texttotype;
// 20210328 Save exercise name instead of just a number.
$erecord->exercisename = $exercisename; // Add the exercise name here.
$erecord->lesson = $lessonid;
$erecord->snumber = ($j + 2) / 2;
$DB->insert_record('mootyper_exercises', $erecord, false);
}
}
/**
* Define the keyboard import function.
* @param string $dafile
*/
function add_keyboard_layout($dafile) {
global $DB;
$periodpos = strrpos($dafile, '.');
$layoutname = substr($dafile, 0, $periodpos);
$record = (object) [
'name' => $layoutname,
];
$DB->insert_record('mootyper_layouts', $record, true);
}
/**
* Define import and update exercise function.
* @param string $dafile
* @param int $lsnid
* @param int $lsn
*/
function update_exercises_file($dafile, $lsnid, $lsn) {
global $DB, $CFG, $USER;
// 202104010 Added this new function.
// Scan the mootyper lessons folder for lessonname.txt files.
$thefile = $CFG->dirroot."/mod/mootyper/lessons/".$dafile;
// Extract the lesson name from the file name.
$record = new stdClass();
$periodpos = strrpos($dafile, '.');
// 20240512 Skip files with names starting with a period.
if ($periodpos <> 0) {
$lessonname = substr($dafile, 0, $periodpos);
// Now read the whole lesson file so we can split it into exercises and exercise names.
$fh = fopen($thefile, 'r');
$thedata = fread($fh, filesize($thefile));
fclose($fh);
$haha = "";
for ($i = 0; $i < strlen($thedata); $i++) {
$haha .= $thedata[$i];
}
$haha = trim($haha);
// Break lesson into an array of separate exercises followed by exercise names.
$splitted = explode ('/**/' , $haha);
for ($j = 0; $j < count($splitted); $j += 2) {
// Remove whitespace from both sides of $splitted.
$fexercise = trim($splitted[$j]);
$fexercisename = trim($splitted[$j + 1]);
// Create sql to see how many exercises are in this lesson.
$sql = "SELECT id, texttotype, exercisename, lesson, snumber
FROM {mootyper_exercises}
WHERE lesson = '".$lsnid."'";
// Get the total number of exercises that belong to this lesson.
$snumber = count($DB->get_records_sql($sql));
$snum = $j / 2 + 1;
$sql = "SELECT id, texttotype, exercisename, lesson, snumber
FROM {mootyper_exercises}
WHERE lesson = '".$lsnid."' AND snumber = '".$snum."'";
$record = $DB->get_record_sql($sql);
// If there is no record, we must be adding a new exercise.
if (!$record) {
$record = new stdClass();
$record->texttotype = $fexercise;
$record->exercisename = $fexercisename;
$record->lesson = $lsnid;
$record->snumber = $snum;
$DB->insert_record('mootyper_exercises', $record, false);
echo "<tr class='table-success'><td><b>$lsn</td><td>"
.get_string('exercise_name_added', 'mootyper', $fexercisename).'</b></td></tr>';
} else if (($record->texttotype == $fexercise) && ($record->exercisename == $fexercisename)) {
// If no changes, then do not need to do anything.
echo "<tr class='table-dark text-dark'><td>$lsn</td><td>".get_string('lsnimportnotadd', 'mootyper').'</td></tr>';
} else if (($record->texttotype == $fexercise) && !($record->exercisename == $fexercisename)) {
// If the text is the same but the exercise name is different, then change it.
$record->exercisename = $fexercisename;
$DB->update_record('mootyper_exercises', $record, false);
echo "<tr class='table-success'><td><b>$lsn</td><td>"
.get_string('exercise_name_updated', 'mootyper', $fexercisename).'</b></td></tr>';
} else if (!($record->texttotype == $fexercise) && ($record->exercisename == $fexercisename)) {
// If the text is different but not the exercise name, then update the text.
// Need updated string for adding changed text to type.
$record->texttotype = $fexercise;
$DB->update_record('mootyper_exercises', $record, false);
echo "<tr class='table-success'><td><b>$lsn</td><td>".get_string('lsnimportadd', 'mootyper').'</b></td></tr>';
}
}
}
}
// Actual page starts here.
$id = optional_param('id', 0, PARAM_INT); // Course ID.
$lsn = optional_param('lsn', 0, PARAM_INT); // Lesson ID to download.
$cm = get_coursemodule_from_id('mootyper', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
require_login($course, true);
$context = context_module::instance($cm->id);
// 20200706 Added to prevent student direct URL access attempts.
if (!(has_capability('mod/mootyper:aftersetup', $context))) {
// Trigger invalid_access_attempt with redirect to course page.
$params = [
'objectid' => $id,
'context' => $context,
'other' => [
'file' => 'lsnimport.php',
],
];
$event = invalid_access_attempt::create($params);
$event->trigger();
redirect('../../course/view.php?id='.$course->id, get_string('invalidaccessexp', 'mootyper'));
}
// Print the page header.
$PAGE->set_url('/mod/mootyper/exercises.php', ['id' => $course->id]);
$PAGE->set_title(get_string('etitle', 'mootyper'));
$PAGE->set_heading(get_string('eheading', 'mootyper'));
// Other things you may want to set - remove if not needed.
$PAGE->set_cacheable(false);
// Output starts here.
echo $OUTPUT->header();
echo '<b>'.get_string('lsnimport', 'mootyper').'</b><br><br>';
echo '<b>'.get_string('sflesson', 'mootyper').'</b><br>';
echo '<table class="table table-hover" style="width:100%">';
echo '<tbody>';
// Set pointer to lessons folder, then get all lesson names in there.
$pth = $CFG->dirroot."/mod/mootyper/lessons";
$res = scandir($pth);
for ($i = 0; $i < count($res); $i++) {
if (is_file($pth."/".$res[$i])) {
// Get a filename from the lessons folder.
$fl = $res[$i]; // Argument list dafile, authorid_arg, visible_arg, editable_arg, course_arg.
// Strip away the .txt portion of the filename.
$periodpos = strrpos($fl, '.');
$lsn = substr($fl, 0, $periodpos);
// Create sql to see if lesson name is already an installed lesson.
$sql = "SELECT lessonname, id
FROM {mootyper_lessons}
WHERE lessonname = '".$lsn."'";
if ($importlesson = $DB->get_record_sql($sql)) {
// If it is found in the db, then check to see if it needs to be updated.
update_exercises_file($fl, $importlesson->id, $lsn);
} else {
// If it's not found in the db, then add the new lesson to the database.
echo "<tr class='table-success'><td><b>$lsn</td><td>".get_string('lsnimportadd', 'mootyper').'</b></td></tr>';
read_lessons_file($fl, $USER->id, 0, 2);
// Since we added a new lesson, make a log entry about it.
$data = new StdClass();
$data->mootyper = $id;
$context = context_module::instance($id);
// Trigger lesson_imported event.
$params = [
'objectid' => $data->mootyper,
'context' => $context,
'other' => $lsn,
];
$event = lesson_imported::create($params);
$event->trigger();
}
}
}
echo '</tbody>';
echo '</table>';
echo '<br><b>'.get_string('layout', 'mootyper').'</b><br>';
echo '<table class="table table-hover" style="width:100%">';
echo '<thead class="thead-dark">';
echo get_string('layout', 'mootyper');
echo '</thead>';
echo '<tbody>';
// Set pointer to keyboard layouts folder, then get all names in there.
$pth2 = $CFG->dirroot."/mod/mootyper/layouts";
$res2 = scandir($pth2);
for ($j = 0; $j < count($res2); $j++) {
if (is_file($pth2."/".$res2[$j]) && ( substr($res2[$j], (strripos($res2[$j], '.') + 1) ) == 'php')) {
// Get a filename from the lessons folder.
$fl2 = $res2[$j];
// Strip away the .txt portion of the filename.
$periodpos = strrpos($fl2, '.');
$kbl = substr($fl2, 0, $periodpos);
// Create sql to see if lesson name is already an installed lesson.
$sql = "SELECT name
FROM {mootyper_layouts}
WHERE name = '".$kbl."'";
if ($importkbl = $DB->get_record_sql($sql)) {
// If it's true the name is already in the database, do nothing.
echo "<tr class='table-dark text-dark'><td>$kbl</td><td>".get_string('kblimportnotadd', 'mootyper').'</td></tr>';
} else {
// If it's not found in the db, then add the new layout to the database.
echo "<tr class='table-success'><td><b>$kbl</td><td>".get_string('kblimportadd', 'mootyper').'</b></td></tr>';
// Actually go add the layout to the database.
add_keyboard_layout($fl2);
// Since we added a new layout, make a log entry about it.
$data = new StdClass();
$data->mootyper = $id;
$context = context_module::instance($id);
// Trigger layout_imported event.
$params = [
'objectid' => $data->mootyper,
'context' => $context,
'other' => $kbl,
];
$event = layout_imported::create($params);
$event->trigger();
}
}
}
echo '</tbody>';
echo '</table>';
$jlnk2 = $CFG->wwwroot . '/mod/mootyper/exercises.php?id='.$id;
// 11/19/19 Change from a, Continue, link to a, Continue, button.
echo '<a href="'.$jlnk2.'" class="btn btn-primary" style="border-radius: 8px">'.get_string('fcontinue', 'mootyper').'</a><br>';
echo $OUTPUT->footer();
return;