| Conditions | 16 |
| Paths | 130 |
| Total Lines | 358 |
| Code Lines | 252 |
| 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 |
||
| 416 | public static function fillCourse( |
||
| 417 | $courseInfo, |
||
| 418 | $fill_with_exemplary_content = null, |
||
| 419 | $authorId = 0 |
||
| 420 | ) { |
||
| 421 | if (is_null($fill_with_exemplary_content)) { |
||
| 422 | $fill_with_exemplary_content = api_get_setting('example_material_course_creation') !== 'false'; |
||
| 423 | } |
||
| 424 | |||
| 425 | $course_id = (int) $courseInfo['real_id']; |
||
| 426 | |||
| 427 | if (empty($courseInfo)) { |
||
| 428 | return false; |
||
| 429 | } |
||
| 430 | $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId; |
||
| 431 | |||
| 432 | $TABLEGROUPCATEGORIES = Database::get_course_table(TABLE_GROUP_CATEGORY); |
||
| 433 | $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 434 | $TABLEGRADEBOOK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 435 | $TABLEGRADEBOOKLINK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); |
||
| 436 | $visible_for_course_admin = 0; |
||
| 437 | $em = Database::getManager(); |
||
| 438 | $course = api_get_course_entity($course_id); |
||
| 439 | $settingsManager = CourseManager::getCourseSettingsManager(); |
||
| 440 | $settingsManager->setCourse($course); |
||
| 441 | |||
| 442 | $alert = api_get_setting('email_alert_manager_on_new_quiz'); |
||
| 443 | $defaultEmailExerciseAlert = 0; |
||
| 444 | if ($alert === 'true') { |
||
| 445 | $defaultEmailExerciseAlert = 1; |
||
| 446 | } |
||
| 447 | |||
| 448 | /* course_setting table (courseinfo tool) */ |
||
| 449 | $settings = [ |
||
| 450 | 'email_alert_manager_on_new_doc' => ['title' => '', 'default' => 0, 'category' => 'work'], |
||
| 451 | 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'], |
||
| 452 | 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'], |
||
| 453 | 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'], |
||
| 454 | 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'], |
||
| 455 | 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'], |
||
| 456 | 'course_theme' => ['default' => '', 'category' => 'theme'], |
||
| 457 | 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'], |
||
| 458 | 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'], |
||
| 459 | 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' => 'registration'], |
||
| 460 | 'allow_user_view_user_list' => ['default' => 1, 'category' => 'user'], |
||
| 461 | 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' => 'thematic_advance'], |
||
| 462 | 'email_alert_students_on_new_homework' => ['default' => 0, 'category' => 'work'], |
||
| 463 | 'enable_lp_auto_launch' => ['default' => 0, 'category' => 'learning_path'], |
||
| 464 | 'enable_exercise_auto_launch' => ['default' => 0, 'category' => 'exercise'], |
||
| 465 | 'enable_document_auto_launch' => ['default' => 0, 'category' => 'document'], |
||
| 466 | 'pdf_export_watermark_text' => ['default' => '', 'category' => 'learning_path'], |
||
| 467 | 'allow_public_certificates' => [ |
||
| 468 | 'default' => api_get_setting('allow_public_certificates') === 'true' ? 1 : '', |
||
| 469 | 'category' => 'certificates', |
||
| 470 | ], |
||
| 471 | 'documents_default_visibility' => ['default' => 'visible', 'category' => 'document'], |
||
| 472 | 'show_course_in_user_language' => ['default' => 2, 'category' => null], |
||
| 473 | 'email_to_teachers_on_new_work_feedback' => ['default' => 1, 'category' => null], |
||
| 474 | ]; |
||
| 475 | |||
| 476 | $counter = 1; |
||
| 477 | foreach ($settings as $variable => $setting) { |
||
| 478 | $title = $setting['title'] ?? ''; |
||
| 479 | Database::query( |
||
| 480 | "INSERT INTO $TABLESETTING (id, c_id, title, variable, value, category) |
||
| 481 | VALUES ($counter, $course_id, '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')" |
||
| 482 | ); |
||
| 483 | $counter++; |
||
| 484 | } |
||
| 485 | |||
| 486 | /* Course homepage tools for platform admin only */ |
||
| 487 | /* Group tool */ |
||
| 488 | Database::insert( |
||
| 489 | $TABLEGROUPCATEGORIES, |
||
| 490 | [ |
||
| 491 | 'c_id' => $course_id, |
||
| 492 | 'id' => 2, |
||
| 493 | 'title' => get_lang('DefaultGroupCategory'), |
||
| 494 | 'description' => '', |
||
| 495 | 'max_student' => 8, |
||
| 496 | 'self_reg_allowed' => 0, |
||
| 497 | 'self_unreg_allowed' => 0, |
||
| 498 | 'groups_per_user' => 0, |
||
| 499 | 'display_order' => 0, |
||
| 500 | 'doc_state' => 1, |
||
| 501 | 'calendar_state' => 1, |
||
| 502 | 'work_state' => 1, |
||
| 503 | 'announcements_state' => 1, |
||
| 504 | 'forum_state' => 1, |
||
| 505 | 'wiki_state' => 1, |
||
| 506 | 'chat_state' => 1, |
||
| 507 | ] |
||
| 508 | ); |
||
| 509 | |||
| 510 | $now = api_get_utc_datetime(); |
||
| 511 | |||
| 512 | $files = [ |
||
| 513 | ['path' => '/shared_folder', 'title' => get_lang('UserFolders'), 'filetype' => 'folder', 'size' => 0], |
||
| 514 | ['path' => '/chat_files', 'title' => get_lang('ChatFiles'), 'filetype' => 'folder', 'size' => 0], |
||
| 515 | ]; |
||
| 516 | |||
| 517 | $counter = 1; |
||
| 518 | foreach ($files as $file) { |
||
| 519 | self::insertDocument($courseInfo, $counter, $file, $authorId); |
||
| 520 | $counter++; |
||
| 521 | } |
||
| 522 | |||
| 523 | $certificateId = 'NULL'; |
||
| 524 | |||
| 525 | /* Documents */ |
||
| 526 | if ($fill_with_exemplary_content) { |
||
| 527 | $files = [ |
||
| 528 | ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0], |
||
| 529 | ['path' => '/images/gallery', 'title' => get_lang('DefaultCourseImages'), 'filetype' => 'folder', 'size' => 0], |
||
| 530 | ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0], |
||
| 531 | ['path' => '/flash', 'title' => get_lang('Flash'), 'filetype' => 'folder', 'size' => 0], |
||
| 532 | ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0], |
||
| 533 | ['path' => '/certificates', 'title' => get_lang('Certificates'), 'filetype' => 'folder', 'size' => 0], |
||
| 534 | ]; |
||
| 535 | |||
| 536 | foreach ($files as $file) { |
||
| 537 | self::insertDocument($courseInfo, $counter, $file, $authorId); |
||
| 538 | $counter++; |
||
| 539 | } |
||
| 540 | |||
| 541 | $finder = new Symfony\Component\Finder\Finder(); |
||
| 542 | $defaultPath = api_get_path(SYS_CODE_PATH).'default_course_document'; |
||
| 543 | $finder->in($defaultPath); |
||
| 544 | /** @var SplFileInfo $file */ |
||
| 545 | foreach ($finder as $file) { |
||
| 546 | $path = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 547 | $parentName = dirname(str_replace($defaultPath, '', $file->getRealPath())); |
||
| 548 | $title = $file->getFilename(); |
||
| 549 | if ($file->isDir()) { |
||
| 550 | create_unexisting_directory( |
||
| 551 | $courseInfo, |
||
| 552 | api_get_user_id(), |
||
| 553 | 0, |
||
| 554 | 0, |
||
| 555 | 0, |
||
| 556 | $path, |
||
| 557 | $path, |
||
| 558 | $title |
||
| 559 | ); |
||
| 560 | } else { |
||
| 561 | $parent = DocumentManager::getDocumentByPathInCourse($courseInfo, $parentName); |
||
| 562 | $parentId = 0; |
||
| 563 | if (!empty($parent)) { |
||
| 564 | $parent = $parent[0]; |
||
| 565 | $parentId = $parent['iid']; |
||
| 566 | } |
||
| 567 | |||
| 568 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 569 | $document = DocumentManager::addDocument( |
||
| 570 | $courseInfo, |
||
| 571 | $realPath, |
||
| 572 | 'file', |
||
| 573 | $file->getSize(), |
||
| 574 | $title, |
||
| 575 | '', |
||
| 576 | null, |
||
| 577 | null, |
||
| 578 | null, |
||
| 579 | null, |
||
| 580 | null, |
||
| 581 | false, |
||
| 582 | null, |
||
| 583 | $parentId, |
||
| 584 | $file->getRealPath() |
||
| 585 | ); |
||
| 586 | |||
| 587 | if ($document && $document->getTitle() === 'default.html') { |
||
| 588 | $certificateId = $document->getIid(); |
||
| 589 | } |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | $agenda = new Agenda('course'); |
||
| 594 | $agenda->set_course($courseInfo); |
||
| 595 | $agenda->addEvent( |
||
| 596 | $now, |
||
| 597 | $now, |
||
| 598 | 0, |
||
| 599 | get_lang('AgendaCreationTitle'), |
||
| 600 | get_lang('AgendaCreationContenu') |
||
| 601 | ); |
||
| 602 | |||
| 603 | /* Links tool */ |
||
| 604 | $link = new Link(); |
||
| 605 | $link->setCourse($courseInfo); |
||
| 606 | $links = [ |
||
| 607 | [ |
||
| 608 | 'c_id' => $course_id, |
||
| 609 | 'url' => 'http://www.google.com', |
||
| 610 | 'title' => 'Google', |
||
| 611 | 'description' => get_lang('Google'), |
||
| 612 | 'category_id' => 0, |
||
| 613 | 'on_homepage' => 0, |
||
| 614 | 'target' => '_self', |
||
| 615 | 'session_id' => 0, |
||
| 616 | ], |
||
| 617 | [ |
||
| 618 | 'c_id' => $course_id, |
||
| 619 | 'url' => 'http://www.wikipedia.org', |
||
| 620 | 'title' => 'Wikipedia', |
||
| 621 | 'description' => get_lang('Wikipedia'), |
||
| 622 | 'category_id' => 0, |
||
| 623 | 'on_homepage' => 0, |
||
| 624 | 'target' => '_self', |
||
| 625 | 'session_id' => 0, |
||
| 626 | ], |
||
| 627 | ]; |
||
| 628 | |||
| 629 | foreach ($links as $params) { |
||
| 630 | $link->save($params); |
||
| 631 | } |
||
| 632 | |||
| 633 | /* Announcement tool */ |
||
| 634 | AnnouncementManager::add_announcement( |
||
| 635 | $courseInfo, |
||
| 636 | 0, |
||
| 637 | get_lang('AnnouncementExampleTitle'), |
||
| 638 | get_lang('AnnouncementEx'), |
||
| 639 | ['everyone' => 'everyone'], |
||
| 640 | null, |
||
| 641 | null, |
||
| 642 | $now |
||
| 643 | ); |
||
| 644 | |||
| 645 | $manager = Database::getManager(); |
||
| 646 | |||
| 647 | /* Introduction text */ |
||
| 648 | $intro_text = '<p style="text-align: center;"> |
||
| 649 | <img src="'.api_get_path(REL_CODE_PATH).'img/mascot.png" alt="Mr. Chamilo" title="Mr. Chamilo" /> |
||
| 650 | <h2>'.get_lang('IntroductionText').'</h2> |
||
| 651 | </p>'; |
||
| 652 | |||
| 653 | $toolIntro = new CToolIntro(); |
||
| 654 | $toolIntro |
||
| 655 | ->setCId($course_id) |
||
| 656 | ->setId(TOOL_COURSE_HOMEPAGE) |
||
| 657 | ->setSessionId(0) |
||
| 658 | ->setIntroText($intro_text); |
||
| 659 | $manager->persist($toolIntro); |
||
| 660 | |||
| 661 | $toolIntro = new CToolIntro(); |
||
| 662 | $toolIntro |
||
| 663 | ->setCId($course_id) |
||
| 664 | ->setId(TOOL_STUDENTPUBLICATION) |
||
| 665 | ->setSessionId(0) |
||
| 666 | ->setIntroText(get_lang('IntroductionTwo')); |
||
| 667 | $manager->persist($toolIntro); |
||
| 668 | |||
| 669 | $toolIntro = new CToolIntro(); |
||
| 670 | $toolIntro |
||
| 671 | ->setCId($course_id) |
||
| 672 | ->setId(TOOL_WIKI) |
||
| 673 | ->setSessionId(0) |
||
| 674 | ->setIntroText(get_lang('IntroductionWiki')); |
||
| 675 | $manager->persist($toolIntro); |
||
| 676 | |||
| 677 | $manager->flush(); |
||
| 678 | |||
| 679 | /* Exercise tool */ |
||
| 680 | $exercise = new Exercise($course_id); |
||
| 681 | $exercise->exercise = get_lang('ExerciceEx'); |
||
| 682 | $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0"> |
||
| 683 | <tr> |
||
| 684 | <td width="220" valign="top" align="left"> |
||
| 685 | <img src="'.api_get_path(WEB_CODE_PATH).'default_course_document/images/mr_chamilo/doubts.png"> |
||
| 686 | </td> |
||
| 687 | <td valign="top" align="left">'.get_lang('Antique').'</td></tr> |
||
| 688 | </table>'; |
||
| 689 | $exercise->type = 1; |
||
| 690 | $exercise->setRandom(0); |
||
| 691 | $exercise->active = 1; |
||
| 692 | $exercise->results_disabled = 0; |
||
| 693 | $exercise->description = $html; |
||
| 694 | $exercise->save(); |
||
| 695 | |||
| 696 | $exercise_id = $exercise->id; |
||
| 697 | |||
| 698 | $question = new MultipleAnswer(); |
||
| 699 | $question->question = get_lang('SocraticIrony'); |
||
| 700 | $question->description = get_lang('ManyAnswers'); |
||
| 701 | $question->weighting = 10; |
||
| 702 | $question->position = 1; |
||
| 703 | $question->course = $courseInfo; |
||
| 704 | $question->save($exercise); |
||
| 705 | $questionId = $question->id; |
||
| 706 | |||
| 707 | $answer = new Answer($questionId, $courseInfo['real_id']); |
||
| 708 | |||
| 709 | $answer->createAnswer(get_lang('Ridiculise'), 0, get_lang('NoPsychology'), -5, 1); |
||
| 710 | $answer->createAnswer(get_lang('AdmitError'), 0, get_lang('NoSeduction'), -5, 2); |
||
| 711 | $answer->createAnswer(get_lang('Force'), 1, get_lang('Indeed'), 5, 3); |
||
| 712 | $answer->createAnswer(get_lang('Contradiction'), 1, get_lang('NotFalse'), 5, 4); |
||
| 713 | $answer->save(); |
||
| 714 | |||
| 715 | /* Forum tool */ |
||
| 716 | |||
| 717 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 718 | |||
| 719 | $params = [ |
||
| 720 | 'forum_category_title' => get_lang('ExampleForumCategory'), |
||
| 721 | 'forum_category_comment' => '', |
||
| 722 | ]; |
||
| 723 | |||
| 724 | $forumCategoryId = store_forumcategory($params, $courseInfo, false); |
||
| 725 | |||
| 726 | $params = [ |
||
| 727 | 'forum_category' => $forumCategoryId, |
||
| 728 | 'forum_title' => get_lang('ExampleForum'), |
||
| 729 | 'forum_comment' => '', |
||
| 730 | 'default_view_type_group' => ['default_view_type' => 'flat'], |
||
| 731 | ]; |
||
| 732 | |||
| 733 | $forumId = store_forum($params, $courseInfo, true); |
||
| 734 | |||
| 735 | $forumInfo = get_forum_information($forumId, $courseInfo['real_id']); |
||
| 736 | |||
| 737 | $params = [ |
||
| 738 | 'post_title' => get_lang('ExampleThread'), |
||
| 739 | 'forum_id' => $forumId, |
||
| 740 | 'post_text' => get_lang('ExampleThreadContent'), |
||
| 741 | 'calification_notebook_title' => '', |
||
| 742 | 'numeric_calification' => '', |
||
| 743 | 'weight_calification' => '', |
||
| 744 | 'forum_category' => $forumCategoryId, |
||
| 745 | 'thread_peer_qualify' => 0, |
||
| 746 | ]; |
||
| 747 | |||
| 748 | store_thread($forumInfo, $params, $courseInfo, false); |
||
| 749 | |||
| 750 | /* Gradebook tool */ |
||
| 751 | $course_code = $courseInfo['code']; |
||
| 752 | // father gradebook |
||
| 753 | Database::query( |
||
| 754 | "INSERT INTO $TABLEGRADEBOOK (name, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id) |
||
| 755 | VALUES ('$course_code','0',0,'',1,$course_id,0,100,0,75,NULL,$certificateId)" |
||
| 756 | ); |
||
| 757 | $gbid = Database:: insert_id(); |
||
| 758 | Database::query( |
||
| 759 | "INSERT INTO $TABLEGRADEBOOK (name, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id) |
||
| 760 | VALUES ('$course_code','0',0,'',1,$course_id,$gbid,100,1,75,NULL,$certificateId)" |
||
| 761 | ); |
||
| 762 | $gbid = Database:: insert_id(); |
||
| 763 | Database::query( |
||
| 764 | "INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, c_id, category_id, created_at, weight, visible, locked) |
||
| 765 | VALUES (1,$exercise_id,1,$course_id,$gbid,'$now',100,1,0)" |
||
| 766 | ); |
||
| 767 | } |
||
| 768 | |||
| 769 | // Installing plugins in course |
||
| 770 | $app_plugin = new AppPlugin(); |
||
| 771 | $app_plugin->install_course_plugins($course_id); |
||
| 772 | |||
| 773 | return true; |
||
| 774 | } |
||
| 1109 |