forked from drachels/moodle-mod_mootyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheedit.php
executable file
·258 lines (222 loc) · 10 KB
/
eedit.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
<?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 edit exercise content. Called from exercises.php.
*
* @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_edited;
use \mod_mootyper\event\invalid_access_attempt;
// Changed to this newer format 20190301.
require(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
global $DB, $OUTPUT, $PAGE, $USER;
$id = optional_param('id', 0, PARAM_INT); // Course ID.
$ex = optional_param('ex', 0, PARAM_INT); // Id of exercise to edit.
$lsnnamepo = '';
$lessonpo = '';
if (! $cm = get_coursemodule_from_id('mootyper', $id)) {
print_error(get_string('mootypercmerror', 'mootyper'));
}
if (! $course = $DB->get_record("course", array('id' => $cm->course))) {
print_error(get_string('mootypercmmisconfig', 'mootyper'));
}
// TODO: Looks like $exercise and $rcrd are exact duplicates. Maybe need to combine?
$exercise = $DB->get_record('mootyper_exercises', array('id' => $ex), '*', MUST_EXIST);
$rcrd = $DB->get_record('mootyper_exercises', array('id' => $ex), '*', MUST_EXIST);
// Get the id of the lesson name from the current exercise, and then use it to get the lesson name.
$lesson = $DB->get_record('mootyper_exercises', array('id' => $ex), 'lesson', MUST_EXIST);
$lessonname = $DB->get_record('mootyper_lessons', array('id' => $lesson->lesson), 'lessonname', MUST_EXIST);
// This context->id will be used for the path to file storage.
$context = context_module::instance($cm->id);
$mootyper = $DB->get_record('mootyper', array('id' => $cm->instance) , '*', MUST_EXIST);
require_login($course, true, $cm);
// 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 = array(
'objectid' => $id,
'context' => $context,
'other' => array(
'file' => 'eedit.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 update record.
$param1 = optional_param('button', '', PARAM_TEXT);
if (isset($param1) && get_string('fconfirm', 'mootyper') == $param1 ) {
$newtext = optional_param('texttotype', '', PARAM_RAW);
$newdictation = optional_param('dictationdata', '', PARAM_RAW);
$rcrd = $DB->get_record('mootyper_exercises', array('id' => $ex), '*', MUST_EXIST);
$updr = new stdClass();
$updr->id = $rcrd->id;
$updr->texttotype = str_replace("\r\n", '\n', $newtext);
$updr->exercisename = $rcrd->exercisename;
$updr->lesson = $rcrd->lesson;
$updr->snumber = $rcrd->snumber;
$DB->update_record('mootyper_exercises', $updr);
// Trigger module exercise_edited event.
$params = array(
'objectid' => $course->id,
'context' => $context,
'other' => array(
'lesson' => $updr->lesson,
'exercise' => $updr->exercisename
)
);
$event = exercise_edited::create($params);
$event->trigger();
$webdir = $CFG->wwwroot . '/mod/mootyper/exercises.php?id='.$id.'&lesson='.$rcrd->lesson;
echo '<script type="text/javascript">window.location="'.$webdir.'";</script>';
}
// 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;
}
$PAGE->set_url('/mod/mootyper/eedit.php', array('id' => $course->id, 'ex' => $ex));
$PAGE->set_title(get_string('etitle', 'mootyper'));
$PAGE->set_heading(get_string('eheading', 'mootyper'));
$PAGE->set_cacheable(false);
echo $OUTPUT->header();
$exercisetoedit = $DB->get_record('mootyper_exercises',
array('id' => $ex), 'id, texttotype, exercisename, lesson, snumber', MUST_EXIST);
?>
<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((exercise_text[i] != '\n' && exercise_text[i] != '\r\n'
&& exercise_text[i] != '\n\r'
&& exercise_text[i] != '\r')
&& !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;
/*var text = (i-3)+'-'+exercise_text[i-3]+"\n";
text += (i-2)+'-'+exercise_text[i-2]+"\n";
text += (i-1)+'-'+exercise_text[i-1]+"\n";
text += i+'-'+exercise_text[i]+"\n";
text += (i+1)+'-'+exercise_text[i+1]+"\n";
text += (i+2)+'-'+exercise_text[i+2];
alert(text);*/
}
else
shown_text += exercise_text[i];
}
if(!ok) {
document.getElementById('text_holder_span').innerHTML = shown_text;
return false;
}
else
return true;
}
</script>
<?php
// 20200625 Get the current MooTyper keyboard background default color for our page background here.
$color3 = $mootyper->keybdbgc;
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;">';
echo get_string('lsnname', 'mod_mootyper').' = '.$lessonname->lessonname.'<br>';
echo '<form method="POST">';
// Get our alignment strings and add a selector for text alignment.
$aligns = array(get_string('defaulttextalign_left', 'mod_mootyper'),
get_string('defaulttextalign_center', 'mod_mootyper'),
get_string('defaulttextalign_right', 'mod_mootyper'));
echo '<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>';
// Create a link back to where we came from in case we want to cancel.
$url = $CFG->wwwroot . '/mod/mootyper/exercises.php?id='.$id.'&lesson='.$rcrd->lesson;
// Add a text area for editing the text of the exercise.
echo '<span id="text_holder_span" class=""></span><br>'
.get_string('fexercise', 'mootyper')
.':<br>'
.'<textarea name="texttotype" id="texttotype" rows="3" cols="60" style="text-align:'
.$align.'">'
.str_replace('\n', " ", $exercisetoedit->texttotype)
.'</textarea>';
$editor = editors_get_preferred_editor(FORMAT_HTML);
$attobuttons = 'files = recordrtc'. PHP_EOL .'list = unorderedlist, orderedlist'. PHP_EOL .'other = html, htmlplus';
$editor->use_editor('exercisetoedit',
['context' => $context,
'enable_filemanagement' => true,
'autosave' => true,
'atto:toolbar' => $attobuttons],
['return_types' => "FILE_EXTERNAL"]
);
// 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();