Code Duplication    Length = 49-51 lines in 2 locations

main/exercice/exercise.class.php 2 locations

@@ 3520-3570 (lines=51) @@
3517
                        //initialise answer tags
3518
                        $user_tags = $correct_tags = $real_text = array();
3519
                        // the loop will stop at the end of the text
3520
                        while (1) {
3521
                            // quits the loop if there are no more blanks (detect '[')
3522
                            if (($pos = api_strpos($temp, '[')) === false) {
3523
                                // adds the end of the text
3524
                                $answer = $temp;
3525
                                $real_text[] = $answer;
3526
                                break; //no more "blanks", quit the loop
3527
                            }
3528
                            // adds the piece of text that is before the blank
3529
                            //and ends with '[' into a general storage array
3530
                            $real_text[] = api_substr($temp, 0, $pos +1);
3531
                            $answer .= api_substr($temp, 0, $pos +1);
3532
                            //take the string remaining (after the last "[" we found)
3533
                            $temp = api_substr($temp, $pos +1);
3534
                            // quit the loop if there are no more blanks, and update $pos to the position of next ']'
3535
                            if (($pos = api_strpos($temp, ']')) === false) {
3536
                                // adds the end of the text
3537
                                $answer .= $temp;
3538
                                break;
3539
                            }
3540
                            if ($from_database) {
3541
                                $queryfill = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
3542
                                          WHERE
3543
                                            exe_id = '".$exeId."' AND
3544
                                            question_id= ".intval($questionId)."";
3545
                                $resfill = Database::query($queryfill);
3546
                                $str = Database::result($resfill, 0, 'answer');
3547
                                api_preg_match_all('#\[([^[]*)\]#', $str, $arr);
3548
                                $str = str_replace('\r\n', '', $str);
3549
3550
                                $choice = $arr[1];
3551
                                if (isset($choice[$j])) {
3552
                                    $tmp = api_strrpos($choice[$j], ' / ');
3553
                                    $choice[$j] = api_substr($choice[$j], 0, $tmp);
3554
                                    $choice[$j] = trim($choice[$j]);
3555
                                    // Needed to let characters ' and " to work as part of an answer
3556
                                    $choice[$j] = stripslashes($choice[$j]);
3557
                                } else {
3558
                                    $choice[$j] = null;
3559
                                }
3560
                            } else {
3561
                                // This value is the user input, not escaped while correct answer is escaped by fckeditor
3562
                                $choice[$j] = api_htmlentities(trim($choice[$j]));
3563
                            }
3564
3565
                            $user_tags[] = $choice[$j];
3566
                            //put the contents of the [] answer tag into correct_tags[]
3567
                            $correct_tags[] = api_substr($temp, 0, $pos);
3568
                            $j++;
3569
                            $temp = api_substr($temp, $pos +1);
3570
                        }
3571
                        $answer = '';
3572
                        $real_correct_tags = $correct_tags;
3573
                        $chosen_list = array();
@@ 3748-3796 (lines=49) @@
3745
                    //initialise answer tags
3746
                    $userTags = $correctTags = $realText = array();
3747
                    // the loop will stop at the end of the text
3748
                    while (1) {
3749
                        // quits the loop if there are no more blanks (detect '[')
3750
                        if (($pos = api_strpos($temp, '[')) === false) {
3751
                            // adds the end of the text
3752
                            $answer = $temp;
3753
                            $realText[] = $answer;
3754
                            break; //no more "blanks", quit the loop
3755
                        }
3756
                        // adds the piece of text that is before the blank
3757
                        //and ends with '[' into a general storage array
3758
                        $realText[] = api_substr($temp, 0, $pos +1);
3759
                        $answer .= api_substr($temp, 0, $pos +1);
3760
                        //take the string remaining (after the last "[" we found)
3761
                        $temp = api_substr($temp, $pos +1);
3762
                        // quit the loop if there are no more blanks, and update $pos to the position of next ']'
3763
                        if (($pos = api_strpos($temp, ']')) === false) {
3764
                            // adds the end of the text
3765
                            $answer .= $temp;
3766
                            break;
3767
                        }
3768
                        if ($from_database) {
3769
                            $queryfill = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
3770
                                          WHERE
3771
                                            exe_id = '".$exeId."' AND
3772
                                            question_id= ".intval($questionId)."";
3773
                            $resfill = Database::query($queryfill);
3774
                            $str = Database::result($resfill, 0, 'answer');
3775
                            api_preg_match_all('#\[([^[]*)\]#', $str, $arr);
3776
                            $str = str_replace('\r\n', '', $str);
3777
                            $choice = $arr[1];
3778
                            if (isset($choice[$j])) {
3779
                                $tmp = api_strrpos($choice[$j], ' / ');
3780
                                $choice[$j] = api_substr($choice[$j], 0, $tmp);
3781
                                $choice[$j] = trim($choice[$j]);
3782
                                // Needed to let characters ' and " to work as part of an answer
3783
                                $choice[$j] = stripslashes($choice[$j]);
3784
                            } else {
3785
                                $choice[$j] = null;
3786
                            }
3787
                        } else {
3788
                            // This value is the user input, not escaped while correct answer is escaped by fckeditor
3789
                            $choice[$j] = api_htmlentities(trim($choice[$j]));
3790
                        }
3791
                        $userTags[] = $choice[$j];
3792
                        //put the contents of the [] answer tag into correct_tags[]
3793
                        $correctTags[] = api_substr($temp, 0, $pos);
3794
                        $j++;
3795
                        $temp = api_substr($temp, $pos +1);
3796
                    }
3797
                    $answer = '';
3798
                    $realCorrectTags = $correctTags;
3799
                    for ($i = 0; $i < count($realCorrectTags); $i++) {