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

lib/assesment/CcAssesmentQuestionMultichoice.php (1 issue)

1
<?php
2
/* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/cc_lib/cc_asssesment.php under GNU/GPL license */
3
4
class CcAssesmentQuestionMultichoice extends CcAssesmentQuestionProcBase
5
{
6
    public function __construct($quiz, $questions, $manifest, $section, $question, $rootpath, $contextid, $outdir)
7
    {
8
        parent::__construct($quiz, $questions, $manifest, $section, $question, $rootpath, $contextid, $outdir);
9
        $this->qtype = CcQtiProfiletype::MULTIPLE_CHOICE;
10
11
        $correctAnswerNode = 0;
12
        $questionScore = 0;
13
        foreach ($question->answers as $answer) {
14
            if ($answer['correct'] > 0) {
15
                $correctAnswerNode = 1;
16
                $this->correctAnswerNodeId = $answer['id'];
17
                $questionScore = $answer['ponderation'];
18
                break;
19
            }
20
        }
21
        if (empty($correctAnswerNode)) {
22
            throw new RuntimeException('No correct answer!');
23
        }
24
        //$this->total_grade_value = ($question_score).'.0000000';
25
        $this->totalGradeValue = $questionScore;
26
    }
27
28
    public function onGenerateAnswers()
29
    {
30
        //add responses holder
31
        $qresponseLid = new CcResponseLidtype();
32
        $this->qresponseLid = $qresponseLid;
33
        $this->qpresentation->setResponseLid($qresponseLid);
34
        $qresponseChoice = new CcAssesmentRenderChoicetype();
35
        $qresponseLid->setRenderChoice($qresponseChoice);
36
37
        //Mark that question has only one correct answer -
38
        //which applies for multiple choice and yes/no questions
39
        $qresponseLid->setRcardinality(CcQtiValues::SINGLE);
40
41
        //are we to shuffle the responses?
42
        $shuffleAnswers = $this->quiz['random_answers'] > 0;
43
44
        $qresponseChoice->enableShuffle($shuffleAnswers);
45
        $answerlist = [];
46
47
        $qaResponses = $this->questionNode->answers;
48
49
        foreach ($qaResponses as $node) {
50
            $answerContent = $node['answer'];
51
            $id = $node['id'];
52
53
            $result = CcHelpers::processLinkedFiles($answerContent,
54
                $this->manifest,
55
                $this->rootpath,
56
                $this->contextid,
57
                $this->outdir);
58
59
            $qresponseLabel = CcAssesmentHelper::addAnswer($qresponseChoice,
60
                $result[0],
61
                CcQtiValues::HTMLTYPE);
62
63
            PkgResourceDependencies::instance()->add($result[1]);
64
65
            $answerIdent = $qresponseLabel->getIdent();
66
            $feedbackIdent = $answerIdent.'_fb';
67
            if (empty($this->correctAnswerIdent) && $id) {
68
                $this->correctAnswerIdent = $answerIdent;
69
            }
70
71
            //add answer specific feedbacks if not empty
72
            $content = $node['comment'];
73
74
            if (!empty($content)) {
75
                $result = CcHelpers::processLinkedFiles($content,
76
                    $this->manifest,
77
                    $this->rootpath,
78
                    $this->contextid,
79
                    $this->outdir);
80
81
                CcAssesmentHelper::addFeedback($this->qitem,
82
                    $result[0],
83
                    CcQtiValues::HTMLTYPE,
84
                    $feedbackIdent);
85
86
                PkgResourceDependencies::instance()->add($result[1]);
87
                $answerlist[$answerIdent] = $feedbackIdent;
88
            }
89
        }
90
        $this->answerlist = $answerlist;
91
    }
92
93
    public function onGenerateFeedbacks()
94
    {
95
        parent::onGenerateFeedbacks();
96
97
        //Question combined feedbacks
98
        $correctQuestionFb = '';
99
        $incorrectQuestionFb = '';
100
101
        if (empty($correctQuestionFb)) {
102
            //Hardcode some text for now
103
            $correctQuestionFb = 'Well done!';
104
        }
105
        if (empty($incorrectQuestionFb)) {
106
            //Hardcode some text for now
107
            $incorrectQuestionFb = 'Better luck next time!';
108
        }
109
110
        $proc = ['correct_fb' => $correctQuestionFb, 'general_incorrect_fb' => $incorrectQuestionFb];
111
        foreach ($proc as $ident => $content) {
112
            if (empty($content)) {
113
                continue;
114
            }
115
            $result = CcHelpers::processLinkedFiles($content,
116
                $this->manifest,
117
                $this->rootpath,
118
                $this->contextid,
119
                $this->outdir);
120
            CcAssesmentHelper::addFeedback($this->qitem,
121
                $result[0],
122
                CcQtiValues::HTMLTYPE,
123
                $ident);
124
            PkgResourceDependencies::instance()->add($result[1]);
125
            if ($ident == 'correct_fb') {
126
                $this->correctFeedbacks[] = $ident;
127
            } else {
128
                $this->incorrectFeedbacks[] = $ident;
129
            }
130
        }
131
    }
132
133
    public function onGenerateResponseProcessing()
134
    {
135
        parent::onGenerateResponseProcessing();
136
137
        //respconditions
138
        /**
139
         * General unconditional feedback must be added as a first respcondition
140
         * without any condition and just displayfeedback (if exists).
141
         */
142
        if (!empty($this->generalFeedback)) {
143
            $qrespcondition = new CcAssesmentRespconditiontype();
144
            $qrespcondition->setTitle('General feedback');
145
            $this->qresprocessing->addRespcondition($qrespcondition);
146
            $qrespcondition->enableContinue();
147
            //define the condition for success
148
            $qconditionvar = new CcAssignmentConditionvar();
149
            $qrespcondition->setConditionvar($qconditionvar);
150
            $qother = new CcAssignmentConditionvarOthertype();
151
            $qconditionvar->setOther($qother);
152
            $qdisplayfeedback = new CcAssignmentDisplayfeedbacktype();
153
            $qrespcondition->addDisplayfeedback($qdisplayfeedback);
154
            $qdisplayfeedback->setFeedbacktype(CcQtiValues::RESPONSE);
155
            $qdisplayfeedback->setLinkrefid('general_fb');
156
        }
157
158
        //success condition
159
        $qrespcondition = new CcAssesmentRespconditiontype();
160
        $qrespcondition->setTitle('Correct');
161
        $this->qresprocessing->addRespcondition($qrespcondition);
162
        $qrespcondition->enableContinue(false);
163
        $qsetvar = new CcAssignmentSetvartype(100);
164
        $qrespcondition->addSetvar($qsetvar);
165
        //define the condition for success
166
        $qconditionvar = new CcAssignmentConditionvar();
167
        $qrespcondition->setConditionvar($qconditionvar);
168
        $qvarequal = new CcAssignmentConditionvarVarequaltype($this->correctAnswerIdent);
169
        $qconditionvar->setVarequal($qvarequal);
170
        $qvarequal->setRespident($this->qresponseLid->getIdent());
171
172
        if (array_key_exists($this->correctAnswerIdent, $this->answerlist)) {
173
            $qdisplayfeedback = new CcAssignmentDisplayfeedbacktype();
174
            $qrespcondition->addDisplayfeedback($qdisplayfeedback);
175
            $qdisplayfeedback->setFeedbacktype(CcQtiValues::RESPONSE);
176
            $qdisplayfeedback->setLinkrefid($this->answerlist[$this->correctAnswerIdent]);
177
        }
178
179
        foreach ($this->correctFeedbacks as $ident) {
180
            $qdisplayfeedback = new CcAssignmentDisplayfeedbacktype();
181
            $qrespcondition->addDisplayfeedback($qdisplayfeedback);
182
            $qdisplayfeedback->setFeedbacktype(CcQtiValues::RESPONSE);
183
            $qdisplayfeedback->setLinkrefid($ident);
184
        }
185
186
        //rest of the conditions
187
        foreach ($this->answerlist as $ident => $refid) {
188
            if ($ident == $this->correctAnswerIdent) {
189
                continue;
190
            }
191
192
            $qrespcondition = new CcAssesmentRespconditiontype();
193
            $this->qresprocessing->addRespcondition($qrespcondition);
194
            $qsetvar = new CcAssignmentSetvartype(0);
195
            $qrespcondition->addSetvar($qsetvar);
196
            //define the condition for fail
197
            $qconditionvar = new CcAssignmentConditionvar();
198
            $qrespcondition->setConditionvar($qconditionvar);
199
            $qvarequal = new CcAssignmentConditionvarVarequaltype($ident);
200
            $qconditionvar->setVarequal($qvarequal);
201
            $qvarequal->setRespident($this->qresponseLid->getIdent());
202
203
            $qdisplayfeedback = new CcAssignmentDisplayfeedbacktype();
204
            $qrespcondition->addDisplayfeedback($qdisplayfeedback);
205
            $qdisplayfeedback->setFeedbacktype(CcQtiValues::RESPONSE);
206
            $qdisplayfeedback->setLinkrefid($refid);
207
208
            foreach ($this->incorrectFeedbacks as $ident) {
0 ignored issues
show
Comprehensibility Bug introduced by
$ident is overwriting a variable from outer foreach loop.
Loading history...
209
                $qdisplayfeedback = new CcAssignmentDisplayfeedbacktype();
210
                $qrespcondition->addDisplayfeedback($qdisplayfeedback);
211
                $qdisplayfeedback->setFeedbacktype(CcQtiValues::RESPONSE);
212
                $qdisplayfeedback->setLinkrefid($ident);
213
            }
214
        }
215
    }
216
}
217