Passed
Push — master ( 3af017...447a4c )
by Julito
09:24
created

UniqueAnswer::createAnswersForm()   F

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 246
Code Lines 166

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 37
eloc 166
nc 42494976
nop 1
dl 0
loc 246
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CQuizAnswer;
5
use ChamiloSession as Session;
6
7
/**
8
 * Class UniqueAnswer.
9
 *
10
 * This class allows to instantiate an object of type UNIQUE_ANSWER
11
 * (MULTIPLE CHOICE, UNIQUE ANSWER),
12
 * extending the class question
13
 *
14
 * @author Eric Marguin
15
 * @author Julio Montoya
16
 *
17
 * @package chamilo.exercise
18
 */
19
class UniqueAnswer extends Question
20
{
21
    public static $typePicture = 'mcua.png';
22
    public static $explanationLangVar = 'UniqueSelect';
23
24
    /**
25
     * Constructor.
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $this->type = UNIQUE_ANSWER;
31
        $this->isContent = $this->getIsContent();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function createAnswersForm($form)
38
    {
39
        // Getting the exercise list
40
        $obj_ex = Session::read('objExercise');
41
42
        $editor_config = [
43
            'ToolbarSet' => 'TestProposedAnswer',
44
            'Width' => '100%',
45
            'Height' => '125',
46
        ];
47
48
        //this line defines how many questions by default appear when creating a choice question
49
        // The previous default value was 2. See task #1759.
50
        $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
51
        $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
52
53
        /*
54
          Types of Feedback
55
          $feedback_option[0]=get_lang('Feedback');
56
          $feedback_option[1]=get_lang('DirectFeedback');
57
          $feedback_option[2]=get_lang('NoFeedback');
58
         */
59
60
        $feedback_title = '';
61
        switch ($obj_ex->getFeedbackType()) {
62
            case EXERCISE_FEEDBACK_TYPE_DIRECT:
63
                // Scenario
64
                $comment_title = '<th width="20%">'.get_lang('Comment').'</th>';
65
                $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>';
66
                break;
67
            case EXERCISE_FEEDBACK_TYPE_POPUP:
0 ignored issues
show
Bug introduced by
The constant EXERCISE_FEEDBACK_TYPE_POPUP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
68
                $comment_title = '<th width="20%">'.get_lang('Comment').'</th>';
69
                break;
70
            default:
71
                $comment_title = '<th width="40%">'.get_lang('Comment').'</th>';
72
                break;
73
        }
74
75
        $html = '<table class="table table-striped table-hover">
76
            <thead>
77
                <tr style="text-align: center;">
78
                    <th width="5%">'.get_lang('Number').'</th>
79
                    <th width="5%"> '.get_lang('True').'</th>
80
                    <th width="40%">'.get_lang('Answer').'</th>
81
                        '.$comment_title.'
82
                        '.$feedback_title.'
83
                    <th width="10%">'.get_lang('Weighting').'</th>
84
                </tr>
85
            </thead>
86
            <tbody>';
87
88
        $form->addHeader(get_lang('Answers'));
89
        $form->addHtml($html);
90
91
        $defaults = [];
92
        $correct = 0;
93
        if (!empty($this->id)) {
94
            $answer = new Answer($this->id);
95
            $answer->read();
96
            if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
97
                $nb_answers = $answer->nbrAnswers;
98
            }
99
        }
100
        $form->addElement('hidden', 'nb_answers');
101
102
        //Feedback SELECT
103
        $question_list = $obj_ex->selectQuestionList();
104
        $select_question = [];
105
        $select_question[0] = get_lang('SelectTargetQuestion');
106
        if (is_array($question_list)) {
107
            foreach ($question_list as $key => $questionid) {
108
                //To avoid warning messages
109
                if (!is_numeric($questionid)) {
110
                    continue;
111
                }
112
                $question = Question::read($questionid);
113
                $questionTitle = strip_tags($question->selectTitle());
114
                $select_question[$questionid] = "Q$key: $questionTitle";
115
            }
116
        }
117
        $select_question[-1] = get_lang('ExitTest');
118
119
        $list = new LearnpathList(api_get_user_id());
120
        $flat_list = $list->get_flat_list();
121
        $select_lp_id = [];
122
        $select_lp_id[0] = get_lang('SelectTargetLP');
123
124
        foreach ($flat_list as $id => $details) {
125
            $select_lp_id[$id] = cut($details['lp_name'], 20);
126
        }
127
128
        $temp_scenario = [];
129
        if ($nb_answers < 1) {
130
            $nb_answers = 1;
131
            echo Display::return_message(
132
                get_lang('YouHaveToCreateAtLeastOneAnswer')
133
            );
134
        }
135
136
        for ($i = 1; $i <= $nb_answers; $i++) {
137
            $form->addHtml('<tr>');
138
            if (isset($answer) && is_object($answer)) {
139
                if (isset($answer->correct[$i]) && $answer->correct[$i]) {
140
                    $correct = $i;
141
                }
142
                $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : '';
143
                $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : '';
144
                $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0;
145
                $item_list = [];
146
                if (isset($answer->destination[$i])) {
147
                    $item_list = explode('@@', $answer->destination[$i]);
148
                }
149
                $try = isset($item_list[0]) ? $item_list[0] : '';
150
                $lp = isset($item_list[1]) ? $item_list[1] : '';
151
                $list_dest = isset($item_list[2]) ? $item_list[2] : '';
152
                $url = isset($item_list[3]) ? $item_list[3] : '';
153
154
                if ($try == 0) {
155
                    $try_result = 0;
156
                } else {
157
                    $try_result = 1;
158
                }
159
                if ($url == 0) {
160
                    $url_result = '';
161
                } else {
162
                    $url_result = $url;
163
                }
164
165
                $temp_scenario['url'.$i] = $url_result;
166
                $temp_scenario['try'.$i] = $try_result;
167
                $temp_scenario['lp'.$i] = $lp;
168
                $temp_scenario['destination'.$i] = $list_dest;
169
            } else {
170
                $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
171
                $defaults['weighting[1]'] = 10;
172
                $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
173
                $defaults['weighting[2]'] = 0;
174
175
                $temp_scenario['destination'.$i] = ['0'];
176
                $temp_scenario['lp'.$i] = ['0'];
177
            }
178
            $defaults['scenario'] = $temp_scenario;
179
180
            $renderer = $form->defaultRenderer();
181
182
            $renderer->setElementTemplate(
183
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
184
                'correct'
185
            );
186
            $renderer->setElementTemplate(
187
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
188
                'counter['.$i.']'
189
            );
190
            $renderer->setElementTemplate(
191
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
192
                'answer['.$i.']'
193
            );
194
            $renderer->setElementTemplate(
195
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
196
                'comment['.$i.']'
197
            );
198
            $renderer->setElementTemplate(
199
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
200
                'weighting['.$i.']'
201
            );
202
203
            $answer_number = $form->addElement(
204
                'text',
205
                'counter['.$i.']',
206
                null,
207
                ' value = "'.$i.'"'
208
            );
209
            $answer_number->freeze();
210
            $form->addElement(
211
                'radio',
212
                'correct',
213
                null,
214
                null,
215
                $i,
216
                'class="checkbox"'
217
            );
218
219
            $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config);
220
221
            $form->addRule(
222
                'answer['.$i.']',
223
                get_lang('ThisFieldIsRequired'),
224
                'required'
225
            );
226
227
            switch ($obj_ex->getFeedbackType()) {
228
                case EXERCISE_FEEDBACK_TYPE_DIRECT:
229
                    $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question);
230
                    break;
231
                case EXERCISE_FEEDBACK_TYPE_POPUP:
232
                default:
233
                    $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config);
234
                    break;
235
            }
236
            $form->addText('weighting['.$i.']', null, null, ['value' => '0']);
237
            $form->addHtml('</tr>');
238
        }
239
240
        $form->addHtml('</tbody>');
241
        $form->addHtml('</table>');
242
243
        global $text;
244
        $buttonGroup = [];
245
246
        if ($obj_ex->edit_exercise_in_lp == true ||
247
            (empty($this->exerciseList) && empty($obj_ex->id))
248
        ) {
249
            //setting the save button here and not in the question class.php
250
            $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
251
            $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
252
            $buttonGroup[] = $form->addButton(
253
                'submitQuestion',
254
                $text,
255
                'check',
256
                'primary',
257
                'default',
258
                null,
259
                ['id' => 'submit-question'],
260
                true
261
            );
262
            $form->addGroup($buttonGroup);
263
        }
264
265
        // We check the first radio button to be sure a radio button will be check
266
        if ($correct == 0) {
267
            $correct = 1;
268
        }
269
270
        $defaults['correct'] = $correct;
271
272
        if (!empty($this->id)) {
273
            $form->setDefaults($defaults);
274
        } else {
275
            if ($this->isContent == 1) {
276
                // Default sample content.
277
                $form->setDefaults($defaults);
278
            } else {
279
                $form->setDefaults(['correct' => 1]);
280
            }
281
        }
282
        $form->setConstants(['nb_answers' => $nb_answers]);
283
    }
284
285
    public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question)
286
    {
287
        $editor_config = [
288
            'ToolbarSet' => 'TestProposedAnswer',
289
            'Width' => '100%',
290
            'Height' => '125',
291
        ];
292
293
        $form->addHtmlEditor(
294
            'comment['.$i.']',
295
            null,
296
            null,
297
            false,
298
            $editor_config
299
        );
300
        // Direct feedback
301
        //Adding extra feedback fields
302
        $group = [];
303
        $group['try'.$i] = $form->createElement(
304
            'checkbox',
305
            'try'.$i,
306
            null,
307
            get_lang('TryAgain')
308
        );
309
        $group['lp'.$i] = $form->createElement(
310
            'select',
311
            'lp'.$i,
312
            get_lang('SeeTheory').': ',
313
            $select_lp_id
314
        );
315
        $group['destination'.$i] = $form->createElement(
316
            'select',
317
            'destination'.$i,
318
            get_lang('GoToQuestion').': ',
319
            $select_question
320
        );
321
        $group['url'.$i] = $form->createElement(
322
            'text',
323
            'url'.$i,
324
            get_lang('Other').': ',
325
            [
326
                'class' => 'col-md-2',
327
                'placeholder' => get_lang('Other'),
328
            ]
329
        );
330
        $form->addGroup($group, 'scenario');
331
332
        $renderer->setElementTemplate(
333
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
334
            'scenario'
335
        );
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function processAnswersCreation($form, $exercise)
342
    {
343
        $questionWeighting = $nbrGoodAnswers = 0;
344
        $correct = $form->getSubmitValue('correct');
345
        $objAnswer = new Answer($this->id);
346
        $nb_answers = $form->getSubmitValue('nb_answers');
347
348
        for ($i = 1; $i <= $nb_answers; $i++) {
349
            $answer = trim($form->getSubmitValue('answer['.$i.']'));
350
            $comment = trim($form->getSubmitValue('comment['.$i.']'));
351
            $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
352
            $scenario = $form->getSubmitValue('scenario');
353
354
            //$list_destination = $form -> getSubmitValue('destination'.$i);
355
            //$destination_str = $form -> getSubmitValue('destination'.$i);
356
357
            $try = !empty($scenario['try'.$i]);
358
            $lp = $scenario['lp'.$i];
359
            $destination = $scenario['destination'.$i];
360
            $url = trim($scenario['url'.$i]);
361
362
            /*
363
            How we are going to parse the destination value
364
365
           here we parse the destination value which is a string
366
            1@@3@@2;4;4;@@http://www.chamilo.org
367
368
            where: try_again@@lp_id@@selected_questions@@url
369
370
           try_again = is 1 || 0
371
           lp_id = id of a learning path (0 if dont select)
372
           selected_questions= ids of questions
373
           url= an url
374
375
            $destination_str='';
376
            foreach ($list_destination as $destination_id)
377
            {
378
                $destination_str.=$destination_id.';';
379
            }*/
380
381
            $goodAnswer = $correct == $i ? true : false;
382
383
            if ($goodAnswer) {
384
                $nbrGoodAnswers++;
385
                $weighting = abs($weighting);
386
                if ($weighting > 0) {
387
                    $questionWeighting += $weighting;
388
                }
389
            }
390
391
            if (empty($try)) {
392
                $try = 0;
393
            }
394
395
            if (empty($lp)) {
396
                $lp = 0;
397
            }
398
399
            if (empty($destination)) {
400
                $destination = 0;
401
            }
402
403
            if ($url == '') {
404
                $url = 0;
405
            }
406
407
            //1@@1;2;@@2;4;4;@@http://www.chamilo.org
408
            $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
409
            $objAnswer->createAnswer(
410
                $answer,
411
                $goodAnswer,
412
                $comment,
413
                $weighting,
0 ignored issues
show
Bug introduced by
It seems like $weighting can also be of type double and string; however, parameter $weighting of Answer::createAnswer() 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

413
                /** @scrutinizer ignore-type */ $weighting,
Loading history...
414
                $i,
415
                null,
416
                null,
417
                $dest
418
            );
419
        }
420
421
        // saves the answers into the data base
422
        $objAnswer->save();
423
424
        // sets the total weighting of the question
425
        $this->updateWeighting($questionWeighting);
0 ignored issues
show
Bug introduced by
It seems like $questionWeighting can also be of type double; however, parameter $weighting of Question::updateWeighting() 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

425
        $this->updateWeighting(/** @scrutinizer ignore-type */ $questionWeighting);
Loading history...
426
        $this->save($exercise);
427
    }
428
429
    /**
430
     * {@inheritdoc}
431
     */
432
    public function return_header(Exercise $exercise, $counter = null, $score = [])
433
    {
434
        $header = parent::return_header($exercise, $counter, $score);
435
        $header .= '<table class="'.$this->question_table_class.'"><tr>';
436
437
        $header .= '<th>'.get_lang('Choice').'</th>';
438
        if ($exercise->showExpectedChoiceColumn()) {
439
            $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
440
        }
441
442
        $header .= '<th>'.get_lang('Answer').'</th>';
443
        if ($exercise->showExpectedChoice()) {
444
            $header .= '<th>'.get_lang('Status').'</th>';
445
        }
446
        $header .= '<th>'.get_lang('Comment').'</th>';
447
        $header .= '</tr>';
448
449
        return $header;
450
    }
451
452
    /**
453
     * Saves one answer to the database.
454
     *
455
     * @param int    $id          The ID of the answer (has to be calculated for this course)
456
     * @param int    $question_id The question ID (to which the answer is attached)
457
     * @param string $title       The text of the answer
458
     * @param string $comment     The feedback for the answer
459
     * @param float  $score       The score you get when picking this answer
460
     * @param int    $correct     Whether this answer is considered *the* correct one (this is the unique answer type)
461
     */
462
    public function addAnswer(
463
        $id,
464
        $question_id,
465
        $title,
466
        $comment,
467
        $score = 0.0,
468
        $correct = 0
469
    ) {
470
        $em = Database::getManager();
471
        $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
472
        $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
473
        $course_id = api_get_course_int_id();
474
        $question_id = intval($question_id);
475
        $score = floatval($score);
476
        $correct = intval($correct);
477
        $title = Database::escape_string($title);
478
        $comment = Database::escape_string($comment);
479
        // Get the max position.
480
        $sql = "SELECT max(position) as max_position
481
                FROM $tbl_quiz_answer
482
                WHERE
483
                    c_id = $course_id AND
484
                    question_id = $question_id";
485
        $rs_max = Database::query($sql);
486
        $row_max = Database::fetch_object($rs_max);
487
        $position = $row_max->max_position + 1;
488
489
        // Insert a new answer
490
        $quizAnswer = new CQuizAnswer();
491
        $quizAnswer
492
            ->setCId($course_id)
493
            ->setId($id)
494
            ->setQuestionId($question_id)
495
            ->setAnswer($title)
496
            ->setCorrect($correct)
497
            ->setComment($comment)
498
            ->setPonderation($score)
499
            ->setPosition($position)
500
            ->setDestination('0@@0@@0@@0');
501
502
        $em->persist($quizAnswer);
503
        $em->flush();
504
505
        $id = $quizAnswer->getIid();
506
507
        if ($id) {
508
            $quizAnswer
509
                ->setId($id);
510
511
            $em->merge($quizAnswer);
512
            $em->flush();
513
        }
514
515
        if ($correct) {
516
            $sql = "UPDATE $tbl_quiz_question
517
                    SET ponderation = (ponderation + $score)
518
                    WHERE c_id = $course_id AND id = ".$question_id;
519
            Database::query($sql);
520
        }
521
    }
522
}
523