Passed
Pull Request — master (#5629)
by Angel Fernando Quiroz
10:06 queued 01:32
created

generateActivityDefinitionFromQuestionType()   D

Complexity

Conditions 28
Paths 70

Size

Total Lines 123
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 87
nc 70
nop 0
dl 0
loc 123
c 1
b 0
f 0
cc 28
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\PluginBundle\XApi\ToolExperience\Activity;
8
9
use Answer;
10
use Chamilo\CourseBundle\Entity\CQuizQuestion;
11
use Xabbuh\XApi\Model\Activity;
12
use Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition;
13
use Xabbuh\XApi\Model\Interaction\InteractionComponent;
14
use Xabbuh\XApi\Model\Interaction\InteractionDefinition;
15
use Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition;
16
use Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition;
17
use Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition;
18
use Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition;
19
use Xabbuh\XApi\Model\IRI;
20
use Xabbuh\XApi\Model\LanguageMap;
21
22
/**
23
 * Class QuizQuestion.
24
 */
25
class QuizQuestion extends BaseActivity
26
{
27
    private $question;
28
29
    public function __construct(CQuizQuestion $question)
30
    {
31
        $this->question = $question;
32
    }
33
34
    public function generate(): Activity
35
    {
36
        $iri = $this->generateIri(
37
            WEB_CODE_PATH,
38
            'xapi/quiz/',
39
            ['question' => $this->question->getId()]
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Chamilo\CourseBundle\Entity\CQuizQuestion. Did you maybe mean getIid()? ( Ignorable by Annotation )

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

39
            ['question' => $this->question->/** @scrutinizer ignore-call */ getId()]

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...
40
        );
41
42
        return new Activity(
43
            IRI::fromString($iri),
44
            $this->generateActivityDefinitionFromQuestionType()
45
        );
46
    }
47
48
    /**
49
     * @return InteractionDefinition
50
     */
51
    private function generateActivityDefinitionFromQuestionType()
52
    {
53
        $languageIso = api_get_language_isocode();
54
        $courseId = api_get_course_int_id();
55
56
        $questionTitle = strip_tags($this->question->getQuestion());
57
        $questionTitle = trim($questionTitle);
58
        $questionDescription = strip_tags($this->question->getDescription());
59
        $questionDescription = trim($questionDescription);
60
61
        $titleMap = LanguageMap::create([$languageIso => $questionTitle]);
62
        $descriptionMap = $questionDescription ? LanguageMap::create([$languageIso => $questionDescription]) : null;
63
64
        $objAnswer = new Answer($this->question->getId(), $courseId);
65
        $objAnswer->read();
66
67
        $type = IRI::fromString('http://adlnet.gov/expapi/activities/question');
68
69
        switch ($this->question->getType()) {
70
            case MULTIPLE_ANSWER:
71
            case UNIQUE_ANSWER:
72
            case UNIQUE_ANSWER_IMAGE:
73
            case READING_COMPREHENSION:
74
                $choices = [];
75
                $correctResponsesPattern = [];
76
77
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
78
                    $choices[] = new InteractionComponent(
79
                        $objAnswer->iid[$i],
80
                        LanguageMap::create([$languageIso => $objAnswer->selectAnswer($i)])
81
                    );
82
83
                    if ($objAnswer->isCorrect($i)) {
84
                        $correctResponsesPattern[] = $objAnswer->iid[$i];
85
                    }
86
                }
87
88
                return new ChoiceInteractionDefinition(
89
                    $titleMap,
90
                    $descriptionMap,
91
                    $type,
92
                    null,
93
                    null,
94
                    [implode('[,]', $correctResponsesPattern)],
95
                    $choices
96
                );
97
98
            case DRAGGABLE:
99
                $choices = [];
100
101
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
102
                    if ((int) $objAnswer->correct[$i] > 0) {
103
                        $choices[] = new InteractionComponent(
104
                            $objAnswer->correct[$i],
105
                            LanguageMap::create([$languageIso => $objAnswer->answer[$i]])
106
                        );
107
                    }
108
                }
109
110
                $correctResponsesPattern = \array_slice($objAnswer->autoId, 0, $objAnswer->nbrAnswers / 2);
111
112
                return new SequencingInteractionDefinition(
113
                    $titleMap,
114
                    $descriptionMap,
115
                    $type,
116
                    null,
117
                    null,
118
                    [implode('[,]', $correctResponsesPattern)],
119
                    $choices
120
                );
121
122
            case MATCHING:
123
            case MATCHING_DRAGGABLE:
124
                /** @var array|InteractionComponent[] $source */
125
                $source = [];
126
127
                /** @var array|InteractionComponent[] $source */
128
                $target = [];
129
                $correctResponsesPattern = [];
130
131
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
132
                    $interactionComponent = new InteractionComponent(
133
                        $objAnswer->selectAutoId($i),
134
                        LanguageMap::create([$languageIso => $objAnswer->selectAnswer($i)])
135
                    );
136
137
                    if ((int) $objAnswer->correct[$i] > 0) {
138
                        $source[] = $interactionComponent;
139
140
                        $correctResponsesPattern[] = $objAnswer->selectAutoId($i).'[.]'.$objAnswer->correct[$i];
141
                    } else {
142
                        $target[] = $interactionComponent;
143
                    }
144
                }
145
146
                return new MatchingInteractionDefinition(
147
                    $titleMap,
148
                    $descriptionMap,
149
                    $type,
150
                    null,
151
                    null,
152
                    [implode('[,]', $correctResponsesPattern)],
153
                    $source,
154
                    $target
155
                );
156
157
            case FREE_ANSWER:
158
                return new LongFillInInteractionDefinition($titleMap, $descriptionMap, $type);
159
160
            case FILL_IN_BLANKS:
161
            case HOT_SPOT:
162
            case HOT_SPOT_DELINEATION:
163
            case MULTIPLE_ANSWER_COMBINATION:
164
            case UNIQUE_ANSWER_NO_OPTION:
165
            case MULTIPLE_ANSWER_TRUE_FALSE:
166
            case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
167
            case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
168
            case GLOBAL_MULTIPLE_ANSWER:
169
            case CALCULATED_ANSWER:
170
            case ANNOTATION:
171
            case ORAL_EXPRESSION:
172
            default:
173
                return new OtherInteractionDefinition($titleMap, $descriptionMap, $type);
174
        }
175
    }
176
}
177