Test Setup Failed
Push — master ( 31ad05...ee210b )
by Yannick
253:41 queued 196:01
created

Answer::readOrderedBy()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 78
Code Lines 50

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 78
rs 5.6321
cc 10
eloc 50
nc 28
nop 2

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
/**
5
 * Class Answer
6
 * Allows to instantiate an object of type Answer
7
 * 5 arrays are created to receive the attributes of each answer belonging to a specified question
8
 * @package chamilo.exercise
9
 *
10
 * @author Olivier Brouckaert
11
 */
12
class Answer
13
{
14
    public $questionId;
15
16
    // these are arrays
17
    public $answer;
18
    public $correct;
19
    public $comment;
20
    public $weighting;
21
    public $position;
22
    public $hotspot_coordinates;
23
    public $hotspot_type;
24
    public $destination;
25
    // these arrays are used to save temporarily new answers
26
    // then they are moved into the arrays above or deleted in the event of cancellation
27
    public $new_answer;
28
    public $new_correct;
29
    public $new_comment;
30
    public $new_weighting;
31
    public $new_position;
32
    public $new_hotspot_coordinates;
33
    public $new_hotspot_type;
34
    public $autoId;
35
    public $nbrAnswers;
36
    public $new_nbrAnswers;
37
    public $new_destination; // id of the next question if feedback option is set to Directfeedback
38
    public $course; //Course information
39
40
    /**
41
     * constructor of the class
42
     *
43
     * @author 	Olivier Brouckaert
44
     * @param int $questionId that answers belong to
45
     * @param int $course_id
46
     */
47
    public function __construct($questionId, $course_id = null)
48
    {
49
        $this->questionId = intval($questionId);
50
        $this->answer = array();
51
        $this->correct = array();
52
        $this->comment = array();
53
        $this->weighting = array();
54
        $this->position = array();
55
        $this->hotspot_coordinates = array();
56
        $this->hotspot_type = array();
57
        $this->destination = array();
58
        // clears $new_* arrays
59
        $this->cancel();
60
61
        if (!empty($course_id)) {
62
            $courseInfo = api_get_course_info_by_id($course_id);
63
        } else {
64
            $courseInfo = api_get_course_info();
65
        }
66
67
        $this->course = $courseInfo;
68
        $this->course_id = $courseInfo['real_id'];
69
70
        // fills arrays
71
        $objExercise = new Exercise($this->course_id);
72
        $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null;
73
        $objExercise->read($exerciseId);
74
75
        if ($objExercise->random_answers == '1') {
76
            $this->readOrderedBy('rand()', '');// randomize answers
77
        } else {
78
            $this->read(); // natural order
79
        }
80
    }
81
82
    /**
83
     * Clears $new_* arrays
84
     *
85
     * @author Olivier Brouckaert
86
     */
87
    public function cancel()
88
    {
89
        $this->new_answer = array();
90
        $this->new_correct = array();
91
        $this->new_comment = array();
92
        $this->new_weighting = array();
93
        $this->new_position = array();
94
        $this->new_hotspot_coordinates = array();
95
        $this->new_hotspot_type = array();
96
        $this->new_nbrAnswers = 0;
97
        $this->new_destination = array();
98
    }
99
100
    /**
101
     * Reads answer information from the database
102
     *
103
     * @author Olivier Brouckaert
104
     */
105
    public function read()
106
    {
107
        $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
108
        $questionId = $this->questionId;
109
110
        $sql = "SELECT * FROM $TBL_ANSWER
111
                WHERE
112
                    c_id = {$this->course_id} AND
113
                    question_id ='".$questionId."'
114
                ORDER BY position";
115
116
        $result = Database::query($sql);
117
        $i=1;
118
119
        // while a record is found
120
        while ($object = Database::fetch_object($result)) {
121
            $this->id[$i] = $object->id;
122
            $this->answer[$i] = $object->answer;
123
            $this->correct[$i] = $object->correct;
124
            $this->comment[$i] = $object->comment;
125
            $this->weighting[$i] = $object->ponderation;
126
            $this->position[$i] = $object->position;
127
            $this->hotspot_coordinates[$i] = $object->hotspot_coordinates;
128
            $this->hotspot_type[$i] = $object->hotspot_type;
129
            $this->destination[$i] = $object->destination;
130
            $this->autoId[$i] = $object->id_auto;
131
            $i++;
132
        }
133
        $this->nbrAnswers = $i-1;
134
    }
135
136
    /**
137
     * @param int $id
138
     *
139
     * @return array
140
     */
141
    public function getAnswerByAutoId($id)
142
    {
143
        foreach ($this->autoId as $key => $autoId) {
144
            if ($autoId == $id) {
145
                $result =  [
146
                    'answer' => $this->answer[$key],
147
                    'correct' => $this->correct[$key],
148
                    'comment' => $this->comment[$key],
149
                ];
150
151
                return $result;
152
            }
153
        }
154
155
        return [];
156
    }
157
158
     /**
159
     * returns all answer ids from this question Id
160
     *
161
     * @author Yoselyn Castillo
162
     * @return array - $id (answer ids)
163
     */
164
    public function selectAnswerId()
165
    {
166
        $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
167
        $questionId = $this->questionId;
168
169
        $sql="SELECT id FROM
170
              $TBL_ANSWER
171
              WHERE c_id = {$this->course_id} AND question_id ='".$questionId."'";
172
173
        $result = Database::query($sql);
174
        $id = array();
175
        // while a record is found
176
        if (Database::num_rows($result) > 0) {
177
            while ($object = Database::fetch_array($result)) {
178
                $id[] = $object['id'];
179
            }
180
        }
181
182
        return $id;
183
	}
184
185
    /**
186
     * Reads answer information from the data base ordered by parameter
187
     * @param	string	Field we want to order by
188
     * @param	string	DESC or ASC
189
     * @author 	Frederic Vauthier
190
     */
191
    public function readOrderedBy($field, $order='ASC')
192
    {
193
		$field = Database::escape_string($field);
194
		if (empty($field)) {
195
			$field = 'position';
196
		}
197
198
		if ($order != 'ASC' && $order!='DESC') {
199
			$order = 'ASC';
200
		}
201
202
		$TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
203
		$TBL_QUIZ = Database::get_course_table(TABLE_QUIZ_QUESTION);
204
		$questionId = intval($this->questionId);
205
206
		$sql = "SELECT type FROM $TBL_QUIZ
207
		        WHERE c_id = {$this->course_id} AND id = $questionId";
208
		$result_question = Database::query($sql);
209
		$questionType = Database::fetch_array($result_question);
210
211
        if ($questionType['type'] == DRAGGABLE) {
212
            // Random is done by submit.js.tpl
213
            $this->read();
214
215
            return true;
216
        }
217
218
		$sql = "SELECT
219
		            answer,
220
		            correct,
221
		            comment,
222
		            ponderation,
223
		            position,
224
		            hotspot_coordinates,
225
		            hotspot_type,
226
		            destination,
227
		            id_auto
228
                FROM $TBL_ANSWER
229
                WHERE
230
                    c_id = {$this->course_id} AND
231
                    question_id='".$questionId."'
232
                ORDER BY $field $order";
233
		$result=Database::query($sql);
234
235
		$i = 1;
236
		// while a record is found
237
		$doubt_data = null;
238
		while ($object = Database::fetch_object($result)) {
239
		    if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && $object->position == 666) {
240
		        $doubt_data = $object;
241
                continue;
242
		    }
243
            $this->answer[$i] = $object->answer;
244
            $this->correct[$i] = $object->correct;
245
            $this->comment[$i] = $object->comment;
246
            $this->weighting[$i] = $object->ponderation;
247
            $this->position[$i] = $object->position;
248
            $this->hotspot_coordinates[$i] = $object->hotspot_coordinates;
249
            $this->hotspot_type[$i] = $object->hotspot_type;
250
            $this->destination[$i] = $object->destination;
251
            $this->autoId[$i] = $object->id_auto;
252
            $i++;
253
		}
254
255
		if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && !empty($doubt_data)) {
256
            $this->answer[$i] = $doubt_data->answer;
257
            $this->correct[$i] = $doubt_data->correct;
258
            $this->comment[$i] = $doubt_data->comment;
259
            $this->weighting[$i] = $doubt_data->ponderation;
260
            $this->position[$i] = $doubt_data->position;
261
            $this->hotspot_coordinates[$i] = $object->hotspot_coordinates;
262
            $this->hotspot_type[$i] = $object->hotspot_type;
263
            $this->destination[$i] = $doubt_data->destination;
264
            $this->autoId[$i] = $doubt_data->id_auto;
265
            $i++;
266
	    }
267
        $this->nbrAnswers = $i-1;
268
	}
269
270
	/**
271
	 * returns the autoincrement id identificator
272
	 *
273
	 * @author Juan Carlos Ra�a
274
	 * @return integer - answer num
275
	 */
276
    public function selectAutoId($id)
277
    {
278
		return isset($this->autoId[$id]) ? $this->autoId[$id] : null;
279
	}
280
281
	/**
282
	 * returns the number of answers in this question
283
	 *
284
	 * @author Olivier Brouckaert
285
	 * @return integer - number of answers
286
	 */
287
	public function selectNbrAnswers()
288
    {
289
		return $this->nbrAnswers;
290
	}
291
292
	/**
293
	 * returns the question ID which the answers belong to
294
	 *
295
	 * @author Olivier Brouckaert
296
	 * @return integer - the question ID
297
	 */
298
	public function selectQuestionId()
299
    {
300
		return $this->questionId;
301
	}
302
303
	/**
304
	 * returns the question ID of the destination question
305
	 *
306
	 * @author Julio Montoya
307
	 * @return integer - the question ID
308
	 */
309
	public function selectDestination($id)
310
    {
311
		return isset($this->destination[$id]) ? $this->destination[$id] : null;
312
	}
313
314
    /**
315
	 * returns the answer title
316
	 *
317
	 * @author Olivier Brouckaert
318
	 * @param - integer $id - answer ID
319
	 * @return string - answer title
320
	 */
321
	public function selectAnswer($id)
322
	{
323
		return isset($this->answer[$id]) ? $this->answer[$id] : null;
324
	}
325
326
	/**
327
	 * return array answer by id else return a bool
328
	 */
329
	public function selectAnswerByAutoId($auto_id)
330
	{
331
		$TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
332
333
		$auto_id = intval($auto_id);
334
		$sql = "SELECT id, answer, id_auto FROM $TBL_ANSWER
335
				WHERE c_id = {$this->course_id} AND id_auto='$auto_id'";
336
		$rs = Database::query($sql);
337
338
		if (Database::num_rows($rs) > 0) {
339
			$row = Database::fetch_array($rs, 'ASSOC');
340
341
			return $row;
342
		}
343
344
		return false;
345
	}
346
347
    /**
348
     * returns the answer title from an answer's position
349
     *
350
     * @author Yannick Warnier
351
     * @param - integer $id - answer ID
352
     * @return bool - answer title
353
     */
354
	public function selectAnswerIdByPosition($pos)
355
	{
356
		foreach ($this->position as $k => $v) {
357
			if ($v != $pos) {
358
				continue;
359
			}
360
361
			return $k;
362
		}
363
364
		return false;
365
	}
366
367
    /**
368
     * Returns a list of answers
369
     * @author Yannick Warnier <[email protected]>
370
     * @return array	List of answers where each answer is an array
371
     * of (id, answer, comment, grade) and grade=weighting
372
     */
373
    public function getAnswersList($decode = false)
374
     {
375
	 	$list = array();
376
         for ($i = 1; $i <= $this->nbrAnswers; $i++) {
377
             if (!empty($this->answer[$i])) {
378
379
	 			//Avoid problems when parsing elements with accents
380
	 			if ($decode) {
381
	        		$this->answer[$i] 	= api_html_entity_decode($this->answer[$i], ENT_QUOTES, api_get_system_encoding());
382
	        		$this->comment[$i]	= api_html_entity_decode($this->comment[$i], ENT_QUOTES, api_get_system_encoding());
383
	 			}
384
385
	 			$list[] = array(
386
                    'id' => $i,
387
                    'answer' => $this->answer[$i],
388
                    'comment' => $this->comment[$i],
389
                    'grade' => $this->weighting[$i],
390
                    'hotspot_coord' => $this->hotspot_coordinates[$i],
391
                    'hotspot_type' => $this->hotspot_type[$i],
392
                    'correct' => $this->correct[$i],
393
                    'destination' => $this->destination[$i]
394
				);
395
            }
396
	 	}
397
398
	 	return $list;
399
	 }
400
401
	/**
402
	 * Returns a list of grades
403
	 * @author Yannick Warnier <[email protected]>
404
	 * @return array	List of grades where grade=weighting (?)
405
	 */
406
    public function getGradesList()
407
     {
408
	 	$list = array();
409
	 	for ($i = 0; $i<$this->nbrAnswers;$i++){
410
	 		if(!empty($this->answer[$i])){
411
	 			$list[$i] = $this->weighting[$i];
412
	 		}
413
	 	}
414
	 	return $list;
415
	 }
416
417
	 /**
418
	  * Returns the question type
419
	  * @author	Yannick Warnier <[email protected]>
420
	  * @return	integer	The type of the question this answer is bound to
421
	  */
422
    public function getQuestionType()
423
     {
424
	 	$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
425
	 	$sql = "SELECT type FROM $TBL_QUESTIONS
426
	 	        WHERE c_id = {$this->course_id} AND id = '".$this->questionId."'";
427
	 	$res = Database::query($sql);
428
	 	if (Database::num_rows($res)<=0){
429
	 		return null;
430
	 	}
431
	 	$row = Database::fetch_array($res);
432
433
	 	return $row['type'];
434
	 }
435
436
437
	/**
438
	 * tells if answer is correct or not
439
	 *
440
	 * @author Olivier Brouckaert
441
	 * @param - integer $id - answer ID
442
	 * @return integer - 0 if bad answer, not 0 if good answer
443
	 */
444
    public function isCorrect($id)
445
	{
446
		return isset($this->correct[$id]) ? $this->correct[$id] : null;
447
	}
448
449
	/**
450
	 * returns answer comment
451
	 *
452
	 * @author Olivier Brouckaert
453
	 * @param - integer $id - answer ID
454
	 * @return string - answer comment
455
	 */
456
    public function selectComment($id)
457
	{
458
        return isset($this->comment[$id]) ? $this->comment[$id] : null;
459
	}
460
461
	/**
462
	 * returns answer weighting
463
	 *
464
	 * @author Olivier Brouckaert
465
	 * @param - integer $id - answer ID
466
	 * @return integer - answer weighting
467
	 */
468
    public function selectWeighting($id)
469
	{
470
		return isset($this->weighting[$id]) ? $this->weighting[$id] : null;
471
	}
472
473
	/**
474
	 * returns answer position
475
	 *
476
	 * @author Olivier Brouckaert
477
	 * @param - integer $id - answer ID
478
	 * @return integer - answer position
479
	 */
480
	function selectPosition($id)
481
	{
482
		return isset($this->position[$id]) ? $this->position[$id] : null;
483
	}
484
485
	/**
486
	 * returns answer hotspot coordinates
487
	 *
488
	 * @author	Olivier Brouckaert
489
	 * @param	integer	Answer ID
490
	 * @return	integer	Answer position
491
	 */
492
    public function selectHotspotCoordinates($id)
493
	{
494
		return isset($this->hotspot_coordinates[$id]) ? $this->hotspot_coordinates[$id] : null;
495
	}
496
497
	/**
498
	 * returns answer hotspot type
499
	 *
500
	 * @author	Toon Keppens
501
	 * @param	integer		Answer ID
502
	 * @return	integer		Answer position
503
	 */
504
    public function selectHotspotType($id)
505
	{
506
		return isset($this->hotspot_type[$id]) ? $this->hotspot_type[$id] : null;
507
	}
508
509
	/**
510
	 * Creates a new answer
511
	 *
512
	 * @author Olivier Brouckaert
513
	 * @param string 	$answer answer title
514
	 * @param integer 	$correct 0 if bad answer, not 0 if good answer
515
	 * @param string 	$comment answer comment
516
	 * @param integer 	$weighting answer weighting
517
	 * @param integer 	$position answer position
518
	 * @param array    $new_hotspot_coordinates Coordinates for hotspot exercises (optional)
519
	 * @param integer	$new_hotspot_type Type for hotspot exercises (optional)
520
     * @param string   $destination
521
	 */
522
    public function createAnswer(
523
        $answer,
524
        $correct,
525
        $comment,
526
        $weighting,
527
        $position,
528
        $new_hotspot_coordinates = null,
529
        $new_hotspot_type = null,
530
        $destination = ''
531
    ) {
532
		$this->new_nbrAnswers++;
533
        $id = $this->new_nbrAnswers;
534
        $this->new_answer[$id] = $answer;
535
        $this->new_correct[$id] = $correct;
536
        $this->new_comment[$id] = $comment;
537
        $this->new_weighting[$id] = $weighting;
538
        $this->new_position[$id] = $position;
539
        $this->new_hotspot_coordinates[$id] = $new_hotspot_coordinates;
540
        $this->new_hotspot_type[$id] = $new_hotspot_type;
541
        $this->new_destination[$id] = $destination;
542
	}
543
544
    /**
545
     * Updates an answer
546
     *
547
     * @author Toon Keppens
548
     *
549
     * @param string $answer
550
     * @param string $comment
551
     * @param string $correct
552
     * @param string $weighting
553
     * @param string $position
554
     * @param string $destination
555
     * @param string $hotspot_coordinates
556
     * @param string $hotspot_type
557
     */
558
    public function updateAnswers(
559
        $autoId,
560
        $answer,
561
        $comment,
562
        $correct,
563
        $weighting,
564
        $position,
565
        $destination,
566
        $hotspot_coordinates,
567
        $hotspot_type
568
    ) {
569
        $answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER);
570
        $autoId = intval($autoId);
571
572
        $params = [
573
            'answer' => $answer,
574
            'comment' => $comment,
575
            'correct' => $correct,
576
            'ponderation' => $weighting,
577
            'position' => $position,
578
            'destination' => $destination,
579
            'hotspot_coordinates' => $hotspot_coordinates,
580
            'hotspot_type' => $hotspot_type,
581
        ];
582
583
        Database::update($answerTable, $params, ['id_auto = ?' => $autoId]);
584
	}
585
586
	/**
587
	 * Records answers into the data base
588
	 *
589
	 * @author Olivier Brouckaert
590
	 */
591
    public function save()
592
    {
593
		$answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
594
		$questionId = intval($this->questionId);
595
596
		$c_id = $this->course['real_id'];
597
        $correctList = [];
598
        $answerList = [];
599
600
		for ($i=1; $i <= $this->new_nbrAnswers; $i++) {
601
			$answer = $this->new_answer[$i];
602
			$correct = $this->new_correct[$i];
603
			$comment = $this->new_comment[$i];
604
			$weighting = $this->new_weighting[$i];
605
			$position = $this->new_position[$i];
606
			$hotspot_coordinates = $this->new_hotspot_coordinates[$i];
607
			$hotspot_type = $this->new_hotspot_type[$i];
608
			$destination = $this->new_destination[$i];
609
            $autoId = $this->selectAutoId($i);
610
611
            if (!isset($this->position[$i])) {
612
                $params = [
613
                    'c_id' => $c_id,
614
                    'question_id' => $questionId,
615
                    'answer' => $answer,
616
                    'correct' => $correct,
617
                    'comment' => $comment,
618
                    'ponderation' => $weighting,
619
                    'position' => $position,
620
                    'hotspot_coordinates' => $hotspot_coordinates,
621
                    'hotspot_type' => $hotspot_type,
622
                    'destination' => $destination
623
                ];
624
                $autoId = Database::insert($answerTable, $params);
625
                if ($autoId) {
626
                    $sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $autoId";
627
                    Database::query($sql);
628
629
                    $questionType = $this->getQuestionType();
630
631
                    if (in_array(
632
                        $questionType,
633
                        [MATCHING, MATCHING_DRAGGABLE]
634
                    )) {
635
                        $answer = new Answer($this->questionId);
636
                        $answer->read();
637
638
                        $correctAnswerId = $answer->selectAnswerIdByPosition($correct);
639
                        $correctAnswerAutoId = $answer->selectAutoId($correctAnswerId);
640
641
                        Database::update(
642
                            $answerTable,
643
                            ['correct' => $correctAnswerAutoId ? $correctAnswerAutoId : 0],
644
                            ['iid = ?' => $autoId]
645
                        );
646
                    }
647
                }
648
            } else {
649
                // https://support.chamilo.org/issues/6558
650
                // function updateAnswers already escape_string, error if we do it twice.
651
                // Feed function updateAnswers with none escaped strings
652
653
                $this->updateAnswers(
654
                    $autoId,
655
                    $this->new_answer[$i],
656
                    $this->new_comment[$i],
657
                    $this->new_correct[$i],
658
                    $this->new_weighting[$i],
659
                    $this->new_position[$i],
660
                    $this->new_destination[$i],
661
                    $this->new_hotspot_coordinates[$i],
662
                    $this->new_hotspot_type[$i]
663
                );
664
            }
665
666
            $answerList[$i] = $autoId;
667
668
            if ($correct) {
669
                $correctList[$autoId] = true;
670
            }
671
        }
672
673
        $questionType = self::getQuestionType();
674
675
        if ($questionType == DRAGGABLE) {
676
            foreach ($this->new_correct as $value => $status) {
677
                if (!empty($status)) {
678
                    $correct = $answerList[$status];
679
                    $myAutoId = $answerList[$value];
680
681
                    $sql = "UPDATE $answerTable
682
                            SET correct = '$correct'
683
                            WHERE
684
                                id_auto = $myAutoId
685
                            ";
686
                    Database::query($sql);
687
                }
688
            }
689
        }
690
691
        if (count($this->position) > $this->new_nbrAnswers) {
692
            $i = $this->new_nbrAnswers + 1;
693
            while ($this->position[$i]) {
694
                $position = $this->position[$i];
695
                $sql = "DELETE FROM $answerTable
696
                		WHERE
697
                			c_id = {$this->course_id} AND
698
                			question_id = '".$questionId."' AND
699
                			position ='$position'";
700
                Database::query($sql);
701
                $i++;
702
            }
703
        }
704
705
		// moves $new_* arrays
706
		$this->answer = $this->new_answer;
707
		$this->correct = $this->new_correct;
708
		$this->comment = $this->new_comment;
709
		$this->weighting = $this->new_weighting;
710
		$this->position = $this->new_position;
711
		$this->hotspot_coordinates = $this->new_hotspot_coordinates;
712
		$this->hotspot_type = $this->new_hotspot_type;
713
714
		$this->nbrAnswers = $this->new_nbrAnswers;
715
		$this->destination = $this->new_destination;
716
		// clears $new_* arrays
717
718
		$this->cancel();
719
	}
720
721
	/**
722
	 * Duplicates answers by copying them into another question
723
	 *
724
	 * @author Olivier Brouckaert
725
	 * @param  int question id
726
     * @param  array destination course info (result of the function api_get_course_info() )
727
	 */
728
    public function duplicate($newQuestionId, $course_info = null)
729
    {
730
        if (empty($course_info)) {
731
            $course_info = $this->course;
732
        }
733
734
		$TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
735
        $fixed_list = array();
736
737
        if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
738
            self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
739
        ) {
740
            // Selecting origin options
741
            $origin_options = Question::readQuestionOption(
742
                $this->selectQuestionId(),
743
                $this->course['real_id']
744
            );
745
746
            if (!empty($origin_options)) {
747
                foreach ($origin_options as $item) {
748
            	   $new_option_list[] = $item['id'];
749
                }
750
            }
751
752
            $destination_options = Question::readQuestionOption($newQuestionId, $course_info['real_id']);
753
            $i = 0;
754
            if (!empty($destination_options)) {
755
                foreach($destination_options as $item) {
756
                    $fixed_list[$new_option_list[$i]] = $item['id'];
757
                    $i++;
758
                }
759
            }
760
        }
761
762
		// if at least one answer
763
		if ($this->nbrAnswers) {
764
			// inserts new answers into data base
765
			$c_id = $course_info['real_id'];
766
767
			for ($i=1;$i <= $this->nbrAnswers;$i++) {
768
                if ($this->course['id'] != $course_info['id']) {
769
                    $this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
770
                        $this->answer[$i],
771
                        $this->course['id'],
772
                        $course_info['id']
773
                    );
774
                    $this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
775
                        $this->comment[$i],
776
                        $this->course['id'],
777
                        $course_info['id']
778
                    );
779
                }
780
781
				$answer = $this->answer[$i];
782
				$correct = $this->correct[$i];
783
784
                if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
785
                    self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
786
                ) {
787
                    $correct = $fixed_list[intval($correct)];
788
                }
789
790
				$comment = $this->comment[$i];
791
				$weighting = $this->weighting[$i];
792
				$position = $this->position[$i];
793
				$hotspot_coordinates = $this->hotspot_coordinates[$i];
794
				$hotspot_type = $this->hotspot_type[$i];
795
				$destination = $this->destination[$i];
796
797
                $params = [
798
                    'c_id' => $c_id,
799
                    'question_id' => $newQuestionId,
800
                    'answer' => $answer,
801
                    'correct' => $correct,
802
                    'comment' => $comment,
803
                    'ponderation' => $weighting,
804
                    'position' => $position,
805
                    'hotspot_coordinates' => $hotspot_coordinates,
806
                    'hotspot_type' => $hotspot_type,
807
                    'destination' => $destination
808
                ];
809
                $id = Database::insert($TBL_REPONSES, $params);
810
811
                if ($id) {
812
                    $sql = "UPDATE $TBL_REPONSES SET id = iid, id_auto = iid WHERE iid = $id";
813
                    Database::query($sql);
814
                }
815
			}
816
        }
817
	}
818
819
    /**
820
     * Get the necessary JavaScript for some answers
821
     * @return string
822
     */
823
    public function getJs()
824
    {
825
        //if ($this->questionId == 2)
826
        return "<script>
827
                jsPlumb.ready(function() {
828
                    if ($('#drag{$this->questionId}_question').length > 0) {
829
                        MatchingDraggable.init('{$this->questionId}');
830
                    }
831
                });
832
            </script>";
833
    }
834
835
	/**
836
	 * Check if a answer is correct by an answer auto id
837
	 * @param $needle int The answer auto id
838
	 * @return bool
839
	 */
840
	public function isCorrectByAutoId($needle)
841
	{
842
		$key = 0;
843
844
		foreach ($this->autoId as $autoIdKey => $autoId) {
845
			if ($autoId == $needle) {
846
				$key = $autoIdKey;
847
			}
848
		}
849
850
		if (!$key) {
851
			return false;
852
		}
853
854
		return $this->isCorrect($key) ? true : false;
855
	}
856
}
857