1
|
|
|
<?php |
2
|
|
|
/* For licensing terms, see /license.txt */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Defines a gradebook Result object. |
6
|
|
|
* |
7
|
|
|
* @author Bert Steppé, Stijn Konings |
8
|
|
|
*/ |
9
|
|
|
class Result |
10
|
|
|
{ |
11
|
|
|
private $id; |
12
|
|
|
private $user_id; |
13
|
|
|
private $evaluation; |
14
|
|
|
private $created_at; |
15
|
|
|
private $score; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Result constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->created_at = api_get_utc_datetime(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function get_id() |
26
|
|
|
{ |
27
|
|
|
return $this->id; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function get_user_id() |
31
|
|
|
{ |
32
|
|
|
return $this->user_id; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function get_evaluation_id() |
36
|
|
|
{ |
37
|
|
|
return $this->evaluation; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function get_date() |
41
|
|
|
{ |
42
|
|
|
return $this->created_at; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function get_score() |
46
|
|
|
{ |
47
|
|
|
return $this->score; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function set_id($id) |
51
|
|
|
{ |
52
|
|
|
$this->id = $id; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function set_user_id($user_id) |
56
|
|
|
{ |
57
|
|
|
$this->user_id = $user_id; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function set_evaluation_id($evaluation_id) |
61
|
|
|
{ |
62
|
|
|
$this->evaluation = $evaluation_id; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $creation_date |
67
|
|
|
*/ |
68
|
|
|
public function set_date($creation_date) |
69
|
|
|
{ |
70
|
|
|
$this->created_at = $creation_date; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param float $score |
75
|
|
|
*/ |
76
|
|
|
public function set_score($score) |
77
|
|
|
{ |
78
|
|
|
$this->score = $score; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieve results and return them as an array of Result objects. |
83
|
|
|
* |
84
|
|
|
* @param $id result id |
85
|
|
|
* @param $user_id user id (student) |
86
|
|
|
* @param $evaluation_id evaluation where this is a result for |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public static function load($id = null, $user_id = null, $evaluation_id = null) |
91
|
|
|
{ |
92
|
|
|
$tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
93
|
|
|
$tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
94
|
|
|
$tbl_course_rel_course = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
95
|
|
|
$tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
96
|
|
|
$sessionId = api_get_session_id(); |
97
|
|
|
$list_user_course_list = []; |
98
|
|
|
|
99
|
|
|
if (is_null($id) && is_null($user_id) && !is_null($evaluation_id)) { |
100
|
|
|
// Verified_if_exist_evaluation |
101
|
|
|
$sql = 'SELECT COUNT(*) AS count |
102
|
|
|
FROM '.$tbl_grade_results.' |
103
|
|
|
WHERE evaluation_id="'.Database::escape_string($evaluation_id).'"'; |
104
|
|
|
|
105
|
|
|
$result = Database::query($sql); |
106
|
|
|
$existEvaluation = Database::result($result, 0, 0); |
107
|
|
|
|
108
|
|
|
if (0 != $existEvaluation) { |
109
|
|
|
if ($sessionId) { |
110
|
|
|
$sql = 'SELECT c_id, user_id as user_id, status |
111
|
|
|
FROM '.$tbl_session_rel_course_user.' |
112
|
|
|
WHERE |
113
|
|
|
status= 0 AND |
114
|
|
|
c_id = "'.api_get_course_int_id().'" AND |
115
|
|
|
session_id = '.$sessionId; |
116
|
|
|
} else { |
117
|
|
|
$sql = 'SELECT c_id, user_id, status |
118
|
|
|
FROM '.$tbl_course_rel_course.' |
119
|
|
|
WHERE status ="'.STUDENT.'" AND c_id = "'.api_get_course_int_id().'" '; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$res_course_rel_user = Database::query($sql); |
123
|
|
|
while ($row_course_rel_user = Database::fetch_array($res_course_rel_user, 'ASSOC')) { |
124
|
|
|
$list_user_course_list[] = $row_course_rel_user; |
125
|
|
|
} |
126
|
|
|
$current_date = api_get_utc_datetime(); |
127
|
|
|
for ($i = 0; $i < count($list_user_course_list); $i++) { |
|
|
|
|
128
|
|
|
$sql_verified = 'SELECT COUNT(*) AS count |
129
|
|
|
FROM '.$tbl_grade_results.' |
130
|
|
|
WHERE |
131
|
|
|
user_id="'.intval($list_user_course_list[$i]['user_id']).'" AND |
132
|
|
|
evaluation_id="'.intval($evaluation_id).'";'; |
133
|
|
|
$res_verified = Database::query($sql_verified); |
134
|
|
|
$info_verified = Database::result($res_verified, 0, 0); |
135
|
|
|
if (0 == $info_verified) { |
136
|
|
|
$sql_insert = 'INSERT INTO '.$tbl_grade_results.'(user_id,evaluation_id,created_at,score) |
137
|
|
|
VALUES ("'.intval($list_user_course_list[$i]['user_id']).'","'.intval($evaluation_id).'","'.$current_date.'",0);'; |
138
|
|
|
Database::query($sql_insert); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$userIdList = []; |
145
|
|
|
foreach ($list_user_course_list as $data) { |
146
|
|
|
$userIdList[] = $data['user_id']; |
147
|
|
|
} |
148
|
|
|
$userIdListToString = implode("', '", $userIdList); |
149
|
|
|
|
150
|
|
|
$sql = "SELECT lastname, gr.id, gr.user_id, gr.evaluation_id, gr.created_at, gr.score |
151
|
|
|
FROM $tbl_grade_results gr |
152
|
|
|
INNER JOIN $tbl_user u |
153
|
|
|
ON gr.user_id = u.user_id "; |
154
|
|
|
|
155
|
|
|
if (!empty($userIdList)) { |
156
|
|
|
$sql .= " AND u.user_id IN ('$userIdListToString')"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$paramcount = 0; |
160
|
|
|
if (!empty($id)) { |
161
|
|
|
$sql .= ' WHERE gr.id = '.intval($id); |
162
|
|
|
$paramcount++; |
163
|
|
|
} |
164
|
|
|
if (!empty($user_id)) { |
165
|
|
|
if (0 != $paramcount) { |
166
|
|
|
$sql .= ' AND'; |
167
|
|
|
} else { |
168
|
|
|
$sql .= ' WHERE'; |
169
|
|
|
} |
170
|
|
|
$sql .= ' gr.user_id = '.intval($user_id); |
171
|
|
|
$paramcount++; |
172
|
|
|
} |
173
|
|
|
if (!empty($evaluation_id)) { |
174
|
|
|
if (0 != $paramcount) { |
175
|
|
|
$sql .= ' AND'; |
176
|
|
|
} else { |
177
|
|
|
$sql .= ' WHERE'; |
178
|
|
|
} |
179
|
|
|
$sql .= ' gr.evaluation_id = '.intval($evaluation_id); |
180
|
|
|
} |
181
|
|
|
$sql .= ' ORDER BY u.lastname, u.firstname'; |
182
|
|
|
|
183
|
|
|
$result = Database::query($sql); |
184
|
|
|
$allres = []; |
185
|
|
|
while ($data = Database::fetch_array($result)) { |
186
|
|
|
$res = new Result(); |
187
|
|
|
$res->set_id($data['id']); |
188
|
|
|
$res->set_user_id($data['user_id']); |
189
|
|
|
$res->set_evaluation_id($data['evaluation_id']); |
190
|
|
|
$res->set_date(api_get_local_time($data['created_at'])); |
191
|
|
|
$res->set_score($data['score']); |
192
|
|
|
$allres[] = $res; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $allres; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Insert this result into the database. |
200
|
|
|
*/ |
201
|
|
|
public function add() |
202
|
|
|
{ |
203
|
|
|
if (isset($this->user_id) && isset($this->evaluation)) { |
204
|
|
|
$tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
205
|
|
|
$sql = "INSERT INTO ".$tbl_grade_results |
206
|
|
|
." (user_id, evaluation_id, |
207
|
|
|
created_at"; |
208
|
|
|
if (isset($this->score)) { |
209
|
|
|
$sql .= ",score"; |
210
|
|
|
} |
211
|
|
|
$sql .= ") VALUES |
212
|
|
|
(".(int) $this->get_user_id().", ".(int) $this->get_evaluation_id() |
213
|
|
|
.", '".$this->get_date()."' "; |
214
|
|
|
if (isset($this->score)) { |
215
|
|
|
$sql .= ", ".$this->get_score(); |
216
|
|
|
} |
217
|
|
|
$sql .= ")"; |
218
|
|
|
Database::query($sql); |
219
|
|
|
} else { |
220
|
|
|
exit('Error in Result add: required field empty'); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* insert log result. |
226
|
|
|
*/ |
227
|
|
|
public function addResultLog($userid, $evaluationid) |
228
|
|
|
{ |
229
|
|
|
if (isset($userid) && isset($evaluationid)) { |
230
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG); |
231
|
|
|
$result = new Result(); |
232
|
|
|
|
233
|
|
|
$arr_result = $result->load(null, $userid, $evaluationid); |
234
|
|
|
$arr = get_object_vars($arr_result[0]); |
235
|
|
|
|
236
|
|
|
$sql = 'INSERT INTO '.$table |
237
|
|
|
.' (id_result,user_id, evaluation_id,created_at'; |
238
|
|
|
if (isset($arr['score'])) { |
239
|
|
|
$sql .= ',score'; |
240
|
|
|
} |
241
|
|
|
$sql .= ') VALUES |
242
|
|
|
('.(int) $arr['id'].','.(int) $arr['user_id'].', '.(int) $arr['evaluation'] |
243
|
|
|
.", '".api_get_utc_datetime()."'"; |
244
|
|
|
if (isset($arr['score'])) { |
245
|
|
|
$sql .= ', '.$arr['score']; |
246
|
|
|
} |
247
|
|
|
$sql .= ')'; |
248
|
|
|
|
249
|
|
|
Database::query($sql); |
250
|
|
|
} else { |
251
|
|
|
exit('Error in Result add: required field empty'); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Update the properties of this result in the database. |
257
|
|
|
*/ |
258
|
|
|
public function save() |
259
|
|
|
{ |
260
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
261
|
|
|
$sql = 'UPDATE '.$table.' |
262
|
|
|
SET user_id = '.$this->get_user_id() |
263
|
|
|
.', evaluation_id = '.$this->get_evaluation_id() |
264
|
|
|
.', score = '; |
265
|
|
|
if (isset($this->score)) { |
266
|
|
|
$sql .= $this->get_score(); |
267
|
|
|
} else { |
268
|
|
|
$sql .= 'null'; |
269
|
|
|
} |
270
|
|
|
if (isset($this->id)) { |
271
|
|
|
$sql .= " WHERE id = {$this->id}"; |
272
|
|
|
} else { |
273
|
|
|
$sql .= " WHERE evaluation_id = {$this->evaluation} |
274
|
|
|
AND user_id = {$this->user_id} |
275
|
|
|
"; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// no need to update creation date |
279
|
|
|
Database::query($sql); |
280
|
|
|
|
281
|
|
|
Evaluation::generateStats($this->get_evaluation_id()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Delete this result from the database. |
286
|
|
|
*/ |
287
|
|
|
public function delete() |
288
|
|
|
{ |
289
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
290
|
|
|
$sql = 'DELETE FROM '.$table.' WHERE id = '.$this->id; |
291
|
|
|
Database::query($sql); |
292
|
|
|
$allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts'); |
293
|
|
|
if ($allowMultipleAttempts) { |
294
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT); |
295
|
|
|
$sql = "DELETE FROM $table WHERE result_id = ".$this->id; |
296
|
|
|
Database::query($sql); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
Evaluation::generateStats($this->get_evaluation_id()); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Check if exists a result with its user and evaluation. |
304
|
|
|
* |
305
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
306
|
|
|
* |
307
|
|
|
* @return bool |
308
|
|
|
*/ |
309
|
|
|
public function exists() |
310
|
|
|
{ |
311
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
312
|
|
|
$sql = "SELECT COUNT(*) AS count |
313
|
|
|
FROM $table gr |
314
|
|
|
WHERE gr.evaluation_id = {$this->evaluation} |
315
|
|
|
AND gr.user_id = {$this->user_id} |
316
|
|
|
"; |
317
|
|
|
$result = Database::query($sql); |
318
|
|
|
$row = Database::fetch_array($result); |
319
|
|
|
$count = (int) $row['count']; |
320
|
|
|
|
321
|
|
|
return $count > 0; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: