| Conditions | 140 |
| Total Lines | 751 |
| Code Lines | 481 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 1359 |