| Conditions | 17 |
| Paths | 1260 |
| Total Lines | 274 |
| Code Lines | 199 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 472 | public function exportCompleteReportXLS( |
||
| 473 | $document_path = '', |
||
| 474 | $user_id = null, |
||
| 475 | $export_user_fields = false, |
||
| 476 | $export_filter = 0, |
||
| 477 | $exercise_id = 0, |
||
| 478 | $hotpotato_name = null |
||
| 479 | ) { |
||
| 480 | global $charset; |
||
| 481 | $this->getExercisesReporting( |
||
| 482 | $document_path, |
||
| 483 | $user_id, |
||
| 484 | $export_filter, |
||
| 485 | $exercise_id, |
||
| 486 | $hotpotato_name |
||
| 487 | ); |
||
| 488 | $now = api_get_local_time(); |
||
| 489 | $filename = 'exercise_results_'.$now.'.xlsx'; |
||
| 490 | if (!empty($user_id)) { |
||
| 491 | $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.xlsx'; |
||
| 492 | } |
||
| 493 | |||
| 494 | $spreadsheet = new PHPExcel(); |
||
| 495 | $spreadsheet->setActiveSheetIndex(0); |
||
| 496 | $worksheet = $spreadsheet->getActiveSheet(); |
||
| 497 | |||
| 498 | $line = 1; // Skip first line |
||
| 499 | $column = 0; //skip the first column (row titles) |
||
| 500 | |||
| 501 | // check if exists column 'user' |
||
| 502 | $with_column_user = false; |
||
| 503 | foreach ($this->results as $result) { |
||
| 504 | if (!empty($result['lastname']) && !empty($result['firstname'])) { |
||
| 505 | $with_column_user = true; |
||
| 506 | break; |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | $officialCodeInList = api_get_setting('show_official_code_exercise_result_list'); |
||
| 511 | |||
| 512 | if ($with_column_user) { |
||
| 513 | if (api_is_western_name_order()) { |
||
| 514 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName')); |
||
| 515 | $column++; |
||
| 516 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName')); |
||
| 517 | $column++; |
||
| 518 | } else { |
||
| 519 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName')); |
||
| 520 | $column++; |
||
| 521 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName')); |
||
| 522 | $column++; |
||
| 523 | } |
||
| 524 | |||
| 525 | if ($officialCodeInList === 'true') { |
||
| 526 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode')); |
||
| 527 | $column++; |
||
| 528 | } |
||
| 529 | |||
| 530 | $worksheet->setCellValueByColumnAndRow($column++, $line, get_lang('LoginName')); |
||
| 531 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email')); |
||
| 532 | $column++; |
||
| 533 | } |
||
| 534 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups')); |
||
| 535 | $column++; |
||
| 536 | |||
| 537 | if ($export_user_fields) { |
||
| 538 | //show user fields section with a big th colspan that spans over all fields |
||
| 539 | $extra_user_fields = UserManager::get_extra_fields( |
||
| 540 | 0, |
||
| 541 | 1000, |
||
| 542 | 5, |
||
| 543 | 'ASC', |
||
| 544 | false, |
||
| 545 | 1 |
||
| 546 | ); |
||
| 547 | |||
| 548 | //show the fields names for user fields |
||
| 549 | foreach ($extra_user_fields as $field) { |
||
| 550 | $worksheet->setCellValueByColumnAndRow( |
||
| 551 | $column, |
||
| 552 | $line, |
||
| 553 | api_html_entity_decode( |
||
| 554 | strip_tags($field[3]), |
||
| 555 | ENT_QUOTES, |
||
| 556 | $charset |
||
| 557 | ) |
||
| 558 | ); |
||
| 559 | $column++; |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title')); |
||
| 564 | $column++; |
||
| 565 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate')); |
||
| 566 | $column++; |
||
| 567 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate')); |
||
| 568 | $column++; |
||
| 569 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')'); |
||
| 570 | $column++; |
||
| 571 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('WeightNecessary')); |
||
| 572 | $column++; |
||
| 573 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score')); |
||
| 574 | $column++; |
||
| 575 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total')); |
||
| 576 | $column++; |
||
| 577 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Status')); |
||
| 578 | $column++; |
||
| 579 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('ToolLearnpath')); |
||
| 580 | $column++; |
||
| 581 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('UserIsCurrentlySubscribed')); |
||
| 582 | $column++; |
||
| 583 | $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('CourseCode')); |
||
| 584 | $line++; |
||
| 585 | |||
| 586 | foreach ($this->results as $row) { |
||
| 587 | $column = 0; |
||
| 588 | if ($with_column_user) { |
||
| 589 | if (api_is_western_name_order()) { |
||
| 590 | $worksheet->setCellValueByColumnAndRow( |
||
| 591 | $column, |
||
| 592 | $line, |
||
| 593 | api_html_entity_decode( |
||
| 594 | strip_tags($row['firstname']), |
||
| 595 | ENT_QUOTES, |
||
| 596 | $charset |
||
| 597 | ) |
||
| 598 | ); |
||
| 599 | $column++; |
||
| 600 | $worksheet->setCellValueByColumnAndRow( |
||
| 601 | $column, |
||
| 602 | $line, |
||
| 603 | api_html_entity_decode( |
||
| 604 | strip_tags($row['lastname']), |
||
| 605 | ENT_QUOTES, |
||
| 606 | $charset |
||
| 607 | ) |
||
| 608 | ); |
||
| 609 | $column++; |
||
| 610 | } else { |
||
| 611 | $worksheet->setCellValueByColumnAndRow( |
||
| 612 | $column, |
||
| 613 | $line, |
||
| 614 | api_html_entity_decode( |
||
| 615 | strip_tags($row['lastname']), |
||
| 616 | ENT_QUOTES, |
||
| 617 | $charset |
||
| 618 | ) |
||
| 619 | ); |
||
| 620 | $column++; |
||
| 621 | $worksheet->setCellValueByColumnAndRow( |
||
| 622 | $column, |
||
| 623 | $line, |
||
| 624 | api_html_entity_decode( |
||
| 625 | strip_tags($row['firstname']), |
||
| 626 | ENT_QUOTES, |
||
| 627 | $charset |
||
| 628 | ) |
||
| 629 | ); |
||
| 630 | $column++; |
||
| 631 | } |
||
| 632 | |||
| 633 | if ($officialCodeInList === 'true') { |
||
| 634 | $worksheet->setCellValueByColumnAndRow( |
||
| 635 | $column, |
||
| 636 | $line, |
||
| 637 | api_html_entity_decode( |
||
| 638 | strip_tags($row['official_code']), |
||
| 639 | ENT_QUOTES, |
||
| 640 | $charset |
||
| 641 | ) |
||
| 642 | ); |
||
| 643 | $column++; |
||
| 644 | } |
||
| 645 | |||
| 646 | $worksheet->setCellValueByColumnAndRow( |
||
| 647 | $column++, |
||
| 648 | $line, |
||
| 649 | api_html_entity_decode( |
||
| 650 | strip_tags($row['username']), |
||
| 651 | ENT_QUOTES, |
||
| 652 | $charset |
||
| 653 | ) |
||
| 654 | ); |
||
| 655 | $worksheet->setCellValueByColumnAndRow( |
||
| 656 | $column, |
||
| 657 | $line, |
||
| 658 | api_html_entity_decode( |
||
| 659 | strip_tags($row['email']), |
||
| 660 | ENT_QUOTES, |
||
| 661 | $charset |
||
| 662 | ) |
||
| 663 | ); |
||
| 664 | $column++; |
||
| 665 | } |
||
| 666 | |||
| 667 | $worksheet->setCellValueByColumnAndRow( |
||
| 668 | $column, |
||
| 669 | $line, |
||
| 670 | api_html_entity_decode( |
||
| 671 | strip_tags( |
||
| 672 | implode( |
||
| 673 | ", ", |
||
| 674 | GroupManager::get_user_group_name($row['user_id']) |
||
| 675 | ) |
||
| 676 | ), |
||
| 677 | ENT_QUOTES, |
||
| 678 | $charset |
||
| 679 | ) |
||
| 680 | ); |
||
| 681 | $column++; |
||
| 682 | |||
| 683 | if ($export_user_fields) { |
||
| 684 | //show user fields data, if any, for this user |
||
| 685 | $user_fields_values = UserManager::get_extra_user_data( |
||
| 686 | $row['user_id'], |
||
| 687 | false, |
||
| 688 | false, |
||
| 689 | false, |
||
| 690 | true |
||
| 691 | ); |
||
| 692 | foreach ($user_fields_values as $value) { |
||
| 693 | $worksheet->setCellValueByColumnAndRow( |
||
| 694 | $column, |
||
| 695 | $line, |
||
| 696 | api_html_entity_decode( |
||
| 697 | strip_tags($value), |
||
| 698 | ENT_QUOTES, |
||
| 699 | $charset |
||
| 700 | ) |
||
| 701 | ); |
||
| 702 | $column++; |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | $worksheet->setCellValueByColumnAndRow( |
||
| 707 | $column, |
||
| 708 | $line, |
||
| 709 | api_html_entity_decode( |
||
| 710 | strip_tags($row['title']), |
||
| 711 | ENT_QUOTES, |
||
| 712 | $charset |
||
| 713 | ) |
||
| 714 | ); |
||
| 715 | |||
| 716 | $column++; |
||
| 717 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); |
||
| 718 | $column++; |
||
| 719 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']); |
||
| 720 | $column++; |
||
| 721 | $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0; |
||
| 722 | $worksheet->setCellValueByColumnAndRow($column, $line, $duration); |
||
| 723 | $column++; |
||
| 724 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['minimun']); |
||
| 725 | $column++; |
||
| 726 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']); |
||
| 727 | $column++; |
||
| 728 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']); |
||
| 729 | $column++; |
||
| 730 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']); |
||
| 731 | $column++; |
||
| 732 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['lp_name']); |
||
| 733 | $column++; |
||
| 734 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['is_user_subscribed']); |
||
| 735 | $column++; |
||
| 736 | $worksheet->setCellValueByColumnAndRow($column, $line, api_get_course_id()); |
||
| 737 | $line++; |
||
| 738 | } |
||
| 739 | |||
| 740 | $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); |
||
| 741 | $writer = new PHPExcel_Writer_Excel2007($spreadsheet); |
||
| 742 | $writer->save($file); |
||
| 743 | DocumentManager::file_send_for_download($file, true, $filename); |
||
| 744 | |||
| 745 | return true; |
||
| 746 | } |
||
| 748 |