From 6612f899749f7de0b9228ff460c6eca4fb022ca5 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 25 Apr 2022 17:22:06 -0500 Subject: [PATCH] Session - Fix gradebook copied from main course when session is created - refs CT#3960 --- main/gradebook/lib/be/category.class.php | 16 ++++++ main/inc/lib/sessionmanager.lib.php | 62 +++++++++++------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/main/gradebook/lib/be/category.class.php b/main/gradebook/lib/be/category.class.php index d8d0fcbdbe..132100c4cb 100755 --- a/main/gradebook/lib/be/category.class.php +++ b/main/gradebook/lib/be/category.class.php @@ -726,6 +726,22 @@ class Category implements GradebookItem Database::query($upd); } + /** + * Update the current parent id. + * + * @param $parentId + * @param $catId + * + * @throws Exception + */ + public function updateParentId($parentId, $catId) + { + $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + $parentId = (int) $parentId; + $upd = 'UPDATE '.$table.' SET parent_id = '.$parentId.' WHERE id = '.$catId; + Database::query($upd); + } + /** * Get the value to Allow skill by subcategory. * diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index 568ef87629..4919d780f0 100755 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -2584,7 +2584,8 @@ class SessionManager // Copy gradebook categories and links (from base course) // to the new course session if ($copyEvaluation) { - $cats = Category::load(null, null, $courseInfo['code']); + // it gets the main categories ordered by parent + $cats = Category::load(null, null, $courseInfo['code'], null, null, null, 'ORDER BY parent_id ASC'); if (!empty($cats)) { $sessionCategory = Category:: load( null, @@ -2596,25 +2597,32 @@ class SessionManager false ); - // @todo remove commented code + $sessionCategoriesId = []; if (empty($sessionCategory)) { - // There is no category for this course+session, so create one - $cat = new Category(); - $sessionName = $session->getName(); - $cat->set_name($courseInfo['code'].' - '.get_lang('Session').' '.$sessionName); - $cat->set_session_id($sessionId); - $cat->set_course_code($courseInfo['code']); - $cat->set_description(null); - //$cat->set_user_id($stud_id); - $cat->set_parent_id(0); - $cat->set_weight(100); - $cat->set_visible(0); - $cat->set_certificate_min_score(75); - $cat->add(); - $sessionGradeBookCategoryId = $cat->get_id(); + // It sets the values from the main categories to be copied + foreach ($cats as $origCat) { + $cat = new Category(); + $sessionName = $session->getName(); + $cat->set_name($origCat->get_name().' - '.get_lang('Session').' '.$sessionName); + $cat->set_session_id($sessionId); + $cat->set_course_code($origCat->get_course_code()); + $cat->set_description($origCat->get_description()); + $cat->set_parent_id($origCat->get_parent_id()); + $cat->set_weight($origCat->get_weight()); + $cat->set_visible(0); + $cat->set_certificate_min_score($origCat->getCertificateMinScore()); + $cat->add(); + $sessionGradeBookCategoryId = $cat->get_id(); + $sessionCategoriesId[$origCat->get_id()] = $sessionGradeBookCategoryId; + + // it updates the new parent id + if ($origCat->get_parent_id() > 0) { + $cat->updateParentId($sessionCategoriesId[$origCat->get_parent_id()], $sessionGradeBookCategoryId); + } + } } else { if (!empty($sessionCategory[0])) { - $sessionGradeBookCategoryId = $sessionCategory[0]->get_id(); + $sessionCategoriesId[0] = $sessionCategory[0]->get_id(); } } @@ -2633,23 +2641,11 @@ class SessionManager 0 ); - //$cat->set_session_id($sessionId); - //$oldCategoryId = $cat->get_id(); - //$newId = $cat->add(); - //$newCategoryIdList[$oldCategoryId] = $newId; - //$parentId = $cat->get_parent_id(); - - /*if (!empty($parentId)) { - $newParentId = $newCategoryIdList[$parentId]; - $cat->set_parent_id($newParentId); - $cat->save(); - }*/ - if (!empty($links)) { /** @var AbstractLink $link */ foreach ($links as $link) { - //$newCategoryId = $newCategoryIdList[$link->get_category_id()]; - $link->set_category_id($sessionGradeBookCategoryId); + $newCategoryId = isset($sessionCategoriesId[$link->getCategory()->get_id()]) ? $sessionCategoriesId[$link->getCategory()->get_id()] : $sessionCategoriesId[0]; + $link->set_category_id($newCategoryId); $link->add(); } } @@ -2664,8 +2660,8 @@ class SessionManager if (!empty($evaluationList)) { /** @var Evaluation $evaluation */ foreach ($evaluationList as $evaluation) { - //$evaluationId = $newCategoryIdList[$evaluation->get_category_id()]; - $evaluation->set_category_id($sessionGradeBookCategoryId); + $newCategoryId = isset($sessionCategoriesId[$evaluation->getCategory()->get_id()]) ? $sessionCategoriesId[$evaluation->getCategory()->get_id()] : $sessionCategoriesId[0]; + $evaluation->set_category_id($newCategoryId); $evaluation->add(); } }