From 6afc752d8a991df37e21bf8c04cb99e797227df4 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Sun, 24 Mar 2024 01:51:57 -0500 Subject: [PATCH] Plugin: Refactoring method to create link in course home #5073 --- public/main/inc/lib/plugin.class.php | 76 +++++++++++++------ public/plugin/positioning/src/Positioning.php | 2 +- .../surveyexportcsv/SurveyExportCsvPlugin.php | 2 +- .../surveyexporttxt/SurveyExportTxtPlugin.php | 2 +- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/public/main/inc/lib/plugin.class.php b/public/main/inc/lib/plugin.class.php index 66b9643edc..57fe9cc654 100644 --- a/public/main/inc/lib/plugin.class.php +++ b/public/main/inc/lib/plugin.class.php @@ -1,8 +1,12 @@ 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; } } diff --git a/public/plugin/positioning/src/Positioning.php b/public/plugin/positioning/src/Positioning.php index 6c05e04f5f..f38ba10f1a 100644 --- a/public/plugin/positioning/src/Positioning.php +++ b/public/plugin/positioning/src/Positioning.php @@ -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() diff --git a/public/plugin/surveyexportcsv/SurveyExportCsvPlugin.php b/public/plugin/surveyexportcsv/SurveyExportCsvPlugin.php index b2d670e564..0a188ccdcd 100644 --- a/public/plugin/surveyexportcsv/SurveyExportCsvPlugin.php +++ b/public/plugin/surveyexportcsv/SurveyExportCsvPlugin.php @@ -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']); } } diff --git a/public/plugin/surveyexporttxt/SurveyExportTxtPlugin.php b/public/plugin/surveyexporttxt/SurveyExportTxtPlugin.php index 40e67291e2..5fd19ce4a7 100644 --- a/public/plugin/surveyexporttxt/SurveyExportTxtPlugin.php +++ b/public/plugin/surveyexporttxt/SurveyExportTxtPlugin.php @@ -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']); } }