Code Duplication    Length = 146-156 lines in 2 locations

main/exercise/exercise.class.php 2 locations

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