Skip to content

Commit

Permalink
CourseRestorer: Add option to reuse lp category - refs BT#17000
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Apr 17, 2020
1 parent 45a6e5d commit f3f77e2
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php
Expand Up @@ -1418,8 +1418,8 @@ public function restore_link_category($id, $session_id = 0)
$new_id = Database::insert($link_cat_table, $params);

if ($new_id) {
$sql = "UPDATE $link_cat_table
SET id = iid
$sql = "UPDATE $link_cat_table
SET id = iid
WHERE iid = $new_id";
Database::query($sql);

Expand Down Expand Up @@ -2604,9 +2604,22 @@ public function restore_survey_question($id, $survey_id)
/**
* @param int $sessionId
* @param bool $baseContent
*
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function restore_learnpath_category($sessionId = 0, $baseContent = false)
{
$reuseExisting = false;

if (isset($this->tool_copy_settings['learnpath_category']) &&
isset($this->tool_copy_settings['learnpath_category']['reuse_existing']) &&
true === $this->tool_copy_settings['learnpath_category']['reuse_existing']
) {
$reuseExisting = true;
}

$tblLpCategory = Database::get_course_table(TABLE_LP_CATEGORY);

if ($this->course->has_resources(RESOURCE_LEARNPATH_CATEGORY)) {
$resources = $this->course->resources;
/** @var LearnPathCategory $item */
Expand All @@ -2615,11 +2628,29 @@ public function restore_learnpath_category($sessionId = 0, $baseContent = false)
$lpCategory = $item->object;

if ($lpCategory) {
$values = [
'c_id' => $this->destination_course_id,
'name' => $lpCategory->getName(),
];
$categoryId = \learnpath::createCategory($values);
$categoryId = 0;

$existingLpCategory = Database::select(
'iid',
$tblLpCategory,
[
'WHERE' => [
'c_id = ? AND name = ?' => [$this->destination_course_id, $lpCategory->getName()]
]
],
'first'
);

if ($reuseExisting && !empty($existingLpCategory)) {
$categoryId = $existingLpCategory['iid'];
} else {
$values = [
'c_id' => $this->destination_course_id,
'name' => $lpCategory->getName(),
];
$categoryId = \learnpath::createCategory($values);
}

if ($categoryId) {
$this->course->resources[RESOURCE_LEARNPATH_CATEGORY][$id]->destination_id = $categoryId;
}
Expand Down

0 comments on commit f3f77e2

Please sign in to comment.