Passed
Pull Request — 1.11.x (#4501)
by Angel Fernando Quiroz
10:34
created

PortfolioCommentScored::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
6
7
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
8
use Chamilo\CoreBundle\Entity\PortfolioComment;
9
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioComment as PortfolioCommentActivity;
10
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User;
11
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Scored;
12
use Xabbuh\XApi\Model\Result;
13
use Xabbuh\XApi\Model\Score;
14
use Xabbuh\XApi\Model\Statement;
15
16
class PortfolioCommentScored extends BaseStatement
17
{
18
    use PortfolioAttachmentsTrait;
19
20
    /** @var PortfolioComment */
21
    private $comment;
22
23
    public function __construct(PortfolioComment $comment)
24
    {
25
        $this->comment = $comment;
26
    }
27
28
    public function generate(): Statement
29
    {
30
        $user = api_get_user_entity(api_get_user_id());
31
32
        $commentAttachments = \Database::getManager()
33
            ->getRepository(PortfolioAttachment::class)
34
            ->findFromComment($this->comment)
35
        ;
36
37
        $maxScore = (float) api_get_course_setting('portfolio_max_score');
38
        $rawScore = $this->comment->getScore();
39
        $scaled = $maxScore ? ($rawScore / $maxScore) : 0;
40
41
        $actor = new User($user);
42
        $verb = new Scored();
43
        $object = new PortfolioCommentActivity($this->comment);
44
        $context = $this->generateContext();
45
        $attachments = $this->generateAttachments($commentAttachments, $this->comment->getAuthor());
46
        $score = new Score($scaled, $rawScore, 0, $maxScore);
47
        $result = new Result($score);
48
49
        return new Statement(
50
            $this->generateStatementId('portfolio-comment'),
51
            $actor->generate(),
52
            $verb->generate(),
53
            $object->generate(),
54
            $result,
55
            null,
56
            api_get_utc_datetime(null, false, true),
57
            null,
58
            $context,
59
            $attachments
60
        );
61
    }
62
}
63