Total Complexity | 218 |
Total Lines | 1339 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like GradebookTable 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 GradebookTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class GradebookTable extends SortableTable |
||
18 | { |
||
19 | public $cats; |
||
20 | public $exportToPdf; |
||
21 | public $teacherView; |
||
22 | public $userId; |
||
23 | public $studentList = []; |
||
24 | private $currentcat; |
||
25 | private $datagen; |
||
26 | private $evals_links; |
||
27 | private $dataForGraph; |
||
28 | /** |
||
29 | * @var array Indicates which columns should be shown in gradebook |
||
30 | * |
||
31 | * @example [1] To add Ranking column |
||
32 | * [2] To add Best Score column |
||
33 | * [3] To add Average column |
||
34 | */ |
||
35 | private $loadStats = []; |
||
36 | |||
37 | /** |
||
38 | * GradebookTable constructor. |
||
39 | * |
||
40 | * @param Category $currentcat |
||
41 | * @param array $cats |
||
42 | * @param array $evals |
||
43 | * @param array $links |
||
44 | * @param null $addparams |
||
|
|||
45 | * @param bool $exportToPdf |
||
46 | * @param null $showTeacherView |
||
47 | * @param int $userId |
||
48 | * @param array $studentList |
||
49 | */ |
||
50 | public function __construct( |
||
166 | ); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return GradebookDataGenerator |
||
174 | */ |
||
175 | public function get_data() |
||
176 | { |
||
177 | return $this->datagen; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Function used by SortableTable to get total number of items in the table. |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | public function get_total_number_of_items() |
||
186 | { |
||
187 | return $this->datagen->get_total_items_count(); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getPreloadDataKey() |
||
194 | { |
||
195 | return 'default_data_'.api_get_course_id().'_'.api_get_session_id(); |
||
196 | } |
||
197 | |||
198 | public function preloadData() |
||
199 | { |
||
200 | $allitems = $this->datagen->items; |
||
201 | usort($allitems, ['GradebookDataGenerator', 'sort_by_name']); |
||
202 | $visibleItems = array_merge($this->datagen->items, $this->evals_links); |
||
203 | $defaultDataFromSession = Session::read($this->getPreloadDataKey()); |
||
204 | if (empty($defaultDataFromSession)) { |
||
205 | $defaultData = []; |
||
206 | /** @var GradebookItem $item */ |
||
207 | foreach ($visibleItems as $item) { |
||
208 | $item->setStudentList($this->studentList); |
||
209 | $itemType = get_class($item); |
||
210 | switch ($itemType) { |
||
211 | case 'Evaluation': |
||
212 | // Best |
||
213 | $best = $this->datagen->buildBestResultColumn($item); |
||
214 | $defaultData[$item->get_id()]['best'] = $best; |
||
215 | // Average |
||
216 | $average = $this->datagen->buildAverageResultColumn($item); |
||
217 | $defaultData[$item->get_id()]['average'] = $average; |
||
218 | break; |
||
219 | case 'ExerciseLink': |
||
220 | /** @var ExerciseLink $item */ |
||
221 | // Best |
||
222 | $best = $this->datagen->buildBestResultColumn($item); |
||
223 | $defaultData[$item->get_id()]['best'] = $best; |
||
224 | // Average |
||
225 | $average = $this->datagen->buildAverageResultColumn($item); |
||
226 | $defaultData[$item->get_id()]['average'] = $average; |
||
227 | // Ranking |
||
228 | /*if (!empty($this->studentList)) { |
||
229 | $invalidateRanking = true; |
||
230 | foreach ($this->studentList as $user) { |
||
231 | $score = $this->datagen->build_result_column( |
||
232 | $user['user_id'], |
||
233 | $item, |
||
234 | false, |
||
235 | true |
||
236 | ); |
||
237 | if (!empty($score['score'])) { |
||
238 | $invalidateRanking = false; |
||
239 | } |
||
240 | $rankingStudentList[$user['user_id']] = $score['score'][0]; |
||
241 | $defaultData[$item->get_id()]['ranking'] = $rankingStudentList; |
||
242 | $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateRanking; |
||
243 | } |
||
244 | }*/ |
||
245 | break; |
||
246 | default: |
||
247 | // Best |
||
248 | $best = $this->datagen->buildBestResultColumn($item); |
||
249 | $defaultData[$item->get_id()]['best'] = $best; |
||
250 | |||
251 | // Average |
||
252 | $average = $this->datagen->buildAverageResultColumn($item); |
||
253 | $defaultData[$item->get_id()]['average'] = $average; |
||
254 | |||
255 | // Ranking |
||
256 | if (!empty($this->studentList)) { |
||
257 | $invalidateRanking = true; |
||
258 | foreach ($this->studentList as $user) { |
||
259 | $score = $this->datagen->build_result_column( |
||
260 | $user['user_id'], |
||
261 | $item, |
||
262 | false, |
||
263 | true |
||
264 | ); |
||
265 | if (!empty($score['score'])) { |
||
266 | $invalidateRanking = false; |
||
267 | } |
||
268 | $rankingStudentList[$user['user_id']] = $score['score'][0]; |
||
269 | $defaultData[$item->get_id()]['ranking'] = $rankingStudentList; |
||
270 | $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateRanking; |
||
271 | } |
||
272 | } |
||
273 | break; |
||
274 | } |
||
275 | } |
||
276 | Session::write($this->getPreloadDataKey(), $defaultData); |
||
277 | } else { |
||
278 | $defaultData = $defaultDataFromSession; |
||
279 | } |
||
280 | |||
281 | return $defaultData; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Function used by SortableTable to generate the data to display. |
||
286 | * |
||
287 | * @param int $from |
||
288 | * @param int $per_page |
||
289 | * @param int $column |
||
290 | * @param string $direction |
||
291 | * @param int $sort |
||
292 | * |
||
293 | * @return array|mixed |
||
294 | */ |
||
295 | public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = null) |
||
296 | { |
||
297 | //variables load in index.php |
||
298 | global $certificate_min_score; |
||
299 | |||
300 | $isAllowedToEdit = api_is_allowed_to_edit(); |
||
301 | $hideLinkForStudent = api_get_configuration_value('gradebook_hide_link_to_item_for_student') ?? false; |
||
302 | // determine sorting type |
||
303 | $col_adjust = $isAllowedToEdit ? 1 : 0; |
||
304 | // By id |
||
305 | $this->column = 5; |
||
306 | |||
307 | switch ($this->column) { |
||
308 | // Type |
||
309 | case 0 + $col_adjust: |
||
310 | $sorting = GradebookDataGenerator::GDG_SORT_TYPE; |
||
311 | break; |
||
312 | case 1 + $col_adjust: |
||
313 | $sorting = GradebookDataGenerator::GDG_SORT_NAME; |
||
314 | break; |
||
315 | case 2 + $col_adjust: |
||
316 | $sorting = GradebookDataGenerator::GDG_SORT_DESCRIPTION; |
||
317 | break; |
||
318 | case 3 + $col_adjust: |
||
319 | $sorting = GradebookDataGenerator::GDG_SORT_WEIGHT; |
||
320 | break; |
||
321 | case 4 + $col_adjust: |
||
322 | $sorting = GradebookDataGenerator::GDG_SORT_DATE; |
||
323 | break; |
||
324 | case 5 + $col_adjust: |
||
325 | $sorting = GradebookDataGenerator::GDG_SORT_ID; |
||
326 | break; |
||
327 | } |
||
328 | |||
329 | if ('DESC' === $this->direction) { |
||
330 | $sorting |= GradebookDataGenerator::GDG_SORT_DESC; |
||
331 | } else { |
||
332 | $sorting |= GradebookDataGenerator::GDG_SORT_ASC; |
||
333 | } |
||
334 | |||
335 | // Status of user in course. |
||
336 | $user_id = $this->userId; |
||
337 | $course_code = api_get_course_id(); |
||
338 | $session_id = api_get_session_id(); |
||
339 | |||
340 | $statusToFilter = 0; |
||
341 | if (empty($session_id)) { |
||
342 | $statusToFilter = STUDENT; |
||
343 | } |
||
344 | |||
345 | if (empty($this->studentList) && $this->loadStats) { |
||
346 | $studentList = CourseManager::get_user_list_from_course_code( |
||
347 | $course_code, |
||
348 | $session_id, |
||
349 | null, |
||
350 | null, |
||
351 | $statusToFilter |
||
352 | ); |
||
353 | $this->studentList = $studentList; |
||
354 | } |
||
355 | |||
356 | $this->datagen->userId = $this->userId; |
||
357 | $data_array = $this->datagen->get_data( |
||
358 | $sorting, |
||
359 | $from, |
||
360 | $this->per_page, |
||
361 | false, |
||
362 | $this->studentList, |
||
363 | $this->loadStats |
||
364 | ); |
||
365 | |||
366 | // generate the data to display |
||
367 | $sortable_data = []; |
||
368 | $weight_total_links = 0; |
||
369 | $main_cat = Category::load( |
||
370 | null, |
||
371 | null, |
||
372 | $course_code, |
||
373 | null, |
||
374 | null, |
||
375 | $session_id, |
||
376 | 'ORDER BY id' |
||
377 | ); |
||
378 | |||
379 | $total_categories_weight = 0; |
||
380 | $scoredisplay = ScoreDisplay::instance(); |
||
381 | $totalBest = [0, 0]; |
||
382 | $totalAverage = [0, 0]; |
||
383 | |||
384 | $type = 'detail'; |
||
385 | if ($this->exportToPdf) { |
||
386 | $type = 'simple'; |
||
387 | } |
||
388 | |||
389 | $model = ExerciseLib::getCourseScoreModel(); |
||
390 | $userExerciseScoreInCategory = api_get_configuration_value( |
||
391 | 'gradebook_use_exercise_score_settings_in_categories' |
||
392 | ); |
||
393 | $useExerciseScoreInTotal = api_get_configuration_value('gradebook_use_exercise_score_settings_in_total'); |
||
394 | $course_code = api_get_course_id(); |
||
395 | $session_id = api_get_session_id(); |
||
396 | $defaultData = Session::read($this->getPreloadDataKey()); |
||
397 | $settings = api_get_configuration_value('gradebook_pdf_export_settings'); |
||
398 | $showWeight = true; |
||
399 | if ($this->exportToPdf && isset($settings['hide_score_weight']) && $settings['hide_score_weight']) { |
||
400 | $showWeight = false; |
||
401 | } |
||
402 | $totalAverageList = []; |
||
403 | // Categories. |
||
404 | if (!empty($data_array)) { |
||
405 | foreach ($data_array as $data) { |
||
406 | // list of items inside the gradebook (exercises, lps, forums, etc) |
||
407 | $row = []; |
||
408 | /** @var AbstractLink $item */ |
||
409 | $item = $data[0]; |
||
410 | // If the item is invisible, wrap it in a span with class invisible |
||
411 | $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '<span class="text-muted">' : ''; |
||
412 | $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '</span>' : ''; |
||
413 | |||
414 | if ($this->teacherView) { |
||
415 | if (false == $this->exportToPdf) { |
||
416 | $row[] = $this->build_id_column($item); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | // Type. |
||
421 | $row[] = $this->build_type_column($item); |
||
422 | |||
423 | // Name. |
||
424 | if ('Category' === get_class($item)) { |
||
425 | $row[] = $invisibility_span_open. |
||
426 | '<strong>'.Security::remove_XSS($item->get_name()).'</strong>'.$invisibility_span_close; |
||
427 | $main_categories[$item->get_id()]['name'] = $item->get_name(); |
||
428 | } else { |
||
429 | |||
430 | // If the item type is 'Evaluation', or the user is not a student, |
||
431 | // or 'gradebook_hide_link_to_item_for_student' it's true, make links |
||
432 | if ($item->get_item_type() === 'E' || $isAllowedToEdit || !$hideLinkForStudent) { |
||
433 | $name = Security::remove_XSS($this->build_name_link($item, $type)); |
||
434 | } else { |
||
435 | $name = Security::remove_XSS( |
||
436 | $item->get_name().' '.Display::label($item->get_type_name(), 'info') |
||
437 | ); |
||
438 | } |
||
439 | |||
440 | $row[] = $invisibility_span_open.$name.$invisibility_span_close; |
||
441 | $main_categories[$item->get_id()]['name'] = $name; |
||
442 | } |
||
443 | |||
444 | $this->dataForGraph['categories'][] = $item->get_name(); |
||
445 | $main_categories[$item->get_id()]['weight'] = $item->get_weight(); |
||
446 | $total_categories_weight += $item->get_weight(); |
||
447 | |||
448 | // Description. |
||
449 | if (false == $this->exportToPdf) { |
||
450 | $row[] = $invisibility_span_open.$data[2].$invisibility_span_close; |
||
451 | } |
||
452 | |||
453 | // Weight. |
||
454 | $weight = $scoredisplay->display_score( |
||
455 | [ |
||
456 | $data['3'], |
||
457 | $this->currentcat->get_weight(), |
||
458 | ], |
||
459 | SCORE_SIMPLE, |
||
460 | SCORE_BOTH, |
||
461 | true |
||
462 | ); |
||
463 | |||
464 | if ($showWeight) { |
||
465 | if ($this->teacherView) { |
||
466 | $row[] = $invisibility_span_open. |
||
467 | Display::tag('p', $weight, ['class' => 'score']). |
||
468 | $invisibility_span_close; |
||
469 | } else { |
||
470 | $row[] = $invisibility_span_open.$weight.$invisibility_span_close; |
||
471 | } |
||
472 | } |
||
473 | |||
474 | $category_weight = $item->get_weight(); |
||
475 | if ($this->teacherView) { |
||
476 | $weight_total_links += $data[3]; |
||
477 | } |
||
478 | |||
479 | // Edit (for admins). |
||
480 | if ($this->teacherView) { |
||
481 | $cat = new Category(); |
||
482 | $show_message = $cat->show_message_resource_delete($item->get_course_code()); |
||
483 | if ($show_message === false) { |
||
484 | $row[] = $this->build_edit_column($item); |
||
485 | } |
||
486 | } else { |
||
487 | // Students get the results and certificates columns |
||
488 | $value_data = isset($data[4]) ? $data[4] : null; |
||
489 | $best = isset($data['best']) ? $data['best'] : null; |
||
490 | $average = isset($data['average']) ? $data['average'] : null; |
||
491 | $ranking = isset($data['ranking']) ? $data['ranking'] : null; |
||
492 | |||
493 | $totalResult = [ |
||
494 | $data['result_score'][0] ?? null, |
||
495 | $data['result_score'][1] ?? null, |
||
496 | ]; |
||
497 | |||
498 | if (empty($model)) { |
||
499 | $totalBest = [ |
||
500 | $scoredisplay->format_score($totalBest[0] + (empty($data['best_score'][0]) ? 0 : $data['best_score'][0])), |
||
501 | $scoredisplay->format_score($totalBest[1] + (empty($data['best_score'][1]) ? 0 : $data['best_score'][1])), |
||
502 | ]; |
||
503 | $totalAverage = [0, 0]; |
||
504 | if (isset($data['average_score']) && !empty($data['average_score'])) { |
||
505 | $totalAverage = [ |
||
506 | $data['average_score'][0], |
||
507 | $data['average_score'][1], |
||
508 | ]; |
||
509 | } |
||
510 | } |
||
511 | |||
512 | // Score |
||
513 | if (empty($model)) { |
||
514 | $row[] = $value_data; |
||
515 | } else { |
||
516 | $row[] = ExerciseLib::show_score( |
||
517 | $data['result_score'][0], |
||
518 | $data['result_score'][1] |
||
519 | ); |
||
520 | } |
||
521 | |||
522 | $totalAverageList[$item->get_id()] = $totalAverage; |
||
523 | $mode = SCORE_AVERAGE; |
||
524 | if ($userExerciseScoreInCategory) { |
||
525 | $mode = SCORE_SIMPLE; |
||
526 | $result = ExerciseLib::convertScoreToPlatformSetting($totalAverage[0], $totalAverage[1]); |
||
527 | $totalAverage[0] = $result['score']; |
||
528 | $totalAverage[1] = $result['weight']; |
||
529 | |||
530 | $result = ExerciseLib::convertScoreToPlatformSetting($totalResult[0], $totalResult[1]); |
||
531 | $totalResult[0] = $result['score']; |
||
532 | $totalResult[1] = $result['weight']; |
||
533 | |||
534 | $result = ExerciseLib::convertScoreToPlatformSetting( |
||
535 | $data['result_score'][0], |
||
536 | $data['result_score'][1] |
||
537 | ); |
||
538 | $data['my_result_no_float'][0] = $result['score']; |
||
539 | } |
||
540 | |||
541 | $totalResultAverageValue = strip_tags( |
||
542 | $scoredisplay->display_score($totalResult, $mode, null, false, false, true) |
||
543 | ); |
||
544 | $totalAverageValue = strip_tags( |
||
545 | $scoredisplay->display_score($totalAverage, $mode, null, false, false, true) |
||
546 | ); |
||
547 | |||
548 | $this->dataForGraph['my_result'][] = floatval($totalResultAverageValue); |
||
549 | $this->dataForGraph['average'][] = floatval($totalAverageValue); |
||
550 | $this->dataForGraph['my_result_no_float'][] = $data['result_score'][0] ?? null; |
||
551 | |||
552 | if (empty($model)) { |
||
553 | // Ranking |
||
554 | if (in_array(1, $this->loadStats)) { |
||
555 | $row[] = $ranking; |
||
556 | } |
||
557 | |||
558 | // Best |
||
559 | if (in_array(2, $this->loadStats)) { |
||
560 | $row[] = $best; |
||
561 | } |
||
562 | |||
563 | // Average |
||
564 | if (in_array(3, $this->loadStats)) { |
||
565 | $row[] = $average; |
||
566 | } |
||
567 | } |
||
568 | |||
569 | if ('Category' === get_class($item)) { |
||
570 | if (false == $this->exportToPdf) { |
||
571 | $row[] = $this->build_edit_column($item); |
||
572 | } |
||
573 | } |
||
574 | } |
||
575 | |||
576 | // Category added. |
||
577 | $sortable_data[] = $row; |
||
578 | |||
579 | // Loading children |
||
580 | if ('Category' === get_class($item)) { |
||
581 | $parent_id = $item->get_id(); |
||
582 | $cats = Category::load( |
||
583 | $parent_id, |
||
584 | null, |
||
585 | null, |
||
586 | null, |
||
587 | null, |
||
588 | null |
||
589 | ); |
||
590 | |||
591 | if (isset($cats[0])) { |
||
592 | /** @var Category $subCategory */ |
||
593 | $subCategory = $cats[0]; |
||
594 | $allcat = $subCategory->get_subcategories($this->userId, $course_code, $session_id); |
||
595 | $alleval = $subCategory->get_evaluations($this->userId); |
||
596 | $alllink = $subCategory->get_links($this->userId); |
||
597 | |||
598 | $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink); |
||
599 | $sub_cat_info->exportToPdf = $this->exportToPdf; |
||
600 | $sub_cat_info->preLoadDataKey = $this->getPreloadDataKey(); |
||
601 | $sub_cat_info->userId = $user_id; |
||
602 | |||
603 | $data_array2 = $sub_cat_info->get_data( |
||
604 | $sorting, |
||
605 | $from, |
||
606 | $this->per_page, |
||
607 | false, |
||
608 | $this->studentList |
||
609 | ); |
||
610 | $total_weight = 0; |
||
611 | |||
612 | // Links. |
||
613 | foreach ($data_array2 as $data) { |
||
614 | $row = []; |
||
615 | $item = $data[0]; |
||
616 | // if the item is invisible, wrap it in a span with class invisible |
||
617 | $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '<span class="text-muted">' : ''; |
||
618 | $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '</span>' : ''; |
||
619 | |||
620 | if (isset($item)) { |
||
621 | $main_categories[$parent_id]['children'][$item->get_id()]['name'] = $item->get_name(); |
||
622 | $main_categories[$parent_id]['children'][$item->get_id()]['weight'] = $item->get_weight(); |
||
623 | } |
||
624 | |||
625 | if ($this->teacherView) { |
||
626 | if (false == $this->exportToPdf) { |
||
627 | $row[] = $this->build_id_column($item); |
||
628 | } |
||
629 | } |
||
630 | |||
631 | // Type |
||
632 | $row[] = $this->build_type_column($item, ['style' => 'padding-left:5px']); |
||
633 | // Name. |
||
634 | $row[] = $invisibility_span_open.' '. |
||
635 | Security::remove_XSS($this->build_name_link($item, $type, 4)).$invisibility_span_close; |
||
636 | |||
637 | // Description. |
||
638 | if (false == $this->exportToPdf) { |
||
639 | $row[] = $invisibility_span_open.$data[2].$invisibility_span_close; |
||
640 | } |
||
641 | |||
642 | $weight = $data[3]; |
||
643 | $total_weight += $weight; |
||
644 | |||
645 | // Weight |
||
646 | if ($showWeight) { |
||
647 | $row[] = $invisibility_span_open.$weight.$invisibility_span_close; |
||
648 | } |
||
649 | |||
650 | // Admins get an edit column. |
||
651 | if (api_is_allowed_to_edit(null, true) && |
||
652 | isset($_GET['user_id']) == false && |
||
653 | (isset($_GET['action']) && $_GET['action'] != 'export_all' || !isset($_GET['action'])) |
||
654 | ) { |
||
655 | $cat = new Category(); |
||
656 | $show_message = $cat->show_message_resource_delete($item->get_course_code()); |
||
657 | if ($show_message === false) { |
||
658 | if ($this->exportToPdf == false) { |
||
659 | $row[] = $this->build_edit_column($item); |
||
660 | } |
||
661 | } |
||
662 | } else { |
||
663 | // Students get the results and certificates columns |
||
664 | $eval_n_links = array_merge($alleval, $alllink); |
||
665 | if (count($eval_n_links) > 0) { |
||
666 | $value_data = isset($data[4]) ? $data[4] : null; |
||
667 | if (!is_null($value_data)) { |
||
668 | // Result |
||
669 | $row[] = $value_data; |
||
670 | $best = isset($data['best']) ? $data['best'] : null; |
||
671 | $average = isset($data['average']) ? $data['average'] : null; |
||
672 | $ranking = isset($data['ranking']) ? $data['ranking'] : null; |
||
673 | if (empty($model)) { |
||
674 | // Ranking |
||
675 | if (in_array(1, $this->loadStats)) { |
||
676 | $row[] = $ranking; |
||
677 | } |
||
678 | |||
679 | // Best |
||
680 | if (in_array(2, $this->loadStats)) { |
||
681 | $row[] = $best; |
||
682 | } |
||
683 | |||
684 | // Average |
||
685 | if (in_array(3, $this->loadStats)) { |
||
686 | $row[] = $average; |
||
687 | } |
||
688 | } |
||
689 | } |
||
690 | } |
||
691 | |||
692 | if (!empty($cats)) { |
||
693 | if (false == $this->exportToPdf) { |
||
694 | $row[] = null; |
||
695 | } |
||
696 | } |
||
697 | } |
||
698 | |||
699 | if (false == $this->exportToPdf) { |
||
700 | $row['child_of'] = $parent_id; |
||
701 | } |
||
702 | $sortable_data[] = $row; |
||
703 | } |
||
704 | |||
705 | // "Warning row" |
||
706 | if (!empty($data_array)) { |
||
707 | if ($this->teacherView) { |
||
708 | // Compare the category weight to the sum of all weights inside the category |
||
709 | if (intval($total_weight) == $category_weight) { |
||
710 | $label = null; |
||
711 | $total = GradebookUtils::score_badges( |
||
712 | [ |
||
713 | $total_weight.' / '.$category_weight, |
||
714 | '100', |
||
715 | ] |
||
716 | ); |
||
717 | } else { |
||
718 | $label = Display::return_icon( |
||
719 | 'warning.png', |
||
720 | sprintf(get_lang('TotalWeightMustBeX'), $category_weight) |
||
721 | ); |
||
722 | $total = Display::badge($total_weight.' / '.$category_weight, 'warning'); |
||
723 | } |
||
724 | $row = [ |
||
725 | null, |
||
726 | null, |
||
727 | " <h5>".get_lang('SubTotal').'</h5>', |
||
728 | null, |
||
729 | $total.' '.$label, |
||
730 | 'child_of' => $parent_id, |
||
731 | ]; |
||
732 | $sortable_data[] = $row; |
||
733 | } |
||
734 | } |
||
735 | } |
||
736 | } |
||
737 | } |
||
738 | } //end looping categories |
||
739 | |||
740 | $main_weight = 0; |
||
741 | if (count($main_cat) > 1) { |
||
742 | /** @var Category $myCat */ |
||
743 | foreach ($main_cat as $myCat) { |
||
744 | $myParentId = $myCat->get_parent_id(); |
||
745 | if (0 == $myParentId) { |
||
746 | $main_weight = (int) $myCat->get_weight(); |
||
747 | } |
||
748 | } |
||
749 | } |
||
750 | |||
751 | if ($this->teacherView) { |
||
752 | // Total for teacher. |
||
753 | if (count($main_cat) > 1) { |
||
754 | if (intval($total_categories_weight) == $main_weight) { |
||
755 | $total = GradebookUtils::score_badges( |
||
756 | [ |
||
757 | $total_categories_weight.' / '.$main_weight, |
||
758 | '100', |
||
759 | ] |
||
760 | ); |
||
761 | } else { |
||
762 | $total = Display::badge($total_categories_weight.' / '.$main_weight, 'warning'); |
||
763 | } |
||
764 | $row = [ |
||
765 | null, |
||
766 | null, |
||
767 | '<strong>'.get_lang('Total').'</strong>', |
||
768 | null, |
||
769 | $total, |
||
770 | ]; |
||
771 | $sortable_data[] = $row; |
||
772 | } |
||
773 | } else { |
||
774 | $showPercentage = false === $this->datagen->hidePercentage; |
||
775 | // Total for student. |
||
776 | if (count($main_cat) > 1) { |
||
777 | $main_weight = (int) $main_cat[0]->get_weight(); |
||
778 | $global = null; |
||
779 | $average = null; |
||
780 | $myTotal = 0; |
||
781 | if (!empty($this->dataForGraph)) { |
||
782 | foreach ($this->dataForGraph['my_result_no_float'] as $result) { |
||
783 | $myTotal += $result; |
||
784 | } |
||
785 | } |
||
786 | |||
787 | $totalResult[0] = $myTotal; |
||
788 | // Overwrite main weight |
||
789 | $totalResult[1] = $main_weight; |
||
790 | |||
791 | if (!empty($model)) { |
||
792 | $totalResult = ExerciseLib::show_score( |
||
793 | $totalResult[0], |
||
794 | $totalResult[1] |
||
795 | ); |
||
796 | } else { |
||
797 | $totalResult = $scoredisplay->display_score( |
||
798 | $totalResult, |
||
799 | SCORE_DIV, |
||
800 | null, |
||
801 | false, |
||
802 | false, |
||
803 | true |
||
804 | ); |
||
805 | |||
806 | if ($useExerciseScoreInTotal) { |
||
807 | $totalResult = ExerciseLib::show_score($myTotal, $main_weight, false); |
||
808 | } |
||
809 | } |
||
810 | |||
811 | $row = [ |
||
812 | null, |
||
813 | '<strong>'.get_lang('Total').'</strong>', |
||
814 | ]; |
||
815 | |||
816 | if (!$this->exportToPdf) { |
||
817 | $row[] = null; |
||
818 | } |
||
819 | |||
820 | if ($showWeight) { |
||
821 | $row[] = $main_weight; |
||
822 | } |
||
823 | |||
824 | $row[] = $totalResult; |
||
825 | $categoryId = $main_cat[0]->get_id(); |
||
826 | |||
827 | if (empty($model)) { |
||
828 | if (in_array(1, $this->loadStats)) { |
||
829 | if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['ranking'])) { |
||
830 | $totalRanking = $defaultData[$categoryId]['ranking']; |
||
831 | $invalidateRanking = $defaultData[$categoryId]['ranking_invalidate']; |
||
832 | $average = 0; |
||
833 | foreach ($totalRanking as $ranking) { |
||
834 | $average += $ranking; |
||
835 | } |
||
836 | } else { |
||
837 | $totalRanking = []; |
||
838 | $invalidateRanking = true; |
||
839 | $average = 0; |
||
840 | $main_cat[0]->setStudentList($this->studentList); |
||
841 | foreach ($this->studentList as $student) { |
||
842 | $score = $main_cat[0]->calc_score( |
||
843 | $student['user_id'], |
||
844 | null, |
||
845 | $course_code, |
||
846 | $session_id |
||
847 | ); |
||
848 | if (!empty($score[0])) { |
||
849 | $invalidateRanking = false; |
||
850 | } |
||
851 | $totalRanking[$student['user_id']] = $score[0]; |
||
852 | $average += $score[0]; |
||
853 | } |
||
854 | $defaultData[$categoryId]['ranking'] = $totalRanking; |
||
855 | $defaultData[$categoryId]['ranking_invalidate'] = $invalidateRanking; |
||
856 | Session::write($this->getPreloadDataKey(), $defaultData); |
||
857 | } |
||
858 | |||
859 | $totalRanking = AbstractLink::getCurrentUserRanking($user_id, $totalRanking); |
||
860 | $totalRanking = $scoredisplay->display_score( |
||
861 | $totalRanking, |
||
862 | SCORE_DIV, |
||
863 | SCORE_BOTH, |
||
864 | true, |
||
865 | true, |
||
866 | true |
||
867 | ); |
||
868 | |||
869 | if ($invalidateRanking) { |
||
870 | $totalRanking = null; |
||
871 | } |
||
872 | $row[] = $totalRanking; |
||
873 | } |
||
874 | |||
875 | if (in_array(2, $this->loadStats)) { |
||
876 | if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['best'])) { |
||
877 | $totalBest = $defaultData[$categoryId]['best']; |
||
878 | } else { |
||
879 | // Overwrite main weight |
||
880 | $totalBest[1] = $main_weight; |
||
881 | $defaultData[$categoryId]['best'] = $totalBest; |
||
882 | } |
||
883 | |||
884 | if ($useExerciseScoreInTotal) { |
||
885 | if (isset($totalBest['score'])) { |
||
886 | $totalBestScore = $totalBest['score']; |
||
887 | } else { |
||
888 | $totalBestScore = $totalBest; |
||
889 | } |
||
890 | |||
891 | $totalBest = ExerciseLib::show_score($totalBestScore[0], $totalBestScore[1], $showPercentage); |
||
892 | } else { |
||
893 | $totalBest = $scoredisplay->display_score( |
||
894 | $totalBest, |
||
895 | SCORE_DIV, |
||
896 | SCORE_BOTH, |
||
897 | true, |
||
898 | false, |
||
899 | true |
||
900 | ); |
||
901 | } |
||
902 | $row[] = $totalBest; |
||
903 | } |
||
904 | |||
905 | if (in_array(3, $this->loadStats)) { |
||
906 | if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['average'])) { |
||
907 | $totalAverage = $defaultData[$categoryId]['average']; |
||
908 | } else { |
||
909 | $averageWeight = 0; |
||
910 | $categoryAverage = 0; |
||
911 | foreach ($totalAverageList as $averageScore) { |
||
912 | $categoryAverage += $averageScore[0]; |
||
913 | $averageWeight += $averageScore[1]; |
||
914 | } |
||
915 | $categoryAverage = $categoryAverage / count($totalAverageList); |
||
916 | //$averageWeight = $averageWeight ($totalAverageList); |
||
917 | |||
918 | // Overwrite main weight |
||
919 | //$totalAverage[0] = $average / count($this->studentList); |
||
920 | //$totalAverage[1] = $main_weight; |
||
921 | $totalAverage[0] = $categoryAverage; |
||
922 | $totalAverage[1] = $averageWeight; |
||
923 | //$defaultData[$categoryId]['average'] = $totalBest; |
||
924 | } |
||
925 | |||
926 | if ($useExerciseScoreInTotal) { |
||
927 | if (isset($totalAverage['score'])) { |
||
928 | $totalAverageScore = $totalAverage['score']; |
||
929 | } else { |
||
930 | $totalAverageScore = $totalAverage; |
||
931 | } |
||
932 | |||
933 | $totalAverage = ExerciseLib::show_score($totalAverageScore[0], $totalAverageScore[1], $showPercentage); |
||
934 | } else { |
||
935 | $totalAverage = $scoredisplay->display_score( |
||
936 | $totalAverage, |
||
937 | SCORE_DIV, |
||
938 | SCORE_BOTH, |
||
939 | true, |
||
940 | false, |
||
941 | true |
||
942 | ); |
||
943 | } |
||
944 | |||
945 | $row[] = $totalAverage; |
||
946 | } |
||
947 | } |
||
948 | |||
949 | if (!empty($row)) { |
||
950 | $sortable_data[] = $row; |
||
951 | } |
||
952 | } |
||
953 | } |
||
954 | |||
955 | Session::write('default_data', $defaultData); |
||
956 | |||
957 | // Warning messages |
||
958 | $view = isset($_GET['view']) ? $_GET['view'] : null; |
||
959 | if ($this->teacherView) { |
||
960 | if (isset($_GET['selectcat']) && |
||
961 | $_GET['selectcat'] > 0 && |
||
962 | $view !== 'presence' |
||
963 | ) { |
||
964 | $id_cat = (int) $_GET['selectcat']; |
||
965 | $category = Category::load($id_cat); |
||
966 | $weight_category = (int) $this->build_weight($category[0]); |
||
967 | $course_code = $this->build_course_code($category[0]); |
||
968 | $weight_total_links = round($weight_total_links); |
||
969 | |||
970 | if ($weight_total_links > $weight_category || |
||
971 | $weight_total_links < $weight_category || |
||
972 | $weight_total_links > $weight_category |
||
973 | ) { |
||
974 | $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $weight_category); |
||
975 | $modify_icons = |
||
976 | '<a |
||
977 | href="gradebook_edit_cat.php?editcat='.$id_cat.'&cidReq='.$course_code.'&id_session='.api_get_session_id().'">'. |
||
978 | Display::return_icon('edit.png', $warning_message, [], ICON_SIZE_SMALL).'</a>'; |
||
979 | $warning_message .= $modify_icons; |
||
980 | echo Display::return_message($warning_message, 'warning', false); |
||
981 | } |
||
982 | |||
983 | $content_html = DocumentManager::replace_user_info_into_html( |
||
984 | api_get_user_id(), |
||
985 | $course_code, |
||
986 | api_get_session_id() |
||
987 | ); |
||
988 | |||
989 | if (!empty($content_html)) { |
||
990 | $new_content = explode('</head>', $content_html['content']); |
||
991 | } |
||
992 | |||
993 | if (empty($new_content[0])) { |
||
994 | // Set default certificate |
||
995 | $courseData = api_get_course_info($course_code); |
||
996 | DocumentManager::generateDefaultCertificate($courseData); |
||
997 | } |
||
998 | } |
||
999 | |||
1000 | if (empty($_GET['selectcat'])) { |
||
1001 | $categories = Category::load(); |
||
1002 | $weight_categories = $certificate_min_scores = $course_codes = []; |
||
1003 | foreach ($categories as $category) { |
||
1004 | $course_code_category = $this->build_course_code($category); |
||
1005 | if (!empty($course_code)) { |
||
1006 | if ($course_code_category == $course_code) { |
||
1007 | $weight_categories[] = intval($this->build_weight($category)); |
||
1008 | $certificate_min_scores[] = intval($this->build_certificate_min_score($category)); |
||
1009 | $course_codes[] = $course_code; |
||
1010 | break; |
||
1011 | } |
||
1012 | } else { |
||
1013 | $weight_categories[] = intval($this->build_weight($category)); |
||
1014 | $certificate_min_scores[] = intval($this->build_certificate_min_score($category)); |
||
1015 | $course_codes[] = $course_code_category; |
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | if (is_array($weight_categories) && |
||
1020 | is_array($certificate_min_scores) && |
||
1021 | is_array($course_codes) |
||
1022 | ) { |
||
1023 | $warning_message = ''; |
||
1024 | for ($x = 0; $x < count($weight_categories); $x++) { |
||
1025 | $weight_category = intval($weight_categories[$x]); |
||
1026 | $certificate_min_score = intval($certificate_min_scores[$x]); |
||
1027 | $course_code = $course_codes[$x]; |
||
1028 | |||
1029 | if (empty($certificate_min_score) || |
||
1030 | ($certificate_min_score > $weight_category) |
||
1031 | ) { |
||
1032 | $warning_message .= $course_code. |
||
1033 | ' - '.get_lang('CertificateMinimunScoreIsRequiredAndMustNotBeMoreThan'). |
||
1034 | ' '.$weight_category.'<br />'; |
||
1035 | } |
||
1036 | } |
||
1037 | |||
1038 | if (!empty($warning_message)) { |
||
1039 | echo Display::return_message($warning_message, 'warning', false); |
||
1040 | } |
||
1041 | } |
||
1042 | } |
||
1043 | } |
||
1044 | |||
1045 | return $sortable_data; |
||
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * @return string |
||
1050 | */ |
||
1051 | public function getGraph() |
||
1052 | { |
||
1053 | $data = $this->getDataForGraph(); |
||
1054 | if (!empty($data) && |
||
1055 | isset($data['categories']) && |
||
1056 | isset($data['my_result']) && |
||
1057 | isset($data['average']) |
||
1058 | ) { |
||
1059 | $dataSet = new pData(); |
||
1060 | $dataSet->addPoints($data['my_result'], get_lang('Me')); |
||
1061 | // In order to generate random values |
||
1062 | // $data['average'] = array(rand(0,50), rand(0,50)); |
||
1063 | $dataSet->addPoints($data['average'], get_lang('Average')); |
||
1064 | $dataSet->addPoints($data['categories'], 'categories'); |
||
1065 | $dataSet->setAbscissa('categories'); |
||
1066 | $xSize = 700; |
||
1067 | $ySize = 500; |
||
1068 | $pChart = new pImage($xSize, $ySize, $dataSet); |
||
1069 | /* Turn of Antialiasing */ |
||
1070 | $pChart->Antialias = false; |
||
1071 | |||
1072 | /* Add a border to the picture */ |
||
1073 | $pChart->drawRectangle( |
||
1074 | 0, |
||
1075 | 0, |
||
1076 | $xSize - 1, |
||
1077 | $ySize - 1, |
||
1078 | ["R" => 0, "G" => 0, "B" => 0] |
||
1079 | ); |
||
1080 | $pChart->drawText( |
||
1081 | 80, |
||
1082 | 16, |
||
1083 | get_lang('Results'), |
||
1084 | ["FontSize" => 11, "Align" => TEXT_ALIGN_BOTTOMMIDDLE] |
||
1085 | ); |
||
1086 | $pChart->setGraphArea(50, 30, $xSize - 50, $ySize - 70); |
||
1087 | $pChart->setFontProperties( |
||
1088 | [ |
||
1089 | 'FontName' => api_get_path(SYS_FONTS_PATH).'Harmattan/Harmattan-Regular.ttf', |
||
1090 | /*'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',*/ |
||
1091 | 'FontSize' => 10, |
||
1092 | ] |
||
1093 | ); |
||
1094 | |||
1095 | /* Draw the scale */ |
||
1096 | $scaleSettings = [ |
||
1097 | "XMargin" => AUTO, |
||
1098 | "YMargin" => 10, |
||
1099 | "Floating" => true, |
||
1100 | "GridR" => 200, |
||
1101 | "GridG" => 200, |
||
1102 | "GridB" => 200, |
||
1103 | "DrawSubTicks" => true, |
||
1104 | "CycleBackground" => true, |
||
1105 | 'LabelRotation' => 10, |
||
1106 | ]; |
||
1107 | $pChart->drawScale($scaleSettings); |
||
1108 | |||
1109 | /* Draw the line chart */ |
||
1110 | $pChart->drawLineChart(); |
||
1111 | $pChart->drawPlotChart( |
||
1112 | [ |
||
1113 | "DisplayValues" => true, |
||
1114 | "PlotBorder" => true, |
||
1115 | "BorderSize" => 2, |
||
1116 | "Surrounding" => -60, |
||
1117 | "BorderAlpha" => 80, |
||
1118 | ] |
||
1119 | ); |
||
1120 | |||
1121 | /* Write the chart legend */ |
||
1122 | $pChart->drawLegend( |
||
1123 | $xSize - 180, |
||
1124 | 9, |
||
1125 | [ |
||
1126 | "Style" => LEGEND_NOBORDER, |
||
1127 | "Mode" => LEGEND_HORIZONTAL, |
||
1128 | "FontR" => 0, |
||
1129 | "FontG" => 0, |
||
1130 | "FontB" => 0, |
||
1131 | ] |
||
1132 | ); |
||
1133 | |||
1134 | $cachePath = api_get_path(SYS_ARCHIVE_PATH); |
||
1135 | $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]); |
||
1136 | $chartHash = $myCache->getHash($dataSet); |
||
1137 | |||
1138 | $myCache->writeToCache($chartHash, $pChart); |
||
1139 | $imgSysPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
1140 | $myCache->saveFromCache($chartHash, $imgSysPath); |
||
1141 | $imgWebPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
1142 | |||
1143 | if (file_exists($imgSysPath)) { |
||
1144 | $result = '<br /><div id="contentArea" style="text-align: center;" >'; |
||
1145 | $result .= '<img src="'.$imgWebPath.'" >'; |
||
1146 | $result .= '</div>'; |
||
1147 | |||
1148 | return $result; |
||
1149 | } |
||
1150 | } |
||
1151 | |||
1152 | return ''; |
||
1153 | } |
||
1154 | |||
1155 | public static function getExtraStatsColumnsToDisplay(): array |
||
1156 | { |
||
1157 | if (api_get_configuration_value('gradebook_enable_best_score') === true) { |
||
1158 | return [2]; |
||
1159 | } |
||
1160 | |||
1161 | $gradebookDisplayExtraStats = api_get_configuration_value('gradebook_display_extra_stats'); |
||
1162 | |||
1163 | /** @see GradebookTable::$loadStats */ |
||
1164 | return $gradebookDisplayExtraStats['columns'] ?? [1, 2, 3]; |
||
1165 | } |
||
1166 | |||
1167 | /** |
||
1168 | * @return array |
||
1169 | */ |
||
1170 | private function getDataForGraph() |
||
1171 | { |
||
1172 | return $this->dataForGraph; |
||
1173 | } |
||
1174 | |||
1175 | /** |
||
1176 | * @param $item |
||
1177 | * |
||
1178 | * @return mixed |
||
1179 | */ |
||
1180 | private function build_certificate_min_score($item) |
||
1181 | { |
||
1182 | return $item->getCertificateMinScore(); |
||
1183 | } |
||
1184 | |||
1185 | /** |
||
1186 | * @param $item |
||
1187 | * |
||
1188 | * @return mixed |
||
1189 | */ |
||
1190 | private function build_weight($item) |
||
1191 | { |
||
1192 | return $item->get_weight(); |
||
1193 | } |
||
1194 | |||
1195 | /** |
||
1196 | * @param $item |
||
1197 | * |
||
1198 | * @return mixed |
||
1199 | */ |
||
1200 | private function build_course_code($item) |
||
1201 | { |
||
1202 | return $item->get_course_code(); |
||
1203 | } |
||
1204 | |||
1205 | /** |
||
1206 | * @param $item |
||
1207 | * |
||
1208 | * @return string |
||
1209 | */ |
||
1210 | private function build_id_column($item) |
||
1222 | } |
||
1223 | } |
||
1224 | |||
1225 | /** |
||
1226 | * @param $item |
||
1227 | * @param array $attributes |
||
1228 | * |
||
1229 | * @return string |
||
1230 | */ |
||
1231 | private function build_type_column($item, $attributes = []) |
||
1232 | { |
||
1233 | return GradebookUtils::build_type_icon_tag($item->get_icon_name(), $attributes); |
||
1234 | } |
||
1235 | |||
1236 | /** |
||
1237 | * Generate name column. |
||
1238 | * |
||
1239 | * @param GradebookItem $item |
||
1240 | * @param string $type simple|detail |
||
1241 | * |
||
1242 | * @return string |
||
1243 | */ |
||
1244 | private function build_name_link($item, $type = 'detail', $spaces = 0) |
||
1336 | } |
||
1337 | } |
||
1338 | |||
1339 | /** |
||
1340 | * @param AbstractLink $item |
||
1341 | * |
||
1342 | * @return string|null |
||
1343 | */ |
||
1344 | private function build_edit_column($item) |
||
1356 | } |
||
1357 | } |
||
1359 |