'', 'CourseDb' => '', 'CourseDir' => '']; $limit_numb_try = 100; $keys_are_unique = false; $try_new_fsc_id = $try_new_fsc_db = $try_new_fsc_dir = 0; while (!$keys_are_unique) { $keys_course_id = $prefix_for_all.$unique_prefix.$wanted_code.$final_suffix['CourseId']; $keys_course_repository = $prefix_for_path.$unique_prefix.$wanted_code.$final_suffix['CourseDir']; $keys_are_unique = true; // Check whether they are unique. $query = "SELECT 1 FROM $course_table WHERE code='".$keys_course_id."' LIMIT 0, 1"; $result = Database::query($query); if (Database::num_rows($result)) { $keys_are_unique = false; $try_new_fsc_id++; $final_suffix['CourseId'] = substr(md5(uniqid(rand())), 0, 4); } if (($try_new_fsc_id + $try_new_fsc_db + $try_new_fsc_dir) > $limit_numb_try) { return $keys; } } $keys['currentCourseCode'] = $keys_course_code; $keys['currentCourseId'] = $keys_course_id; $keys['currentCourseRepository'] = $keys_course_repository; return $keys; } /** * Gets an array with all the course tables (deprecated?). * * @return array * * @assert (null) !== null */ public static function get_course_tables() { $tables = []; //$tables[] = 'item_property'; $tables[] = 'tool'; $tables[] = 'tool_intro'; $tables[] = 'group_info'; $tables[] = 'group_category'; $tables[] = 'group_rel_user'; $tables[] = 'group_rel_tutor'; $tables[] = 'userinfo_content'; $tables[] = 'userinfo_def'; $tables[] = 'course_description'; $tables[] = 'calendar_event'; $tables[] = 'calendar_event_repeat'; $tables[] = 'calendar_event_repeat_not'; $tables[] = 'calendar_event_attachment'; $tables[] = 'announcement'; $tables[] = 'announcement_attachment'; //$tables[] = 'resource'; $tables[] = 'student_publication'; $tables[] = 'student_publication_assignment'; $tables[] = 'document'; /*$tables[] = 'forum_category'; $tables[] = 'forum_forum'; $tables[] = 'forum_thread'; $tables[] = 'forum_post'; $tables[] = 'forum_mailcue'; $tables[] = 'forum_attachment'; $tables[] = 'forum_notification'; $tables[] = 'forum_thread_qualify'; $tables[] = 'forum_thread_qualify_log';*/ $tables[] = 'link'; $tables[] = 'link_category'; $tables[] = 'online_connected'; $tables[] = 'online_link'; $tables[] = 'chat_connected'; $tables[] = 'quiz'; $tables[] = 'quiz_rel_question'; $tables[] = 'quiz_question'; $tables[] = 'quiz_answer'; $tables[] = 'quiz_question_option'; $tables[] = 'quiz_question_category'; $tables[] = 'quiz_question_rel_category'; $tables[] = 'dropbox_post'; $tables[] = 'dropbox_file'; $tables[] = 'dropbox_person'; $tables[] = 'dropbox_category'; $tables[] = 'dropbox_feedback'; $tables[] = 'lp'; $tables[] = 'lp_item'; $tables[] = 'lp_view'; $tables[] = 'lp_item_view'; $tables[] = 'lp_iv_interaction'; $tables[] = 'lp_iv_objective'; $tables[] = 'blog'; $tables[] = 'blog_comment'; $tables[] = 'blog_post'; $tables[] = 'blog_rating'; $tables[] = 'blog_rel_user'; $tables[] = 'blog_task'; $tables[] = 'blog_task_rel_user'; $tables[] = 'blog_attachment'; $tables[] = 'permission_group'; $tables[] = 'permission_user'; $tables[] = 'permission_task'; $tables[] = 'role'; $tables[] = 'role_group'; $tables[] = 'role_permissions'; $tables[] = 'role_user'; $tables[] = 'survey'; $tables[] = 'survey_question'; $tables[] = 'survey_question_option'; $tables[] = 'survey_invitation'; $tables[] = 'survey_answer'; $tables[] = 'survey_group'; $tables[] = 'wiki'; $tables[] = 'wiki_conf'; $tables[] = 'wiki_discuss'; $tables[] = 'wiki_mailcue'; $tables[] = 'course_setting'; $tables[] = 'glossary'; $tables[] = 'notebook'; $tables[] = 'attendance'; $tables[] = 'attendance_sheet'; $tables[] = 'attendance_calendar'; $tables[] = 'attendance_result'; $tables[] = 'attendance_sheet_log'; $tables[] = 'thematic'; $tables[] = 'thematic_plan'; $tables[] = 'thematic_advance'; return $tables; } /** * Executed only before create_course_tables(). * * @assert (null) === null */ public static function drop_course_tables() { $list = self::get_course_tables(); foreach ($list as $table) { $sql = "DROP TABLE IF EXISTS ".DB_COURSE_PREFIX.$table; Database::query($sql); } } /** * Sorts pictures by type (used?). * * @param array List of files (sthg like array(0=>array('png'=>1))) * @param string $type * * @return array The received array without files not matching type * @assert (array(),null) === array() */ public static function sort_pictures($files, $type) { $pictures = []; foreach ($files as $value) { if (isset($value[$type]) && '' != $value[$type]) { $pictures[][$type] = $value[$type]; } } return $pictures; } /** * Populates the course with essential settings, group categories, example content, and installs course plugins. * * This method initializes a new course by setting up various course settings, creating a default group category, * inserting example content if required, and installing course-specific plugins. The method can also fill the course * with exemplary content based on the parameter provided or the system setting for example content creation. * * @param Course $course The course entity to be populated. * @param bool|null $fillWithExemplaryContent Determines whether to fill the course with exemplary content. If null, * the system setting for example material course creation is used. * @param int $authorId The ID of the user who is considered the author of the exemplary content. Defaults to the * current user if not specified. * * @return bool Returns true if the course is successfully populated, false otherwise. * * @throws Exception Throws exception on error. */ public static function fillCourse(Course $course, bool $fillWithExemplaryContent = null, int $authorId = 0): bool { $authorId = $authorId ?: api_get_user_id(); self::insertCourseSettings($course); self::createGroupCategory($course); if ($fillWithExemplaryContent ?? api_get_setting('example_material_course_creation') !== 'false') { $gradebook = self::createRootGradebook($course); self::insertExampleContent($course, $authorId, $gradebook); } self::installCoursePlugins($course->getId()); return true; } /** * Inserts default and specified settings for a given course. * * This method takes a Course object as input and applies a predefined * set of settings. These settings include configurations for email alerts, * permissions for users to edit various components like agenda and announcements, * theme settings, and more. It also handles the case where a course needs to * enable certain features by default based on platform-wide settings. * * @param Course $course The course object to which the settings will be applied. * * @return void * @throws Exception */ private static function insertCourseSettings(Course $course): void { $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING); $settingsManager = Container::getCourseSettingsManager(); $settingsManager->setCourse($course); $alert = api_get_setting('email_alert_manager_on_new_quiz'); $defaultEmailExerciseAlert = 0; if ('true' === $alert) { $defaultEmailExerciseAlert = 1; } /* course_setting table (courseinfo tool) */ $settings = [ 'email_alert_manager_on_new_doc' => ['title' => '', 'default' => 0, 'category' => 'work'], 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'], 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'], 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'], 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'], 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'], 'course_theme' => ['default' => '', 'category' => 'theme'], 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'], 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'], 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' => 'registration'], 'allow_user_view_user_list' => ['default' => 1, 'category' => 'user'], 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' => 'thematic_advance'], 'email_alert_students_on_new_homework' => ['default' => 0, 'category' => 'work'], 'enable_lp_auto_launch' => ['default' => 0, 'category' => 'learning_path'], 'enable_exercise_auto_launch' => ['default' => 0, 'category' => 'exercise'], 'enable_document_auto_launch' => ['default' => 0, 'category' => 'document'], 'pdf_export_watermark_text' => ['default' => '', 'category' => 'learning_path'], 'allow_public_certificates' => [ 'default' => 'true' === api_get_setting('allow_public_certificates') ? 1 : '', 'category' => 'certificates', ], 'documents_default_visibility' => ['default' => 'visible', 'category' => 'document'], 'show_course_in_user_language' => ['default' => 2, 'category' => null], 'email_to_teachers_on_new_work_feedback' => ['default' => 1, 'category' => null], ]; $counter = 1; foreach ($settings as $variable => $setting) { $title = $setting['title'] ?? ''; Database::query( "INSERT INTO $TABLESETTING (c_id, title, variable, value, category) VALUES ({$course->getId()}, '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')" ); $counter++; } } /** * Creates a default group category for the specified course. * * This method initializes a new group category with a default title * and associates it with the given course. The default group category * is essential for organizing groups within the course, allowing for * better management and classification of course participants. * * @param Course $course The course object for which the default group category is created. * * @return void */ private static function createGroupCategory(Course $course): void { $groupCategory = new CGroupCategory(); $groupCategory ->setTitle(get_lang('Default groups')) ->setParent($course) ->addCourseLink($course) ; Database::getManager()->persist($groupCategory); Database::getManager()->flush(); } /** * Inserts example content into a given course. * * This method populates the specified course with a predefined set of example * content, including documents, links, announcements, and exercises. This content * serves as a template or starting point for instructors, showcasing the various * types of materials and activities that can be included in a course. The content * is added under the authority of the specified author ID. * * @param Course $course The course object into which the example content will be inserted. * @param int $authorId The ID of the user who will be listed as the author of the inserted content. * @param GradebookCategory|null $gradebook * * @return void * @throws Exception */ private static function insertExampleContent(Course $course, int $authorId, ?GradebookCategory $gradebook): void { $now = api_get_utc_datetime(); $files = [ ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0], ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0], ['path' => '/images/gallery', 'title' => get_lang('Gallery'), 'filetype' => 'folder', 'size' => 0], ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0], ]; $paths = []; $courseInfo = ['real_id' => $course->getId(), 'code' => $course->getCode()]; $counter = 1; foreach ($files as $file) { $doc = self::insertDocument($courseInfo, $counter, $file, $authorId); $paths[$file['path']] = $doc->getIid(); $counter++; } $finder = new Symfony\Component\Finder\Finder(); $defaultPath = api_get_path(SYS_PUBLIC_PATH).'img/document'; $finder->in($defaultPath); /** @var SplFileInfo $file */ foreach ($finder as $file) { $parentName = dirname(str_replace($defaultPath, '', $file->getRealPath())); if ('/' === $parentName || '/certificates' === $parentName) { continue; } $title = $file->getFilename(); $parentId = $paths[$parentName]; if ($file->isDir()) { $realPath = str_replace($defaultPath, '', $file->getRealPath()); $document = DocumentManager::addDocument( $courseInfo, $realPath, 'folder', null, $title, '', null, null, null, null, null, false, null, $parentId, $file->getRealPath() ); $paths[$realPath] = $document->getIid(); } else { $realPath = str_replace($defaultPath, '', $file->getRealPath()); $document = DocumentManager::addDocument( $courseInfo, $realPath, 'file', $file->getSize(), $title, '', null, null, null, null, null, false, null, $parentId, $file->getRealPath() ); if ($document && 'default.html' === $document->getTitle()) { $certificateId = $document->getIid(); } } } $agenda = new Agenda('course'); $agenda->set_course($courseInfo); $agenda->addEvent( $now, $now, 0, get_lang('Course creation'), get_lang('This course was created at this time') ); /* Links tool */ $link = new Link(); $link->setCourse($courseInfo); $links = [ [ 'c_id' => $course->getId(), 'url' => 'http://www.google.com', 'title' => 'Quick and powerful search engine', 'description' => get_lang('Quick and powerful search engine'), 'category_id' => 0, 'on_homepage' => 0, 'target' => '_self', 'session_id' => 0, ], [ 'c_id' => $course->getId(), 'url' => 'http://www.wikipedia.org', 'title' => 'Free online encyclopedia', 'description' => get_lang('Free online encyclopedia'), 'category_id' => 0, 'on_homepage' => 0, 'target' => '_self', 'session_id' => 0, ], ]; foreach ($links as $params) { $link->save($params, false, false); } /* Announcement tool */ AnnouncementManager::add_announcement( $courseInfo, 0, get_lang('This is an announcement example'), get_lang('This is an announcement example. Only trainers are allowed to publish announcements.'), ['everyone' => 'everyone'], null, null, $now ); /* Exercise tool */ $exercise = new Exercise($course->getId()); $exercise->exercise = get_lang('Sample test'); $html = '
![]() |
'.get_lang('Irony').' |