-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheditstage.php
245 lines (225 loc) · 11.2 KB
/
editstage.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
<?php
// This file is part of Treasurehunt for Moodle
//
// 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/>.
/**
* Page to edit stage
*
* @package mod_treasurehunt
* @copyright 2016 onwards Adrian Rodriguez Fernandez <[email protected]>, Juan Pablo de Castro <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/editstage_form.php');
require_once("$CFG->dirroot/mod/treasurehunt/locallib.php");
global $COURSE, $PAGE, $CFG, $USER;
// You will process some page parameters at the top here and get the info about
// what instance of your module and what course you're in etc. Make sure you
// include hidden variable in your forms which have their defaults set in set_data
// which pass these variables from page to page.
// Setup $PAGE here.
// Print the page header.
$cmid = required_param('cmid', PARAM_INT); // Course_module ID.
$id = optional_param('id', 0, PARAM_INT); // EntryID.
$roadid = optional_param('roadid', 0, PARAM_INT);
$addanswers = optional_param('addanswers', '', PARAM_TEXT);
list ($course, $cm) = get_course_and_cm_from_cmid($cmid, 'treasurehunt');
$treasurehunt = $DB->get_record('treasurehunt', array('id' => $cm->instance), '*', MUST_EXIST);
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
$url = new moodle_url('/mod/treasurehunt/editstage.php', array('cmid' => $cmid));
if (!empty($id)) {
$url->param('id', $id);
}
if (!empty($roadid)) {
$url->param('roadid', $roadid);
}
$PAGE->set_url($url);
require_capability('mod/treasurehunt:managetreasurehunt', $context);
$PAGE->requires->jquery();
if (!treasurehunt_is_edition_loked($treasurehunt->id, $USER->id)) {
$lockid = treasurehunt_renew_edition_lock($treasurehunt->id, $USER->id);
$renewlocktime = (treasurehunt_get_setting_lock_time() - 5) * 1000;
$PAGE->requires->js_call_amd('mod_treasurehunt/renewlock', 'renew_edition_lock',
array($treasurehunt->id, $lockid, $renewlocktime));
if ($id) { // If entry is specified.
require_capability('mod/treasurehunt:editstage', $context);
$title = get_string('editingstage', 'treasurehunt');
$stage = $DB->get_record('treasurehunt_stages', ['id' => $id], '*', MUST_EXIST);
// Si existe la pregunta recojo las respuestas.
if ($stage->questiontext !== '') {
// Hago que se muestre la pregunta.
$stage->addsimplequestion = optional_param('addsimplequestion', 1, PARAM_INT);
$sqlanswers = 'SELECT a.id,a.answertext,a.answertextformat,a.answertexttrust,'
. ' a.correct FROM {treasurehunt_answers} a INNER JOIN'
. ' {treasurehunt_stages} r ON r.id=a.stageid WHERE r.id=?';
$params = array($stage->id);
$answers = $DB->get_records_sql($sqlanswers, $params);
$stage->answers = $answers;
$stage->noanswers = optional_param('noanswers', count($answers), PARAM_INT);
if (!empty($addanswers)) {
$stage->noanswers += NUMBER_NEW_ANSWERS;
}
}
} else { // New entry.
require_capability('mod/treasurehunt:addstage', $context);
$title = get_string('addingstage', 'treasurehunt');
$roadid = required_param('roadid', PARAM_INT);
$select = 'id = ?';
$params = array($roadid);
// Compruebo si existe el camino.
if (!$DB->record_exists_select('treasurehunt_roads', $select, $params)) {
print_error('invalidentry');
}
// Compruebo si no esta bloqueado y por tanto no se puede anadir ninguna etapa.
if (treasurehunt_check_road_is_blocked($roadid)) {
print_error('notcreatestage', 'treasurehunt', $returnurl);
}
$stage = new stdClass();
$stage->id = null;
$stage->roadid = $roadid;
}
if (!isset($stage->questiontext) || $stage->questiontext === '') {
$stage->addsimplequestion = optional_param('addsimplequestion', 0, PARAM_INT);
$stage->noanswers = optional_param('noanswers', 2, PARAM_INT);
if (!empty($addanswers)) {
$stage->noanswers += NUMBER_NEW_ANSWERS;
}
}
$stage->cmid = $cmid;
$returnurl = new moodle_url('/mod/treasurehunt/edit.php', array('id' => $cmid, 'roadid' => $stage->roadid));
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
$editoroptions = array('trusttext' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes,
'context' => $context,
'subdirs' => file_area_contains_subdirs($context, 'mod_treasurehunt', 'cluetext', $stage->id));
// List activities with Completion enabled.
$completioninfo = new completion_info($course);
$completionactivities = $completioninfo->get_activities();
// Name of the form you defined in file above.
$mform = new stage_form(null, array('current' => $stage, 'context' => $context, 'editoroptions' => $editoroptions,
'completionactivities' => $completionactivities));
if ($mform->is_reloaded()) {
// Ignore this event. Some data may be changes.
echo ' ';
} else if ($mform->is_cancelled()) {
// You need this section if you have a cancel button on your form
// here you tell php what to do if your user presses cancel
// probably a redirect is called for!
// PLEASE NOTE: is_cancelled() should be called before get_data().
redirect($returnurl);
} else if ($stage = $mform->get_data()) {
// Actualizamos los campos.
$timenow = time();
$stage->name = trim($stage->name);
$stage->cluetext = ''; // Updated later.
$stage->cluetextformat = FORMAT_HTML; // Updated later.
$stage->cluetexttrust = 0; // Updated later.
$stage->questiontext = ''; // Updated later.
$stage->questiontextformat = FORMAT_HTML; // Updated later.
$stage->questiontexttrust = 0; // Updated later.
if (empty($stage->id)) {
$stage->timecreated = $timenow;
$stage->id = treasurehunt_insert_stage_form($stage);
$isnewentry = true;
} else {
$stage->timemodified = $timenow;
$isnewentry = false;
}
// This branch is where you process validated data.
// Do stuff ...
// Typically you finish up by redirecting to somewhere where the user
// can see what they did.
// save and relink embedded images and save attachments
$stage = file_postupdate_standard_editor($stage, 'cluetext', $editoroptions, $context,
'mod_treasurehunt', 'cluetext', $stage->id);
// Store the updated value values.
if ($stage->addsimplequestion) {
// Proceso los ficheros del editor de pregunta.
$stage = file_postupdate_standard_editor($stage, 'questiontext', $editoroptions, $context,
'mod_treasurehunt', 'questiontext', $stage->id);
if (isset($stage->answertext_editor)) {
// Process answer texts and save answers.
foreach ($stage->answertext_editor as $key => $answertext) {
if (isset($answers) && count($answers) > 0) {
$answer = array_shift($answers);
if (trim($answertext['text']) === '') {
$DB->delete_records('treasurehunt_answers', array('id' => $answer->id));
continue;
}
$answer->timemodified = $timenow;
$answer->correct = $stage->correct[$key];
} else {
if (trim($answertext['text']) === '') {
continue;
}
$answer = new stdClass();
$answer->answertext = ''; // Updated later.
$answer->answertextformat = FORMAT_HTML; // Updated later.
$answer->answertexttrust = 0; // Updated later.
$answer->timecreated = $timenow;
$answer->stageid = $stage->id;
$answer->correct = $stage->correct[$key];
$answer->id = $DB->insert_record('treasurehunt_answers', $answer);
}
$answer->answertext_editor = $answertext;
$answer = file_postupdate_standard_editor($answer, 'answertext', $editoroptions, $context,
'mod_treasurehunt', 'answertext', $answer->id);
$DB->update_record('treasurehunt_answers', $answer);
}
}
} else {
if (isset($answers)) {
// Elimino las anteriores respuestas.
foreach ($answers as $answer) {
$DB->delete_records('treasurehunt_answers', array('id' => $answer->id));
}
}
}
// Actualizo la etapa con los ficheros procesados.
$DB->update_record('treasurehunt_stages', $stage);
// Trigger event and update completion (if entry was created).
$eventparams = array(
'context' => $context,
'objectid' => $stage->id,
'other' => $stage->name,
);
if ($isnewentry) {
$event = \mod_treasurehunt\event\stage_created::create($eventparams);
} else {
$event = \mod_treasurehunt\event\stage_updated::create($eventparams);
}
$event->trigger();
// Actualizo el tiempo de modificacion del camino.
$road = new stdClass();
$road->id = $stage->roadid;
$road->timemodified = time();
$DB->update_record('treasurehunt_roads', $road);
redirect($returnurl);
}
} else {
$returnurl = new moodle_url('/mod/treasurehunt/view.php', array('id' => $cmid));
print_error('treasurehuntislocked', 'treasurehunt', $returnurl, treasurehunt_get_username_blocking_edition($treasurehunt->id));
}
$PAGE->navbar->add(get_string('edittreasurehunt', 'treasurehunt'), $returnurl);
$PAGE->navbar->add(get_string('editstage', 'treasurehunt'), $url);
$PAGE->set_title($title);
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_pagelayout('standard');
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
// Support for QR scan.
treasurehunt_qr_support($PAGE, 'enableEditForm');
// End QR.
$mform->display();
echo $OUTPUT->footer();