Passed
Push — 1.11.x ( ef1e71...906f76 )
by Yannick
08:39
created

CcAssesmentQuestionFib::__construct()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 36
rs 9.1608
cc 5
nc 6
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 CcAssesmentQuestionFib extends CcAssesmentQuestionProcBase
5
{
6
    public function __construct($quiz, $questions, $manifest, $section, $questionNode, $rootpath, $contextid, $outdir)
7
    {
8
        parent::__construct($quiz, $questions, $manifest, $section, $questionNode, $rootpath, $contextid, $outdir);
9
        $this->qtype = CcQtiProfiletype::FIELD_ENTRY;
10
        $correctAnswerNodes = [];
11
        $questionScore = 0;
12
        foreach ($questionNode->answers as $index => $answer) {
13
            $answerScore = 0;
14
            $answerText = $answer['answer'];
15
            $pos = strrpos($answerText, '::');
16
            list($answerText, $answerRules) = explode('::', $answerText);
17
            $matches = [];
18
            list($weights, $sizes, $others) = explode(':', $answerRules);
19
            $weights = explode(',', $weights);
20
            $i = 0;
21
            // Todo: improve to tolerate all separators
22
            if (preg_match_all('/\[(.*?)\]/', $answerText, $matches)) {
23
                foreach ($matches[1] as $match) {
24
                    $correctAnswerNodes[] = $match;
25
                    $questionScore += $weights[$i];
26
                    $answerScore += $weights[$i];
27
                    $i++;
28
                }
29
            }
30
            $questionNode->answers[$index] = [
31
                'answer' => $answerText,
32
                'ponderation' => $answerScore,
33
                'comment' => $answer['comment'],
34
            ];
35
            $questionScore ++;
36
        }
37
        if (count($correctAnswerNodes) == 0) {
38
            throw new RuntimeException('No correct answer!');
39
        }
40
        $this->correctAnswers = $correctAnswerNodes;
41
        $this->totalGradeValue = $questionScore;
42
    }
43
44
    public function onGenerateAnswers()
45
    {
46
        //add responses holder
47
        $qresponseLid = new CcResponseLidtype();
48
        $this->qresponseLid = $qresponseLid;
49
        $this->qpresentation->setResponseLid($qresponseLid);
50
        $qresponseChoice = new CcAssesmentRenderFibtype();
51
        $qresponseLid->setRenderFib($qresponseChoice);
52
53
        //Mark that question has more than one correct answer
54
        $qresponseLid->setRcardinality(CcQtiValues::MULTIPLE);
55
56
        //are we to shuffle the responses?
57
        $shuffleAnswers = $this->quiz['random_answers'] > 0;
58
59
        $qresponseChoice->enableShuffle($shuffleAnswers);
60
        $answerlist = [];
61
62
        $qaResponses = $this->questionNode->answers;
63
64
        foreach ($qaResponses as $node) {
65
            $answerContent = $node['answer'];
66
            $answerGradeFraction = $node['ponderation'];
67
68
            $result = CcHelpers::processLinkedFiles($answerContent,
69
                $this->manifest,
70
                $this->rootpath,
71
                $this->contextid,
72
                $this->outdir);
73
74
            $qresponseLabel = CcAssesmentHelper::addAnswer($qresponseChoice,
75
                $result[0],
76
                CcQtiValues::HTMLTYPE);
77
78
            PkgResourceDependencies::instance()->add($result[1]);
79
80
            $answerIdent = $qresponseLabel->getIdent();
81
            $feedbackIdent = $answerIdent.'_fb';
82
            //add answer specific feedbacks if not empty
83
            $content = $node['comment'];
84
            if (!empty($content)) {
85
                $result = CcHelpers::processLinkedFiles($content,
86
                    $this->manifest,
87
                    $this->rootpath,
88
                    $this->contextid,
89
                    $this->outdir);
90
91
                CcAssesmentHelper::addFeedback($this->qitem,
92
                    $result[0],
93
                    CcQtiValues::HTMLTYPE,
94
                    $feedbackIdent);
95
96
                PkgResourceDependencies::instance()->add($result[1]);
97
            }
98
            $answerlist[$answerIdent] = [$feedbackIdent, ($answerGradeFraction > 0)];
99
        }
100
        $this->answerlist = $answerlist;
101
    }
102
103
    public function onGenerateFeedbacks()
104
    {
105
        parent::onGenerateFeedbacks();
106
    }
107
108
    public function onGenerateResponseProcessing()
109
    {
110
        parent::onGenerateResponseProcessing();
111
112
        /**
113
         * General unconditional feedback must be added as a first respcondition
114
         * without any condition and just displayfeedback (if exists).
115
         */
116
        $qrespcondition = new CcAssesmentRespconditiontype();
117
        $qrespcondition->setTitle('General feedback');
118
        $this->qresprocessing->addRespcondition($qrespcondition);
119
        $qrespcondition->enableContinue();
120
    }
121
}
122