Code Duplication    Length = 49-51 lines in 2 locations

main/exercice/exercise.class.php 2 locations

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