| Conditions | 20 |
| Paths | > 20000 |
| Total Lines | 364 |
| Code Lines | 285 |
| 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 |
||
| 425 | private function insertExampleContent(Course $course, GradebookCategory $gradebook): void |
||
| 426 | { |
||
| 427 | /** @var User $currentUser */ |
||
| 428 | $currentUser = $this->security->getUser(); |
||
| 429 | if (!$currentUser instanceof User) { |
||
| 430 | $currentUser = $this->getFallbackAdminUser(); |
||
| 431 | } |
||
| 432 | |||
| 433 | $docsCreated = 0; |
||
| 434 | $foldersCreated = 0; |
||
| 435 | $filesCreated = 0; |
||
| 436 | $linksCreated = 0; |
||
| 437 | $announcementId = null; |
||
| 438 | $agendaAdded = false; |
||
| 439 | $exerciseId = null; |
||
| 440 | $forumCategoryId = null; |
||
| 441 | $forumId = null; |
||
| 442 | $threadId = null; |
||
| 443 | |||
| 444 | $this->debugLog('insertExampleContent:begin', ['courseId' => $course->getId()]); |
||
| 445 | |||
| 446 | $files = [ |
||
| 447 | ['path' => '/audio', 'title' => $this->translator->trans('Audio'), 'filetype' => 'folder', 'size' => 0], |
||
| 448 | ['path' => '/images', 'title' => $this->translator->trans('Images'), 'filetype' => 'folder', 'size' => 0], |
||
| 449 | ['path' => '/images/gallery', 'title' => $this->translator->trans('Gallery'), 'filetype' => 'folder', 'size' => 0], |
||
| 450 | ['path' => '/video', 'title' => $this->translator->trans('Video'), 'filetype' => 'folder', 'size' => 0], |
||
| 451 | ]; |
||
| 452 | $paths = []; |
||
| 453 | $courseInfo = ['real_id' => $course->getId(), 'code' => $course->getCode()]; |
||
| 454 | foreach ($files as $file) { |
||
| 455 | try { |
||
| 456 | $doc = DocumentManager::addDocument( |
||
| 457 | $courseInfo, |
||
| 458 | $file['path'], |
||
| 459 | $file['filetype'], |
||
| 460 | $file['size'], |
||
| 461 | $file['title'], |
||
| 462 | null, |
||
| 463 | 0, |
||
| 464 | null, |
||
| 465 | 0, |
||
| 466 | 0, |
||
| 467 | 0, |
||
| 468 | false |
||
| 469 | ); |
||
| 470 | $iid = method_exists($doc, 'getIid') ? $doc->getIid() : null; |
||
| 471 | $paths[$file['path']] = $iid; |
||
| 472 | $foldersCreated++; |
||
| 473 | $docsCreated++; |
||
| 474 | $this->debugLog('insertExampleContent:document:root', [ |
||
| 475 | 'path' => $file['path'], |
||
| 476 | 'title' => $file['title'], |
||
| 477 | 'iid' => $iid, |
||
| 478 | ]); |
||
| 479 | } catch (\Throwable $e) { |
||
| 480 | $this->debugLog('insertExampleContent:document:root:error', [ |
||
| 481 | 'path' => $file['path'], |
||
| 482 | 'msg' => $e->getMessage(), |
||
| 483 | ]); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | $projectDir = $this->parameterBag->get('kernel.project_dir'); |
||
| 488 | $defaultPath = $projectDir.'/public/img/document'; |
||
| 489 | |||
| 490 | $request = $this->requestStack->getCurrentRequest(); |
||
| 491 | $baseUrl = $request->getSchemeAndHttpHost().$request->getBasePath(); |
||
| 492 | |||
| 493 | $finder = new Finder(); |
||
| 494 | if (is_dir($defaultPath)) { |
||
| 495 | $finder->in($defaultPath); |
||
| 496 | $countFound = 0; |
||
| 497 | |||
| 498 | /** @var SplFileInfo $file */ |
||
| 499 | foreach ($finder as $file) { |
||
| 500 | $countFound++; |
||
| 501 | $parentName = \dirname(str_replace($defaultPath, '', $file->getRealPath())); |
||
| 502 | if ('/' === $parentName || '/certificates' === $parentName) { |
||
| 503 | continue; |
||
| 504 | } |
||
| 505 | |||
| 506 | $title = $file->getFilename(); |
||
| 507 | $parentId = $paths[$parentName] ?? null; |
||
| 508 | |||
| 509 | if ($file->isDir()) { |
||
| 510 | try { |
||
| 511 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 512 | $document = DocumentManager::addDocument( |
||
| 513 | $courseInfo, |
||
| 514 | $realPath, |
||
| 515 | 'folder', |
||
| 516 | null, |
||
| 517 | $title, |
||
| 518 | '', |
||
| 519 | null, |
||
| 520 | null, |
||
| 521 | null, |
||
| 522 | null, |
||
| 523 | null, |
||
| 524 | false, |
||
| 525 | null, |
||
| 526 | $parentId, |
||
| 527 | $file->getRealPath() |
||
| 528 | ); |
||
| 529 | $iid = method_exists($document, 'getIid') ? $document->getIid() : null; |
||
| 530 | $paths[$realPath] = $iid; |
||
| 531 | $foldersCreated++; |
||
| 532 | $docsCreated++; |
||
| 533 | $this->debugLog('insertExampleContent:document:folder', [ |
||
| 534 | 'path' => $realPath, |
||
| 535 | 'title' => $title, |
||
| 536 | 'iid' => $iid, |
||
| 537 | 'parentId' => $parentId, |
||
| 538 | ]); |
||
| 539 | } catch (\Throwable $e) { |
||
| 540 | $this->debugLog('insertExampleContent:document:folder:error', [ |
||
| 541 | 'title' => $title, |
||
| 542 | 'msg' => $e->getMessage(), |
||
| 543 | ]); |
||
| 544 | } |
||
| 545 | } else { |
||
| 546 | try { |
||
| 547 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 548 | $document = DocumentManager::addDocument( |
||
| 549 | $courseInfo, |
||
| 550 | $realPath, |
||
| 551 | 'file', |
||
| 552 | $file->getSize(), |
||
| 553 | $title, |
||
| 554 | '', |
||
| 555 | null, |
||
| 556 | null, |
||
| 557 | null, |
||
| 558 | null, |
||
| 559 | null, |
||
| 560 | false, |
||
| 561 | null, |
||
| 562 | $parentId, |
||
| 563 | $file->getRealPath() |
||
| 564 | ); |
||
| 565 | $iid = method_exists($document, 'getIid') ? $document->getIid() : null; |
||
| 566 | $filesCreated++; |
||
| 567 | $docsCreated++; |
||
| 568 | $this->debugLog('insertExampleContent:document:file', [ |
||
| 569 | 'path' => $realPath, |
||
| 570 | 'title' => $title, |
||
| 571 | 'iid' => $iid, |
||
| 572 | 'parentId' => $parentId, |
||
| 573 | 'size' => $file->getSize(), |
||
| 574 | ]); |
||
| 575 | } catch (\Throwable $e) { |
||
| 576 | $this->debugLog('insertExampleContent:document:file:error', [ |
||
| 577 | 'title' => $title, |
||
| 578 | 'msg' => $e->getMessage(), |
||
| 579 | ]); |
||
| 580 | } |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | $this->debugLog('insertExampleContent:document:scan', [ |
||
| 585 | 'basePath' => $defaultPath, |
||
| 586 | 'found' => $countFound, |
||
| 587 | 'foldersCreated' => $foldersCreated, |
||
| 588 | 'filesCreated' => $filesCreated, |
||
| 589 | 'totalDocsCreated' => $docsCreated, |
||
| 590 | ]); |
||
| 591 | } else { |
||
| 592 | $this->debugLog('insertExampleContent:document:basePathMissing', ['basePath' => $defaultPath]); |
||
| 593 | } |
||
| 594 | |||
| 595 | try { |
||
| 596 | $now = new DateTime('now', new DateTimeZone('UTC')); |
||
| 597 | $formattedNow = $now->format('Y-m-d H:i:s'); |
||
| 598 | |||
| 599 | $agenda = new Agenda('course'); |
||
| 600 | $agenda->set_course($courseInfo); |
||
| 601 | $agenda->addEvent( |
||
| 602 | $formattedNow, |
||
| 603 | $formattedNow, |
||
| 604 | 0, |
||
| 605 | $this->translator->trans('Course creation'), |
||
| 606 | $this->translator->trans('This course was created at this time'), |
||
| 607 | ['everyone' => 'everyone'] |
||
| 608 | ); |
||
| 609 | $agendaAdded = true; |
||
| 610 | $this->debugLog('insertExampleContent:agenda:ok'); |
||
| 611 | } catch (\Throwable $e) { |
||
| 612 | $this->debugLog('insertExampleContent:agenda:error', ['msg' => $e->getMessage()]); |
||
| 613 | } |
||
| 614 | |||
| 615 | try { |
||
| 616 | $link = new \Link(); |
||
| 617 | $link->setCourse($courseInfo); |
||
| 618 | $links = [ |
||
| 619 | [ |
||
| 620 | 'c_id' => $course->getId(), |
||
| 621 | 'url' => 'http://www.google.com', |
||
| 622 | 'title' => 'Quick and powerful search engine', |
||
| 623 | 'description' => $this->translator->trans('Quick and powerful search engine'), |
||
| 624 | 'category_id' => 0, |
||
| 625 | 'on_homepage' => 0, |
||
| 626 | 'target' => '_self', |
||
| 627 | 'session_id' => 0, |
||
| 628 | ], |
||
| 629 | [ |
||
| 630 | 'c_id' => $course->getId(), |
||
| 631 | 'url' => 'http://www.wikipedia.org', |
||
| 632 | 'title' => 'Free online encyclopedia', |
||
| 633 | 'description' => $this->translator->trans('Free online encyclopedia'), |
||
| 634 | 'category_id' => 0, |
||
| 635 | 'on_homepage' => 0, |
||
| 636 | 'target' => '_self', |
||
| 637 | 'session_id' => 0, |
||
| 638 | ], |
||
| 639 | ]; |
||
| 640 | |||
| 641 | foreach ($links as $params) { |
||
| 642 | $link->save($params, false, false); |
||
| 643 | $linksCreated++; |
||
| 644 | } |
||
| 645 | $this->debugLog('insertExampleContent:links:ok', ['linksCreated' => $linksCreated]); |
||
| 646 | } catch (\Throwable $e) { |
||
| 647 | $this->debugLog('insertExampleContent:links:error', ['msg' => $e->getMessage()]); |
||
| 648 | } |
||
| 649 | |||
| 650 | try { |
||
| 651 | $announcementId = \AnnouncementManager::add_announcement( |
||
| 652 | $courseInfo, |
||
| 653 | 0, |
||
| 654 | $this->translator->trans('This is an announcement example'), |
||
| 655 | $this->translator->trans('This is an announcement example. Only trainers are allowed to publish announcements.'), |
||
| 656 | ['everyone' => 'everyone'], |
||
| 657 | null, |
||
| 658 | null, |
||
| 659 | (new DateTime('now', new DateTimeZone('UTC')))->format('Y-m-d H:i:s') |
||
| 660 | ); |
||
| 661 | $this->debugLog('insertExampleContent:announcement:ok', ['announcementId' => $announcementId]); |
||
| 662 | } catch (\Throwable $e) { |
||
| 663 | $this->debugLog('insertExampleContent:announcement:error', ['msg' => $e->getMessage()]); |
||
| 664 | } |
||
| 665 | |||
| 666 | try { |
||
| 667 | $exercise = new \Exercise($course->getId()); |
||
| 668 | $exercise->exercise = $this->translator->trans('Sample test'); |
||
| 669 | $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0"> |
||
| 670 | <tr> |
||
| 671 | <td width="220" valign="top" align="left"> |
||
| 672 | <img src="'.$baseUrl.'/public/img/document/images/mr_chamilo/doubts.png"> |
||
| 673 | </td> |
||
| 674 | <td valign="top" align="left">'.$this->translator->trans('Irony').'</td></tr> |
||
| 675 | </table>'; |
||
| 676 | $exercise->type = 1; |
||
| 677 | $exercise->setRandom(0); |
||
| 678 | $exercise->active = 1; |
||
| 679 | $exercise->results_disabled = 0; |
||
| 680 | $exercise->description = $html; |
||
| 681 | $exercise->save(); |
||
| 682 | $exerciseId = $exercise->id; |
||
| 683 | $this->debugLog('insertExampleContent:exercise:created', ['exerciseId' => $exerciseId]); |
||
| 684 | |||
| 685 | $question = new \MultipleAnswer(); |
||
| 686 | $question->course = $courseInfo; |
||
| 687 | $question->question = $this->translator->trans('Socratic irony is...'); |
||
| 688 | $question->description = $this->translator->trans('(more than one answer can be true)'); |
||
| 689 | $question->weighting = 10; |
||
| 690 | $question->position = 1; |
||
| 691 | $question->course = $courseInfo; |
||
| 692 | $question->save($exercise); |
||
| 693 | $questionId = $question->id; |
||
| 694 | |||
| 695 | $answer = new \Answer($questionId, $courseInfo['real_id']); |
||
| 696 | $answer->createAnswer($this->translator->trans("Ridiculise one's interlocutor in order to have him concede he is wrong."), 0, $this->translator->trans('No. Socratic irony is not a matter of psychology, it concerns argumentation.'), -5, 1); |
||
| 697 | $answer->createAnswer($this->translator->trans("Admit one's own errors to invite one's interlocutor to do the same."), 0, $this->translator->trans('No. Socratic irony is not a seduction strategy or a method based on the example.'), -5, 2); |
||
| 698 | $answer->createAnswer($this->translator->trans("Compell one's interlocutor, by a series of questions and sub-questions, to admit he doesn't know what he claims to know."), 1, $this->translator->trans('Indeed. Socratic irony is an interrogative method. The Greek "eirotao" means "ask questions"'), 5, 3); |
||
| 699 | $answer->createAnswer($this->translator->trans("Use the Principle of Non Contradiction to force one's interlocutor into a dead end."), 1, $this->translator->trans("This answer is not false. It is true that the revelation of the interlocutor's ignorance means showing the contradictory conclusions where lead his premisses."), 5, 4); |
||
| 700 | $answer->save(); |
||
| 701 | $this->debugLog('insertExampleContent:exercise:questionAnswers:ok', ['questionId' => $questionId]); |
||
| 702 | |||
| 703 | // Gradebook link |
||
| 704 | $manager = $this->entityManager; |
||
| 705 | $childGradebookCategory = new GradebookCategory(); |
||
| 706 | $childGradebookCategory->setTitle($course->getCode()); |
||
| 707 | $childGradebookCategory->setLocked(0); |
||
| 708 | $childGradebookCategory->setGenerateCertificates(false); |
||
| 709 | $childGradebookCategory->setDescription(''); |
||
| 710 | $childGradebookCategory->setCourse($course); |
||
| 711 | $childGradebookCategory->setWeight(100); |
||
| 712 | $childGradebookCategory->setVisible(true); |
||
| 713 | $childGradebookCategory->setCertifMinScore(75); |
||
| 714 | $childGradebookCategory->setParent($gradebook); |
||
| 715 | $childGradebookCategory->setUser(api_get_user_entity()); |
||
| 716 | |||
| 717 | $manager->persist($childGradebookCategory); |
||
| 718 | $manager->flush(); |
||
| 719 | |||
| 720 | $gradebookLink = new GradebookLink(); |
||
| 721 | $gradebookLink->setType(1); |
||
| 722 | $gradebookLink->setRefId($exerciseId); |
||
| 723 | $gradebookLink->setUserScoreList([]); |
||
| 724 | $gradebookLink->setCourse($course); |
||
| 725 | $gradebookLink->setCategory($childGradebookCategory); |
||
| 726 | $gradebookLink->setCreatedAt(new DateTime()); |
||
| 727 | $gradebookLink->setWeight(100); |
||
| 728 | $gradebookLink->setVisible(1); |
||
| 729 | $gradebookLink->setLocked(0); |
||
| 730 | |||
| 731 | $manager->persist($gradebookLink); |
||
| 732 | $manager->flush(); |
||
| 733 | $this->debugLog('insertExampleContent:gradebookLink:ok', [ |
||
| 734 | 'childCategoryId' => $childGradebookCategory->getId(), |
||
| 735 | 'refId' => $exerciseId, |
||
| 736 | ]); |
||
| 737 | } catch (\Throwable $e) { |
||
| 738 | $this->debugLog('insertExampleContent:exercise:error', ['msg' => $e->getMessage()]); |
||
| 739 | } |
||
| 740 | |||
| 741 | try { |
||
| 742 | $params = [ |
||
| 743 | 'forum_category_title' => $this->translator->trans('Example Forum Category'), |
||
| 744 | 'forum_category_comment' => '', |
||
| 745 | ]; |
||
| 746 | $forumCategoryId = \saveForumCategory($params, $courseInfo, false); |
||
| 747 | $this->debugLog('insertExampleContent:forum:category', ['forumCategoryId' => $forumCategoryId]); |
||
| 748 | |||
| 749 | $params = [ |
||
| 750 | 'forum_category' => $forumCategoryId, |
||
| 751 | 'forum_title' => $this->translator->trans('Example Forum'), |
||
| 752 | 'forum_comment' => '', |
||
| 753 | 'default_view_type_group' => ['default_view_type' => 'flat'], |
||
| 754 | ]; |
||
| 755 | $forumId = \store_forum($params, $courseInfo, true); |
||
| 756 | $this->debugLog('insertExampleContent:forum:forum', ['forumId' => $forumId]); |
||
| 757 | |||
| 758 | $repo = $this->entityManager->getRepository(CForum::class); |
||
| 759 | $forumEntity = $repo->find($forumId); |
||
| 760 | |||
| 761 | $params = [ |
||
| 762 | 'post_title' => $this->translator->trans('Example Thread'), |
||
| 763 | 'forum_id' => $forumId, |
||
| 764 | 'post_text' => $this->translator->trans('Example content'), |
||
| 765 | 'calification_notebook_title' => '', |
||
| 766 | 'numeric_calification' => '', |
||
| 767 | 'weight_calification' => '', |
||
| 768 | 'forum_category' => $forumCategoryId, |
||
| 769 | 'thread_peer_qualify' => 0, |
||
| 770 | ]; |
||
| 771 | $threadId = \saveThread($forumEntity, $params, $courseInfo, false); |
||
| 772 | $this->debugLog('insertExampleContent:forum:thread', ['threadId' => $threadId]); |
||
| 773 | } catch (\Throwable $e) { |
||
| 774 | $this->debugLog('insertExampleContent:forum:error', ['msg' => $e->getMessage()]); |
||
| 775 | } |
||
| 776 | |||
| 777 | $this->debugLog('insertExampleContent:end', [ |
||
| 778 | 'courseId' => $course->getId(), |
||
| 779 | 'docsCreated' => $docsCreated, |
||
| 780 | 'foldersCreated' => $foldersCreated, |
||
| 781 | 'filesCreated' => $filesCreated, |
||
| 782 | 'linksCreated' => $linksCreated, |
||
| 783 | 'agendaAdded' => $agendaAdded, |
||
| 784 | 'announcementId' => $announcementId, |
||
| 785 | 'exerciseId' => $exerciseId, |
||
| 786 | 'forumCategoryId' => $forumCategoryId, |
||
| 787 | 'forumId' => $forumId, |
||
| 788 | 'threadId' => $threadId, |
||
| 789 | ]); |
||
| 1018 |