Total Complexity | 144 |
Total Lines | 992 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Evaluation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Evaluation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Evaluation implements GradebookItem |
||
12 | { |
||
13 | public $studentList; |
||
14 | public GradebookEvaluation $entity; |
||
15 | private int $id; |
||
16 | private string $name; |
||
17 | private string $description; |
||
18 | private int $user_id; |
||
19 | private ?string $course_code; |
||
20 | private Category $category; |
||
21 | private string $created_at; |
||
22 | private $weight; |
||
23 | private $eval_max; |
||
24 | private int $visible; |
||
25 | private ?int $courseId; |
||
26 | private int $sessionId; |
||
27 | protected string $type; |
||
28 | protected int $locked; |
||
29 | |||
30 | /** |
||
31 | * Construct. |
||
32 | */ |
||
33 | public function __construct() |
||
34 | { |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return Category |
||
39 | */ |
||
40 | public function getCategory() |
||
41 | { |
||
42 | return $this->category; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param Category $category |
||
47 | */ |
||
48 | public function setCategory($category) |
||
49 | { |
||
50 | $this->category = $category; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return int |
||
55 | */ |
||
56 | public function get_category_id() |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param int $category_id |
||
63 | */ |
||
64 | public function set_category_id($category_id) |
||
65 | { |
||
66 | $categories = Category::load($category_id); |
||
67 | if (isset($categories[0])) { |
||
68 | $this->setCategory($categories[0]); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function get_id() |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return string |
||
82 | */ |
||
83 | public function get_name() |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function get_description() |
||
94 | } |
||
95 | |||
96 | public function get_user_id() |
||
97 | { |
||
98 | return $this->user_id; |
||
99 | } |
||
100 | |||
101 | public function get_course_code() |
||
102 | { |
||
103 | return $this->course_code; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return int |
||
108 | */ |
||
109 | public function getSessionId() |
||
110 | { |
||
111 | return $this->sessionId; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param int $sessionId |
||
116 | */ |
||
117 | public function setSessionId($sessionId) |
||
120 | } |
||
121 | |||
122 | public function set_session_id($sessionId) |
||
123 | { |
||
124 | $this->setSessionId($sessionId); |
||
125 | } |
||
126 | |||
127 | public function get_date() |
||
128 | { |
||
129 | return $this->created_at; |
||
130 | } |
||
131 | |||
132 | public function get_weight() |
||
133 | { |
||
134 | return $this->weight; |
||
135 | } |
||
136 | |||
137 | public function get_max() |
||
138 | { |
||
139 | return $this->eval_max; |
||
140 | } |
||
141 | |||
142 | public function get_type() |
||
143 | { |
||
144 | return $this->type; |
||
145 | } |
||
146 | |||
147 | public function is_visible() |
||
148 | { |
||
149 | return $this->visible; |
||
150 | } |
||
151 | |||
152 | public function get_locked() |
||
153 | { |
||
154 | return $this->locked; |
||
155 | } |
||
156 | |||
157 | public function is_locked() |
||
158 | { |
||
159 | return isset($this->locked) && 1 == $this->locked ? true : false; |
||
160 | } |
||
161 | |||
162 | public function set_id($id) |
||
163 | { |
||
164 | $this->id = (int) $id; |
||
165 | } |
||
166 | |||
167 | public function set_name($name) |
||
168 | { |
||
169 | $this->name = $name; |
||
170 | } |
||
171 | |||
172 | public function set_description($description) |
||
173 | { |
||
174 | $this->description = $description; |
||
175 | } |
||
176 | |||
177 | public function set_user_id($user_id) |
||
178 | { |
||
179 | $this->user_id = $user_id; |
||
180 | } |
||
181 | |||
182 | public function getCourseId() |
||
183 | { |
||
184 | return $this->courseId; |
||
185 | } |
||
186 | |||
187 | public function set_date($date) |
||
188 | { |
||
189 | $this->created_at = $date; |
||
190 | } |
||
191 | |||
192 | public function set_weight($weight) |
||
193 | { |
||
194 | $this->weight = $weight; |
||
195 | } |
||
196 | |||
197 | public function set_max($max) |
||
198 | { |
||
199 | $this->eval_max = $max; |
||
200 | } |
||
201 | |||
202 | public function set_visible($visible) |
||
203 | { |
||
204 | $this->visible = $visible; |
||
205 | } |
||
206 | |||
207 | public function set_type($type) |
||
208 | { |
||
209 | $this->type = $type; |
||
210 | } |
||
211 | |||
212 | public function set_locked($locked) |
||
213 | { |
||
214 | $this->locked = $locked; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Retrieve evaluations and return them as an array of Evaluation objects. |
||
219 | * |
||
220 | * @param ?int $id evaluation id |
||
221 | * @param ?int $userId user id (evaluation owner) |
||
222 | * @param ?int $courseId course code |
||
223 | * @param ?int $categoryId parent category |
||
224 | * @param ?int $visible Whether it is visible or not |
||
225 | * @param ?int $locked Whether it is locked or not |
||
226 | * |
||
227 | * @return array |
||
228 | * @throws Exception |
||
229 | */ |
||
230 | public static function load( |
||
231 | ?int $id = 0, |
||
232 | ?int $userId = 0, |
||
233 | ?int $courseId = 0, |
||
234 | ?int $categoryId = 0, |
||
235 | ?int $visible = -1, |
||
236 | ?int $locked = -1 |
||
237 | ) { |
||
238 | $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
239 | $sql = 'SELECT * FROM '.$table; |
||
240 | $parametersCount = 0; |
||
241 | |||
242 | if (!empty($id)) { |
||
243 | $sql .= ' WHERE id = '.$id; |
||
244 | $parametersCount++; |
||
245 | } |
||
246 | |||
247 | if (!empty($userId)) { |
||
248 | if (0 != $parametersCount) { |
||
249 | $sql .= ' AND'; |
||
250 | } else { |
||
251 | $sql .= ' WHERE'; |
||
252 | } |
||
253 | $sql .= ' user_id = '.$userId; |
||
254 | $parametersCount++; |
||
255 | } |
||
256 | |||
257 | if (!empty($courseId) && 0 < $courseId) { |
||
258 | if (0 != $parametersCount) { |
||
259 | $sql .= ' AND'; |
||
260 | } else { |
||
261 | $sql .= ' WHERE'; |
||
262 | } |
||
263 | $sql .= " c_id = $courseId"; |
||
264 | $parametersCount++; |
||
265 | } |
||
266 | |||
267 | if (!empty($categoryId)) { |
||
268 | if (0 != $parametersCount) { |
||
269 | $sql .= ' AND'; |
||
270 | } else { |
||
271 | $sql .= ' WHERE'; |
||
272 | } |
||
273 | $sql .= ' category_id = '.$categoryId; |
||
274 | $parametersCount++; |
||
275 | } |
||
276 | |||
277 | if (isset($visible) && -1 < $visible) { |
||
278 | if (0 != $parametersCount) { |
||
279 | $sql .= ' AND'; |
||
280 | } else { |
||
281 | $sql .= ' WHERE'; |
||
282 | } |
||
283 | $sql .= ' visible = '.$visible; |
||
284 | $parametersCount++; |
||
285 | } |
||
286 | |||
287 | if (isset($locked) && -1 < $locked) { |
||
288 | if (0 != $parametersCount) { |
||
289 | $sql .= ' AND'; |
||
290 | } else { |
||
291 | $sql .= ' WHERE'; |
||
292 | } |
||
293 | $sql .= ' locked = '.$locked; |
||
294 | } |
||
295 | |||
296 | $result = Database::query($sql); |
||
297 | |||
298 | return self::create_evaluation_objects_from_sql_result($result); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Insert this evaluation into the database. |
||
303 | * @throws \Doctrine\ORM\Exception\ORMException |
||
304 | */ |
||
305 | public function add() |
||
306 | { |
||
307 | if (isset($this->name) && |
||
308 | isset($this->user_id) && |
||
309 | isset($this->weight) && |
||
310 | isset($this->eval_max) && |
||
311 | isset($this->visible) |
||
312 | ) { |
||
313 | if (empty($this->type)) { |
||
314 | $this->type = 'evaluation'; |
||
315 | } |
||
316 | $em = Database::getManager(); |
||
317 | |||
318 | $category = null; |
||
319 | if (!empty($this->get_category_id())) { |
||
320 | $category = $em->getRepository(GradebookCategory::class)->find($this->get_category_id()); |
||
321 | } |
||
322 | |||
323 | $courseId = $this->getCourseId(); |
||
324 | |||
325 | $evaluation = new GradebookEvaluation(); |
||
326 | $evaluation |
||
327 | ->setDescription($this->description) |
||
328 | ->setCourse(api_get_course_entity($courseId)) |
||
329 | ->setTitle($this->get_name()) |
||
330 | ->setCategory($category) |
||
331 | ->setUser(api_get_user_entity($this->get_user_id())) |
||
332 | ->setWeight(api_float_val($this->get_weight())) |
||
333 | ->setMax(api_float_val($this->get_max())) |
||
334 | ->setVisible($this->is_visible()) |
||
335 | ->setType($this->type) |
||
336 | ; |
||
337 | $em->persist($evaluation); |
||
338 | $em->flush(); |
||
339 | $this->set_id($evaluation->getId()); |
||
340 | } |
||
341 | |||
342 | return false; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param int $id |
||
347 | */ |
||
348 | public function addEvaluationLog($id) |
||
349 | { |
||
350 | if (!empty($id)) { |
||
351 | $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
352 | $tbl_grade_linkeval_log = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG); |
||
353 | $eval = new Evaluation(); |
||
354 | $dateobject = $eval->load($id, null, null, null, null); |
||
355 | $arreval = get_object_vars($dateobject[0]); |
||
356 | if (!empty($arreval['id'])) { |
||
357 | $sql = 'SELECT weight from '.$tbl_grade_evaluations.' |
||
358 | WHERE id='.$arreval['id']; |
||
359 | $rs = Database::query($sql); |
||
360 | $row_old_weight = Database::fetch_array($rs, 'ASSOC'); |
||
361 | $current_date = api_get_utc_datetime(); |
||
362 | $params = [ |
||
363 | 'id_linkeval_log' => $arreval['id'], |
||
364 | 'title' => $arreval['name'], |
||
365 | 'description' => $arreval['description'], |
||
366 | 'created_at' => $current_date, |
||
367 | 'weight' => $row_old_weight['weight'], |
||
368 | 'visible' => $arreval['visible'], |
||
369 | 'type' => 'evaluation', |
||
370 | 'user_id_log' => api_get_user_id(), |
||
371 | ]; |
||
372 | Database::insert($tbl_grade_linkeval_log, $params); |
||
373 | } |
||
374 | } |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Update the properties of this evaluation in the database. |
||
379 | * @throws Exception |
||
380 | */ |
||
381 | public function save() |
||
382 | { |
||
383 | $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
384 | $sql = 'UPDATE '.$tbl_grade_evaluations |
||
385 | ." SET title = '".Database::escape_string($this->get_name())."'" |
||
386 | .', description = '; |
||
387 | if (isset($this->description)) { |
||
388 | $sql .= "'".Database::escape_string($this->get_description())."'"; |
||
389 | } else { |
||
390 | $sql .= 'null'; |
||
391 | } |
||
392 | $sql .= ', user_id = '.$this->get_user_id() |
||
393 | .', c_id = '; |
||
394 | if (isset($this->courseId)) { |
||
395 | $sql .= $this->getCourseId(); |
||
396 | } else { |
||
397 | $sql .= 'null'; |
||
398 | } |
||
399 | $sql .= ', category_id = '; |
||
400 | if (!empty($this->category)) { |
||
401 | $sql .= $this->get_category_id(); |
||
402 | } else { |
||
403 | $sql .= 'null'; |
||
404 | } |
||
405 | $sql .= ', weight = "'.Database::escape_string($this->get_weight()).'" ' |
||
406 | .', max = '.intval($this->get_max()) |
||
407 | .', visible = '.intval($this->is_visible()) |
||
408 | .' WHERE id = '.intval($this->id); |
||
409 | //recorded history |
||
410 | |||
411 | $eval_log = new Evaluation(); |
||
412 | $eval_log->addEvaluationLog($this->id); |
||
413 | Database::query($sql); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Delete this evaluation from the database. |
||
418 | * @throws Exception |
||
419 | */ |
||
420 | public function delete(): void |
||
421 | { |
||
422 | $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
423 | $sql = 'DELETE FROM '.$table.' |
||
424 | WHERE id = '.$this->get_id(); |
||
425 | Database::query($sql); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Check if an evaluation name (with the same parent category) already exists. |
||
430 | * |
||
431 | * @param string $name to check (if not given, the name property of this object will be checked) |
||
432 | * @param $parent parent category |
||
433 | * |
||
434 | * @return bool |
||
435 | * @throws Exception |
||
436 | */ |
||
437 | public function does_name_exist($name, $parent) |
||
438 | { |
||
439 | if (!isset($name)) { |
||
440 | $name = $this->name; |
||
441 | $parent = $this->category; |
||
442 | } |
||
443 | $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
444 | $sql = "SELECT count(id) AS number |
||
445 | FROM $tbl_grade_evaluations |
||
446 | WHERE title = '".Database::escape_string($name)."'"; |
||
447 | |||
448 | if (api_is_allowed_to_edit()) { |
||
449 | $parent = Category::load($parent); |
||
450 | $courseId = $parent[0]->getCourseId(); |
||
451 | if (isset($courseId) && !empty($courseId)) { |
||
452 | $table = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
453 | $sql .= ' AND user_id IN ( |
||
454 | SELECT user_id FROM '.$table.' |
||
455 | WHERE |
||
456 | c_id = '.$courseId.' AND |
||
457 | status = '.COURSEMANAGER.' |
||
458 | )'; |
||
459 | } else { |
||
460 | $sql .= ' AND user_id = '.api_get_user_id(); |
||
461 | } |
||
462 | } else { |
||
463 | $sql .= ' AND user_id = '.api_get_user_id(); |
||
464 | } |
||
465 | |||
466 | if (!isset($parent)) { |
||
467 | $sql .= ' AND category_id is null'; |
||
468 | } else { |
||
469 | $sql .= ' AND category_id = '.intval($parent); |
||
470 | } |
||
471 | $result = Database::query($sql); |
||
472 | $number = Database::fetch_row($result); |
||
473 | |||
474 | return 0 != $number[0]; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Are there any results for this evaluation yet ? |
||
479 | * The 'max' property should not be changed then. |
||
480 | * |
||
481 | * @return bool |
||
482 | * @throws Exception |
||
483 | */ |
||
484 | public function has_results(): bool |
||
485 | { |
||
486 | $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
487 | $sql = 'SELECT count(id) AS number |
||
488 | FROM '.$table.' |
||
489 | WHERE evaluation_id = '.$this->get_id().' AND score IS NOT NULL'; |
||
490 | $result = Database::query($sql); |
||
491 | $number = Database::fetch_row($result); |
||
492 | |||
493 | return 0 != $number[0]; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Delete all results for this evaluation. |
||
498 | * @throws Exception |
||
499 | */ |
||
500 | public function delete_results(): void |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Delete this evaluation and all underlying results. |
||
510 | * @throws Exception |
||
511 | */ |
||
512 | public function delete_with_results(): void |
||
513 | { |
||
514 | $this->delete_results(); |
||
515 | $this->delete(); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Check if the given score is possible for this evaluation. |
||
520 | */ |
||
521 | public function is_valid_score(mixed $score): bool |
||
522 | { |
||
523 | return is_numeric($score) && $score >= 0 && $score <= $this->eval_max; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Calculate the score of this evaluation. |
||
528 | * |
||
529 | * @param int $studentId (default: all students who have results for this eval - then the average is returned) |
||
530 | * @param string $type (best, average, ranking) |
||
531 | * |
||
532 | * @return array (score, max) if student is given |
||
533 | * array (sum of scores, number of scores) otherwise |
||
534 | * or null if no scores available |
||
535 | */ |
||
536 | public function calc_score($studentId = null, $type = null): array |
||
537 | { |
||
538 | $allowStats = ('true' === api_get_setting('gradebook.allow_gradebook_stats')); |
||
539 | |||
540 | if ($allowStats) { |
||
541 | $evaluation = $this->entity; |
||
542 | if (!empty($evaluation)) { |
||
543 | $weight = $evaluation->getMax(); |
||
544 | switch ($type) { |
||
545 | case 'best': |
||
546 | $bestResult = $evaluation->getBestScore(); |
||
547 | $result = [$bestResult, $weight]; |
||
548 | |||
549 | return $result; |
||
550 | //break; |
||
551 | case 'average': |
||
552 | $count = count($evaluation->getUserScoreList()); |
||
553 | if (empty($count)) { |
||
554 | $result = [0, $weight]; |
||
555 | |||
556 | return $result; |
||
557 | } |
||
558 | |||
559 | $sumResult = array_sum($evaluation->getUserScoreList()); |
||
560 | $result = [$sumResult / $count, $weight]; |
||
561 | |||
562 | return $result; |
||
563 | //break; |
||
564 | case 'ranking': |
||
565 | $ranking = AbstractLink::getCurrentUserRanking($studentId, $evaluation->getUserScoreList()); |
||
566 | |||
567 | return $ranking; |
||
568 | //break; |
||
569 | default: |
||
570 | $weight = $evaluation->getMax(); |
||
571 | if (!empty($studentId)) { |
||
572 | $scoreList = $evaluation->getUserScoreList(); |
||
573 | $result = [0, $weight]; |
||
574 | if (isset($scoreList[$studentId])) { |
||
575 | $result = [$scoreList[$studentId], $weight]; |
||
576 | } |
||
577 | |||
578 | return $result; |
||
579 | } else { |
||
580 | $studentCount = count($evaluation->getUserScoreList()); |
||
581 | $sumResult = array_sum($evaluation->getUserScoreList()); |
||
582 | $result = [$sumResult, $studentCount]; |
||
583 | } |
||
584 | |||
585 | return $result; |
||
586 | //break; |
||
587 | } |
||
588 | } |
||
589 | } |
||
590 | |||
591 | $useSession = true; |
||
592 | if (isset($studentId) && empty($type)) { |
||
593 | $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id.'_'.$studentId; |
||
594 | $data = Session::read('calc_score'); |
||
595 | $results = isset($data[$key]) ? $data[$key] : null; |
||
596 | |||
597 | if (false == $useSession) { |
||
|
|||
598 | $results = null; |
||
599 | } |
||
600 | $results = null; |
||
601 | if (empty($results)) { |
||
602 | $results = Result::load(null, $studentId, $this->id); |
||
603 | Session::write('calc_score', [$key => $results]); |
||
604 | } |
||
605 | |||
606 | $score = null; |
||
607 | if (!empty($results)) { |
||
608 | /** @var Result $res */ |
||
609 | foreach ($results as $res) { |
||
610 | $score = $res->get_score(); |
||
611 | } |
||
612 | } |
||
613 | |||
614 | return [$score, $this->get_max()]; |
||
615 | } else { |
||
616 | $count = 0; |
||
617 | $sum = 0; |
||
618 | $bestResult = 0; |
||
619 | $weight = 0; |
||
620 | $sumResult = 0; |
||
621 | |||
622 | $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id; |
||
623 | $data = Session::read('calc_score'); |
||
624 | $allResults = isset($data[$key]) ? $data[$key] : null; |
||
625 | if (false == $useSession) { |
||
626 | $allResults = null; |
||
627 | } |
||
628 | |||
629 | if (empty($allResults)) { |
||
630 | $allResults = Result::load(null, null, $this->id); |
||
631 | Session::write($key, $allResults); |
||
632 | } |
||
633 | |||
634 | $students = []; |
||
635 | /** @var Result $res */ |
||
636 | foreach ($allResults as $res) { |
||
637 | $score = $res->get_score(); |
||
638 | if (!empty($score) || '0' == $score) { |
||
639 | $count++; |
||
640 | $sum += $score / $this->get_max(); |
||
641 | $sumResult += $score; |
||
642 | if ($score > $bestResult) { |
||
643 | $bestResult = $score; |
||
644 | } |
||
645 | $weight = $this->get_max(); |
||
646 | } |
||
647 | $students[$res->get_user_id()] = $score; |
||
648 | } |
||
649 | |||
650 | if (empty($count)) { |
||
651 | return [null, null]; |
||
652 | } |
||
653 | |||
654 | switch ($type) { |
||
655 | case 'best': |
||
656 | return [$bestResult, $weight]; |
||
657 | //break; |
||
658 | case 'average': |
||
659 | return [$sumResult / $count, $weight]; |
||
660 | //break; |
||
661 | case 'ranking': |
||
662 | $students = []; |
||
663 | /** @var Result $res */ |
||
664 | foreach ($allResults as $res) { |
||
665 | $score = $res->get_score(); |
||
666 | $students[$res->get_user_id()] = $score; |
||
667 | } |
||
668 | |||
669 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
670 | //break; |
||
671 | default: |
||
672 | return [$sum, $count]; |
||
673 | //break; |
||
674 | } |
||
675 | } |
||
676 | } |
||
677 | |||
678 | /** |
||
679 | * Generate an array of possible categories where this evaluation can be moved to. |
||
680 | * Notice: its own parent will be included in the list: it's up to the frontend |
||
681 | * to disable this element. |
||
682 | * |
||
683 | * @return array 2-dimensional array - every element contains 3 subelements (id, name, level) |
||
684 | */ |
||
685 | public function get_target_categories(): array |
||
686 | { |
||
687 | // - course independent evaluation |
||
688 | // -> movable to root or other course independent categories |
||
689 | // - evaluation inside a course |
||
690 | // -> movable to root, independent categories or categories inside the course |
||
691 | $user = api_is_platform_admin() ? null : api_get_user_id(); |
||
692 | $targets = []; |
||
693 | $level = 0; |
||
694 | $root = [0, get_lang('Main folder'), $level]; |
||
695 | $targets[] = $root; |
||
696 | |||
697 | if (!empty($this->courseId)) { |
||
698 | $crscats = Category::load(null, null, $this->courseId, 0); |
||
699 | foreach ($crscats as $cat) { |
||
700 | $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1]; |
||
701 | $targets = $this->addTargetSubcategories($targets, $level + 1, $cat->get_id()); |
||
702 | } |
||
703 | } |
||
704 | |||
705 | $indcats = Category::load(null, $user, 0, 0); |
||
706 | foreach ($indcats as $cat) { |
||
707 | $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1]; |
||
708 | $targets = $this->addTargetSubcategories( |
||
709 | $targets, |
||
710 | $level + 1, |
||
711 | $cat->get_id() |
||
712 | ); |
||
713 | } |
||
714 | |||
715 | return $targets; |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * Move this evaluation to the given category. |
||
720 | * If this evaluation moves from inside a course to outside, |
||
721 | * its course code is also changed. |
||
722 | */ |
||
723 | public function move_to_cat(GradebookCategory $cat) |
||
724 | { |
||
725 | $this->set_category_id($cat->getId()); |
||
726 | $categoryCourseId = $cat->getCourse()->getId(); |
||
727 | if ($this->getCourseId() != $categoryCourseId) { |
||
728 | $this->setCourseId($categoryCourseId); |
||
729 | } |
||
730 | $this->save(); |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * Retrieve evaluations where a student has results for |
||
735 | * and return them as an array of Evaluation objects. |
||
736 | * |
||
737 | * @param int $stud_id student id |
||
738 | * @param ?int $cat_id parent category (use 'null' to retrieve them in all categories) |
||
739 | * |
||
740 | * @return array |
||
741 | * @throws Exception |
||
742 | */ |
||
743 | public static function get_evaluations_with_result_for_student(int $stud_id, ?int $cat_id = null): array |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * Get a list of students that do not have a result record for this evaluation. |
||
769 | * |
||
770 | * @param string $first_letter_user |
||
771 | * |
||
772 | * @return array |
||
773 | */ |
||
774 | public function get_not_subscribed_students($first_letter_user = '') |
||
775 | { |
||
776 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
777 | $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
778 | |||
779 | $sql = "SELECT user_id,lastname,firstname,username |
||
780 | FROM $tbl_user |
||
781 | WHERE |
||
782 | lastname LIKE '".Database::escape_string($first_letter_user)."%' AND |
||
783 | status = ".STUDENT." AND user_id NOT IN ( |
||
784 | SELECT user_id FROM $table |
||
785 | WHERE evaluation_id = ".$this->get_id()." |
||
786 | ) |
||
787 | ORDER BY lastname"; |
||
788 | |||
789 | $result = Database::query($sql); |
||
790 | $users = Database::store_result($result); |
||
791 | |||
792 | return $users; |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * Find evaluations by name. |
||
797 | * |
||
798 | * @param string $name_mask search string |
||
799 | * |
||
800 | * @return array evaluation objects matching the search criterium |
||
801 | * |
||
802 | * @todo can be written more efficiently using a new (but very complex) sql query |
||
803 | */ |
||
804 | public static function findEvaluations($name_mask, $selectcat) |
||
805 | { |
||
806 | $rootcat = Category::load($selectcat); |
||
807 | $evals = $rootcat[0]->get_evaluations( |
||
808 | (api_is_allowed_to_create_course() ? null : api_get_user_id()), |
||
809 | true |
||
810 | ); |
||
811 | $foundevals = []; |
||
812 | foreach ($evals as $eval) { |
||
813 | if (!(false === api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)))) { |
||
814 | $foundevals[] = $eval; |
||
815 | } |
||
816 | } |
||
817 | |||
818 | return $foundevals; |
||
819 | } |
||
820 | |||
821 | public function get_item_type() |
||
822 | { |
||
823 | return 'E'; |
||
824 | } |
||
825 | |||
826 | public function get_icon_name() |
||
829 | } |
||
830 | |||
831 | /** |
||
832 | * Locks an evaluation, only one who can unlock it is the platform administrator. |
||
833 | * |
||
834 | * @param int locked 1 or unlocked 0 |
||
835 | */ |
||
836 | public function lock($locked) |
||
837 | { |
||
838 | $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION); |
||
839 | $sql = "UPDATE $table_evaluation |
||
840 | SET locked = '".intval($locked)."' |
||
841 | WHERE id='".$this->get_id()."'"; |
||
842 | Database::query($sql); |
||
843 | } |
||
844 | |||
845 | public function check_lock_permissions() |
||
846 | { |
||
847 | if (api_is_platform_admin()) { |
||
848 | return true; |
||
849 | } else { |
||
850 | if ($this->is_locked()) { |
||
851 | api_not_allowed(); |
||
852 | } |
||
853 | } |
||
854 | } |
||
855 | |||
856 | public function delete_linked_data() |
||
857 | { |
||
858 | } |
||
859 | |||
860 | /** |
||
861 | * @return mixed |
||
862 | */ |
||
863 | public function getStudentList() |
||
864 | { |
||
865 | return $this->studentList; |
||
866 | } |
||
867 | |||
868 | /** |
||
869 | * @param $list |
||
870 | */ |
||
871 | public function setStudentList($list) |
||
872 | { |
||
873 | $this->studentList = $list; |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * @param int $evaluationId |
||
878 | */ |
||
879 | public static function generateStats($evaluationId) |
||
880 | { |
||
881 | $allowStats = ('true' === api_get_setting('gradebook.allow_gradebook_stats')); |
||
882 | if ($allowStats) { |
||
883 | $evaluation = self::load($evaluationId); |
||
884 | |||
885 | $results = Result::load(null, null, $evaluationId); |
||
886 | $sumResult = 0; |
||
887 | $bestResult = 0; |
||
888 | $average = 0; |
||
889 | $scoreList = []; |
||
890 | |||
891 | if (!empty($results)) { |
||
892 | /** @var Result $result */ |
||
893 | foreach ($results as $result) { |
||
894 | $score = $result->get_score(); |
||
895 | $scoreList[$result->get_user_id()] = $score; |
||
896 | $sumResult += $score; |
||
897 | if ($score > $bestResult) { |
||
898 | $bestResult = $score; |
||
899 | } |
||
900 | } |
||
901 | $average = $sumResult / count($results); |
||
902 | } |
||
903 | |||
904 | /** @var Evaluation $evaluation */ |
||
905 | $evaluation = $evaluation[0]; |
||
906 | $evaluation = $evaluation->entity; |
||
907 | $evaluation |
||
908 | ->setBestScore($bestResult) |
||
909 | ->setAverageScore($average) |
||
910 | ->setUserScoreList($scoreList) |
||
911 | ; |
||
912 | |||
913 | $em = Database::getManager(); |
||
914 | $em->persist($evaluation); |
||
915 | $em->flush(); |
||
916 | } |
||
917 | } |
||
918 | |||
919 | /** |
||
920 | * |
||
921 | * @param int $courseId |
||
922 | * |
||
923 | * @return Evaluation |
||
924 | */ |
||
925 | public function setCourseId(?int $courseId = null): Evaluation |
||
937 | } |
||
938 | |||
939 | /** |
||
940 | * @param array $result |
||
941 | * |
||
942 | * @return array |
||
943 | */ |
||
944 | private static function create_evaluation_objects_from_sql_result($result) |
||
945 | { |
||
946 | $alleval = []; |
||
947 | $allow = ('true' === api_get_setting('gradebook.allow_gradebook_stats')); |
||
948 | if ($allow) { |
||
949 | $em = Database::getManager(); |
||
950 | $repo = $em->getRepository(GradebookEvaluation::class); |
||
951 | } |
||
952 | |||
953 | if (Database::num_rows($result)) { |
||
954 | while ($data = Database::fetch_array($result)) { |
||
955 | $eval = new Evaluation(); |
||
956 | $eval->set_id($data['id']); |
||
957 | $eval->set_name($data['title']); |
||
958 | $eval->set_description($data['description']); |
||
959 | $eval->set_user_id($data['user_id']); |
||
960 | $eval->setCourseId($data['c_id']); |
||
961 | $eval->set_category_id($data['category_id']); |
||
962 | $eval->set_date(api_get_local_time($data['created_at'])); |
||
963 | $eval->set_weight($data['weight']); |
||
964 | $eval->set_max($data['max']); |
||
965 | $eval->set_visible($data['visible']); |
||
966 | $eval->set_type($data['type']); |
||
967 | $eval->set_locked($data['locked']); |
||
968 | $eval->setSessionId(api_get_session_id()); |
||
969 | |||
970 | if ($allow) { |
||
971 | $eval->entity = $repo->find($data['id']); |
||
972 | } |
||
973 | |||
974 | $alleval[] = $eval; |
||
975 | } |
||
976 | } |
||
977 | |||
978 | return $alleval; |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * Internal function used by get_target_categories(). |
||
983 | * |
||
984 | * @param array $targets |
||
985 | * @param int $level |
||
986 | * @param int $categoryId |
||
987 | * |
||
988 | * @return array |
||
989 | */ |
||
990 | private function addTargetSubcategories($targets, $level, $categoryId) |
||
1003 | } |
||
1004 | } |
||
1005 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.