Completed
Push — master ( af42cb...3888f0 )
by Julito
13:17
created

TestToPDF   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 203
rs 8.64
c 0
b 0
f 0
wmc 47

How to fix   Complexity   

Complex Class

Complex classes like TestToPDF often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TestToPDF, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* For license terms, see /license.txt */
3
4
require_once '../config.php';
5
6
$plugin = Test2pdfPlugin::create();
7
$enable = $plugin->get('enable_plugin') == 'true';
8
if (!$enable) {
9
    header('Location: ../../../index.php');
10
    exit;
11
}
12
13
api_protect_course_script();
14
15
$courseId = intval($_GET['c_id']);
16
$sessionId = api_get_session_id();
17
$quizId = intval($_GET['id_quiz']);
18
19
$infoCourse = api_get_course_info_by_id($courseId);
20
$infoQuiz = getInfoQuiz($courseId, $quizId);
21
$titleCourse = removeHtml($infoCourse['title']);
22
$titleQuiz = removeHtml($infoQuiz['title']);
23
24
$mpdf = new PDF();
25
$mpdf->set_header($infoCourse);
26
$mpdf->set_footer();
27
$pdf = $mpdf->pdf;
28
$pdf->SetTitle($titleCourse.' - '.$titleQuiz);
29
$pdf->AddPage();
30
31
$pdf->SetFont('Arial', '', 16);
32
$pdf->SetTextColor(64);
33
$pdf->MultiCell(0, 7, $infoQuiz['title'], 0, 'L', false);
34
if (!empty($infoQuiz['description'])) {
35
    $pdf->WriteHTML(removeQuotes($infoQuiz['description']));
36
}
37
38
// Select all questions of the supported types from the given course
39
$questionsList = getQuestions($courseId, $quizId, $sessionId);
40
41
// Go through all questions and get the answers
42
if ($_GET['type'] == 'question' || $_GET['type'] == 'all') {
43
    $j = 1;
44
    foreach ($questionsList as $key => $value) {
45
        $infoQuestion = getInfoQuestion($courseId, $value);
46
        if ($pdf->y > 240) {
47
            $pdf->AddPage();
48
        }
49
        $pdf->SetFont('Arial', '', 12);
50
        $pdf->SetTextColor(64);
51
        $pdf->MultiCell(0, 7, ($key + $j).' - '.$infoQuestion['question'], 0, 'L', false);
52
        if (!empty($infoQuestion['description'])) {
53
            $pdf->WriteHTML(removeQuotes($infoQuestion['description']));
54
        }
55
56
        $infoAnswer = getAnswers($courseId, $value);
57
        foreach ($infoAnswer as $key2 => $value2) {
58
            $pdf->SetFont('Arial', 'I', 10);
59
            $pdf->SetTextColor(96);
60
            $pdf->Cell(1, 7, '', 0, 0);
61
            $pdf->Rect($pdf->x + 2, $pdf->y, 4, 4);
62
            $pdf->Cell(7, 7, '', 0, 0);
63
            $pdf->MultiCell(0, 5, $letters[$key2].' - '.removeHtml($value2['answer']), 0, 'L', false);
64
            $pdf->Ln(1);
65
        }
66
        $pdf->Ln(4);
67
    }
68
}
69
$j = 1;
70
if ($_GET['type'] == 'answer' || $_GET['type'] == 'all') {
71
    $answerList = [];
72
    foreach ($questionsList as $key => $value) {
73
        $infoQuestion = getInfoQuestion($courseId, $value);
74
        if ($infoQuestion['question'] == $plugin->get_lang('Statement')) {
75
            $j = 0;
76
        } else {
77
            $answers = '';
78
            $infoQuestion = getInfoQuestion($courseId, $value);
79
            if ($infoQuestion['type'] == 2 ||
80
                $infoQuestion['type'] == 9 ||
81
                $infoQuestion['type'] == 11 ||
82
                $infoQuestion['type'] == 12 ||
83
                $infoQuestion['type'] == 14
84
            ) {
85
                $infoAnswer = getAnswers($courseId, $value);
86
                $answers .= ' '.($key + $j).' -';
87
                foreach ($infoAnswer as $key2 => $value2) {
88
                    if ($value2['correct'] == 1) {
89
                        $answers .= ' '.$letters[$key2].',';
90
                    }
91
                }
92
                $i = strrpos($answers, ',');
93
                $answers = substr($answers, 0, $i);
94
                $answers .= ' ';
95
                $answerList[] = $answers;
96
            } else {
97
                $infoAnswer = getAnswers($courseId, $value);
98
                foreach ($infoAnswer as $key2 => $value2) {
99
                    if ($value2['correct'] == 1) {
100
                        $answers .= ' '.($key + $j).' - '.$letters[$key2].' ';
101
                        break;
102
                    }
103
                }
104
                $answerList[] = $answers;
105
            }
106
        }
107
    }
108
    $pdf->SetFont('Arial', '', 12);
109
    $pdf->SetTextColor(64);
110
    $pdf->Cell(0, 7, $plugin->get_lang('AnswersColumn'), 0, 1, 'L', false);
111
112
    $pdf->SetFont('Arial', 'I', 10);
113
    $pdf->SetTextColor(64, 64, 255);
114
    $i = 1;
115
    foreach ($answerList as $resp) {
116
        $pdf->Cell(50, 6, $resp, 0);
117
        if ($i % 4 == 0) {
118
            $pdf->Ln();
119
        }
120
        $i++;
121
    }
122
}
123
124
$pdf->Output();
125