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