Glossary: Use entity to save glossary

Refs advisory GHSA-gw58-89f7-4xgj
pull/6031/head
Angel Fernando Quiroz Campos 8 months ago
parent 8c9caf6ad3
commit 89d2026720
No known key found for this signature in database
GPG Key ID: B284841AE3E562CD
  1. 49
      main/inc/lib/glossary.lib.php
  2. 5
      src/Chamilo/CourseBundle/Entity/CGlossary.php

@ -1,6 +1,7 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CourseBundle\Entity\CGlossary;
use ChamiloSession as Session; use ChamiloSession as Session;
/** /**
@ -131,18 +132,19 @@ class GlossaryManager
* This functions stores the glossary in the database. * This functions stores the glossary in the database.
* *
* @param array $values Array of title + description (name => $title, description => $comment) * @param array $values Array of title + description (name => $title, description => $comment)
* @param bool $showMessage
*
* @return bool|int Term id on success, false on failure
* *
* @return mixed Term id on success, false on failure * @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public static function save_glossary($values, $showMessage = true) public static function save_glossary(array $values, bool $showMessage = true)
{ {
if (!is_array($values) || !isset($values['name'])) { if (!isset($values['name'])) {
return false; return false;
} }
// Database table definition
$table = Database::get_course_table(TABLE_GLOSSARY);
// get the maximum display order of all the glossary items // get the maximum display order of all the glossary items
$max_glossary_item = self::get_max_glossary_item(); $max_glossary_item = self::get_max_glossary_item();
@ -159,30 +161,30 @@ class GlossaryManager
} }
return false; return false;
} else { }
$params = [
'glossary_id' => 0, $glossary = (new CGlossary())
'c_id' => api_get_course_int_id(), ->setGlossaryId(0)
'name' => $values['name'], ->setCId(api_get_course_int_id())
'description' => isset($values['description']) ? $values['description'] : "", ->setName($values['name'])
'display_order' => $max_glossary_item + 1, ->setDescription($values['description'] ?? "")
'session_id' => $session_id, ->setDisplayOrder($max_glossary_item + 1)
]; ->setSessionId($session_id);
$id = Database::insert($table, $params); ;
Database::getManager()->persist($glossary);
if ($id) { Database::getManager()->flush();
$sql = "UPDATE $table SET glossary_id = $id WHERE iid = $id";
Database::query($sql); $glossary->setGlossaryId($glossary->getIid());
Database::getManager()->flush();
//insert into item_property //insert into item_property
api_item_property_update( api_item_property_update(
api_get_course_info(), api_get_course_info(),
TOOL_GLOSSARY, TOOL_GLOSSARY,
$id, $glossary->getIid(),
'GlossaryAdded', 'GlossaryAdded',
api_get_user_id() api_get_user_id()
); );
}
// display the feedback message // display the feedback message
if ($showMessage) { if ($showMessage) {
Display::addFlash( Display::addFlash(
@ -190,8 +192,7 @@ class GlossaryManager
); );
} }
return $id; return $glossary->getIid();
}
} }
/** /**

@ -213,4 +213,9 @@ class CGlossary
{ {
return $this->cId; return $this->cId;
} }
public function getIid(): int
{
return $this->iid;
}
} }

Loading…
Cancel
Save