-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
189 lines (140 loc) · 6.6 KB
/
action.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
<?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/>.
/**
* In this file, incoming AJAX request from practice.js are handled.
*
* @package mod_cardbox
* @copyright 2019 RWTH Aachen (see README.md)
* @author Anna Heynkes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/cardbox/locallib.php');
$cmid = required_param('id', PARAM_INT);
list ($course, $cm) = get_course_and_cm_from_cmid($cmid, 'cardbox');
$cardbox = $DB->get_record('cardbox', array('id' => $cm->instance), '*', MUST_EXIST);
require_login($course, true, $cm);
require_sesskey();
$context = context_module::instance($cmid);
$action = required_param('action', PARAM_ALPHA); // ...'$action' determines what is to be done; see below.
if ($action === 'review') {
global $DB;
require_once($CFG->dirroot . '/mod/cardbox/classes/output/review.php');
$cardid = required_param('cardid', PARAM_INT);
$newstatus = required_param('status', PARAM_TEXT);
$nextcard = optional_param('nextcard', 0, PARAM_INT);
$dataobject = new stdClass();
$dataobject->id = $cardid;
switch($newstatus) {
case 'approve':
$dataobject->approved = '1';
$dataobject->approvedby = $USER->id;
$dataobject->area = CARD_MAIN_INFORMATION;
$success = $DB->update_record('cardbox_cards', $dataobject, false);
$success = $DB->update_record('cardbox_contents', $dataobject, false);
break;
case 'reject':
$cardapproved = cardbox_card_approved($cardid);
if ($cardapproved) {
$success = $DB->delete_records('cardbox_contents',
array('card' => $cardid, 'area' => CARD_ANSERSUGGESTION_INFORMATION));
} else {
$success = cardbox_delete_card($cardid);
}
break;
case 'skip':
$success = 1;
break;
}
if (empty($success)) {
echo json_encode(['status' => 'error', 'reason' => get_string('error:updateafterreview', 'cardbox')]);
}
if ($nextcard != 0) {
$renderer = $PAGE->get_renderer('mod_cardbox');
$review = new cardbox_review($context, null, $nextcard);
$newdata = $review->export_for_template($renderer);
echo json_encode(['status' => 'success', 'finished' => 0, 'newdata' => $newdata]);
} else {
echo json_encode(['status' => 'success', 'finished' => 1]);
}
}
/* * ********************** move card to the next box and return next card *********************** */
if ($action === 'updateandnext') {
require_once($CFG->dirroot . '/mod/cardbox/locallib.php');
require_once($CFG->dirroot . '/mod/cardbox/classes/output/practice.php');
$cardid = required_param('cardid', PARAM_INT);
$iscorrect = required_param('iscorrect', PARAM_INT);
$next = required_param('next', PARAM_INT);
$isrepetition = required_param('isrepetition', PARAM_INT);
$case = optional_param('case', 1, PARAM_INT);
$cardsleft = required_param('cardsleft', PARAM_INT);
$correction = required_param('mode', PARAM_INT);
$dataobject = $DB->get_record('cardbox_progress', array('userid' => $USER->id, 'card' => $cardid), $fields = '*', MUST_EXIST);
if (empty($dataobject)) {
echo json_encode(['status' => 'error', 'reason' => 'nocardboxentryfound']);
}
$lastposition = $dataobject->cardposition;
$cardisdue = cardbox_is_card_due($dataobject);
// 1. Update the card entry in the DB if
// a) this is the first time the card was answered in this session and
// b) the card is (over)due and/or was answered incorrectly.
if ($isrepetition == 0 && ($cardisdue == true || $iscorrect == 0) ) {
$success = cardbox_update_card_progress($dataobject, $iscorrect);
if (empty($success)) {
echo json_encode(['status' => 'error', 'reason' => 'failedtoupdate']);
}
}
// 2. Get next card and pass it to javascript for rendering.
if ($next != 0) {
$renderer = $PAGE->get_renderer('mod_cardbox');
$practice = new cardbox_practice($case, $context, $next, $cardsleft, !$correction);
$newdata = $practice->export_for_template($renderer);
echo json_encode(['status' => 'success', 'lastposition' => $lastposition, 'newdata' => $newdata]);
} else {
echo json_encode(['status' => 'finished', 'lastposition' => $lastposition]);
}
}
/* * ********************** Save performance at the end of a practice session *********************** */
if ($action === 'saveperformance') {
global $DB, $USER;
$countright = required_param('countright', PARAM_INT);
$countwrong = required_param('countwrong', PARAM_INT);
$starttime = required_param('starttime', PARAM_TEXT);
$percentcorrect = 100 * $countright / ($countright + $countwrong);
$data = new stdClass();
$data->userid = $USER->id;
$data->cardboxid = $cardbox->id;
$data->timeofpractice = time();
$data->numberofcards = $countright + $countwrong;
$data->duration = $data->timeofpractice - $starttime;
$data->percentcorrect = round($percentcorrect, 0, PHP_ROUND_HALF_UP);
$success = $DB->insert_record('cardbox_statistics', $data);
if (empty($success)) {
echo json_encode(['status' => 'error', 'reason' => 'failedtosaveperformance']);
} else {
echo json_encode(['status' => 'success']);
}
}
/* ****************************************** Suggest answer for a card **************************************************** */
if ($action === 'savesuggestedanswer') {
require_once($CFG->dirroot . '/mod/cardbox/classes/output/practice.php');
$cardid = required_param('cardid', PARAM_INT);
$case = optional_param('case', 1, PARAM_INT);
$userinput = required_param('userinput', PARAM_TEXT);
if (!(empty($userinput) || $userinput === "")) {
cardbox_save_new_cardcontent($cardid, CARDBOX_CARDSIDE_ANSWER, CARDBOX_CONTENTTYPE_TEXT, 3, $userinput);
}
}