Code Duplication    Length = 146-156 lines in 2 locations

main/exercice/exercise.class.php 2 locations

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