-
Notifications
You must be signed in to change notification settings - Fork 18
/
eins.php
executable file
·331 lines (288 loc) · 13 KB
/
eins.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
<?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 is is used to add a new lesson/category.
*
* Settings for category name, visibility and who can edit the exercise, are included.
*
* @package mod_mootyper
* @copyright 2011 Jaka Luthar ([email protected])
* @copyright 2016 onwards AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
**/
use mod_mootyper\event\exercise_added;
use mod_mootyper\event\invalid_access_attempt;
use mod_mootyper\local\lessons;
// Changed to this newer format 03/01/2019.
require(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
global $USER, $DB;
// 20200224 Switched $id to Course_module ID vice course ID.
$id = optional_param('id', 0, PARAM_INT); // Course ID.
$cm = get_coursemodule_from_id('mootyper', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
// If lessonid is available, put in lsnnamepos, otherwise leave it blank.
$lsnnamepo = optional_param('lesson', '', PARAM_TEXT);
// If lessonid is available, put it in lsnnamepos, otherwise enter a -1.
// Flag for new lesson name is -1.
$lessonpo = optional_param('lesson', -1, PARAM_INT);
require_login($course, true);
$context = context_module::instance($cm->id);
$mootyper = $DB->get_record('mootyper', ['id' => $cm->instance], '*', MUST_EXIST);
// 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' => 'eins.php',
],
];
$event = invalid_access_attempt::create($params);
$event->trigger();
redirect('../../course/view.php?id='.$course->id, get_string('invalidaccessexp', 'mootyper'));
}
// Check to see if Confirm button is clicked and returning 'Confirm' to trigger insert record.
$param1 = optional_param('button', '', PARAM_TEXT);
// DB insert.
if (isset($param1) && get_string('fconfirm', 'mootyper') == $param1 ) {
$texttotypeepo = optional_param('texttotype', '', PARAM_RAW);
// If new lesson, add lesson details to mdl_mootyper_lessons table.
if ($lessonpo == -1) {
$lsnnamepo = optional_param('lessonname', '', PARAM_TEXT);
$lsnrecord = new stdClass();
$lsnrecord->lessonname = $lsnnamepo;
$lsnrecord->visible = optional_param('visible', '', PARAM_TEXT);
$lsnrecord->editable = optional_param('editable', '', PARAM_TEXT);
$lsnrecord->authorid = $USER->id;
$lsnrecord->courseid = $course->id;
$lessonid = $DB->insert_record('mootyper_lessons', $lsnrecord, true);
} else {
$lessonid = $lessonpo;
}
// Get next sequence number for this exercise.
$snum = lessons::get_new_snumber($lessonid);
// Add exercise to mdl_mootyper_exercises table.
// 20210325 Added a new exercise_name to snum.
$exercisenamepo = optional_param('exercise_name', '', PARAM_TEXT);
$erecord = new stdClass();
// 20210325 Add new exercise name to the auto sname and add to the table.
$erecord->exercisename = "".$snum.' '.$exercisenamepo;
$erecord->snumber = $snum;
$erecord->lesson = $lessonid;
$erecord->texttotype = str_replace("\r\n", '\n', $texttotypeepo);
$DB->insert_record('mootyper_exercises', $erecord, false);
$webdir = $CFG->wwwroot . '/mod/mootyper/exercises.php?id='.$id.'&lesson='.$lessonid;
// Get the lesson name to use in the exercise_added event.
if ($lsnnamepo) {
$lesson = $lsnnamepo;
} else {
$lesson = $lessonpo;
}
echo '<script type="text/javascript">window.location="'.$webdir.'";</script>';
// Trigger module exercise_added event.
$params = [
'objectid' => $course->id,
'context' => $context,
'other' => [
'lesson' => $lesson,
'exercisename' => $erecord->exercisename,
],
];
$event = exercise_added::create($params);
$event->trigger();
}
// Get all the default configuration settings for MooTyper.
$moocfg = get_config('mod_mootyper');
// Check to see if configuration for MooTyper defaulteditalign is set.
if (isset($moocfg->defaulteditalign)) {
// Current MooTyper edittalign is set so use it.
$editalign = optional_param('editalign', $moocfg->defaulteditalign, PARAM_INT);
$align = $editalign;
} else {
// Current MooTyper edittalign is NOT set so set it to left.
$editalign = optional_param('editalign', 0, PARAM_INT);
$align = $editalign;
}
// Print the page header.
$PAGE->set_url('/mod/mootyper/eins.php', ['id' => $course->id]);
$PAGE->set_title(get_string('etitle', 'mootyper'));
$PAGE->set_heading(get_string('eheading', 'mootyper'));
$PAGE->set_pagelayout('standard');
// Other things you may want to set - remove if not needed.
$PAGE->set_cacheable(false);
// Output starts here.
echo $OUTPUT->header();
// Get all MooTyper lessons.
$lessonsg = lessons::get_typerlessons();
if (has_capability('mod/mootyper:editall', context_course::instance($course->id))) {
// If manager or admin get all lessons.
$lessons = $lessonsg;
} else {
$lessons = [];
// If a teacher, only get ones they can edit.
foreach ($lessonsg as $lsng) {
if (lessons::is_editable_by_me($USER->id, $id, $lsng['id'])) {
$lessons[] = $lsng;
}
}
}
// 20200625 Get the current MooTyper keyboard background default color for our page background here.
$color3 = $mootyper->keybdbgc;
// Add colored background with border.
echo '<div align="center" style="font-size:1em;
font-weight:bold;background: '.$color3.';
border:2px solid black;
-webkit-border-radius:16px;
-moz-border-radius:16px;border-radius:16px;">'.'<br>';
// Start page form and add lesson name selector.
echo '<form method="POST">';
// Add the label, Exercise will be a part of lesson.
echo get_string('fnewexercise', 'mootyper').' ';
echo '<select onchange="this.form.submit()" name="lesson">';
// Add, create new lesson, as first selection on the lesson name selector list.
echo '<option value="-1">'.get_string('fnewlesson', 'mootyper').'</option>';
// Check the selection list of lesson names for match to lessonpo.
for ($ij = 0; $ij < count($lessons); $ij++) {
if ($lessons[$ij]['id'] == $lessonpo) {
// If selection matches lessonpo (id), show it in the text box.
echo '<option selected="true" value="'.$lessons[$ij]['id'].'">'.$lessons[$ij]['lessonname'].'</option>';
} else {
// Add the rest of the lesson names to the list.
echo '<option value="'.$lessons[$ij]['id'].'">'.$lessons[$ij]['lessonname'].'</option>';
}
}
echo '</select>';
// 20210325 Prevent error before exercise name is actually created.
if ($snum = null) {
$num = ' ';
}
// Add lesson name, visibility, and editing options if this is a new lesson.
if ($lessonpo == -1) {
// Set up place to enter new lesson name.
echo '<br><br>'.get_string('lsnname', 'mootyper').': <input type="text" name="lessonname" id="lessonname">
<span style="color:red;" id="namemsg"></span>';
// 20210325 Prevent error before exercise name is actually created.
if ($snum = null) {
$num = ' ';
}
// 20210325 Set up place to enter new exercise name.
echo '<br><br>'.get_string('exercise_name', 'mootyper').': '
.$snum.' <input type="text" name="exercise_name" id="exercise_name"></span>';
// Set up visibility selector options for this new lesson.
echo '<br><br>'.get_string('visibility', 'mootyper').': <select name="visible">';
echo '<option value="2">'.get_string('vaccess2', 'mootyper').'</option>';
echo '<option value="1">'.get_string('vaccess1', 'mootyper').'</option>';
if (is_siteadmin()) {
echo '<option value="0">'.get_string('vaccess0', 'mootyper').'</option>';
}
echo '</select>';
// Set up editing selector options for this new lesson.
echo '<br><br>'.get_string('editable', 'mootyper').': <select name="editable">';
echo '<option value="2">'.get_string('eaccess2', 'mootyper').'</option>';
echo '<option value="1">'.get_string('eaccess1', 'mootyper').'</option>';
if (is_siteadmin()) {
echo '<option value="0">'.get_string('eaccess0', 'mootyper').'</option>';
}
echo '</select>';
} else {
// 20210325 Set up place to enter new exercise name.
echo '<br><br>'.get_string('exercise_name', 'mootyper').': '
.$snum.' <input type="text" name="exercise_name" id="exercise_name"></span>';
}
// Get our alignment strings and add a selector for text alignment.
$aligns = [get_string('defaulttextalign_left', 'mod_mootyper'),
get_string('defaulttextalign_center', 'mod_mootyper'),
get_string('defaulttextalign_right', 'mod_mootyper'), ];
echo '<br><br><span id="editalign" class="">'.get_string('defaulttextalign', 'mootyper').': ';
echo '<select onchange="this.form.submit()" name="editalign">';
// This will loop through ALL three alignments and show current alignment setting.
foreach ($aligns as $akey => $aval) {
// The first if is executed ONLY when, when defaulttextalign matches one of the alignments
// and it will then show that alignment in the selector.
if ($akey == $editalign) {
echo '<option value="'.$akey.'" selected="true">'.$aval.'</option>';
$align = $aval;
} else {
// This part of the if is reached the most and its when an alignment
// is is not the one selected.
echo '<option value="'.$akey.'">'.$aval.'</option>';
}
}
echo '</select></span>'.get_string('defaulttextalign_warning', 'mootyper');
?>
<script type="text/javascript">
function isLetter(str) {
// var pattern = /[a-z,ก-๛,а-я,א-ת,ㄱ-ㅣ,äáàâãčćçëéèêđïîíöôóõüúùûµšžº¡ñ]/i;
var pattern = /[!-ﻼ]/i;
return str.length === 1 && str.match(pattern);
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var ok = true;
function clClick() {
var exercise_text = document.getElementById("texttotype").value;
var allowed_chars = ['\\', '~', '!', '@', '#', '$', '%', '^', '&', '(', ')',
'*', '_', '+', ':', ';', '"', '{', '}', '>', '<', '?', '\'',
'-', '/', '=', '.', ',', ' ', '|', '¡', '`', 'ç', 'ñ', 'º',
'¿', 'ª', '·', '\n', '\r', '\r\n', '\n\r', ']', '[', '¬',
'´', '`', '§', '°', '€', '¦', '¢', '£', '₢', '¹', '²', '³',
'¨', 'Ё', '№', 'ё', 'ë', 'ù', 'µ', 'ï','÷', '×', 'ł', 'Ł', 'ß',
'¤', '«', '»', '₪', '־', 'װ', 'ױ', 'ײ', 'ˇ', '½'];
var shown_text = "";
ok = true;
for(var i=0; i<exercise_text.length; i++) {
if(!isLetter(exercise_text[i]) && !isNumber(exercise_text[i]) && allowed_chars.indexOf(exercise_text[i]) == -1) {
shown_text += '<span style="color: red;">'+exercise_text[i]+'</span>';
ok = false;
}
else
shown_text += exercise_text[i];
}
if(!ok) {
document.getElementById('text_holder_span').innerHTML = shown_text;
return false;
}
if(document.getElementById("lessonname").value == "") {
document.getElementById("namemsg").innerHTML = '<?php echo get_string('reqfield', 'mootyper'); // phpcs:ignore?>';
return false;
}
else
return true;
}
</script>
<?php // phpcs:ignore
// Create a link back to where we came from in case we want to cancel.
if ($lessonpo == -1) {
// 20200414 On cancel, go back to the MT activity if invoked from there.
$url = $CFG->wwwroot . '/mod/mootyper/view.php?id='.$id;
} else {
// Use this link for new lesson with exercise.
$url = $CFG->wwwroot . '/mod/mootyper/exercises.php?id='.$id.'&lesson='.$lessonpo;
}
// Add a text area for adding the text of the exercise.
echo '<br><span id="text_holder_span" class=""></span><br>'.get_string('fexercise', 'mootyper').':<br>'
.'<textarea rows="4" cols="60" name="texttotype" id="texttotype" style="text-align:'.$align.'"></textarea>';
// Add a confirm and cancel button.
echo '<br><br><input class="btn btn-primary" style="border-radius: 8px"
name="button" onClick="return clClick()" type="submit" value="'
.get_string('fconfirm', 'mootyper').'"> <a href="'
.$url.'" class="btn btn-secondary" style="border-radius: 8px">'
.get_string('cancel', 'mootyper').'</a>'.'</form>';
echo '</div>';
echo $OUTPUT->footer();