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

main/exercise/global_multiple_answer.class.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use ChamiloSession as Session;
6
7
/**
8
 * Class GlobalMultipleAnswer.
9
 */
10
class GlobalMultipleAnswer extends Question
11
{
12
    public $typePicture = 'mcmagl.png';
13
    public $explanationLangVar = 'GlobalMultipleAnswer';
14
15
    /**
16
     * GlobalMultipleAnswer constructor.
17
     */
18
    public function __construct()
19
    {
20
        parent::__construct();
21
        $this->type = GLOBAL_MULTIPLE_ANSWER;
22
        $this->isContent = $this->getIsContent();
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function createAnswersForm($form)
29
    {
30
        $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
31
        $nb_answers += isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0);
32
33
        $obj_ex = Session::read('objExercise');
34
35
        $form->addHeader(get_lang('Answers'));
36
        /* Mise en variable de Affichage "Reponses" et son icone, "N�", "Vrai", "Reponse" */
37
        $html = '<table class="table table-striped table-hover">
38
                <tr>
39
                    <th width="10px">'.get_lang('Number').'</th>
40
                    <th width="10px">'.get_lang('True').'</th>
41
                    <th width="50%">'.get_lang('Answer').'</th>
42
                    <th width="50%">'.get_lang('Comment').'</th>
43
                </tr>
44
                ';
45
        $form->addHtml($html);
46
47
        $defaults = [];
48
        $correct = 0;
49
        $answer = false;
50
        if (!empty($this->iid)) {
51
            $answer = new Answer($this->iid);
52
            $answer->read();
53
            if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
54
                $nb_answers = $answer->nbrAnswers;
55
            }
56
        }
57
58
        //  le nombre de r�ponses est bien enregistr� sous la forme int(nb)
59
        /* Ajout mise en forme nb reponse */
60
        $form->addElement('hidden', 'nb_answers');
61
        $boxes_names = [];
62
63
        if ($nb_answers < 1) {
64
            $nb_answers = 1;
65
            echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal');
66
        }
67
68
        //D�but affichage score global dans la modification d'une question
69
        $scoreA = 0; //par reponse
70
        $scoreG = 0; //Global
71
72
        /* boucle pour sauvegarder les donn�es dans le tableau defaults */
73
        for ($i = 1; $i <= $nb_answers; $i++) {
74
            /* si la reponse est de type objet */
75
            if (is_object($answer)) {
76
                $defaults['answer['.$i.']'] = $answer->answer[$i];
77
                $defaults['comment['.$i.']'] = $answer->comment[$i];
78
                $defaults['correct['.$i.']'] = $answer->correct[$i];
79
80
                // start
81
                $scoreA = $answer->weighting[$i];
82
            }
83
            if ($scoreA > 0) {
84
                $scoreG = $scoreG + $scoreA;
85
            }
86
            //------------- Fin
87
            //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
88
            if ($scoreA == 0) {
89
                $defaults['pts'] = 1;
90
            }
91
92
            $renderer = &$form->defaultRenderer();
93
94
            $renderer->setElementTemplate(
95
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
96
                'correct['.$i.']'
97
            );
98
            $renderer->setElementTemplate(
99
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
100
                'counter['.$i.']'
101
            );
102
            $renderer->setElementTemplate(
103
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
104
                'answer['.$i.']'
105
            );
106
            $renderer->setElementTemplate(
107
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
108
                'comment['.$i.']'
109
            );
110
111
            $answer_number = $form->addElement(
112
                'text',
113
                'counter['.$i.']',
114
                null,
115
                'value="'.$i.'"'
116
            );
117
            $answer_number->freeze();
118
119
            $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox"');
120
            $boxes_names[] = 'correct['.$i.']';
121
122
            $form->addElement(
123
                'html_editor',
124
                'answer['.$i.']',
125
                null,
126
                [],
127
                [
128
                    'ToolbarSet' => 'TestProposedAnswer',
129
                    'Width' => '100%',
130
                    'Height' => '100',
131
                ]
132
            );
133
            $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
134
            $form->addElement(
135
                'html_editor',
136
                'comment['.$i.']',
137
                null,
138
                [],
139
                [
140
                    'ToolbarSet' => 'TestProposedAnswer',
141
                    'Width' => '100%',
142
                    'Height' => '100',
143
                ]
144
            );
145
146
            $form->addElement('html', '</tr>');
147
        }
148
        //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
149
        $defaults['weighting[1]'] = (round($scoreG));
150
        $form->addElement('html', '</div></div></table>');
151
        $form->add_multiple_required_rule(
152
            $boxes_names,
153
            get_lang('ChooseAtLeastOneCheckbox'),
154
            'multiple_required'
155
        );
156
157
        //only 1 answer the all deal ...
158
        $form->addElement('text', 'weighting[1]', get_lang('Score'));
159
160
        //--------- Creation coche pour ne pas prendre en compte les n�gatifs
161
        $form->addElement('checkbox', 'pts', '', get_lang('NoNegativeScore'));
162
        $form->addElement('html', '<br />');
163
164
        // Affiche un message si le score n'est pas renseign�
165
        $form->addRule('weighting[1]', get_lang('ThisFieldIsRequired'), 'required');
166
167
        global $text;
168
169
        if ($obj_ex->edit_exercise_in_lp ||
170
            (empty($this->exerciseList) && empty($obj_ex->iid))
171
        ) {
172
            // setting the save button here and not in the question class.php
173
            $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers');
174
            $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers');
175
            $form->addButtonSave($text, 'submitQuestion');
176
        }
177
178
        $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $renderer does not seem to be defined for all execution paths leading up to this point.
Loading history...
179
        $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
180
        $renderer->setElementTemplate('{element}', 'moreAnswers');
181
182
        $form->addElement('html', '</div></div>');
183
184
        $defaults['correct'] = $correct;
185
186
        if (!empty($this->iid)) {
187
            $form->setDefaults($defaults);
188
        } else {
189
            if ($this->isContent == 1) {
190
                $form->setDefaults($defaults);
191
            }
192
        }
193
        $form->setConstants(['nb_answers' => $nb_answers]);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function processAnswersCreation($form, $exercise)
200
    {
201
        $objAnswer = new Answer($this->iid);
202
        $nb_answers = $form->getSubmitValue('nb_answers');
203
204
        // Score total
205
        $answer_score = trim($form->getSubmitValue('weighting[1]'));
206
207
        // Reponses correctes
208
        $nbr_corrects = 0;
209
        for ($i = 1; $i <= $nb_answers; $i++) {
210
            $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
211
            if ($goodAnswer) {
212
                $nbr_corrects++;
213
            }
214
        }
215
        // Set question weighting (score total)
216
        $questionWeighting = $answer_score;
217
218
        // Set score per answer
219
        $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
220
        $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
221
222
        $answer_score = $answer_score / $nbr_corrects;
223
224
        //$answer_score �quivaut � la valeur d'une bonne r�ponse
225
        // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
226
        $test = $form->getSubmitValue('pts');
227
228
        for ($i = 1; $i <= $nb_answers; $i++) {
229
            $answer = trim($form->getSubmitValue('answer['.$i.']'));
230
            $comment = trim($form->getSubmitValue('comment['.$i.']'));
231
            $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
232
233
            if ($goodAnswer) {
234
                $weighting = abs($answer_score);
235
            } else {
236
                if ($test == 1) {
237
                    $weighting = 0;
238
                } else {
239
                    $weighting = -abs($answer_score);
240
                }
241
            }
242
243
            $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
244
        }
245
        // saves the answers into the data base
246
        $objAnswer->save();
247
248
        // sets the total weighting of the question --> sert � donner le score total pendant l'examen
249
        $this->updateWeighting($questionWeighting);
250
        $this->save($exercise);
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256
    public function return_header(Exercise $exercise, $counter = null, $score = [])
257
    {
258
        $header = parent::return_header($exercise, $counter, $score);
259
        $header .= '<table class="'.$this->question_table_class.'"><tr>';
260
261
        if (!in_array($exercise->results_disabled, [RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER])) {
262
            $header .= '<th>'.get_lang('Choice').'</th>';
263
            if ($exercise->showExpectedChoiceColumn()) {
264
                $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
265
            }
266
        }
267
268
        $header .= '<th>'.get_lang('Answer').'</th>';
269
        if ($exercise->showExpectedChoice()) {
270
            $header .= '<th>'.get_lang('Status').'</th>';
271
        }
272
        if (false === $exercise->hideComment) {
273
            $header .= '<th>'.get_lang('Comment').'</th>';
274
        }
275
        $header .= '</tr>';
276
277
        return $header;
278
    }
279
}
280