From 589d8acbc6230214fd1114f9459d509967eb2d5a Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Mon, 25 Mar 2024 20:12:02 +0100 Subject: [PATCH] Gradebook: Create root course gradebook upon course creation - refs #5278 --- public/main/inc/lib/add_course.lib.inc.php | 59 +++++++++++++++------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/public/main/inc/lib/add_course.lib.inc.php b/public/main/inc/lib/add_course.lib.inc.php index 97efab95f1..fd6823ff92 100644 --- a/public/main/inc/lib/add_course.lib.inc.php +++ b/public/main/inc/lib/add_course.lib.inc.php @@ -249,6 +249,7 @@ class AddCourse self::insertCourseSettings($course); self::createGroupCategory($course); + $gradebook = self::createRootGradebook($course); if ($fillWithExemplaryContent ?? api_get_setting('example_material_course_creation') !== 'false') { self::insertExampleContent($course, $authorId, $entityManager); @@ -361,11 +362,12 @@ class AddCourse * * @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 $gradebook * * @return void * @throws Exception */ - private static function insertExampleContent(Course $course, int $authorId): void + private static function insertExampleContent(Course $course, int $authorId, GradebookCategory $gradebook): void { $now = api_get_utc_datetime(); $files = [ @@ -560,7 +562,7 @@ class AddCourse saveThread($forumEntity, $params, $courseInfo, false); - self::createGradebookStructure($course, $exercise->id); + self::createExampleGradebookContent($course, $gradebook, $exercise->id); } /** @@ -576,27 +578,13 @@ class AddCourse * * @return void */ - private static function createGradebookStructure(Course $course, int $refId): void + private static function createExampleGradebookContent(Course $course, GradebookCategory $parentCategory, int $refId): void { $manager = Database::getManager(); /* Gradebook tool */ $courseCode = $course->getCode(); - $parentGradebookCategory = new GradebookCategory(); - $parentGradebookCategory->setTitle($courseCode); - $parentGradebookCategory->setLocked(0); - $parentGradebookCategory->setGenerateCertificates(false); - $parentGradebookCategory->setDescription(''); - $parentGradebookCategory->setCourse($course); - $parentGradebookCategory->setWeight(100); - $parentGradebookCategory->setVisible(false); - $parentGradebookCategory->setCertifMinScore(75); - $parentGradebookCategory->setUser(api_get_user_entity()); - - $manager->persist($parentGradebookCategory); - $manager->flush(); - $childGradebookCategory = new GradebookCategory(); $childGradebookCategory->setTitle($courseCode); $childGradebookCategory->setLocked(0); @@ -606,7 +594,7 @@ class AddCourse $childGradebookCategory->setWeight(100); $childGradebookCategory->setVisible(true); $childGradebookCategory->setCertifMinScore(75); - $childGradebookCategory->setParent($parentGradebookCategory); + $childGradebookCategory->setParent($parentCategory); $childGradebookCategory->setUser(api_get_user_entity()); $manager->persist($childGradebookCategory); @@ -628,6 +616,41 @@ class AddCourse $manager->flush(); } + /** + * Creates the basic 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. + * + * @param Course $course The course entity for which the gradebook structure will be created. + * + * @return GradebookCategory The created gradebook's ID + * @throws \Doctrine\ORM\Exception\ORMException + */ + private static function createRootGradebook(Course $course): GradebookCategory + { + $manager = Database::getManager(); + + /* Gradebook tool */ + $courseCode = $course->getCode(); + + $parentGradebookCategory = new GradebookCategory(); + $parentGradebookCategory->setTitle($courseCode); + $parentGradebookCategory->setLocked(0); + $parentGradebookCategory->setGenerateCertificates(false); + $parentGradebookCategory->setDescription(''); + $parentGradebookCategory->setCourse($course); + $parentGradebookCategory->setWeight(100); + $parentGradebookCategory->setVisible(false); + $parentGradebookCategory->setCertifMinScore(75); + $parentGradebookCategory->setUser(api_get_user_entity()); + + $manager->persist($parentGradebookCategory); + $manager->flush(); + + return $parentGradebookCategory; + } + /** * Installs plugins for a given course. *