@ -4,6 +4,8 @@
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\GradebookCategory;
use Chamilo\CoreBundle\Entity\GradebookLink;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CGroupCategory;
use Chamilo\CourseBundle\Entity\CGroupCategory;
use Chamilo\CourseBundle\Entity\CToolIntro;
use Chamilo\CourseBundle\Entity\CToolIntro;
@ -224,33 +226,57 @@ class AddCourse
}
}
/**
/**
* Fills the course database with some required content and example content .
* Populates the course with essential settings, group categories, example content, and installs course plugins .
*
*
* @param bool Whether to fill the course with example content
* This method initializes a new course by setting up various course settings, creating a default group category,
* @param int $authorId
* 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 False on error, true otherwise
* @return bool Returns true if the course is successfully populated, false otherwise.
*
*
* @version 1.2
* @throws Exception Throws exception on error.
* @assert (null, '', '', null) === false
* @assert (1, 'ABC', null, null) === false
* @assert (1, 'TEST', 'spanish', true) === true
*/
*/
public static function fillCourse(
public static function fillCourse(Course $course, bool $fillWithExemplaryContent = null, int $authorId = 0): bool
Course $course,
{
$fill_with_exemplary_content = null,
$entityManager = Database::getManager();
$authorId = 0
$authorId = $authorId ?: api_get_user_id();
) {
if (is_null($fill_with_exemplary_content)) {
self::insertCourseSettings($course);
$fill_with_exemplary_content = 'false' !== api_get_setting('example_material_course_creation');
self::createGroupCategory($course);
if ($fillWithExemplaryContent ?? api_get_setting('example_material_course_creation') !== 'false') {
self::insertExampleContent($course, $authorId, $entityManager);
}
}
$course_id = $course->getId();
self::installCoursePlugins($course->getId());
$authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
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);
$TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING);
$TABLEGRADEBOOK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
$TABLEGRADEBOOKLINK = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
$settingsManager = Container::getCourseSettingsManager();
$settingsManager = Container::getCourseSettingsManager();
$settingsManager->setCourse($course);
$settingsManager->setCourse($course);
@ -294,13 +320,26 @@ class AddCourse
$title = $setting['title'] ?? '';
$title = $setting['title'] ?? '';
Database::query(
Database::query(
"INSERT INTO $TABLESETTING (c_id, title, variable, value, category)
"INSERT INTO $TABLESETTING (c_id, title, variable, value, category)
VALUES ($course_id , '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')"
VALUES ({$course->getId()} , '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')"
);
);
$counter++;
$counter++;
}
}
}
/* Course homepage tools for platform admin only */
/**
/* Group tool */
* 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 = new CGroupCategory();
$groupCategory
$groupCategory
->setTitle(get_lang('Default groups'))
->setTitle(get_lang('Default groups'))
@ -309,38 +348,35 @@ class AddCourse
;
;
Database::getManager()->persist($groupCategory);
Database::getManager()->persist($groupCategory);
Database::getManager()->flush();
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.
*
* @return void
* @throws Exception
*/
private static function insertExampleContent(Course $course, int $authorId): void
{
$now = api_get_utc_datetime();
$now = api_get_utc_datetime();
/*$files = [
['path' => '/shared_folder', 'title' => get_lang('Folders of users'), 'filetype' => 'folder', 'size' => 0],
[
'path' => '/chat_files',
'title' => get_lang('Chat conversations history'),
'filetype' => 'folder',
'size' => 0,
],
['path' => '/certificates', 'title' => get_lang('Certificates'), 'filetype' => 'folder', 'size' => 0],
];
$counter = 1;
foreach ($files as $file) {
self::insertDocument($courseInfo, $counter, $file, $authorId);
$counter++;
}*/
$certificateId = 'NULL';
/* Documents */
if ($fill_with_exemplary_content) {
$files = [
$files = [
['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0],
['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0],
//['path' => '/flash', 'title' => get_lang('Flash'), 'filetype' => 'folder', 'size' => 0],
['path' => '/images', 'title' => get_lang('Images'), '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' => '/images/gallery', 'title' => get_lang('Gallery'), 'filetype' => 'folder', 'size' => 0],
['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0],
['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0],
//['path' => '/video/flv', 'title' => 'flv', 'filetype' => 'folder', 'size' => 0],
];
];
$paths = [];
$paths = [];
$courseInfo = ['real_id' => $course->getId()];
$courseInfo = ['real_id' => $course->getId(), 'code' => $course->getCode()];
$counter = 1;
foreach ($files as $file) {
foreach ($files as $file) {
$doc = self::insertDocument($courseInfo, $counter, $file, $authorId);
$doc = self::insertDocument($courseInfo, $counter, $file, $authorId);
$paths[$file['path']] = $doc->getIid();
$paths[$file['path']] = $doc->getIid();
@ -422,7 +458,7 @@ class AddCourse
$link->setCourse($courseInfo);
$link->setCourse($courseInfo);
$links = [
$links = [
[
[
'c_id' => $course_id ,
'c_id' => $course->getId() ,
'url' => 'http://www.google.com',
'url' => 'http://www.google.com',
'title' => 'Quick and powerful search engine',
'title' => 'Quick and powerful search engine',
'description' => get_lang('Quick and powerful search engine'),
'description' => get_lang('Quick and powerful search engine'),
@ -432,7 +468,7 @@ class AddCourse
'session_id' => 0,
'session_id' => 0,
],
],
[
[
'c_id' => $course_id ,
'c_id' => $course->getId() ,
'url' => 'http://www.wikipedia.org',
'url' => 'http://www.wikipedia.org',
'title' => 'Free online encyclopedia',
'title' => 'Free online encyclopedia',
'description' => get_lang('Free online encyclopedia'),
'description' => get_lang('Free online encyclopedia'),
@ -459,39 +495,8 @@ class AddCourse
$now
$now
);
);
$manager = Database::getManager();
/* Introduction text */
$intro_text = '< p style = "text-align: center;" >
< img src = "'.api_get_path(REL_CODE_PATH).'img/mascot.png" alt = "Mr. Chamilo" title = "Mr. Chamilo" / >
< h2 > '.get_lang('Introduction text').'< / h2 >
< / p > ';
$toolIntro = (new CToolIntro())
->setIntroText($intro_text)
->addCourseLink($course)
;
$manager->persist($toolIntro);
$toolIntro = (new CToolIntro())
->setIntroText(get_lang('This page allows users and groups to publish documents.'))
->addCourseLink($course)
;
$manager->persist($toolIntro);
$toolIntro = (new CToolIntro())
->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.'
)
)
->addCourseLink($course)
;
$manager->persist($toolIntro);
$manager->flush();
/* Exercise tool */
/* Exercise tool */
$exercise = new Exercise($course_id );
$exercise = new Exercise($course->getId());
$exercise->exercise = get_lang('Sample test');
$exercise->exercise = get_lang('Sample test');
$html = '< table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" >
$html = '< table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" >
< tr >
< tr >
@ -507,8 +512,6 @@ class AddCourse
$exercise->description = $html;
$exercise->description = $html;
$exercise->save();
$exercise->save();
$exercise_id = $exercise->id;
$question = new MultipleAnswer();
$question = new MultipleAnswer();
$question->course = $courseInfo;
$question->course = $courseInfo;
$question->question = get_lang('Socratic irony is...');
$question->question = get_lang('Socratic irony is...');
@ -557,30 +560,90 @@ class AddCourse
saveThread($forumEntity, $params, $courseInfo, false);
saveThread($forumEntity, $params, $courseInfo, false);
self::createGradebookStructure($course, $exercise->id);
}
/**
* Creates the gradebook structure for a course.
*
* This method sets up the initial gradebook categories and links for a new course.
* It creates a parent gradebook category representing the course itself and a child
* gradebook category for course activities. It then creates a gradebook link associated
* with a specific course activity, identified by the $refId parameter.
*
* @param Course $course The course entity for which the gradebook structure will be created.
* @param int $refId The reference ID of the course activity to link in the gradebook.
*
* @return void
*/
private static function createGradebookStructure(Course $course, int $refId): void
{
$manager = Database::getManager();
/* Gradebook tool */
/* Gradebook tool */
$course_code = $courseInfo['code'];
$courseCode = $course->getCode();
// father gradebook
Database::query(
$parentGradebookCategory = new GradebookCategory();
"INSERT INTO $TABLEGRADEBOOK (title, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id)
$parentGradebookCategory->setTitle($courseCode);
VALUES ('$course_code','0',0,'',1,$course_id,0,100,0,75,NULL,$certificateId)"
$parentGradebookCategory->setLocked(0);
);
$parentGradebookCategory->setGenerateCertificates(false);
$gbid = Database::insert_id();
$parentGradebookCategory->setDescription('');
Database::query(
$parentGradebookCategory->setCourse($course);
"INSERT INTO $TABLEGRADEBOOK (title, locked, generate_certificates, description, user_id, c_id, parent_id, weight, visible, certif_min_score, session_id, document_id)
$parentGradebookCategory->setWeight(100);
VALUES ('$course_code','0',0,'',1,$course_id,$gbid,100,1,75,NULL,$certificateId)"
$parentGradebookCategory->setVisible(false);
);
$parentGradebookCategory->setCertifMinScore(75);
$gbid = Database:: insert_id();
$parentGradebookCategory->setUser(api_get_user_entity());
Database::query(
"INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, c_id, category_id, created_at, weight, visible, locked)
$manager->persist($parentGradebookCategory);
VALUES (1,$exercise_id,1,$course_id,$gbid,'$now',100,1,0)"
$manager->flush();
);
$childGradebookCategory = new GradebookCategory();
$childGradebookCategory->setTitle($courseCode);
$childGradebookCategory->setLocked(0);
$childGradebookCategory->setGenerateCertificates(false);
$childGradebookCategory->setDescription('');
$childGradebookCategory->setCourse($course);
$childGradebookCategory->setWeight(100);
$childGradebookCategory->setVisible(true);
$childGradebookCategory->setCertifMinScore(75);
$childGradebookCategory->setParent($parentGradebookCategory);
$childGradebookCategory->setUser(api_get_user_entity());
$manager->persist($childGradebookCategory);
$manager->flush();
$gradebookLink = new GradebookLink();
$gradebookLink->setType(1);
$gradebookLink->setRefId($refId);
$gradebookLink->setUser(api_get_user_entity());
$gradebookLink->setCourse($course);
$gradebookLink->setCategory($childGradebookCategory);
$gradebookLink->setCreatedAt(new \DateTime());
$gradebookLink->setWeight(100);
$gradebookLink->setVisible(1);
$gradebookLink->setLocked(0);
$manager->persist($gradebookLink);
$manager->flush();
}
}
// Installing plugins in course
/**
* Installs plugins for a given course.
*
* This method takes a course ID and uses the AppPlugin service to install
* all necessary or default plugins for that specific course. These plugins
* can enhance the functionality of the course by adding new features or
* tools that are not part of the core Chamilo platform.
*
* @param int $courseId The ID of the course for which the plugins will be installed.
*
* @return void
*/
private static function installCoursePlugins(int $courseId): void
{
$app_plugin = new AppPlugin();
$app_plugin = new AppPlugin();
$app_plugin->install_course_plugins($course_id);
$app_plugin->install_course_plugins($courseId);
return true;
}
}
/**
/**