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