Completed
Pull Request — 1.10.x (#1334)
by
unknown
426:55 queued 372:12
created

Answer::save()   F

Complexity

Conditions 20
Paths 8196

Size

Total Lines 129
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

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