-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
259 lines (209 loc) · 6.47 KB
/
functions.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
<?php
/* Loose code to organize */
// API routes
function register_qbank_api_routes() {
register_rest_route('qbank/v1', '/save-answer', array(
'methods' => 'POST',
'callback' => 'save_answer_to_question'
));
register_rest_route('qbank/v1', '/session', array(
'methods' => 'POST',
'callback' => 'qbank_session_create_callback',
'args' => array(),
));
register_rest_route('qbank/v1', '/session/end', array(
'methods' => 'POST',
'callback' => 'qbank_session_end_callback',
'args' => array(),
));
}
function qbank_session_create_callback($request) {
/*
@TODO check if user has open session, if they do, close it.
*/
$quiz_id_sent = $request->get_param('quiz_id');
if( $quiz_id_sent ) {
$quiz_id = $quiz_id_sent;
} else {
$quiz_id = 0;
}
$user_id = get_current_user_id();
if ( $user_id === 0 ) {
return rest_ensure_response(array(
'error' => 'Invalid user, or user not logged in.'
));
}
$session_data = array(
'user_id' => $user_id,
'quiz_id' => $quiz_id
);
$result = qbank_create_session($session_data);
if ($result !== false) {
return rest_ensure_response(array(
'success' => true
));
} else {
return rest_ensure_response(array(
'success' => false,
'error' => 'Student session saved.'
));
}
}
function qbank_session_end_callback($request) {
$session_id = $request->get_param('session_id');
$user_id = get_current_user_id();
/*
@TODO
fetch existing session to check that it exists.
update session row with the current timestamp in datetime format.
send response confirming session ended.
*/
return rest_ensure_response(array(
'message' => 'Session ended.',
));
}
function save_answer_to_question($request) {
// Parse JSON params.
$answer_data = $request->get_json_params();
$question_id = $answer_data['question_id'];
$answer_index = (int) $answer_data['answer_index'];
$question_revision_id = $answer_data['question_revision_id'];
if (empty($answer_data) || ! isset($answer_index)) {
return rest_ensure_response(array(
'error' => 'Invalid data. Please provide an "answer" field in the request.'
));
}
// Get the correct answer index.
$answer_correct_index = answer_correct_index($question_id);
// Check if answer is true or false.
$is_correct = is_answer_correct($question_id, $answer_index);
$correct = ($is_correct === true) ? 1 : 0;
// Create an object to pass to the qbank_answer_create function
$answer_object = (object) array(
'user' => get_current_user_id(),
'question' => $question_id,
'question_revision' => $question_revision_id,
'answer_index' => $answer_index,
'correct' => $correct
);
// Attempt to insert the answer into the database
$inserted_id = qbank_answer_create($answer_object);
if ($inserted_id === false) {
return rest_ensure_response(array(
'error' => 'Error saving the answer.'
));
} else {
return rest_ensure_response(array(
'message' => 'Answer saved successfully',
'question_id' => $question_id,
'student_id' => $student_id,
'answer_correct' => $is_correct,
'answer_index' => $answer_index,
'inserted_id' => $inserted_id,
'answer_correct_index' => $answer_correct_index
));
}
}
add_action('rest_api_init', 'register_qbank_api_routes');
// SAVE ANSWER RECORDS
function qbank_answer_create($answer) {
global $wpdb; // WordPress database object
// Define the table name
$table_name = $wpdb->prefix . 'qbank_answer';
// Extract properties from the object
$user = $answer->user;
$question = $answer->question;
$answer_index = $answer->answer_index;
// Prepare data for insertion
$data = array(
'user' => $answer->user,
'question' => $answer->question,
'question_revision' => $answer->question_revision,
'answer_index' => $answer->answer_index,
'correct' => $answer->correct
);
// Insert the data into the custom table
$result = $wpdb->insert($table_name, $data);
if ($result === false) {
// Handle the insertion error if needed
return false;
} else {
// Return the ID of the inserted row
return $wpdb->insert_id;
}
}
function is_answer_correct($question_id, $answer_index) {
$answers = get_field('answers', $question_id);
if (empty($answers) || !is_array($answers)) {
return false;
}
foreach ($answers as $answer_check_index => $answer) {
if (isset($answer['correct']) && $answer['correct'] && $answer_check_index == $answer_index) {
return true;
}
}
return false;
}
function answer_correct_index($question_id) {
$answers = get_field('answers', $question_id);
if (empty($answers) || !is_array($answers)) {
return false;
}
foreach ($answers as $answer_index => $answer) {
if (isset($answer['correct']) && $answer['correct']) {
return $answer_index;
}
}
return false;
}
/**
*
* QBank Create Session.
*
* Example Usage:
*
* This code demonstrates how to use the qbank_create_session function to insert a new session
* into the wp_qbank_session table.
*
* Usage:
*
$session_data = array(
'user_id' => 123,
'quiz_id' => 456,
'start' => '2023-10-21 09:00:00',
'end' => '2023-10-21 10:00:00',
);
$result = qbank_create_session($session_data);
if ($result !== false) {
// Data inserted successfully, and $result contains the inserted row's ID
} else {
Failed to insert data
}
*
* @param array $session_data An array containing session data, including user_id, quiz_id, start, and end.
*
* @return int|false The ID of the inserted row on success, or false on failure.
*/
function qbank_create_session($session_data) {
global $wpdb;
// Define the table name
$table_name = $wpdb->prefix . 'qbank_session';
// Extract data from the session_data array
$user_id = $session_data['user_id'];
$quiz_id = $session_data['quiz_id'];
// Prepare the data for insertion
$data = array(
'user_id' => $user_id,
'quiz_id' => $quiz_id
);
// Define the data format for the prepared statement
$data_format = array('%d', '%d', '%s', '%s');
// Insert the data into the database table
$wpdb->insert($table_name, $data, $data_format);
// Check for errors and return the result
if ($wpdb->last_error) {
return false; // Failed to insert data
} else {
return $wpdb->insert_id; // Returns the ID of the inserted row
}
}