Completed
Push — 1.10.x ( c11148...3f49cd )
by Angel Fernando Quiroz
162:29 queued 117:01
created

Answer::getAnswerByAutoId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

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