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. 50
      main/inc/lib/sessionmanager.lib.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.
*

@ -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
// It sets the values from the main categories to be copied
foreach ($cats as $origCat) {
$cat = new Category();
$sessionName = $session->getName();
$cat->set_name($courseInfo['code'].' - '.get_lang('Session').' '.$sessionName);
$cat->set_name($origCat->get_name().' - '.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_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(75);
$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();
}
}

Loading…
Cancel
Save