| Total Complexity | 76 |
| Total Lines | 974 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AddCourse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AddCourse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AddCourse |
||
| 17 | { |
||
| 18 | public const FIRST_EXPIRATION_DATE = 31536000; // 365 days in seconds |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Defines the four needed keys to create a course based on several parameters. |
||
| 22 | * |
||
| 23 | * @param string The code you want for this course |
||
| 24 | * @param string Prefix added for ALL keys |
||
| 25 | * @param string Prefix added for databases only |
||
| 26 | * @param string Prefix added for paths only |
||
| 27 | * @param bool Add unique prefix |
||
| 28 | * @param bool Use code-independent keys |
||
| 29 | * |
||
| 30 | * @return array An array with the needed keys ['currentCourseCode'], ['currentCourseId'], ['currentCourseDbName'], |
||
| 31 | * ['currentCourseRepository'] |
||
| 32 | * |
||
| 33 | * @todo Eliminate the global variables. |
||
| 34 | * @assert (null) === false |
||
| 35 | */ |
||
| 36 | public static function define_course_keys( |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Gets an array with all the course tables (deprecated?). |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | * |
||
| 96 | * @assert (null) !== null |
||
| 97 | */ |
||
| 98 | public static function get_course_tables() |
||
| 99 | { |
||
| 100 | $tables = []; |
||
| 101 | //$tables[] = 'item_property'; |
||
| 102 | $tables[] = 'tool'; |
||
| 103 | $tables[] = 'tool_intro'; |
||
| 104 | $tables[] = 'group_info'; |
||
| 105 | $tables[] = 'group_category'; |
||
| 106 | $tables[] = 'group_rel_user'; |
||
| 107 | $tables[] = 'group_rel_tutor'; |
||
| 108 | $tables[] = 'userinfo_content'; |
||
| 109 | $tables[] = 'userinfo_def'; |
||
| 110 | $tables[] = 'course_description'; |
||
| 111 | $tables[] = 'calendar_event'; |
||
| 112 | $tables[] = 'calendar_event_repeat'; |
||
| 113 | $tables[] = 'calendar_event_repeat_not'; |
||
| 114 | $tables[] = 'calendar_event_attachment'; |
||
| 115 | $tables[] = 'announcement'; |
||
| 116 | $tables[] = 'announcement_attachment'; |
||
| 117 | //$tables[] = 'resource'; |
||
| 118 | $tables[] = 'student_publication'; |
||
| 119 | $tables[] = 'student_publication_assignment'; |
||
| 120 | $tables[] = 'document'; |
||
| 121 | /*$tables[] = 'forum_category'; |
||
| 122 | $tables[] = 'forum_forum'; |
||
| 123 | $tables[] = 'forum_thread'; |
||
| 124 | $tables[] = 'forum_post'; |
||
| 125 | $tables[] = 'forum_mailcue'; |
||
| 126 | $tables[] = 'forum_attachment'; |
||
| 127 | $tables[] = 'forum_notification'; |
||
| 128 | $tables[] = 'forum_thread_qualify'; |
||
| 129 | $tables[] = 'forum_thread_qualify_log';*/ |
||
| 130 | $tables[] = 'link'; |
||
| 131 | $tables[] = 'link_category'; |
||
| 132 | $tables[] = 'online_connected'; |
||
| 133 | $tables[] = 'online_link'; |
||
| 134 | $tables[] = 'chat_connected'; |
||
| 135 | $tables[] = 'quiz'; |
||
| 136 | $tables[] = 'quiz_rel_question'; |
||
| 137 | $tables[] = 'quiz_question'; |
||
| 138 | $tables[] = 'quiz_answer'; |
||
| 139 | $tables[] = 'quiz_question_option'; |
||
| 140 | $tables[] = 'quiz_question_category'; |
||
| 141 | $tables[] = 'quiz_question_rel_category'; |
||
| 142 | $tables[] = 'dropbox_post'; |
||
| 143 | $tables[] = 'dropbox_file'; |
||
| 144 | $tables[] = 'dropbox_person'; |
||
| 145 | $tables[] = 'dropbox_category'; |
||
| 146 | $tables[] = 'dropbox_feedback'; |
||
| 147 | $tables[] = 'lp'; |
||
| 148 | $tables[] = 'lp_item'; |
||
| 149 | $tables[] = 'lp_view'; |
||
| 150 | $tables[] = 'lp_item_view'; |
||
| 151 | $tables[] = 'lp_iv_interaction'; |
||
| 152 | $tables[] = 'lp_iv_objective'; |
||
| 153 | $tables[] = 'blog'; |
||
| 154 | $tables[] = 'blog_comment'; |
||
| 155 | $tables[] = 'blog_post'; |
||
| 156 | $tables[] = 'blog_rating'; |
||
| 157 | $tables[] = 'blog_rel_user'; |
||
| 158 | $tables[] = 'blog_task'; |
||
| 159 | $tables[] = 'blog_task_rel_user'; |
||
| 160 | $tables[] = 'blog_attachment'; |
||
| 161 | $tables[] = 'permission_group'; |
||
| 162 | $tables[] = 'permission_user'; |
||
| 163 | $tables[] = 'permission_task'; |
||
| 164 | $tables[] = 'role'; |
||
| 165 | $tables[] = 'role_group'; |
||
| 166 | $tables[] = 'role_permissions'; |
||
| 167 | $tables[] = 'role_user'; |
||
| 168 | $tables[] = 'survey'; |
||
| 169 | $tables[] = 'survey_question'; |
||
| 170 | $tables[] = 'survey_question_option'; |
||
| 171 | $tables[] = 'survey_invitation'; |
||
| 172 | $tables[] = 'survey_answer'; |
||
| 173 | $tables[] = 'survey_group'; |
||
| 174 | $tables[] = 'wiki'; |
||
| 175 | $tables[] = 'wiki_conf'; |
||
| 176 | $tables[] = 'wiki_discuss'; |
||
| 177 | $tables[] = 'wiki_mailcue'; |
||
| 178 | $tables[] = 'course_setting'; |
||
| 179 | $tables[] = 'glossary'; |
||
| 180 | $tables[] = 'notebook'; |
||
| 181 | $tables[] = 'attendance'; |
||
| 182 | $tables[] = 'attendance_sheet'; |
||
| 183 | $tables[] = 'attendance_calendar'; |
||
| 184 | $tables[] = 'attendance_result'; |
||
| 185 | $tables[] = 'attendance_sheet_log'; |
||
| 186 | $tables[] = 'thematic'; |
||
| 187 | $tables[] = 'thematic_plan'; |
||
| 188 | $tables[] = 'thematic_advance'; |
||
| 189 | |||
| 190 | return $tables; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Executed only before create_course_tables(). |
||
| 195 | * |
||
| 196 | * @assert (null) === null |
||
| 197 | */ |
||
| 198 | public static function drop_course_tables() |
||
| 199 | { |
||
| 200 | $list = self::get_course_tables(); |
||
| 201 | foreach ($list as $table) { |
||
| 202 | $sql = "DROP TABLE IF EXISTS ".DB_COURSE_PREFIX.$table; |
||
| 203 | Database::query($sql); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Sorts pictures by type (used?). |
||
| 209 | * |
||
| 210 | * @param array List of files (sthg like array(0=>array('png'=>1))) |
||
| 211 | * @param string $type |
||
| 212 | * |
||
| 213 | * @return array The received array without files not matching type |
||
| 214 | * @assert (array(),null) === array() |
||
| 215 | */ |
||
| 216 | public static function sort_pictures($files, $type) |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Populates the course with essential settings, group categories, example content, and installs course plugins. |
||
| 230 | * |
||
| 231 | * This method initializes a new course by setting up various course settings, creating a default group category, |
||
| 232 | * inserting example content if required, and installing course-specific plugins. The method can also fill the course |
||
| 233 | * with exemplary content based on the parameter provided or the system setting for example content creation. |
||
| 234 | * |
||
| 235 | * @param Course $course The course entity to be populated. |
||
| 236 | * @param bool|null $fillWithExemplaryContent Determines whether to fill the course with exemplary content. If null, |
||
| 237 | * the system setting for example material course creation is used. |
||
| 238 | * @param int $authorId The ID of the user who is considered the author of the exemplary content. Defaults to the |
||
| 239 | * current user if not specified. |
||
| 240 | * |
||
| 241 | * @return bool Returns true if the course is successfully populated, false otherwise. |
||
| 242 | * |
||
| 243 | * @throws Exception Throws exception on error. |
||
| 244 | */ |
||
| 245 | public static function fillCourse(Course $course, bool $fillWithExemplaryContent = null, int $authorId = 0): bool |
||
| 246 | { |
||
| 247 | $entityManager = Database::getManager(); |
||
| 248 | $authorId = $authorId ?: api_get_user_id(); |
||
| 249 | |||
| 250 | self::insertCourseSettings($course); |
||
| 251 | self::createGroupCategory($course); |
||
| 252 | $gradebook = self::createRootGradebook($course); |
||
| 253 | |||
| 254 | if ($fillWithExemplaryContent ?? api_get_setting('example_material_course_creation') !== 'false') { |
||
| 255 | self::insertExampleContent($course, $authorId, $entityManager); |
||
| 256 | } |
||
| 257 | |||
| 258 | self::installCoursePlugins($course->getId()); |
||
| 259 | |||
| 260 | return true; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Inserts default and specified settings for a given course. |
||
| 265 | * |
||
| 266 | * This method takes a Course object as input and applies a predefined |
||
| 267 | * set of settings. These settings include configurations for email alerts, |
||
| 268 | * permissions for users to edit various components like agenda and announcements, |
||
| 269 | * theme settings, and more. It also handles the case where a course needs to |
||
| 270 | * enable certain features by default based on platform-wide settings. |
||
| 271 | * |
||
| 272 | * @param Course $course The course object to which the settings will be applied. |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | * @throws Exception |
||
| 276 | */ |
||
| 277 | private static function insertCourseSettings(Course $course): void |
||
| 278 | { |
||
| 279 | |||
| 280 | $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 281 | |||
| 282 | $settingsManager = Container::getCourseSettingsManager(); |
||
| 283 | $settingsManager->setCourse($course); |
||
| 284 | |||
| 285 | $alert = api_get_setting('email_alert_manager_on_new_quiz'); |
||
| 286 | $defaultEmailExerciseAlert = 0; |
||
| 287 | if ('true' === $alert) { |
||
| 288 | $defaultEmailExerciseAlert = 1; |
||
| 289 | } |
||
| 290 | |||
| 291 | /* course_setting table (courseinfo tool) */ |
||
| 292 | $settings = [ |
||
| 293 | 'email_alert_manager_on_new_doc' => ['title' => '', 'default' => 0, 'category' => 'work'], |
||
| 294 | 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'], |
||
| 295 | 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'], |
||
| 296 | 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'], |
||
| 297 | 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'], |
||
| 298 | 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'], |
||
| 299 | 'course_theme' => ['default' => '', 'category' => 'theme'], |
||
| 300 | 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'], |
||
| 301 | 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'], |
||
| 302 | 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' => 'registration'], |
||
| 303 | 'allow_user_view_user_list' => ['default' => 1, 'category' => 'user'], |
||
| 304 | 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' => 'thematic_advance'], |
||
| 305 | 'email_alert_students_on_new_homework' => ['default' => 0, 'category' => 'work'], |
||
| 306 | 'enable_lp_auto_launch' => ['default' => 0, 'category' => 'learning_path'], |
||
| 307 | 'enable_exercise_auto_launch' => ['default' => 0, 'category' => 'exercise'], |
||
| 308 | 'enable_document_auto_launch' => ['default' => 0, 'category' => 'document'], |
||
| 309 | 'pdf_export_watermark_text' => ['default' => '', 'category' => 'learning_path'], |
||
| 310 | 'allow_public_certificates' => [ |
||
| 311 | 'default' => 'true' === api_get_setting('allow_public_certificates') ? 1 : '', |
||
| 312 | 'category' => 'certificates', |
||
| 313 | ], |
||
| 314 | 'documents_default_visibility' => ['default' => 'visible', 'category' => 'document'], |
||
| 315 | 'show_course_in_user_language' => ['default' => 2, 'category' => null], |
||
| 316 | 'email_to_teachers_on_new_work_feedback' => ['default' => 1, 'category' => null], |
||
| 317 | ]; |
||
| 318 | |||
| 319 | $counter = 1; |
||
| 320 | foreach ($settings as $variable => $setting) { |
||
| 321 | $title = $setting['title'] ?? ''; |
||
| 322 | Database::query( |
||
| 323 | "INSERT INTO $TABLESETTING (c_id, title, variable, value, category) |
||
| 324 | VALUES ({$course->getId()}, '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')" |
||
| 325 | ); |
||
| 326 | $counter++; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Creates a default group category for the specified course. |
||
| 332 | * |
||
| 333 | * This method initializes a new group category with a default title |
||
| 334 | * and associates it with the given course. The default group category |
||
| 335 | * is essential for organizing groups within the course, allowing for |
||
| 336 | * better management and classification of course participants. |
||
| 337 | * |
||
| 338 | * @param Course $course The course object for which the default group category is created. |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | private static function createGroupCategory(Course $course): void |
||
| 343 | { |
||
| 344 | $groupCategory = new CGroupCategory(); |
||
| 345 | $groupCategory |
||
| 346 | ->setTitle(get_lang('Default groups')) |
||
| 347 | ->setParent($course) |
||
| 348 | ->addCourseLink($course) |
||
| 349 | ; |
||
| 350 | Database::getManager()->persist($groupCategory); |
||
| 351 | Database::getManager()->flush(); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Inserts example content into a given course. |
||
| 356 | * |
||
| 357 | * This method populates the specified course with a predefined set of example |
||
| 358 | * content, including documents, links, announcements, and exercises. This content |
||
| 359 | * serves as a template or starting point for instructors, showcasing the various |
||
| 360 | * types of materials and activities that can be included in a course. The content |
||
| 361 | * is added under the authority of the specified author ID. |
||
| 362 | * |
||
| 363 | * @param Course $course The course object into which the example content will be inserted. |
||
| 364 | * @param int $authorId The ID of the user who will be listed as the author of the inserted content. |
||
| 365 | * @param GradebookCategory $gradebook |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | * @throws Exception |
||
| 369 | */ |
||
| 370 | private static function insertExampleContent(Course $course, int $authorId, GradebookCategory $gradebook): void |
||
| 371 | { |
||
| 372 | $now = api_get_utc_datetime(); |
||
| 373 | $files = [ |
||
| 374 | ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0], |
||
| 375 | ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0], |
||
| 376 | ['path' => '/images/gallery', 'title' => get_lang('Gallery'), 'filetype' => 'folder', 'size' => 0], |
||
| 377 | ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0], |
||
| 378 | ]; |
||
| 379 | $paths = []; |
||
| 380 | $courseInfo = ['real_id' => $course->getId(), 'code' => $course->getCode()]; |
||
| 381 | $counter = 1; |
||
| 382 | foreach ($files as $file) { |
||
| 383 | $doc = self::insertDocument($courseInfo, $counter, $file, $authorId); |
||
| 384 | $paths[$file['path']] = $doc->getIid(); |
||
| 385 | $counter++; |
||
| 386 | } |
||
| 387 | |||
| 388 | $finder = new Symfony\Component\Finder\Finder(); |
||
| 389 | $defaultPath = api_get_path(SYS_PUBLIC_PATH).'img/document'; |
||
| 390 | $finder->in($defaultPath); |
||
| 391 | |||
| 392 | /** @var SplFileInfo $file */ |
||
| 393 | foreach ($finder as $file) { |
||
| 394 | $parentName = dirname(str_replace($defaultPath, '', $file->getRealPath())); |
||
| 395 | if ('/' === $parentName || '/certificates' === $parentName) { |
||
| 396 | continue; |
||
| 397 | } |
||
| 398 | |||
| 399 | $title = $file->getFilename(); |
||
| 400 | $parentId = $paths[$parentName]; |
||
| 401 | |||
| 402 | if ($file->isDir()) { |
||
| 403 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 404 | $document = DocumentManager::addDocument( |
||
| 405 | $courseInfo, |
||
| 406 | $realPath, |
||
| 407 | 'folder', |
||
| 408 | null, |
||
| 409 | $title, |
||
| 410 | '', |
||
| 411 | null, |
||
| 412 | null, |
||
| 413 | null, |
||
| 414 | null, |
||
| 415 | null, |
||
| 416 | false, |
||
| 417 | null, |
||
| 418 | $parentId, |
||
| 419 | $file->getRealPath() |
||
| 420 | ); |
||
| 421 | $paths[$realPath] = $document->getIid(); |
||
| 422 | } else { |
||
| 423 | $realPath = str_replace($defaultPath, '', $file->getRealPath()); |
||
| 424 | $document = DocumentManager::addDocument( |
||
| 425 | $courseInfo, |
||
| 426 | $realPath, |
||
| 427 | 'file', |
||
| 428 | $file->getSize(), |
||
| 429 | $title, |
||
| 430 | '', |
||
| 431 | null, |
||
| 432 | null, |
||
| 433 | null, |
||
| 434 | null, |
||
| 435 | null, |
||
| 436 | false, |
||
| 437 | null, |
||
| 438 | $parentId, |
||
| 439 | $file->getRealPath() |
||
| 440 | ); |
||
| 441 | |||
| 442 | if ($document && 'default.html' === $document->getTitle()) { |
||
| 443 | $certificateId = $document->getIid(); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | $agenda = new Agenda('course'); |
||
| 449 | $agenda->set_course($courseInfo); |
||
| 450 | $agenda->addEvent( |
||
| 451 | $now, |
||
| 452 | $now, |
||
| 453 | 0, |
||
| 454 | get_lang('Course creation'), |
||
| 455 | get_lang('This course was created at this time') |
||
| 456 | ); |
||
| 457 | |||
| 458 | /* Links tool */ |
||
| 459 | $link = new Link(); |
||
| 460 | $link->setCourse($courseInfo); |
||
| 461 | $links = [ |
||
| 462 | [ |
||
| 463 | 'c_id' => $course->getId(), |
||
| 464 | 'url' => 'http://www.google.com', |
||
| 465 | 'title' => 'Quick and powerful search engine', |
||
| 466 | 'description' => get_lang('Quick and powerful search engine'), |
||
| 467 | 'category_id' => 0, |
||
| 468 | 'on_homepage' => 0, |
||
| 469 | 'target' => '_self', |
||
| 470 | 'session_id' => 0, |
||
| 471 | ], |
||
| 472 | [ |
||
| 473 | 'c_id' => $course->getId(), |
||
| 474 | 'url' => 'http://www.wikipedia.org', |
||
| 475 | 'title' => 'Free online encyclopedia', |
||
| 476 | 'description' => get_lang('Free online encyclopedia'), |
||
| 477 | 'category_id' => 0, |
||
| 478 | 'on_homepage' => 0, |
||
| 479 | 'target' => '_self', |
||
| 480 | 'session_id' => 0, |
||
| 481 | ], |
||
| 482 | ]; |
||
| 483 | |||
| 484 | foreach ($links as $params) { |
||
| 485 | $link->save($params, false, false); |
||
| 486 | } |
||
| 487 | |||
| 488 | /* Announcement tool */ |
||
| 489 | AnnouncementManager::add_announcement( |
||
| 490 | $courseInfo, |
||
| 491 | 0, |
||
| 492 | get_lang('This is an announcement example'), |
||
| 493 | get_lang('This is an announcement example. Only trainers are allowed to publish announcements.'), |
||
| 494 | ['everyone' => 'everyone'], |
||
| 495 | null, |
||
| 496 | null, |
||
| 497 | $now |
||
| 498 | ); |
||
| 499 | |||
| 500 | /* Exercise tool */ |
||
| 501 | $exercise = new Exercise($course->getId()); |
||
| 502 | $exercise->exercise = get_lang('Sample test'); |
||
| 503 | $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0"> |
||
| 504 | <tr> |
||
| 505 | <td width="220" valign="top" align="left"> |
||
| 506 | <img src="'.api_get_path(WEB_PUBLIC_PATH).'img/document/images/mr_chamilo/doubts.png"> |
||
| 507 | </td> |
||
| 508 | <td valign="top" align="left">'.get_lang('Irony').'</td></tr> |
||
| 509 | </table>'; |
||
| 510 | $exercise->type = 1; |
||
| 511 | $exercise->setRandom(0); |
||
| 512 | $exercise->active = 1; |
||
| 513 | $exercise->results_disabled = 0; |
||
| 514 | $exercise->description = $html; |
||
| 515 | $exercise->save(); |
||
| 516 | |||
| 517 | $question = new MultipleAnswer(); |
||
| 518 | $question->course = $courseInfo; |
||
| 519 | $question->question = get_lang('Socratic irony is...'); |
||
| 520 | $question->description = get_lang('(more than one answer can be true)'); |
||
| 521 | $question->weighting = 10; |
||
| 522 | $question->position = 1; |
||
| 523 | $question->course = $courseInfo; |
||
| 524 | $question->save($exercise); |
||
| 525 | $questionId = $question->id; |
||
| 526 | |||
| 527 | $answer = new Answer($questionId, $courseInfo['real_id']); |
||
| 528 | $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); |
||
| 529 | $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); |
||
| 530 | $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); |
||
| 531 | $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); |
||
| 532 | $answer->save(); |
||
| 533 | // Forums. |
||
| 534 | $params = [ |
||
| 535 | 'forum_category_title' => get_lang('Example Forum Category'), |
||
| 536 | 'forum_category_comment' => '', |
||
| 537 | ]; |
||
| 538 | |||
| 539 | $forumCategoryId = saveForumCategory($params, $courseInfo, false); |
||
| 540 | |||
| 541 | $params = [ |
||
| 542 | 'forum_category' => $forumCategoryId, |
||
| 543 | 'forum_title' => get_lang('Example Forum'), |
||
| 544 | 'forum_comment' => '', |
||
| 545 | 'default_view_type_group' => ['default_view_type' => 'flat'], |
||
| 546 | ]; |
||
| 547 | |||
| 548 | $forumId = store_forum($params, $courseInfo, true); |
||
| 549 | $repo = Container::getForumRepository(); |
||
| 550 | $forumEntity = $repo->find($forumId); |
||
| 551 | |||
| 552 | $params = [ |
||
| 553 | 'post_title' => get_lang('Example Thread'), |
||
| 554 | 'forum_id' => $forumId, |
||
| 555 | 'post_text' => get_lang('Example ThreadContent'), |
||
| 556 | 'calification_notebook_title' => '', |
||
| 557 | 'numeric_calification' => '', |
||
| 558 | 'weight_calification' => '', |
||
| 559 | 'forum_category' => $forumCategoryId, |
||
| 560 | 'thread_peer_qualify' => 0, |
||
| 561 | ]; |
||
| 562 | |||
| 563 | saveThread($forumEntity, $params, $courseInfo, false); |
||
| 564 | |||
| 565 | self::createExampleGradebookContent($course, $gradebook, $exercise->id); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Creates the gradebook structure for a course. |
||
| 570 | * |
||
| 571 | * This method sets up the initial gradebook categories and links for a new course. |
||
| 572 | * It creates a parent gradebook category representing the course itself and a child |
||
| 573 | * gradebook category for course activities. It then creates a gradebook link associated |
||
| 574 | * with a specific course activity, identified by the $refId parameter. |
||
| 575 | * |
||
| 576 | * @param Course $course The course entity for which the gradebook structure will be created. |
||
| 577 | * @param int $refId The reference ID of the course activity to link in the gradebook. |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | private static function createExampleGradebookContent(Course $course, GradebookCategory $parentCategory, int $refId): void |
||
| 582 | { |
||
| 583 | $manager = Database::getManager(); |
||
| 584 | |||
| 585 | /* Gradebook tool */ |
||
| 586 | $courseCode = $course->getCode(); |
||
| 587 | |||
| 588 | $childGradebookCategory = new GradebookCategory(); |
||
| 589 | $childGradebookCategory->setTitle($courseCode); |
||
| 590 | $childGradebookCategory->setLocked(0); |
||
| 591 | $childGradebookCategory->setGenerateCertificates(false); |
||
| 592 | $childGradebookCategory->setDescription(''); |
||
| 593 | $childGradebookCategory->setCourse($course); |
||
| 594 | $childGradebookCategory->setWeight(100); |
||
| 595 | $childGradebookCategory->setVisible(true); |
||
| 596 | $childGradebookCategory->setCertifMinScore(75); |
||
| 597 | $childGradebookCategory->setParent($parentCategory); |
||
| 598 | $childGradebookCategory->setUser(api_get_user_entity()); |
||
| 599 | |||
| 600 | $manager->persist($childGradebookCategory); |
||
| 601 | $manager->flush(); |
||
| 602 | |||
| 603 | $gradebookLink = new GradebookLink(); |
||
| 604 | |||
| 605 | $gradebookLink->setType(1); |
||
| 606 | $gradebookLink->setRefId($refId); |
||
| 607 | $gradebookLink->setUser(api_get_user_entity()); |
||
| 608 | $gradebookLink->setCourse($course); |
||
| 609 | $gradebookLink->setCategory($childGradebookCategory); |
||
| 610 | $gradebookLink->setCreatedAt(new \DateTime()); |
||
| 611 | $gradebookLink->setWeight(100); |
||
| 612 | $gradebookLink->setVisible(1); |
||
| 613 | $gradebookLink->setLocked(0); |
||
| 614 | |||
| 615 | $manager->persist($gradebookLink); |
||
| 616 | $manager->flush(); |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Creates the basic gradebook structure for a course. |
||
| 621 | * |
||
| 622 | * This method sets up the initial gradebook categories and links for a new course. |
||
| 623 | * It creates a parent gradebook category representing the course itself. |
||
| 624 | * |
||
| 625 | * @param Course $course The course entity for which the gradebook structure will be created. |
||
| 626 | * |
||
| 627 | * @return GradebookCategory The created gradebook's ID |
||
| 628 | * @throws \Doctrine\ORM\Exception\ORMException |
||
| 629 | */ |
||
| 630 | private static function createRootGradebook(Course $course): GradebookCategory |
||
| 631 | { |
||
| 632 | $manager = Database::getManager(); |
||
| 633 | |||
| 634 | /* Gradebook tool */ |
||
| 635 | $courseCode = $course->getCode(); |
||
| 636 | |||
| 637 | $parentGradebookCategory = new GradebookCategory(); |
||
| 638 | $parentGradebookCategory->setTitle($courseCode); |
||
| 639 | $parentGradebookCategory->setLocked(0); |
||
| 640 | $parentGradebookCategory->setGenerateCertificates(false); |
||
| 641 | $parentGradebookCategory->setDescription(''); |
||
| 642 | $parentGradebookCategory->setCourse($course); |
||
| 643 | $parentGradebookCategory->setWeight(100); |
||
| 644 | $parentGradebookCategory->setVisible(false); |
||
| 645 | $parentGradebookCategory->setCertifMinScore(75); |
||
| 646 | $parentGradebookCategory->setUser(api_get_user_entity()); |
||
| 647 | |||
| 648 | $manager->persist($parentGradebookCategory); |
||
| 649 | $manager->flush(); |
||
| 650 | |||
| 651 | return $parentGradebookCategory; |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Installs plugins for a given course. |
||
| 656 | * |
||
| 657 | * This method takes a course ID and uses the AppPlugin service to install |
||
| 658 | * all necessary or default plugins for that specific course. These plugins |
||
| 659 | * can enhance the functionality of the course by adding new features or |
||
| 660 | * tools that are not part of the core Chamilo platform. |
||
| 661 | * |
||
| 662 | * @param int $courseId The ID of the course for which the plugins will be installed. |
||
| 663 | * |
||
| 664 | * @return void |
||
| 665 | */ |
||
| 666 | private static function installCoursePlugins(int $courseId): void |
||
| 667 | { |
||
| 668 | $app_plugin = new AppPlugin(); |
||
| 669 | $app_plugin->install_course_plugins($courseId); |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param array $courseInfo |
||
| 674 | * @param int $counter |
||
| 675 | * @param array $file |
||
| 676 | * @param int $authorId |
||
| 677 | */ |
||
| 678 | public static function insertDocument($courseInfo, $counter, $file, $authorId = 0) |
||
| 679 | { |
||
| 680 | return DocumentManager::addDocument( |
||
| 681 | $courseInfo, |
||
| 682 | $file['path'], |
||
| 683 | $file['filetype'], |
||
| 684 | $file['size'], |
||
| 685 | $file['title'], |
||
| 686 | null, |
||
| 687 | 0, |
||
| 688 | null, |
||
| 689 | 0, |
||
| 690 | 0, |
||
| 691 | 0, |
||
| 692 | false |
||
| 693 | ); |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * string2binary converts the string "true" or "false" to the boolean true false (0 or 1) |
||
| 698 | * This is used for the Chamilo Config Settings as these store true or false as string |
||
| 699 | * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for |
||
| 700 | * the visibility of the tool). |
||
| 701 | * |
||
| 702 | * @param string $variable |
||
| 703 | * |
||
| 704 | * @return bool |
||
| 705 | * |
||
| 706 | * @author Patrick Cool, [email protected] |
||
| 707 | * @assert ('true') === true |
||
| 708 | * @assert ('false') === false |
||
| 709 | */ |
||
| 710 | public static function string2binary($variable) |
||
| 711 | { |
||
| 712 | if ('true' == $variable) { |
||
| 713 | return true; |
||
| 714 | } |
||
| 715 | if ('false' == $variable) { |
||
| 716 | return false; |
||
| 717 | } |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Function register_course to create a record in the course table of the main database. |
||
| 722 | * |
||
| 723 | * @param array $params Course details (see code for details). |
||
| 724 | * |
||
| 725 | * @throws Exception |
||
| 726 | * |
||
| 727 | * @return Course|null |
||
| 728 | */ |
||
| 729 | public static function register_course(array $params): ?Course |
||
| 730 | { |
||
| 731 | global $error_msg; |
||
| 732 | $title = $params['title']; |
||
| 733 | // Fix amp |
||
| 734 | $title = str_replace('&', '&', $title); |
||
| 735 | $code = $params['code']; |
||
| 736 | $visual_code = $params['visual_code']; |
||
| 737 | $directory = $params['directory']; |
||
| 738 | $tutor_name = $params['tutor_name'] ?? null; |
||
| 739 | $course_language = !empty($params['course_language']) ? $params['course_language'] : api_get_setting( |
||
| 740 | 'platformLanguage' |
||
| 741 | ); |
||
| 742 | $department_name = $params['department_name'] ?? null; |
||
| 743 | $department_url = $params['department_url'] ?? null; |
||
| 744 | $disk_quota = $params['disk_quota'] ?? null; |
||
| 745 | |||
| 746 | if (!isset($params['visibility'])) { |
||
| 747 | $default_course_visibility = api_get_setting('courses_default_creation_visibility'); |
||
| 748 | $visibility = $default_course_visibility ?? Course::OPEN_PLATFORM; |
||
| 749 | } else { |
||
| 750 | $visibility = $params['visibility']; |
||
| 751 | } |
||
| 752 | |||
| 753 | $subscribe = false; |
||
| 754 | if (isset($params['subscribe'])) { |
||
| 755 | $subscribe = 1 === (int) $params['subscribe']; |
||
| 756 | } elseif (Course::OPEN_PLATFORM == $visibility) { |
||
| 757 | $subscribe = true; |
||
| 758 | } |
||
| 759 | |||
| 760 | //$subscribe = isset($params['subscribe']) ? (int) $params['subscribe'] : COURSE_VISIBILITY_OPEN_PLATFORM == $visibility ? 1 : 0; |
||
| 761 | $unsubscribe = isset($params['unsubscribe']) ? (int) $params['unsubscribe'] : 0; |
||
| 762 | $expiration_date = $params['expiration_date'] ?? null; |
||
| 763 | $teachers = $params['teachers'] ?? null; |
||
| 764 | $categories = $params['course_categories'] ?? null; |
||
| 765 | $valid = true; |
||
| 766 | |||
| 767 | // Check whether all the needed parameters are present. |
||
| 768 | if (empty($code)) { |
||
| 769 | $error_msg[] = 'courseSysCode is missing'; |
||
| 770 | $valid = false; |
||
| 771 | } |
||
| 772 | if (empty($visual_code)) { |
||
| 773 | $error_msg[] = 'courseScreenCode is missing'; |
||
| 774 | $valid = false; |
||
| 775 | } |
||
| 776 | if (empty($directory)) { |
||
| 777 | $error_msg[] = 'courseRepository is missing'; |
||
| 778 | $valid = false; |
||
| 779 | } |
||
| 780 | |||
| 781 | if (empty($title)) { |
||
| 782 | $error_msg[] = 'title is missing'; |
||
| 783 | $valid = false; |
||
| 784 | } |
||
| 785 | |||
| 786 | if (empty($expiration_date)) { |
||
| 787 | $expiration_date = api_get_utc_datetime( |
||
| 788 | time() + self::FIRST_EXPIRATION_DATE |
||
| 789 | ); |
||
| 790 | } else { |
||
| 791 | $expiration_date = api_get_utc_datetime($expiration_date); |
||
| 792 | } |
||
| 793 | |||
| 794 | if ($visibility < 0 || $visibility > 4) { |
||
| 795 | $error_msg[] = 'visibility is invalid'; |
||
| 796 | $valid = false; |
||
| 797 | } |
||
| 798 | |||
| 799 | if (empty($disk_quota)) { |
||
| 800 | $disk_quota = api_get_setting('default_document_quotum'); |
||
| 801 | } |
||
| 802 | |||
| 803 | if (false === stripos($department_url, 'http://') && false === stripos($department_url, 'https://')) { |
||
| 804 | $department_url = 'http://'.$department_url; |
||
| 805 | } |
||
| 806 | |||
| 807 | // just in case |
||
| 808 | if ('http://' === $department_url) { |
||
| 809 | $department_url = ''; |
||
| 810 | } |
||
| 811 | |||
| 812 | $userId = empty($params['user_id']) ? api_get_user_id() : (int) $params['user_id']; |
||
| 813 | $user = api_get_user_entity($userId); |
||
| 814 | if (null === $user) { |
||
| 815 | error_log(sprintf('user_id "%s" is invalid', $userId)); |
||
| 816 | |||
| 817 | return null; |
||
| 818 | } |
||
| 819 | |||
| 820 | $course = null; |
||
| 821 | if ($valid) { |
||
| 822 | $repo = Container::getCourseRepository(); |
||
| 823 | $categoryRepo = Container::getCourseCategoryRepository(); |
||
| 824 | |||
| 825 | $course = new Course(); |
||
| 826 | $course |
||
| 827 | ->setTitle($title) |
||
| 828 | ->setCode($code) |
||
| 829 | ->setCourseLanguage($course_language) |
||
| 830 | ->setDescription(get_lang('Course Description')) |
||
| 831 | ->setVisibility($visibility) |
||
| 832 | ->setShowScore(1) |
||
| 833 | ->setDiskQuota($disk_quota) |
||
| 834 | ->setExpirationDate(new DateTime($expiration_date)) |
||
| 835 | ->setDepartmentName((string) $department_name) |
||
| 836 | ->setDepartmentUrl($department_url) |
||
| 837 | ->setSubscribe($subscribe) |
||
| 838 | ->setSticky(1 === (int) ($params['sticky'] ?? 0)) |
||
| 839 | ->setVideoUrl($params['video_url'] ?? '') |
||
| 840 | ->setUnsubscribe($unsubscribe) |
||
| 841 | ->setVisualCode($visual_code) |
||
| 842 | ->setCreator(api_get_user_entity()) |
||
| 843 | ; |
||
| 844 | |||
| 845 | if (!empty($categories)) { |
||
| 846 | if (!is_array($categories)) { |
||
| 847 | $categories = [$categories]; |
||
| 848 | } |
||
| 849 | |||
| 850 | foreach ($categories as $key) { |
||
| 851 | if (empty($key)) { |
||
| 852 | continue; |
||
| 853 | } |
||
| 854 | |||
| 855 | $category = $categoryRepo->find($key); |
||
| 856 | if (null !== $category) { |
||
| 857 | $course->addCategory($category); |
||
| 858 | } |
||
| 859 | } |
||
| 860 | } |
||
| 861 | |||
| 862 | $sort = api_max_sort_value('0', api_get_user_id()); |
||
| 863 | // Default true |
||
| 864 | $addTeacher = $params['add_user_as_teacher'] ?? true; |
||
| 865 | if ($addTeacher) { |
||
| 866 | $iCourseSort = CourseManager::userCourseSort($userId, $code); |
||
| 867 | $courseRelTutor = (new CourseRelUser()) |
||
| 868 | ->setCourse($course) |
||
| 869 | ->setUser($user) |
||
| 870 | ->setStatus(1) |
||
| 871 | ->setTutor(true) |
||
| 872 | ->setSort($iCourseSort) |
||
| 873 | ->setRelationType(0) |
||
| 874 | ->setUserCourseCat(0) |
||
| 875 | ; |
||
| 876 | $course->addSubscription($courseRelTutor); |
||
| 877 | } |
||
| 878 | |||
| 879 | if (!empty($teachers)) { |
||
| 880 | $sort = $user->getMaxSortValue(); |
||
| 881 | if (!is_array($teachers)) { |
||
| 882 | $teachers = [$teachers]; |
||
| 883 | } |
||
| 884 | foreach ($teachers as $key) { |
||
| 885 | // Just in case. |
||
| 886 | if ($key == $userId) { |
||
| 887 | continue; |
||
| 888 | } |
||
| 889 | if (empty($key)) { |
||
| 890 | continue; |
||
| 891 | } |
||
| 892 | $teacher = api_get_user_entity($key); |
||
| 893 | if (is_null($teacher)) { |
||
| 894 | continue; |
||
| 895 | } |
||
| 896 | $courseRelTeacher = (new CourseRelUser()) |
||
| 897 | ->setCourse($course) |
||
| 898 | ->setUser($teacher) |
||
| 899 | ->setStatus(1) |
||
| 900 | ->setTutor(false) |
||
| 901 | ->setSort($sort + 1) |
||
| 902 | ->setRelationType(0) |
||
| 903 | ->setUserCourseCat(0) |
||
| 904 | ; |
||
| 905 | $course->addSubscription($courseRelTeacher); |
||
| 906 | } |
||
| 907 | } |
||
| 908 | |||
| 909 | $repo->create($course); |
||
| 910 | |||
| 911 | $course_id = $course->getId(); |
||
| 912 | if ($course_id) { |
||
| 913 | // Add event to the system log. |
||
| 914 | Event::addEvent( |
||
| 915 | LOG_COURSE_CREATE, |
||
| 916 | LOG_COURSE_CODE, |
||
| 917 | $code, |
||
| 918 | api_get_utc_datetime(), |
||
| 919 | $userId, |
||
| 920 | $course_id |
||
| 921 | ); |
||
| 922 | |||
| 923 | $send_mail_to_admin = api_get_setting('send_email_to_admin_when_create_course'); |
||
| 924 | |||
| 925 | // @todo Improve code to send to all current portal administrators. |
||
| 926 | if ('true' === $send_mail_to_admin) { |
||
| 927 | $siteName = api_get_setting('siteName'); |
||
| 928 | $recipient_email = api_get_setting('emailAdministrator'); |
||
| 929 | $recipient_name = api_get_person_name( |
||
| 930 | api_get_setting('administratorName'), |
||
| 931 | api_get_setting('administratorSurname') |
||
| 932 | ); |
||
| 933 | $iname = api_get_setting('Institution'); |
||
| 934 | $subject = get_lang('NewCourseCreatedIn').' '.$siteName.' - '.$iname; |
||
| 935 | $message = get_lang('Dear').' '.$recipient_name.",\n\n". |
||
| 936 | get_lang('MessageOfNewCourseToAdmin').' '.$siteName.' - '.$iname."\n"; |
||
| 937 | $message .= get_lang('Course name').' '.$title."\n"; |
||
| 938 | |||
| 939 | if ($course->getCategories()->count() > 0) { |
||
| 940 | foreach ($course->getCategories() as $category) { |
||
| 941 | $message .= get_lang('Category').': '.$category->getCode()."\n"; |
||
| 942 | } |
||
| 943 | } |
||
| 944 | $message .= get_lang('Coach').' '.$tutor_name."\n"; |
||
| 945 | $message .= get_lang('Language').' '.$course_language; |
||
| 946 | |||
| 947 | api_mail_html( |
||
| 948 | $recipient_name, |
||
| 949 | $recipient_email, |
||
| 950 | $subject, |
||
| 951 | $message, |
||
| 952 | $siteName, |
||
| 953 | $recipient_email, |
||
| 954 | null, |
||
| 955 | null, |
||
| 956 | null |
||
| 957 | ); |
||
| 958 | } |
||
| 959 | } |
||
| 960 | } |
||
| 961 | |||
| 962 | return $course; |
||
| 963 | } |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Generate a new id for c_tool table. |
||
| 967 | * |
||
| 968 | * @param int $courseId The course id |
||
| 969 | * |
||
| 970 | * @return int the new id |
||
| 971 | */ |
||
| 972 | public static function generateToolId($courseId) |
||
| 990 | } |
||
| 991 | } |
||
| 992 |