Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

xapi/src/ToolExperience/Activity/QuizQuestion.php (2 issues)

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

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

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

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39
        );
40
41
        return new Activity(
42
            IRI::fromString($iri),
43
            $this->generateActivityDefinitionFromQuestionType()
44
        );
45
    }
46
47
    /**
48
     * @return \Xabbuh\XApi\Model\Interaction\InteractionDefinition
49
     */
50
    private function generateActivityDefinitionFromQuestionType()
51
    {
52
        $languageIso = api_get_language_isocode();
53
        $courseId = api_get_course_int_id();
54
55
        $questionTitle = strip_tags($this->question->getQuestion());
56
        $questionTitle = trim($questionTitle);
57
        $questionDescription = strip_tags($this->question->getDescription());
58
        $questionDescription = trim($questionDescription);
59
60
        $titleMap = LanguageMap::create([$languageIso => $questionTitle]);
61
        $descriptionMap = $questionDescription ? LanguageMap::create([$languageIso => $questionDescription]) : null;
62
63
        $objAnswer = new Answer($this->question->getId(), $courseId);
0 ignored issues
show
Deprecated Code introduced by
The function Chamilo\CourseBundle\Entity\CQuizQuestion::getId() has been deprecated: Use getIid() ( Ignorable by Annotation )

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

63
        $objAnswer = new Answer(/** @scrutinizer ignore-deprecated */ $this->question->getId(), $courseId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
        $objAnswer->read();
65
66
        $type = IRI::fromString('http://adlnet.gov/expapi/activities/question');
67
68
        switch ($this->question->getType()) {
69
            case MULTIPLE_ANSWER:
70
            case UNIQUE_ANSWER:
71
            case UNIQUE_ANSWER_IMAGE:
72
            case READING_COMPREHENSION:
73
                $choices = [];
74
                $correctResponsesPattern = [];
75
76
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
77
                    $choices[] = new InteractionComponent(
78
                        $objAnswer->iid[$i],
79
                        LanguageMap::create([$languageIso => $objAnswer->selectAnswer($i)])
80
                    );
81
82
                    if ($objAnswer->isCorrect($i)) {
83
                        $correctResponsesPattern[] = $objAnswer->iid[$i];
84
                    }
85
                }
86
87
                return new ChoiceInteractionDefinition(
88
                    $titleMap,
89
                    $descriptionMap,
90
                    $type,
91
                    null,
92
                    null,
93
                    [implode('[,]', $correctResponsesPattern)],
94
                    $choices
95
                );
96
            case DRAGGABLE:
97
                $choices = [];
98
99
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
100
                    if ((int) $objAnswer->correct[$i] > 0) {
101
                        $choices[] = new InteractionComponent(
102
                            $objAnswer->correct[$i],
103
                            LanguageMap::create([$languageIso => $objAnswer->answer[$i]])
104
                        );
105
                    }
106
                }
107
108
                $correctResponsesPattern = array_slice($objAnswer->autoId, 0, $objAnswer->nbrAnswers / 2);
109
110
                return new SequencingInteractionDefinition(
111
                    $titleMap,
112
                    $descriptionMap,
113
                    $type,
114
                    null,
115
                    null,
116
                    [implode('[,]', $correctResponsesPattern)],
117
                    $choices
118
                );
119
            case MATCHING:
120
            case MATCHING_DRAGGABLE:
121
                /** @var array|InteractionComponent[] $source */
122
                $source = [];
123
                /** @var array|InteractionComponent[] $source */
124
                $target = [];
125
                $correctResponsesPattern = [];
126
127
                for ($i = 1; $i <= $objAnswer->nbrAnswers; $i++) {
128
                    $interactionComponent = new InteractionComponent(
129
                        $objAnswer->selectAutoId($i),
130
                        LanguageMap::create([$languageIso => $objAnswer->selectAnswer($i)])
131
                    );
132
133
                    if ((int) $objAnswer->correct[$i] > 0) {
134
                        $source[] = $interactionComponent;
135
136
                        $correctResponsesPattern[] = $objAnswer->selectAutoId($i).'[.]'.$objAnswer->correct[$i];
137
                    } else {
138
                        $target[] = $interactionComponent;
139
                    }
140
                }
141
142
                return new MatchingInteractionDefinition(
143
                    $titleMap,
144
                    $descriptionMap,
145
                    $type,
146
                    null,
147
                    null,
148
                    [implode('[,]', $correctResponsesPattern)],
149
                    $source,
150
                    $target
151
                );
152
            case FREE_ANSWER:
153
                return new LongFillInInteractionDefinition($titleMap, $descriptionMap, $type);
154
            case FILL_IN_BLANKS:
155
            case HOT_SPOT:
156
            case HOT_SPOT_DELINEATION:
157
            case MULTIPLE_ANSWER_COMBINATION:
158
            case UNIQUE_ANSWER_NO_OPTION:
159
            case MULTIPLE_ANSWER_TRUE_FALSE:
160
            case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
161
            case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
162
            case GLOBAL_MULTIPLE_ANSWER:
163
            case CALCULATED_ANSWER:
164
            case ANNOTATION:
165
            case ORAL_EXPRESSION:
166
            default:
167
                return new OtherInteractionDefinition($titleMap, $descriptionMap, $type);
168
        }
169
    }
170
}
171