Plugin: Refactoring method to create link in course home #5073

pull/5292/head
Angel Fernando Quiroz Campos 8 months ago
parent eee0ab6672
commit 6afc752d8a
  1. 76
      public/main/inc/lib/plugin.class.php
  2. 2
      public/plugin/positioning/src/Positioning.php
  3. 2
      public/plugin/surveyexportcsv/SurveyExportCsvPlugin.php
  4. 2
      public/plugin/surveyexporttxt/SurveyExportTxtPlugin.php

@ -1,8 +1,12 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CourseBundle\Entity\CTool;
use Doctrine\ORM\Exception\NotSupported;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
/**
* Class Plugin
@ -507,7 +511,7 @@ class Plugin
*
* @return bool|null False on error, null otherwise
*/
public function install_course_fields($courseId, $add_tool_link = true, $iconName = '')
public function install_course_fields($courseId, $add_tool_link = true)
{
$plugin_name = $this->get_name();
$t_course = Database::get_course_table(TABLE_COURSE_SETTING);
@ -584,7 +588,7 @@ class Plugin
}
// Add an icon in the table tool list
$this->createLinkToCourseTool($plugin_name, $courseId, $iconName);
$this->createLinkToCourseTool($plugin_name, $courseId);
}
/**
@ -638,14 +642,14 @@ class Plugin
*
* @param bool $add_tool_link Whether we want to add a plugin link on the course homepage
*/
public function install_course_fields_in_all_courses($add_tool_link = true, $iconName = '')
public function install_course_fields_in_all_courses($add_tool_link = true)
{
// Update existing courses to add plugin settings
$table = Database::get_main_table(TABLE_MAIN_COURSE);
$sql = "SELECT id FROM $table ORDER BY id";
$res = Database::query($sql);
while ($row = Database::fetch_assoc($res)) {
$this->install_course_fields($row['id'], $add_tool_link, $iconName);
$this->install_course_fields($row['id'], $add_tool_link);
}
}
@ -1041,16 +1045,16 @@ class Plugin
}
/**
* Add an link for a course tool.
* Add a link for a course tool.
*
* @param string $name The tool name
* @param int $courseId The course ID
* @param string|null $iconName Optional. Icon file name
* @param string|null $link Optional. Link URL
* @param string $name The tool name
* @param int $courseId The course ID
*
* @return CTool|null
* @throws NotSupported
* @throws ORMException
* @throws OptimisticLockException
*/
protected function createLinkToCourseTool(string $name, int $courseId, string $iconName = null, string $link = null): ?CTool
protected function createLinkToCourseTool(string $name, int $courseId): ?CTool
{
if (!$this->addCourseTool) {
return null;
@ -1059,30 +1063,52 @@ class Plugin
$visibilityPerStatus = $this->getToolIconVisibilityPerUserStatus();
$visibility = $this->isIconVisibleByDefault();
$course = api_get_course_entity($courseId);
$user = api_get_user_entity();
$em = Database::getManager();
/** @var CTool $tool */
$tool = $em->getRepository(CTool::class)->findOneBy([
'title' => $name,
'course' => api_get_course_entity($courseId),
$toolRepo = $em->getRepository(Tool::class);
$cToolRepo = $em->getRepository(CTool::class);
/** @var CTool $cTool */
$cTool = $cToolRepo->findOneBy([
'title' => $name.$visibilityPerStatus,
'course' => $course,
]);
if (!$tool) {
$pluginName = $this->get_name();
if (!$cTool) {
$tool = $toolRepo->findOneBy(['title' => $name]);
$toolEntity = new Tool();
$toolEntity->setTitle($pluginName);
if (!$tool) {
$tool = new Tool();
$tool->setTitle($name);
$tool = new CTool();
$tool->setCourse(api_get_course_entity($courseId))
$em->persist($tool);
$em->flush();
}
$cTool = new CTool();
$cTool
->setTool($tool)
->setTitle($name.$visibilityPerStatus)
->setVisibility($visibility)
->setTool($toolEntity)
$em->persist($tool);
->setParent($course)
->setCreator($user)
->addCourseLink(
$course,
null,
null,
$visibility ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT
)
;
$course->addTool($cTool);
$em->persist($cTool);
$em->flush();
}
return $tool;
return $cTool;
}
}

@ -47,7 +47,7 @@ class Positioning extends Plugin
Database::query($sql);
// Installing course settings
$this->install_course_fields_in_all_courses(true, 'positioning.png');
$this->install_course_fields_in_all_courses(true);
}
public function uninstall()

@ -82,7 +82,7 @@ class SurveyExportCsvPlugin extends Plugin
->getResult();
foreach ($result as $item) {
$this->createLinkToCourseTool($this->get_name().':teacher', $item['id'], 'survey.png');
$this->createLinkToCourseTool($this->get_name().':teacher', $item['id']);
}
}

@ -82,7 +82,7 @@ class SurveyExportTxtPlugin extends Plugin
->getResult();
foreach ($result as $item) {
$this->createLinkToCourseTool($this->get_name().':teacher', $item['id'], 'survey.png');
$this->createLinkToCourseTool($this->get_name().':teacher', $item['id']);
}
}

Loading…
Cancel
Save