Code Duplication    Length = 146-156 lines in 2 locations

main/exercise/exercise.class.php 2 locations

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