Passed
Push — master ( 95b92f...37aede )
by Yannick
10:09 queued 01:17
created

QuizQuestionAnswered   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 57
c 1
b 0
f 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 33 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
8
9
use Chamilo\CoreBundle\Entity\TrackEAttempt;
10
use Chamilo\CourseBundle\Entity\CQuiz;
11
use Chamilo\CourseBundle\Entity\CQuizQuestion;
12
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\Quiz as QuizActivity;
13
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\QuizQuestion as QuizQuestionActivity;
14
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User as UserActor;
15
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Answered as AnsweredVerb;
16
use Xabbuh\XApi\Model\Result;
17
use Xabbuh\XApi\Model\Score;
18
use Xabbuh\XApi\Model\Statement;
19
20
/**
21
 * Class QuizQuestionAnswered.
22
 */
23
class QuizQuestionAnswered extends BaseStatement
24
{
25
    /**
26
     * @var TrackEAttempt
27
     */
28
    private $attempt;
29
30
    /**
31
     * @var CQuizQuestion
32
     */
33
    private $question;
34
35
    /**
36
     * @var CQuiz
37
     */
38
    private $quiz;
39
40
    public function __construct(TrackEAttempt $attempt, CQuizQuestion $question, CQuiz $quiz)
41
    {
42
        $this->attempt = $attempt;
43
        $this->question = $question;
44
        $this->quiz = $quiz;
45
    }
46
47
    public function generate(): Statement
48
    {
49
        $user = api_get_user_entity($this->attempt->getUserId());
0 ignored issues
show
Bug introduced by
The method getUserId() does not exist on Chamilo\CoreBundle\Entity\TrackEAttempt. Did you maybe mean getUser()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $user = api_get_user_entity($this->attempt->/** @scrutinizer ignore-call */ getUserId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
51
        $userActor = new UserActor($user);
52
        $answeredVerb = new AnsweredVerb();
53
        $questionActivity = new QuizQuestionActivity($this->question);
54
        $quizActivity = new QuizActivity($this->quiz);
55
56
        $rawResult = $this->attempt->getMarks();
57
        $maxResult = $this->question->getPonderation();
58
        $scaledResult = $maxResult ? ($rawResult / $maxResult) : 0;
59
60
        $context = $this->generateContext();
61
        $contextActivities = $context
62
            ->getContextActivities()
63
            ->withAddedGroupingActivity($quizActivity->generate())
64
        ;
65
66
        return new Statement(
67
            $this->generateStatementId('exercise-question'),
68
            $userActor->generate(),
69
            $answeredVerb->generate(),
70
            $questionActivity->generate(),
71
            new Result(
72
                new Score($scaledResult, $rawResult, null, $maxResult),
73
                $rawResult > 0,
74
                true
75
            ),
76
            null,
77
            $this->attempt->getTms(),
78
            null,
79
            $context->withContextActivities($contextActivities)
80
        );
81
    }
82
}
83