Code Duplication    Length = 146-156 lines in 2 locations

main/exercise/exercise.class.php 2 locations

@@ 4327-4472 (lines=146) @@
4324
                            }
4325
                            $user_array = substr($user_array,0,-1);
4326
4327
                            if ($next) {
4328
                                $user_answer = $user_array;
4329
                                // we compare only the delineation not the other points
4330
                                $answer_question = $_SESSION['hotspot_coord'][1];
4331
                                $answerDestination = $_SESSION['hotspot_dest'][1];
4332
4333
                                //calculating the area
4334
                                $poly_user = convert_coordinates($user_answer, '/');
4335
                                $poly_answer = convert_coordinates($answer_question, '|');
4336
                                $max_coord = poly_get_max($poly_user, $poly_answer);
4337
                                $poly_user_compiled = poly_compile($poly_user, $max_coord);
4338
                                $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4339
                                $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
4340
4341
                                $overlap = $poly_results['both'];
4342
                                $poly_answer_area = $poly_results['s1'];
4343
                                $poly_user_area = $poly_results['s2'];
4344
                                $missing = $poly_results['s1Only'];
4345
                                $excess = $poly_results['s2Only'];
4346
4347
                                //$overlap = round(polygons_overlap($poly_answer,$poly_user));
4348
                                // //this is an area in pixels
4349
                                if ($debug > 0) {
4350
                                    error_log(__LINE__ . ' - Polygons results are ' . print_r($poly_results, 1), 0);
4351
                                }
4352
4353
                                if ($overlap < 1) {
4354
                                    //shortcut to avoid complicated calculations
4355
                                    $final_overlap = 0;
4356
                                    $final_missing = 100;
4357
                                    $final_excess = 100;
4358
                                } else {
4359
                                    // the final overlap is the percentage of the initial polygon
4360
                                    // that is overlapped by the user's polygon
4361
                                    $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
4362
                                    if ($debug > 1) {
4363
                                        error_log(__LINE__ . ' - Final overlap is ' . $final_overlap, 0);
4364
                                    }
4365
                                    // the final missing area is the percentage of the initial polygon
4366
                                    // that is not overlapped by the user's polygon
4367
                                    $final_missing = 100 - $final_overlap;
4368
                                    if ($debug > 1) {
4369
                                        error_log(__LINE__ . ' - Final missing is ' . $final_missing, 0);
4370
                                    }
4371
                                    // the final excess area is the percentage of the initial polygon's size
4372
                                    // that is covered by the user's polygon outside of the initial polygon
4373
                                    $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
4374
                                    if ($debug > 1) {
4375
                                        error_log(__LINE__ . ' - Final excess is ' . $final_excess, 0);
4376
                                    }
4377
                                }
4378
4379
                                //checking the destination parameters parsing the "@@"
4380
                                $destination_items = explode(
4381
                                    '@@',
4382
                                    $answerDestination
4383
                                );
4384
                                $threadhold_total = $destination_items[0];
4385
                                $threadhold_items = explode(
4386
                                    ';',
4387
                                    $threadhold_total
4388
                                );
4389
                                $threadhold1 = $threadhold_items[0]; // overlap
4390
                                $threadhold2 = $threadhold_items[1]; // excess
4391
                                $threadhold3 = $threadhold_items[2];     //missing
4392
4393
                                // if is delineation
4394
                                if ($answerId === 1) {
4395
                                    //setting colors
4396
                                    if ($final_overlap >= $threadhold1) {
4397
                                        $overlap_color = true; //echo 'a';
4398
                                    }
4399
                                    //echo $excess.'-'.$threadhold2;
4400
                                    if ($final_excess <= $threadhold2) {
4401
                                        $excess_color = true; //echo 'b';
4402
                                    }
4403
                                    //echo '--------'.$missing.'-'.$threadhold3;
4404
                                    if ($final_missing <= $threadhold3) {
4405
                                        $missing_color = true; //echo 'c';
4406
                                    }
4407
4408
                                    // if pass
4409
                                    if (
4410
                                        $final_overlap >= $threadhold1 &&
4411
                                        $final_missing <= $threadhold3 &&
4412
                                        $final_excess <= $threadhold2
4413
                                    ) {
4414
                                        $next=1; //go to the oars
4415
                                        $result_comment=get_lang('Acceptable');
4416
                                        $final_answer = 1;	// do not update with  update_exercise_attempt
4417
                                    } else {
4418
                                        $next=0;
4419
                                        $result_comment=get_lang('Unacceptable');
4420
                                        $comment=$answerDestination=$objAnswerTmp->selectComment(1);
4421
                                        $answerDestination=$objAnswerTmp->selectDestination(1);
4422
                                        //checking the destination parameters parsing the "@@"
4423
                                        $destination_items= explode('@@', $answerDestination);
4424
                                    }
4425
                                } elseif($answerId>1) {
4426
                                    if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
4427
                                        if ($debug>0) {
4428
                                            error_log(__LINE__.' - answerId is of type noerror',0);
4429
                                        }
4430
                                        //type no error shouldn't be treated
4431
                                        $next = 1;
4432
                                        continue;
4433
                                    }
4434
                                    if ($debug>0) {
4435
                                        error_log(__LINE__.' - answerId is >1 so we\'re probably in OAR',0);
4436
                                    }
4437
                                    //check the intersection between the oar and the user
4438
                                    //echo 'user';	print_r($x_user_list);		print_r($y_user_list);
4439
                                    //echo 'official';print_r($x_list);print_r($y_list);
4440
                                    //$result = get_intersection_data($x_list,$y_list,$x_user_list,$y_user_list);
4441
                                    $inter = $result['success'];
4442
                                    $delineation_cord = $objAnswerTmp->selectHotspotCoordinates($answerId);
4443
                                    $poly_answer = convert_coordinates($delineation_cord,'|');
4444
                                    $max_coord = poly_get_max($poly_user,$poly_answer);
4445
                                    $poly_answer_compiled = poly_compile($poly_answer,$max_coord);
4446
                                    $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled,$max_coord);
4447
4448
                                    if ($overlap == false) {
4449
                                        //all good, no overlap
4450
                                        $next = 1;
4451
                                        continue;
4452
                                    } else {
4453
                                        if ($debug>0) {
4454
                                            error_log(__LINE__.' - Overlap is '.$overlap.': OAR hit',0);
4455
                                        }
4456
                                        $organs_at_risk_hit++;
4457
                                        //show the feedback
4458
                                        $next = 0;
4459
                                        $comment = $answerDestination=$objAnswerTmp->selectComment($answerId);
4460
                                        $answerDestination=$objAnswerTmp->selectDestination($answerId);
4461
4462
                                        $destination_items= explode('@@', $answerDestination);
4463
                                        $try_hotspot = $destination_items[1];
4464
                                        $lp_hotspot = $destination_items[2];
4465
                                        $select_question_hotspot = $destination_items[3];
4466
                                        $url_hotspot = $destination_items[4];
4467
                                    }
4468
                                }
4469
                            } else {	// the first delineation feedback
4470
                                if ($debug>0) {
4471
                                    error_log(__LINE__.' first',0);
4472
                                }
4473
                            }
4474
                        } elseif (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
4475
                            echo '<tr>';
@@ 4657-4812 (lines=156) @@
4654
                            break;
4655
                        case HOT_SPOT_DELINEATION:
4656
                            $user_answer = $user_array;
4657
                            if ($next) {
4658
                                //$tbl_track_e_hotspot = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
4659
                                // Save into db
4660
                                /*	$sql = "INSERT INTO $tbl_track_e_hotspot (
4661
                                 * hotspot_user_id,
4662
                                 *  hotspot_course_code,
4663
                                 *  hotspot_exe_id,
4664
                                 *  hotspot_question_id,
4665
                                 *  hotspot_answer_id,
4666
                                 *  hotspot_correct,
4667
                                 *  hotspot_coordinate
4668
                                 *  )
4669
                                VALUES (
4670
                                 * '".Database::escape_string($_user['user_id'])."',
4671
                                 *  '".Database::escape_string($_course['id'])."',
4672
                                 *  '".Database::escape_string($exeId)."', '".Database::escape_string($questionId)."',
4673
                                 *  '".Database::escape_string($answerId)."',
4674
                                 *  '".Database::escape_string($studentChoice)."',
4675
                                 *  '".Database::escape_string($user_array)."')";
4676
                                $result = Database::query($sql,__FILE__,__LINE__);
4677
                                 */
4678
                                $user_answer = $user_array;
4679
                                // we compare only the delineation not the other points
4680
                                $answer_question = $_SESSION['hotspot_coord'][1];
4681
                                $answerDestination = $_SESSION['hotspot_dest'][1];
4682
4683
                                // calculating the area
4684
                                $poly_user = convert_coordinates($user_answer, '/');
4685
                                $poly_answer = convert_coordinates($answer_question, '|');
4686
                                $max_coord = poly_get_max($poly_user, $poly_answer);
4687
                                $poly_user_compiled = poly_compile($poly_user, $max_coord);
4688
                                $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4689
                                $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
4690
4691
                                $overlap = $poly_results['both'];
4692
                                $poly_answer_area = $poly_results['s1'];
4693
                                $poly_user_area = $poly_results['s2'];
4694
                                $missing = $poly_results['s1Only'];
4695
                                $excess = $poly_results['s2Only'];
4696
                                if ($debug > 0) {
4697
                                    error_log(__LINE__ . ' - Polygons results are ' . print_r($poly_results, 1), 0);
4698
                                }
4699
                                if ($overlap < 1) {
4700
                                    //shortcut to avoid complicated calculations
4701
                                    $final_overlap = 0;
4702
                                    $final_missing = 100;
4703
                                    $final_excess = 100;
4704
                                } else {
4705
                                    // the final overlap is the percentage of the initial polygon that is overlapped by the user's polygon
4706
                                    $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
4707
                                    if ($debug > 1) {
4708
                                        error_log(__LINE__ . ' - Final overlap is ' . $final_overlap, 0);
4709
                                    }
4710
                                    // the final missing area is the percentage of the initial polygon that is not overlapped by the user's polygon
4711
                                    $final_missing = 100 - $final_overlap;
4712
                                    if ($debug > 1) {
4713
                                        error_log(__LINE__ . ' - Final missing is ' . $final_missing, 0);
4714
                                    }
4715
                                    // the final excess area is the percentage of the initial polygon's size that is covered by the user's polygon outside of the initial polygon
4716
                                    $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
4717
                                    if ($debug > 1) {
4718
                                        error_log(__LINE__ . ' - Final excess is ' . $final_excess, 0);
4719
                                    }
4720
                                }
4721
4722
                                // Checking the destination parameters parsing the "@@"
4723
                                $destination_items = explode('@@', $answerDestination);
4724
                                $threadhold_total = $destination_items[0];
4725
                                $threadhold_items = explode(';', $threadhold_total);
4726
                                $threadhold1 = $threadhold_items[0]; // overlap
4727
                                $threadhold2 = $threadhold_items[1]; // excess
4728
                                $threadhold3 = $threadhold_items[2];  //missing
4729
                                // if is delineation
4730
                                if ($answerId === 1) {
4731
                                    //setting colors
4732
                                    if ($final_overlap >= $threadhold1) {
4733
                                        $overlap_color = true; //echo 'a';
4734
                                    }
4735
                                    //echo $excess.'-'.$threadhold2;
4736
                                    if ($final_excess <= $threadhold2) {
4737
                                        $excess_color = true; //echo 'b';
4738
                                    }
4739
                                    //echo '--------'.$missing.'-'.$threadhold3;
4740
                                    if ($final_missing <= $threadhold3) {
4741
                                        $missing_color = true; //echo 'c';
4742
                                    }
4743
4744
                                    // if pass
4745
                                    if ($final_overlap >= $threadhold1 &&
4746
                                        $final_missing <= $threadhold3 &&
4747
                                        $final_excess <= $threadhold2
4748
                                    ) {
4749
                                        $next = 1; //go to the oars
4750
                                        $result_comment = get_lang('Acceptable');
4751
                                        $final_answer = 1; // do not update with  update_exercise_attempt
4752
                                    } else {
4753
                                        $next = 0;
4754
                                        $result_comment = get_lang('Unacceptable');
4755
                                        $comment = $answerDestination = $objAnswerTmp->selectComment(1);
4756
                                        $answerDestination = $objAnswerTmp->selectDestination(1);
4757
                                        //checking the destination parameters parsing the "@@"
4758
                                        $destination_items = explode('@@', $answerDestination);
4759
                                    }
4760
                                } elseif ($answerId > 1) {
4761
                                    if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
4762
                                        if ($debug > 0) {
4763
                                            error_log(__LINE__ . ' - answerId is of type noerror', 0);
4764
                                        }
4765
                                        //type no error shouldn't be treated
4766
                                        $next = 1;
4767
                                        continue;
4768
                                    }
4769
                                    if ($debug > 0) {
4770
                                        error_log(__LINE__ . ' - answerId is >1 so we\'re probably in OAR', 0);
4771
                                    }
4772
                                    //check the intersection between the oar and the user
4773
                                    //echo 'user';	print_r($x_user_list);		print_r($y_user_list);
4774
                                    //echo 'official';print_r($x_list);print_r($y_list);
4775
                                    //$result = get_intersection_data($x_list,$y_list,$x_user_list,$y_user_list);
4776
                                    $inter = $result['success'];
4777
4778
                                    //$delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
4779
                                    $delineation_cord = $objAnswerTmp->selectHotspotCoordinates($answerId);
4780
4781
                                    $poly_answer = convert_coordinates($delineation_cord, '|');
4782
                                    $max_coord = poly_get_max($poly_user, $poly_answer);
4783
                                    $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4784
                                    $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled,$max_coord);
4785
4786
                                    if ($overlap == false) {
4787
                                        //all good, no overlap
4788
                                        $next = 1;
4789
                                        continue;
4790
                                    } else {
4791
                                        if ($debug > 0) {
4792
                                            error_log(__LINE__ . ' - Overlap is ' . $overlap . ': OAR hit', 0);
4793
                                        }
4794
                                        $organs_at_risk_hit++;
4795
                                        //show the feedback
4796
                                        $next = 0;
4797
                                        $comment = $answerDestination = $objAnswerTmp->selectComment($answerId);
4798
                                        $answerDestination = $objAnswerTmp->selectDestination($answerId);
4799
4800
                                        $destination_items = explode('@@', $answerDestination);
4801
                                        $try_hotspot = $destination_items[1];
4802
                                        $lp_hotspot = $destination_items[2];
4803
                                        $select_question_hotspot = $destination_items[3];
4804
                                        $url_hotspot=$destination_items[4];
4805
                                    }
4806
                                }
4807
                            } else {	// the first delineation feedback
4808
                                if ($debug > 0) {
4809
                                    error_log(__LINE__ . ' first', 0);
4810
                                }
4811
                            }
4812
                            break;
4813
                        case HOT_SPOT_ORDER:
4814
                            ExerciseShowFunctions::display_hotspot_order_answer(
4815
                                $feedback_type,