Code Duplication    Length = 146-156 lines in 2 locations

main/exercice/exercise.class.php 2 locations

@@ 4254-4399 (lines=146) @@
4251
                            }
4252
                            $user_array = substr($user_array,0,-1);
4253
4254
                            if ($next) {
4255
4256
                                $user_answer = $user_array;
4257
4258
                                // we compare only the delineation not the other points
4259
                                $answer_question = $_SESSION['hotspot_coord'][1];
4260
                                $answerDestination = $_SESSION['hotspot_dest'][1];
4261
4262
                                //calculating the area
4263
                                $poly_user = convert_coordinates($user_answer, '/');
4264
                                $poly_answer = convert_coordinates($answer_question, '|');
4265
                                $max_coord = poly_get_max($poly_user, $poly_answer);
4266
                                $poly_user_compiled = poly_compile($poly_user, $max_coord);
4267
                                $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4268
                                $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
4269
4270
                                $overlap = $poly_results['both'];
4271
                                $poly_answer_area = $poly_results['s1'];
4272
                                $poly_user_area = $poly_results['s2'];
4273
                                $missing = $poly_results['s1Only'];
4274
                                $excess = $poly_results['s2Only'];
4275
4276
                                //$overlap = round(polygons_overlap($poly_answer,$poly_user));
4277
                                // //this is an area in pixels
4278
                                if ($debug > 0) {
4279
                                    error_log(__LINE__ . ' - Polygons results are ' . print_r($poly_results, 1), 0);
4280
                                }
4281
4282
                                if ($overlap < 1) {
4283
                                    //shortcut to avoid complicated calculations
4284
                                    $final_overlap = 0;
4285
                                    $final_missing = 100;
4286
                                    $final_excess = 100;
4287
                                } else {
4288
                                    // the final overlap is the percentage of the initial polygon
4289
                                    // that is overlapped by the user's polygon
4290
                                    $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
4291
                                    if ($debug > 1) {
4292
                                        error_log(__LINE__ . ' - Final overlap is ' . $final_overlap, 0);
4293
                                    }
4294
                                    // the final missing area is the percentage of the initial polygon
4295
                                    // that is not overlapped by the user's polygon
4296
                                    $final_missing = 100 - $final_overlap;
4297
                                    if ($debug > 1) {
4298
                                        error_log(__LINE__ . ' - Final missing is ' . $final_missing, 0);
4299
                                    }
4300
                                    // the final excess area is the percentage of the initial polygon's size
4301
                                    // that is covered by the user's polygon outside of the initial polygon
4302
                                    $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
4303
                                    if ($debug > 1) {
4304
                                        error_log(__LINE__ . ' - Final excess is ' . $final_excess, 0);
4305
                                    }
4306
                                }
4307
4308
                                //checking the destination parameters parsing the "@@"
4309
                                $destination_items= explode('@@', $answerDestination);
4310
                                $threadhold_total = $destination_items[0];
4311
                                $threadhold_items=explode(';',$threadhold_total);
4312
                                $threadhold1 = $threadhold_items[0]; // overlap
4313
                                $threadhold2 = $threadhold_items[1]; // excess
4314
                                $threadhold3 = $threadhold_items[2];	 //missing
4315
4316
                                // if is delineation
4317
                                if ($answerId===1) {
4318
                                    //setting colors
4319
                                    if ($final_overlap>=$threadhold1) {
4320
                                        $overlap_color=true; //echo 'a';
4321
                                    }
4322
                                    //echo $excess.'-'.$threadhold2;
4323
                                    if ($final_excess<=$threadhold2) {
4324
                                        $excess_color=true; //echo 'b';
4325
                                    }
4326
                                    //echo '--------'.$missing.'-'.$threadhold3;
4327
                                    if ($final_missing<=$threadhold3) {
4328
                                        $missing_color=true; //echo 'c';
4329
                                    }
4330
4331
                                    // if pass
4332
                                    if (
4333
                                        $final_overlap >= $threadhold1 &&
4334
                                        $final_missing <= $threadhold3 &&
4335
                                        $final_excess <= $threadhold2
4336
                                    ) {
4337
                                        $next=1; //go to the oars
4338
                                        $result_comment=get_lang('Acceptable');
4339
                                        $final_answer = 1;	// do not update with  update_exercise_attempt
4340
                                    } else {
4341
                                        $next=0;
4342
                                        $result_comment=get_lang('Unacceptable');
4343
                                        $comment=$answerDestination=$objAnswerTmp->selectComment(1);
4344
                                        $answerDestination=$objAnswerTmp->selectDestination(1);
4345
                                        //checking the destination parameters parsing the "@@"
4346
                                        $destination_items= explode('@@', $answerDestination);
4347
                                    }
4348
                                } elseif($answerId>1) {
4349
                                    if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
4350
                                        if ($debug>0) {
4351
                                            error_log(__LINE__.' - answerId is of type noerror',0);
4352
                                        }
4353
                                        //type no error shouldn't be treated
4354
                                        $next = 1;
4355
                                        continue;
4356
                                    }
4357
                                    if ($debug>0) {
4358
                                        error_log(__LINE__.' - answerId is >1 so we\'re probably in OAR',0);
4359
                                    }
4360
                                    //check the intersection between the oar and the user
4361
                                    //echo 'user';	print_r($x_user_list);		print_r($y_user_list);
4362
                                    //echo 'official';print_r($x_list);print_r($y_list);
4363
                                    //$result = get_intersection_data($x_list,$y_list,$x_user_list,$y_user_list);
4364
                                    $inter= $result['success'];
4365
4366
                                    //$delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
4367
                                    $delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
4368
4369
                                    $poly_answer = convert_coordinates($delineation_cord,'|');
4370
                                    $max_coord = poly_get_max($poly_user,$poly_answer);
4371
                                    $poly_answer_compiled = poly_compile($poly_answer,$max_coord);
4372
                                    $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled,$max_coord);
4373
4374
                                    if ($overlap == false) {
4375
                                        //all good, no overlap
4376
                                        $next = 1;
4377
                                        continue;
4378
                                    } else {
4379
                                        if ($debug>0) {
4380
                                            error_log(__LINE__.' - Overlap is '.$overlap.': OAR hit',0);
4381
                                        }
4382
                                        $organs_at_risk_hit++;
4383
                                        //show the feedback
4384
                                        $next=0;
4385
                                        $comment=$answerDestination=$objAnswerTmp->selectComment($answerId);
4386
                                        $answerDestination=$objAnswerTmp->selectDestination($answerId);
4387
4388
                                        $destination_items= explode('@@', $answerDestination);
4389
                                        $try_hotspot=$destination_items[1];
4390
                                        $lp_hotspot=$destination_items[2];
4391
                                        $select_question_hotspot=$destination_items[3];
4392
                                        $url_hotspot=$destination_items[4];
4393
                                    }
4394
                                }
4395
                            } else {	// the first delineation feedback
4396
                                if ($debug>0) {
4397
                                    error_log(__LINE__.' first',0);
4398
                                }
4399
                            }
4400
                        } elseif (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
4401
                            echo '<tr>';
4402
                            echo Display::tag('td', $answerMatching[$answerId]);
@@ 4560-4715 (lines=156) @@
4557
                            break;
4558
                        case HOT_SPOT_DELINEATION:
4559
                            $user_answer = $user_array;
4560
                            if ($next) {
4561
                                //$tbl_track_e_hotspot = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
4562
                                // Save into db
4563
                                /*	$sql = "INSERT INTO $tbl_track_e_hotspot (
4564
                                 * hotspot_user_id,
4565
                                 *  hotspot_course_code,
4566
                                 *  hotspot_exe_id,
4567
                                 *  hotspot_question_id,
4568
                                 *  hotspot_answer_id,
4569
                                 *  hotspot_correct,
4570
                                 *  hotspot_coordinate
4571
                                 *  )
4572
                                VALUES (
4573
                                 * '".Database::escape_string($_user['user_id'])."',
4574
                                 *  '".Database::escape_string($_course['id'])."',
4575
                                 *  '".Database::escape_string($exeId)."', '".Database::escape_string($questionId)."',
4576
                                 *  '".Database::escape_string($answerId)."',
4577
                                 *  '".Database::escape_string($studentChoice)."',
4578
                                 *  '".Database::escape_string($user_array)."')";
4579
                                $result = Database::query($sql,__FILE__,__LINE__);
4580
                                 */
4581
                                $user_answer = $user_array;
4582
4583
                                // we compare only the delineation not the other points
4584
                                $answer_question = $_SESSION['hotspot_coord'][1];
4585
                                $answerDestination = $_SESSION['hotspot_dest'][1];
4586
4587
                                //calculating the area
4588
                                $poly_user = convert_coordinates($user_answer, '/');
4589
                                $poly_answer = convert_coordinates($answer_question, '|');
4590
4591
                                $max_coord = poly_get_max($poly_user, $poly_answer);
4592
                                $poly_user_compiled = poly_compile($poly_user, $max_coord);
4593
                                $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4594
                                $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
4595
4596
                                $overlap = $poly_results['both'];
4597
                                $poly_answer_area = $poly_results['s1'];
4598
                                $poly_user_area = $poly_results['s2'];
4599
                                $missing = $poly_results['s1Only'];
4600
                                $excess = $poly_results['s2Only'];
4601
4602
                                //$overlap = round(polygons_overlap($poly_answer,$poly_user)); //this is an area in pixels
4603
                                if ($debug > 0) {
4604
                                    error_log(__LINE__ . ' - Polygons results are ' . print_r($poly_results, 1), 0);
4605
                                }
4606
                                if ($overlap < 1) {
4607
                                    //shortcut to avoid complicated calculations
4608
                                    $final_overlap = 0;
4609
                                    $final_missing = 100;
4610
                                    $final_excess = 100;
4611
                                } else {
4612
                                    // the final overlap is the percentage of the initial polygon that is overlapped by the user's polygon
4613
                                    $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
4614
                                    if ($debug > 1) {
4615
                                        error_log(__LINE__ . ' - Final overlap is ' . $final_overlap, 0);
4616
                                    }
4617
                                    // the final missing area is the percentage of the initial polygon that is not overlapped by the user's polygon
4618
                                    $final_missing = 100 - $final_overlap;
4619
                                    if ($debug > 1) {
4620
                                        error_log(__LINE__ . ' - Final missing is ' . $final_missing, 0);
4621
                                    }
4622
                                    // 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
4623
                                    $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
4624
                                    if ($debug > 1) {
4625
                                        error_log(__LINE__ . ' - Final excess is ' . $final_excess, 0);
4626
                                    }
4627
                                }
4628
4629
                                //checking the destination parameters parsing the "@@"
4630
                                $destination_items = explode('@@', $answerDestination);
4631
                                $threadhold_total = $destination_items[0];
4632
                                $threadhold_items = explode(';', $threadhold_total);
4633
                                $threadhold1 = $threadhold_items[0]; // overlap
4634
                                $threadhold2 = $threadhold_items[1]; // excess
4635
                                $threadhold3 = $threadhold_items[2];  //missing
4636
                                // if is delineation
4637
                                if ($answerId === 1) {
4638
                                    //setting colors
4639
                                    if ($final_overlap >= $threadhold1) {
4640
                                        $overlap_color = true; //echo 'a';
4641
                                    }
4642
                                    //echo $excess.'-'.$threadhold2;
4643
                                    if ($final_excess <= $threadhold2) {
4644
                                        $excess_color = true; //echo 'b';
4645
                                    }
4646
                                    //echo '--------'.$missing.'-'.$threadhold3;
4647
                                    if ($final_missing <= $threadhold3) {
4648
                                        $missing_color = true; //echo 'c';
4649
                                    }
4650
4651
                                    // if pass
4652
                                    if ($final_overlap >= $threadhold1 && $final_missing <= $threadhold3 && $final_excess <= $threadhold2) {
4653
                                        $next = 1; //go to the oars
4654
                                        $result_comment = get_lang('Acceptable');
4655
                                        $final_answer = 1; // do not update with  update_exercise_attempt
4656
                                    } else {
4657
                                        $next = 0;
4658
                                        $result_comment = get_lang('Unacceptable');
4659
                                        $comment = $answerDestination = $objAnswerTmp->selectComment(1);
4660
                                        $answerDestination = $objAnswerTmp->selectDestination(1);
4661
                                        //checking the destination parameters parsing the "@@"
4662
                                        $destination_items = explode('@@', $answerDestination);
4663
                                    }
4664
                                } elseif ($answerId > 1) {
4665
                                    if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
4666
                                        if ($debug > 0) {
4667
                                            error_log(__LINE__ . ' - answerId is of type noerror', 0);
4668
                                        }
4669
                                        //type no error shouldn't be treated
4670
                                        $next = 1;
4671
                                        continue;
4672
                                    }
4673
                                    if ($debug > 0) {
4674
                                        error_log(__LINE__ . ' - answerId is >1 so we\'re probably in OAR', 0);
4675
                                    }
4676
                                    //check the intersection between the oar and the user
4677
                                    //echo 'user';	print_r($x_user_list);		print_r($y_user_list);
4678
                                    //echo 'official';print_r($x_list);print_r($y_list);
4679
                                    //$result = get_intersection_data($x_list,$y_list,$x_user_list,$y_user_list);
4680
                                    $inter = $result['success'];
4681
4682
                                    //$delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
4683
                                    $delineation_cord = $objAnswerTmp->selectHotspotCoordinates($answerId);
4684
4685
                                    $poly_answer = convert_coordinates($delineation_cord, '|');
4686
                                    $max_coord = poly_get_max($poly_user, $poly_answer);
4687
                                    $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
4688
                                    $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled,$max_coord);
4689
4690
                                    if ($overlap == false) {
4691
                                        //all good, no overlap
4692
                                        $next = 1;
4693
                                        continue;
4694
                                    } else {
4695
                                        if ($debug > 0) {
4696
                                            error_log(__LINE__ . ' - Overlap is ' . $overlap . ': OAR hit', 0);
4697
                                        }
4698
                                        $organs_at_risk_hit++;
4699
                                        //show the feedback
4700
                                        $next = 0;
4701
                                        $comment = $answerDestination = $objAnswerTmp->selectComment($answerId);
4702
                                        $answerDestination = $objAnswerTmp->selectDestination($answerId);
4703
4704
                                        $destination_items = explode('@@', $answerDestination);
4705
                                        $try_hotspot = $destination_items[1];
4706
                                        $lp_hotspot = $destination_items[2];
4707
                                        $select_question_hotspot = $destination_items[3];
4708
                                        $url_hotspot=$destination_items[4];
4709
                                    }
4710
                                }
4711
                            } else {	// the first delineation feedback
4712
                                if ($debug > 0) {
4713
                                    error_log(__LINE__ . ' first', 0);
4714
                                }
4715
                            }
4716
                            break;
4717
                        case HOT_SPOT_ORDER:
4718
                            ExerciseShowFunctions::display_hotspot_order_answer(