Completed
Push — master ( bd22e5...ae5621 )
by Julito
12:43
created

QuizQuestion   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 167
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A add_option() 0 3 1
B addPicture() 0 24 6
A add_answer() 0 20 1
A show() 0 4 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Component\CourseCopy\Resources;
5
6
use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
7
8
/**
9
 * Exercises questions backup script
10
 * Class QuizQuestion.
11
 *
12
 * @author Bart Mollet <[email protected]>
13
 *
14
 * @package chamilo.backup
15
 */
16
class QuizQuestion extends Resource
17
{
18
    /**
19
     * The question.
20
     */
21
    public $question;
22
23
    /**
24
     * The description.
25
     */
26
    public $description;
27
28
    /**
29
     * Ponderation.
30
     */
31
    public $ponderation;
32
33
    /**
34
     * Type.
35
     */
36
    public $quiz_type;
37
38
    /**
39
     * Position.
40
     */
41
    public $position;
42
43
    /**
44
     * Level.
45
     */
46
    public $level;
47
48
    /**
49
     * Answers.
50
     */
51
    public $answers;
52
53
    /**
54
     * Picture.
55
     */
56
    public $picture;
57
    public $extra;
58
59
    /**
60
     * @var int the question category if any, 0 by default
61
     */
62
    public $question_category;
63
64
    /**
65
     * QuizQuestion constructor.
66
     *
67
     * @param int    $id
68
     * @param string $question
69
     * @param string $description
70
     * @param int    $ponderation
71
     * @param        $type
72
     * @param        $position
73
     * @param string $picture
74
     * @param        $level
75
     * @param        $extra
76
     * @param int    $question_category
77
     */
78
    public function __construct(
79
        $id,
80
        $question,
81
        $description,
82
        $ponderation,
83
        $type,
84
        $position,
85
        $picture,
86
        $level,
87
        $extra,
88
        $question_category = 0
89
    ) {
90
        parent::__construct($id, RESOURCE_QUIZQUESTION);
91
        $this->question = $question;
92
        $this->description = $description;
93
        $this->ponderation = $ponderation;
94
        $this->quiz_type = $type;
95
        $this->position = $position;
96
        $this->level = $level;
97
        $this->answers = [];
98
        $this->extra = $extra;
99
        $this->question_category = $question_category;
100
        $this->picture = $picture;
101
    }
102
103
    /**
104
     * @param CourseBuilder $courseBuilder
105
     */
106
    public function addPicture(CourseBuilder $courseBuilder)
107
    {
108
        if (!empty($this->picture)) {
109
            $courseInfo = $courseBuilder->course->info;
0 ignored issues
show
Bug introduced by
The property info does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
110
            $courseId = $courseInfo['real_id'];
111
            $courseCode = $courseInfo['code'];
112
            $questionId = $this->source_id;
113
            $question = \Question::read($questionId, $courseId);
114
            $pictureId = $question->getPictureId();
115
            // Add the picture document in the builder
116
            if (!empty($pictureId)) {
117
                $itemsToAdd[] = $pictureId;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$itemsToAdd was never initialized. Although not strictly required by PHP, it is generally a good practice to add $itemsToAdd = array(); before regardless.
Loading history...
118
                // Add the "images" folder needed for correct restore
119
                $documentData = \DocumentManager::get_document_data_by_id($pictureId, $courseCode, true);
0 ignored issues
show
Bug introduced by
It seems like $pictureId can also be of type string; however, parameter $id of DocumentManager::get_document_data_by_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

119
                $documentData = \DocumentManager::get_document_data_by_id(/** @scrutinizer ignore-type */ $pictureId, $courseCode, true);
Loading history...
120
                if ($documentData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $documentData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121
                    if (isset($documentData['parents'])) {
122
                        foreach ($documentData['parents'] as $parent) {
123
                            $itemsToAdd[] = $parent['id'];
124
                        }
125
                    }
126
                }
127
128
                // Add the picture
129
                $courseBuilder->build_documents(api_get_session_id(), $courseId, false, $itemsToAdd);
130
            }
131
        }
132
    }
133
134
    /**
135
     * Add an answer to this QuizQuestion.
136
     *
137
     * @param int    $answer_id
138
     * @param string $answer_text
139
     * @param string $correct
140
     * @param string $comment
141
     * @param string $ponderation
142
     * @param string $position
143
     * @param string $hotspot_coordinates
144
     * @param string $hotspot_type
145
     */
146
    public function add_answer(
147
        $answer_id,
148
        $answer_text,
149
        $correct,
150
        $comment,
151
        $ponderation,
152
        $position,
153
        $hotspot_coordinates,
154
        $hotspot_type
155
    ) {
156
        $answer = [];
157
        $answer['id'] = $answer_id;
158
        $answer['answer'] = $answer_text;
159
        $answer['correct'] = $correct;
160
        $answer['comment'] = $comment;
161
        $answer['ponderation'] = $ponderation;
162
        $answer['position'] = $position;
163
        $answer['hotspot_coordinates'] = $hotspot_coordinates;
164
        $answer['hotspot_type'] = $hotspot_type;
165
        $this->answers[] = $answer;
166
    }
167
168
    /**
169
     * @param QuizQuestionOption $option
170
     */
171
    public function add_option($option)
172
    {
173
        $this->question_options[$option->obj->id] = $option;
0 ignored issues
show
Bug Best Practice introduced by
The property question_options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
174
    }
175
176
    /**
177
     * Show this question.
178
     */
179
    public function show()
180
    {
181
        parent::show();
182
        echo $this->question;
183
    }
184
}
185