Completed
Push — 1.10.x ( 484d85...fbe198 )
by Julito
35:25
created

Answer::updateAnswers()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
c 0
b 0
f 0
nc 1
nop 9
dl 0
loc 26
rs 8.8571

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
740
	 */
741
    public function duplicate($newQuestion, $course_info = null)
742
    {
743
         $newQuestionId = $newQuestion->id;
744
745
        if (empty($course_info)) {
746
            $course_info = $this->course;
747
        }
748
749
        $fixed_list = array();
750
        $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
751
752
        if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
753
            self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
754
        ) {
755
            // Selecting origin options
756
            $origin_options = Question::readQuestionOption(
757
                $this->selectQuestionId(),
758
                $this->course['real_id']
759
            );
760
761
            if (!empty($origin_options)) {
762
                foreach ($origin_options as $item) {
763
            	   $new_option_list[] = $item['id'];
764
                }
765
            }
766
767
            $destination_options = Question::readQuestionOption(
768
                $newQuestionId,
769
                $course_info['real_id']
770
            );
771
            $i = 0;
772
            if (!empty($destination_options)) {
773
                foreach($destination_options as $item) {
774
                    $fixed_list[$new_option_list[$i]] = $item['id'];
775
                    $i++;
776
                }
777
            }
778
        }
779
780
		// if at least one answer
781
		if ($this->nbrAnswers) {
782
			// inserts new answers into data base
783
			$courseId = $course_info['real_id'];
784
			$correctAnswers = [];
785
			$onlyAnswers = [];
786
			$allAnswers = [];
787
788
			$em = Database::getManager();
789
790
            if (in_array($newQuestion->type, [MATCHING, MATCHING_DRAGGABLE])) {
791
                $temp = array();
792
                for ($i = 1; $i <= $this->nbrAnswers; $i++) {
793
                    $answer = [
794
                        'id' => $this->id[$i],
795
                        'answer' => $this->answer[$i],
796
                        'correct' => $this->correct[$i],
797
                        'comment' => $this->comment[$i],
798
                        'weighting' => $this->weighting[$i],
799
                        'ponderation' => $this->weighting[$i],
800
                        'position' => $this->position[$i],
801
                        'hotspot_coordinates' => $this->hotspot_coordinates[$i],
802
                        'hotspot_type' => $this->hotspot_type[$i],
803
                        'destination' => $this->destination[$i],
804
                    ];
805
                    $temp[$answer['position']] = $answer;
806
                    $allAnswers[$this->id[$i]] = $this->answer[$i];
807
                }
808
809
                foreach ($temp as $index => $answer) {
810
                    if ($this->course['id'] != $course_info['id']) {
811
                        // check resources inside html from ckeditor tool and copy correct urls into recipient course
812
                        $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
813
                            $answer['answer'],
814
                            $this->course['id'],
815
                            $course_info['id']
816
                        );
817
818
                        $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
819
                            $answer['comment'],
820
                            $this->course['id'],
821
                            $course_info['id']
822
                        );
823
                    }
824
825
                    $quizAnswer = new CQuizAnswer();
826
                    $quizAnswer
827
                        ->setCId($courseId)
828
                        ->setQuestionId($newQuestionId)
829
                        ->setAnswer($answer['answer'])
830
                        ->setCorrect($answer['correct'])
831
                        ->setComment($answer['comment'])
832
                        ->setPonderation($answer['ponderation'])
833
                        ->setPosition($answer['position'])
834
                        ->setHotspotCoordinates($answer['hotspot_coordinates'])
835
                        ->setHotspotType($answer['hotspot_type'])
836
                        ->setIdAuto(0);
837
838
                    $em->persist($quizAnswer);
839
                    $em->flush();
840
841
                    $answerId = $quizAnswer->getIid();
842
843 View Code Duplication
                    if ($answerId) {
844
                        $quizAnswer
845
                            ->setId($answerId)
846
                            ->setIdAuto($answerId);
847
848
                        $em->merge($quizAnswer);
849
                        $em->flush();
850
851
                        $correctAnswers[$answerId] = $answer['correct'];
852
                        $onlyAnswers[$answerId] = $answer['answer'];
853
                    }
854
				}
855
            } else {
856
                for ($i = 1; $i <= $this->nbrAnswers; $i++) {
857
                    if ($this->course['id'] != $course_info['id']) {
858
                        $this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
859
                            $this->answer[$i],
860
                            $this->course['id'],
861
                            $course_info['id']
862
                        );
863
                        $this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
864
                            $this->comment[$i],
865
                            $this->course['id'],
866
                            $course_info['id']
867
                        );
868
                    }
869
870
                    $correct = $this->correct[$i];
871
                    if ($newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE ||
872
                        $newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE
873
                    ) {
874
                        $correct = $fixed_list[intval($correct)];
875
                    }
876
877
                    $quizAnswer = new CQuizAnswer();
878
                    $quizAnswer
879
                        ->setCId($courseId)
880
                        ->setQuestionId($newQuestionId)
881
                        ->setAnswer($this->answer[$i])
882
                        ->setCorrect($correct)
883
                        ->setComment($this->comment[$i])
884
                        ->setPonderation($this->weighting[$i])
885
                        ->setPosition($this->position[$i])
886
                        ->setHotspotCoordinates($this->hotspot_coordinates[$i])
887
                        ->setHotspotType($this->hotspot_type[$i])
888
                        ->setDestination($this->destination[$i]);
889
890
                    $em->persist($quizAnswer);
891
                    $em->flush();
892
893
                    $answerId = $quizAnswer->getIid();
894
                    $quizAnswer
895
                        ->setId($answerId)
896
                        ->setIdAuto($answerId);
897
898
                    $em->merge($quizAnswer);
899
                    $em->flush();
900
901
                    $correctAnswers[$answerId] = $correct;
902
                    $onlyAnswers[$answerId] = $this->answer[$i];
903
                    $allAnswers[$this->id[$i]] = $this->answer[$i];
904
                }
905
            }
906
907
            // Fix correct answers
908 View Code Duplication
            if (in_array($newQuestion->type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
909
                $onlyAnswersFlip = array_flip($onlyAnswers);
910
                foreach ($correctAnswers as $answer_id => $correct_answer) {
911
                    $params = array();
912
                    if (isset($allAnswers[$correct_answer]) &&
913
                        isset($onlyAnswersFlip[$allAnswers[$correct_answer]])
914
                    ) {
915
                        $params['correct'] = $onlyAnswersFlip[$allAnswers[$correct_answer]];
916
                        Database::update(
917
                            $tableAnswer,
918
                            $params,
919
                            array(
920
                                'id = ? AND c_id = ? AND question_id = ? ' => array(
921
                                    $answer_id,
922
                                    $courseId,
923
                                    $newQuestionId,
924
                                ),
925
                            )
926
                        );
927
                    }
928
                }
929
            }
930
        }
931
	}
932
933
    /**
934
     * Get the necessary JavaScript for some answers
935
     * @return string
936
     */
937
    public function getJs()
938
    {
939
        //if ($this->questionId == 2)
940
        return "<script>
941
                jsPlumb.ready(function() {
942
                    if ($('#drag{$this->questionId}_question').length > 0) {
943
                        MatchingDraggable.init('{$this->questionId}');
944
                    }
945
                });
946
            </script>";
947
    }
948
949
}
950