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

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

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

@ -82,7 +82,7 @@ class SurveyExportCsvPlugin extends Plugin
->getResult(); ->getResult();
foreach ($result as $item) { 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(); ->getResult();
foreach ($result as $item) { 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