diff --git a/main/inc/lib/PortfolioController.php b/main/inc/lib/PortfolioController.php index c72fe04913..41c0b08d87 100644 --- a/main/inc/lib/PortfolioController.php +++ b/main/inc/lib/PortfolioController.php @@ -2407,6 +2407,11 @@ class PortfolioController $em->persist($item); $em->flush(); + HookPortfolioItemScored::create() + ->setEventData(['item' => $item]) + ->notifyItemScored() + ; + Display::addFlash( Display::return_message(get_lang('PortfolioItemGraded'), 'success') ); @@ -2476,6 +2481,11 @@ class PortfolioController $em->persist($comment); $em->flush(); + HookPortfolioCommentScored::create() + ->setEventData(['comment' => $comment]) + ->notifyCommentScored() + ; + Display::addFlash( Display::return_message(get_lang('PortfolioCommentGraded'), 'success') ); diff --git a/main/inc/lib/hook/HookPortfolioCommentScored.php b/main/inc/lib/hook/HookPortfolioCommentScored.php new file mode 100644 index 0000000000..d073ffe239 --- /dev/null +++ b/main/inc/lib/hook/HookPortfolioCommentScored.php @@ -0,0 +1,19 @@ +observers as $observer) { + $observer->hookCommentScored($this); + } + } +} diff --git a/main/inc/lib/hook/HookPortfolioItemScored.php b/main/inc/lib/hook/HookPortfolioItemScored.php new file mode 100644 index 0000000000..b333fe886a --- /dev/null +++ b/main/inc/lib/hook/HookPortfolioItemScored.php @@ -0,0 +1,19 @@ +observers as $observer) { + $observer->hookItemScored($this); + } + } +} diff --git a/main/inc/lib/hook/interfaces/HookPortfolioCommentScoredEventInterface.php b/main/inc/lib/hook/interfaces/HookPortfolioCommentScoredEventInterface.php new file mode 100644 index 0000000000..544f5d03cf --- /dev/null +++ b/main/inc/lib/hook/interfaces/HookPortfolioCommentScoredEventInterface.php @@ -0,0 +1,8 @@ +getEventData()['comment']; + + $statement = (new PortfolioCommentScored($comment))->generate(); + + $this->saveSharedStatement($statement); + } +} diff --git a/plugin/xapi/src/Hook/XApiPortfolioItemScoredHookObserver.php b/plugin/xapi/src/Hook/XApiPortfolioItemScoredHookObserver.php new file mode 100644 index 0000000000..bed2dfb942 --- /dev/null +++ b/plugin/xapi/src/Hook/XApiPortfolioItemScoredHookObserver.php @@ -0,0 +1,19 @@ +getEventData()['item']; + + $statement = (new PortfolioItemScored($item))->generate(); + + $this->saveSharedStatement($statement); + } +} diff --git a/plugin/xapi/src/ToolExperience/Statement/PortfolioCommentScored.php b/plugin/xapi/src/ToolExperience/Statement/PortfolioCommentScored.php new file mode 100644 index 0000000000..b0907ee48b --- /dev/null +++ b/plugin/xapi/src/ToolExperience/Statement/PortfolioCommentScored.php @@ -0,0 +1,62 @@ +comment = $comment; + } + + public function generate(): Statement + { + $user = api_get_user_entity(api_get_user_id()); + + $commentAttachments = \Database::getManager() + ->getRepository(PortfolioAttachment::class) + ->findFromComment($this->comment) + ; + + $maxScore = (float) api_get_course_setting('portfolio_max_score'); + $rawScore = $this->comment->getScore(); + $scaled = $maxScore ? ($rawScore / $maxScore) : 0; + + $actor = new User($user); + $verb = new Scored(); + $object = new PortfolioCommentActivity($this->comment); + $context = $this->generateContext(); + $attachments = $this->generateAttachments($commentAttachments, $this->comment->getAuthor()); + $score = new Score($scaled, $rawScore, 0, $maxScore); + $result = new Result($score); + + return new Statement( + $this->generateStatementId('portfolio-comment'), + $actor->generate(), + $verb->generate(), + $object->generate(), + $result, + null, + api_get_utc_datetime(null, false, true), + null, + $context, + $attachments + ); + } +} diff --git a/plugin/xapi/src/ToolExperience/Statement/PortfolioItemScored.php b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemScored.php new file mode 100644 index 0000000000..71c98b83ce --- /dev/null +++ b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemScored.php @@ -0,0 +1,53 @@ +getRepository(PortfolioAttachment::class) + ->findFromItem($this->item) + ; + + $maxScore = (float) api_get_course_setting('portfolio_max_score'); + $rawScore = $this->item->getScore(); + $scaled = $maxScore ? ($rawScore / $maxScore) : 0; + + $actor = new User($user); + $verb = new Scored(); + $object = new PortfolioItemActivity($this->item); + $context = $this->generateContext(); + $attachments = $this->generateAttachments($itemAttachments, $this->item->getUser()); + $score = new Score($scaled, $rawScore, 0, $maxScore); + $result = new Result($score); + + return new Statement( + $this->generateStatementId('portfolio-item'), + $actor->generate(), + $verb->generate(), + $object->generate(), + $result, + null, + api_get_utc_datetime(null, false, true), + null, + $context, + $attachments + ); + } +} diff --git a/plugin/xapi/src/ToolExperience/Verb/Scored.php b/plugin/xapi/src/ToolExperience/Verb/Scored.php new file mode 100644 index 0000000000..8206999dc0 --- /dev/null +++ b/plugin/xapi/src/ToolExperience/Verb/Scored.php @@ -0,0 +1,16 @@ +detach($learningPathItemViewedHook); HookLearningPathEnd::create()->detach($learningPathEndHook); @@ -160,6 +162,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface HookPortfolioItemCommented::create()->detach($portfolioItemCommentedHook); HookPortfolioItemHighlighted::create()->detach($portfolioItemHighlightedHook); HookPortfolioDownloaded::create()->detach($portfolioDownloaded); + HookPortfolioItemScored::create()->detach($portfolioItemScoredHook); + HookPortfolioCommentScored::create()->detach($portfolioCommentedScoredHook); return 1; } @@ -247,6 +251,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface $portfolioItemViewedHook = XApiPortfolioItemViewedHookObserver::create(); $portfolioItemHighlightedHook = XApiPortfolioItemHighlightedHookObserver::create(); $portfolioDownloadedHook = XApiPortfolioDownloadedHookObserver::create(); + $portfolioItemScoredHook = XApiPortfolioItemScoredHookObserver::create(); + $portfolioCommentScoredHook = XApiPortfolioCommentScoredHookObserver::create(); $learningPathItemViewedEvent = HookLearningPathItemViewed::create(); $learningPathEndEvent = HookLearningPathEnd::create(); @@ -257,6 +263,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface $portfolioItemViewedEvent = HookPortfolioItemViewed::create(); $portfolioItemHighlightedEvent = HookPortfolioItemHighlighted::create(); $portfolioDownloadedEvent = HookPortfolioDownloaded::create(); + $portfolioItemScoredEvent = HookPortfolioItemScored::create(); + $portfolioCommentScoredEvent = HookPortfolioCommentScored::create(); if ('true' === $this->get(self::SETTING_LRS_LP_ITEM_ACTIVE)) { $learningPathItemViewedEvent->attach($learningPathItemViewedHook); @@ -288,12 +296,16 @@ class XApiPlugin extends Plugin implements HookPluginInterface $portfolioItemViewedEvent->attach($portfolioItemViewedHook); $portfolioItemHighlightedEvent->attach($portfolioItemHighlightedHook); $portfolioDownloadedEvent->attach($portfolioDownloadedHook); + $portfolioItemScoredEvent->attach($portfolioItemScoredHook); + $portfolioCommentScoredEvent->attach($portfolioCommentScoredHook); } else { $portfolioItemAddedEvent->detach($portfolioItemAddedHook); $portfolioItemCommentedEvent->detach($portfolioItemCommentedHook); $portfolioItemViewedEvent->detach($portfolioItemViewedHook); $portfolioItemHighlightedEvent->detach($portfolioItemHighlightedHook); $portfolioDownloadedEvent->detach($portfolioDownloadedHook); + $portfolioItemScoredEvent->detach($portfolioItemScoredHook); + $portfolioCommentScoredEvent->detach($portfolioCommentScoredHook); } return $this;