Session - Fix gradebook copied from main course when session is created - refs CT#3960

pull/4261/head
Christian 4 years ago
parent 7ddf395201
commit 6612f89974
  1. 16
      main/gradebook/lib/be/category.class.php
  2. 62
      main/inc/lib/sessionmanager.lib.php

@ -726,6 +726,22 @@ class Category implements GradebookItem
Database::query($upd); 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. * Get the value to Allow skill by subcategory.
* *

@ -2584,7 +2584,8 @@ class SessionManager
// Copy gradebook categories and links (from base course) // Copy gradebook categories and links (from base course)
// to the new course session // to the new course session
if ($copyEvaluation) { 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)) { if (!empty($cats)) {
$sessionCategory = Category:: load( $sessionCategory = Category:: load(
null, null,
@ -2596,25 +2597,32 @@ class SessionManager
false false
); );
// @todo remove commented code $sessionCategoriesId = [];
if (empty($sessionCategory)) { if (empty($sessionCategory)) {
// There is no category for this course+session, so create one // It sets the values from the main categories to be copied
$cat = new Category(); foreach ($cats as $origCat) {
$sessionName = $session->getName(); $cat = new Category();
$cat->set_name($courseInfo['code'].' - '.get_lang('Session').' '.$sessionName); $sessionName = $session->getName();
$cat->set_session_id($sessionId); $cat->set_name($origCat->get_name().' - '.get_lang('Session').' '.$sessionName);
$cat->set_course_code($courseInfo['code']); $cat->set_session_id($sessionId);
$cat->set_description(null); $cat->set_course_code($origCat->get_course_code());
//$cat->set_user_id($stud_id); $cat->set_description($origCat->get_description());
$cat->set_parent_id(0); $cat->set_parent_id($origCat->get_parent_id());
$cat->set_weight(100); $cat->set_weight($origCat->get_weight());
$cat->set_visible(0); $cat->set_visible(0);
$cat->set_certificate_min_score(75); $cat->set_certificate_min_score($origCat->getCertificateMinScore());
$cat->add(); $cat->add();
$sessionGradeBookCategoryId = $cat->get_id(); $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 { } else {
if (!empty($sessionCategory[0])) { if (!empty($sessionCategory[0])) {
$sessionGradeBookCategoryId = $sessionCategory[0]->get_id(); $sessionCategoriesId[0] = $sessionCategory[0]->get_id();
} }
} }
@ -2633,23 +2641,11 @@ class SessionManager
0 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)) { if (!empty($links)) {
/** @var AbstractLink $link */ /** @var AbstractLink $link */
foreach ($links as $link) { foreach ($links as $link) {
//$newCategoryId = $newCategoryIdList[$link->get_category_id()]; $newCategoryId = isset($sessionCategoriesId[$link->getCategory()->get_id()]) ? $sessionCategoriesId[$link->getCategory()->get_id()] : $sessionCategoriesId[0];
$link->set_category_id($sessionGradeBookCategoryId); $link->set_category_id($newCategoryId);
$link->add(); $link->add();
} }
} }
@ -2664,8 +2660,8 @@ class SessionManager
if (!empty($evaluationList)) { if (!empty($evaluationList)) {
/** @var Evaluation $evaluation */ /** @var Evaluation $evaluation */
foreach ($evaluationList as $evaluation) { foreach ($evaluationList as $evaluation) {
//$evaluationId = $newCategoryIdList[$evaluation->get_category_id()]; $newCategoryId = isset($sessionCategoriesId[$evaluation->getCategory()->get_id()]) ? $sessionCategoriesId[$evaluation->getCategory()->get_id()] : $sessionCategoriesId[0];
$evaluation->set_category_id($sessionGradeBookCategoryId); $evaluation->set_category_id($newCategoryId);
$evaluation->add(); $evaluation->add();
} }
} }

Loading…
Cancel
Save