| Conditions | 16 |
| Paths | 66 |
| Total Lines | 348 |
| Code Lines | 238 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 240 | public static function fillCourse( |
||
| 241 | $courseInfo, |
||
| 242 | $fill_with_exemplary_content = null, |
||
| 243 | $authorId = 0 |
||
| 244 | ) { |
||
| 245 | if (is_null($fill_with_exemplary_content)) { |
||
| 246 | $fill_with_exemplary_content = 'false' !== api_get_setting('example_material_course_creation'); |
||
| 247 | } |
||
| 248 | |||
| 249 | $course_id = (int) $courseInfo['real_id']; |
||
| 250 | |||
| 251 | if (empty($courseInfo)) { |
||
| 252 | return false; |
||
| 253 | } |
||
| 254 | $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId; |
||
| 255 | |||
| 256 | $TABLEGROUPCATEGORIES = Database::get_course_table(TABLE_GROUP_CATEGORY); |
||
| 257 | $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 258 | $TABLEGRADEBOOK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 259 | $TABLEGRADEBOOKLINK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); |
||
| 260 | $course = api_get_course_entity($course_id); |
||
| 261 | $settingsManager = Container::getCourseSettingsManager(); |
||
| 262 | $settingsManager->setCourse($course); |
||
| 263 | |||
| 264 | $alert = api_get_setting('email_alert_manager_on_new_quiz'); |
||
| 265 | $defaultEmailExerciseAlert = 0; |
||
| 266 | if ('true' === $alert) { |
||
| 267 | $defaultEmailExerciseAlert = 1; |
||
| 268 | } |
||
| 269 | |||
| 270 | /* course_setting table (courseinfo tool) */ |
||
| 271 | $settings = [ |
||
| 272 | 'email_alert_manager_on_new_doc' => ['title' => '', 'default' => 0, 'category' => 'work'], |
||
| 273 | 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'], |
||
| 274 | 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'], |
||
| 275 | 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'], |
||
| 276 | 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'], |
||
| 277 | 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'], |
||
| 278 | 'course_theme' => ['default' => '', 'category' => 'theme'], |
||
| 279 | 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'], |
||
| 280 | 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'], |
||
| 281 | 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' => 'registration'], |
||
| 282 | 'allow_user_view_user_list' => ['default' => 1, 'category' => 'user'], |
||
| 283 | 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' => 'thematic_advance'], |
||
| 284 | 'email_alert_students_on_new_homework' => ['default' => 0, 'category' => 'work'], |
||
| 285 | 'enable_lp_auto_launch' => ['default' => 0, 'category' => 'learning_path'], |
||
| 286 | 'enable_exercise_auto_launch' => ['default' => 0, 'category' => 'exercise'], |
||
| 287 | 'enable_document_auto_launch' => ['default' => 0, 'category' => 'document'], |
||
| 288 | 'pdf_export_watermark_text' => ['default' => '', 'category' => 'learning_path'], |
||
| 289 | 'allow_public_certificates' => [ |
||
| 290 | 'default' => 'true' === api_get_setting('allow_public_certificates') ? 1 : '', |
||
| 291 | 'category' => 'certificates', |
||
| 292 | ], |
||
| 293 | 'documents_default_visibility' => ['default' => 'visible', 'category' => 'document'], |
||
| 294 | 'show_course_in_user_language' => ['default' => 2, 'category' => null], |
||
| 295 | 'email_to_teachers_on_new_work_feedback' => ['default' => 1, 'category' => null], |
||
| 296 | ]; |
||
| 297 | |||
| 298 | $counter = 1; |
||
| 299 | foreach ($settings as $variable => $setting) { |
||
| 300 | $title = $setting['title'] ?? ''; |
||
| 301 | Database::query( |
||
| 302 | "INSERT INTO $TABLESETTING (c_id, title, variable, value, category) |
||
| 303 | VALUES ($course_id, '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')" |
||
| 304 | ); |
||
| 305 | $counter++; |
||
| 306 | } |
||
| 307 | |||
| 308 | /* Course homepage tools for platform admin only */ |
||
| 309 | /* Group tool */ |
||
| 310 | $groupCategory = new CGroupCategory(); |
||
| 311 | $groupCategory |
||
| 312 | ->setTitle(get_lang('Default groups')) |
||
| 313 | ->setParent($course) |
||
| 314 | ->addCourseLink($course) |
||
| 315 | ; |
||
| 316 | Database::getManager()->persist($groupCategory); |
||
| 317 | |||
| 318 | $now = api_get_utc_datetime(); |
||
| 319 | /*$files = [ |
||
| 320 | ['path' => '/shared_folder', 'title' => get_lang('Folders of users'), 'filetype' => 'folder', 'size' => 0], |
||
| 321 | [ |
||
| 322 | 'path' => '/chat_files', |
||
| 323 | 'title' => get_lang('Chat conversations history'), |
||
| 324 | 'filetype' => 'folder', |
||
| 325 | 'size' => 0, |
||
| 326 | ], |
||
| 327 | ['path' => '/certificates', 'title' => get_lang('Certificates'), 'filetype' => 'folder', 'size' => 0], |
||
| 328 | ]; |
||
| 329 | |||
| 330 | $counter = 1; |
||
| 331 | foreach ($files as $file) { |
||
| 332 | self::insertDocument($courseInfo, $counter, $file, $authorId); |
||
| 333 | $counter++; |
||
| 334 | }*/ |
||
| 335 | |||
| 336 | $certificateId = 'NULL'; |
||
| 337 | /* Documents */ |
||
| 338 | if ($fill_with_exemplary_content) { |
||
| 339 | $files = [ |
||
| 340 | ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0], |
||
| 341 | //['path' => '/flash', 'title' => get_lang('Flash'), 'filetype' => 'folder', 'size' => 0], |
||
| 342 | ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0], |
||
| 343 | ['path' => '/images/gallery', 'title' => get_lang('Gallery'), 'filetype' => 'folder', 'size' => 0], |
||
| 344 | ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0], |
||
| 345 | //['path' => '/video/flv', 'title' => 'flv', 'filetype' => 'folder', 'size' => 0], |
||
| 346 | ]; |
||
| 347 | $paths = []; |
||
| 348 | foreach ($files as $file) { |
||
| 349 | $doc = self::insertDocument($courseInfo, $counter, $file, $authorId); |
||
| 350 | $paths[$file['path']] = $doc->getIid(); |
||
| 351 | $counter++; |
||
| 352 | } |
||
| 353 | |||
| 354 | $finder = new Symfony\Component\Finder\Finder(); |
||
| 355 | $defaultPath = api_get_path(SYS_PUBLIC_PATH).'img/document'; |
||
| 356 | $finder->in($defaultPath); |
||
| 357 | |||
| 358 | /** @var SplFileInfo $file */ |
||
| 359 | foreach ($finder as $file) { |
||
| 360 | $parentName = dirname(str_replace($defaultPath, '', $file->getRealPath())); |
||
| 361 | if ('/' === $parentName || '/certificates' === $parentName) { |
||
| 362 | continue; |
||
| 363 | } |
||
| 364 | |||
| 365 | $title = $file->getFilename(); |
||
| 366 | $parentId = $paths[$parentName]; |
||
| 367 | |||
| 368 | if ($file->isDir()) { |
||
| 369 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 370 | $document = DocumentManager::addDocument( |
||
| 371 | $courseInfo, |
||
| 372 | $realPath, |
||
| 373 | 'folder', |
||
| 374 | null, |
||
| 375 | $title, |
||
| 376 | '', |
||
| 377 | null, |
||
| 378 | null, |
||
| 379 | null, |
||
| 380 | null, |
||
| 381 | null, |
||
| 382 | false, |
||
| 383 | null, |
||
| 384 | $parentId, |
||
| 385 | $file->getRealPath() |
||
| 386 | ); |
||
| 387 | $paths[$realPath] = $document->getIid(); |
||
| 388 | } else { |
||
| 389 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 390 | $document = DocumentManager::addDocument( |
||
| 391 | $courseInfo, |
||
| 392 | $realPath, |
||
| 393 | 'file', |
||
| 394 | $file->getSize(), |
||
| 395 | $title, |
||
| 396 | '', |
||
| 397 | null, |
||
| 398 | null, |
||
| 399 | null, |
||
| 400 | null, |
||
| 401 | null, |
||
| 402 | false, |
||
| 403 | null, |
||
| 404 | $parentId, |
||
| 405 | $file->getRealPath() |
||
| 406 | ); |
||
| 407 | |||
| 408 | if ($document && 'default.html' === $document->getTitle()) { |
||
| 409 | $certificateId = $document->getIid(); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | $agenda = new Agenda('course'); |
||
| 415 | $agenda->set_course($courseInfo); |
||
| 416 | $agenda->addEvent( |
||
| 417 | $now, |
||
| 418 | $now, |
||
| 419 | 0, |
||
| 420 | get_lang('Course creation'), |
||
| 421 | get_lang('This course was created at this time') |
||
| 422 | ); |
||
| 423 | |||
| 424 | /* Links tool */ |
||
| 425 | $link = new Link(); |
||
| 426 | $link->setCourse($courseInfo); |
||
| 427 | $links = [ |
||
| 428 | [ |
||
| 429 | 'c_id' => $course_id, |
||
| 430 | 'url' => 'http://www.google.com', |
||
| 431 | 'title' => 'Quick and powerful search engine', |
||
| 432 | 'description' => get_lang('Quick and powerful search engine'), |
||
| 433 | 'category_id' => 0, |
||
| 434 | 'on_homepage' => 0, |
||
| 435 | 'target' => '_self', |
||
| 436 | 'session_id' => 0, |
||
| 437 | ], |
||
| 438 | [ |
||
| 439 | 'c_id' => $course_id, |
||
| 440 | 'url' => 'http://www.wikipedia.org', |
||
| 441 | 'title' => 'Free online encyclopedia', |
||
| 442 | 'description' => get_lang('Free online encyclopedia'), |
||
| 443 | 'category_id' => 0, |
||
| 444 | 'on_homepage' => 0, |
||
| 445 | 'target' => '_self', |
||
| 446 | 'session_id' => 0, |
||
| 447 | ], |
||
| 448 | ]; |
||
| 449 | |||
| 450 | foreach ($links as $params) { |
||
| 451 | $link->save($params, false, false); |
||
| 452 | } |
||
| 453 | |||
| 454 | /* Announcement tool */ |
||
| 455 | AnnouncementManager::add_announcement( |
||
| 456 | $courseInfo, |
||
| 457 | 0, |
||
| 458 | get_lang('This is an announcement example'), |
||
| 459 | get_lang('This is an announcement example. Only trainers are allowed to publish announcements.'), |
||
| 460 | ['everyone' => 'everyone'], |
||
| 461 | null, |
||
| 462 | null, |
||
| 463 | $now |
||
| 464 | ); |
||
| 465 | |||
| 466 | $manager = Database::getManager(); |
||
| 467 | |||
| 468 | /* Introduction text */ |
||
| 469 | $intro_text = '<p style="text-align: center;"> |
||
| 470 | <img src="'.api_get_path(REL_CODE_PATH).'img/mascot.png" alt="Mr. Chamilo" title="Mr. Chamilo" /> |
||
| 471 | <h2>'.get_lang('Introduction text').'</h2> |
||
| 472 | </p>'; |
||
| 473 | |||
| 474 | $toolIntro = new CToolIntro(); |
||
| 475 | $toolIntro |
||
| 476 | ->setCId($course_id) |
||
| 477 | ->setSessionId(0) |
||
| 478 | ->setIntroText($intro_text); |
||
| 479 | $manager->persist($toolIntro); |
||
| 480 | |||
| 481 | $toolIntro = new CToolIntro(); |
||
| 482 | $toolIntro |
||
| 483 | ->setCId($course_id) |
||
| 484 | ->setSessionId(0) |
||
| 485 | ->setIntroText(get_lang('This page allows users and groups to publish documents.')); |
||
| 486 | $manager->persist($toolIntro); |
||
| 487 | |||
| 488 | $toolIntro = new CToolIntro(); |
||
| 489 | $toolIntro |
||
| 490 | ->setCId($course_id) |
||
| 491 | ->setSessionId(0) |
||
| 492 | ->setIntroText(get_lang('The word Wiki is short for WikiWikiWeb. Wikiwiki is a Hawaiian word, meaning "fast" or "speed". In a wiki, people write pages together. If one person writes something wrong, the next person can correct it. The next person can also add something new to the page. Because of this, the pages improve continuously.')); |
||
| 493 | $manager->persist($toolIntro); |
||
| 494 | |||
| 495 | $manager->flush(); |
||
| 496 | |||
| 497 | /* Exercise tool */ |
||
| 498 | $exercise = new Exercise($course_id); |
||
| 499 | $exercise->exercise = get_lang('Sample test'); |
||
| 500 | $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0"> |
||
| 501 | <tr> |
||
| 502 | <td width="220" valign="top" align="left"> |
||
| 503 | <img src="'.api_get_path(WEB_PUBLIC_PATH).'img/document/images/mr_chamilo/doubts.png"> |
||
| 504 | </td> |
||
| 505 | <td valign="top" align="left">'.get_lang('Irony').'</td></tr> |
||
| 506 | </table>'; |
||
| 507 | $exercise->type = 1; |
||
| 508 | $exercise->setRandom(0); |
||
| 509 | $exercise->active = 1; |
||
| 510 | $exercise->results_disabled = 0; |
||
| 511 | $exercise->description = $html; |
||
| 512 | $exercise->save(); |
||
| 513 | |||
| 514 | $exercise_id = $exercise->id; |
||
| 515 | |||
| 516 | $question = new MultipleAnswer(); |
||
| 517 | $question->course = $courseInfo; |
||
| 518 | $question->question = get_lang('Socratic irony is...'); |
||
| 519 | $question->description = get_lang('(more than one answer can be true)'); |
||
| 520 | $question->weighting = 10; |
||
| 521 | $question->position = 1; |
||
| 522 | $question->course = $courseInfo; |
||
| 523 | $question->save($exercise); |
||
| 524 | $questionId = $question->id; |
||
| 525 | |||
| 526 | $answer = new Answer($questionId, $courseInfo['real_id']); |
||
| 527 | $answer->createAnswer(get_lang('Ridiculise one\'s interlocutor in order to have him concede he is wrong.'), 0, get_lang('No. Socratic irony is not a matter of psychology, it concerns argumentation.'), -5, 1); |
||
| 528 | $answer->createAnswer(get_lang('Admit one\'s own errors to invite one\'s interlocutor to do the same.'), 0, get_lang('No. Socratic irony is not a seduction strategy or a method based on the example.'), -5, 2); |
||
| 529 | $answer->createAnswer(get_lang('Compell one\'s interlocutor, by a series of questions and sub-questions, to admit he doesn\'t know what he claims to know.'), 1, get_lang('Indeed'), 5, 3); |
||
| 530 | $answer->createAnswer(get_lang('Use the Principle of Non Contradiction to force one\'s interlocutor into a dead end.'), 1, get_lang('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); |
||
| 531 | $answer->save(); |
||
| 532 | // Forums. |
||
| 533 | $params = [ |
||
| 534 | 'forum_category_title' => get_lang('Example Forum Category'), |
||
| 535 | 'forum_category_comment' => '', |
||
| 536 | ]; |
||
| 537 | |||
| 538 | $forumCategoryId = store_forumcategory($params, $courseInfo, false); |
||
| 539 | |||
| 540 | $params = [ |
||
| 541 | 'forum_category' => $forumCategoryId, |
||
| 542 | 'forum_title' => get_lang('Example Forum'), |
||
| 543 | 'forum_comment' => '', |
||
| 544 | 'default_view_type_group' => ['default_view_type' => 'flat'], |
||
| 545 | ]; |
||
| 546 | |||
| 547 | $forumId = store_forum($params, $courseInfo, true); |
||
| 548 | $repo = Container::getForumRepository(); |
||
| 549 | $forumEntity = $repo->find($forumId); |
||
| 550 | |||
| 551 | $params = [ |
||
| 552 | 'post_title' => get_lang('Example Thread'), |
||
| 553 | 'forum_id' => $forumId, |
||
| 554 | 'post_text' => get_lang('Example ThreadContent'), |
||
| 555 | 'calification_notebook_title' => '', |
||
| 556 | 'numeric_calification' => '', |
||
| 557 | 'weight_calification' => '', |
||
| 558 | 'forum_category' => $forumCategoryId, |
||
| 559 | 'thread_peer_qualify' => 0, |
||
| 560 | ]; |
||
| 561 | |||
| 562 | saveThread($forumEntity, $params, $courseInfo, false); |
||
| 563 | |||
| 564 | /* Gradebook tool */ |
||
| 565 | $course_code = $courseInfo['code']; |
||
| 566 | // father gradebook |
||
| 567 | Database::query( |
||
| 568 | "INSERT INTO $TABLEGRADEBOOK (name, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id) |
||
| 569 | VALUES ('$course_code','0',0,'',1,$course_id,0,100,0,75,NULL,$certificateId)" |
||
| 570 | ); |
||
| 571 | $gbid = Database::insert_id(); |
||
| 572 | Database::query( |
||
| 573 | "INSERT INTO $TABLEGRADEBOOK (name, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id) |
||
| 574 | VALUES ('$course_code','0',0,'',1,$course_id,$gbid,100,1,75,NULL,$certificateId)" |
||
| 575 | ); |
||
| 576 | $gbid = Database:: insert_id(); |
||
| 577 | Database::query( |
||
| 578 | "INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, c_id, category_id, created_at, weight, visible, locked) |
||
| 579 | VALUES (1,$exercise_id,1,$course_id,$gbid,'$now',100,1,0)" |
||
| 580 | ); |
||
| 581 | } |
||
| 582 | |||
| 583 | // Installing plugins in course |
||
| 584 | $app_plugin = new AppPlugin(); |
||
| 585 | $app_plugin->install_course_plugins($course_id); |
||
| 586 | |||
| 587 | return true; |
||
| 588 | } |
||
| 936 |