exercise_submit_modal.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. $language_file=array('exercice');
  3. include_once('../inc/global.inc.php');
  4. include api_get_path(INCLUDE_PATH) . 'reduced_header.inc.php';
  5. include_once(api_get_path(LIBRARY_PATH).'geometry.lib.php');
  6. $dbg_local = 0;
  7. // answer types
  8. define('UNIQUE_ANSWER', 1);
  9. define('MULTIPLE_ANSWER', 2);
  10. define('FILL_IN_BLANKS', 3);
  11. define('MATCHING', 4);
  12. define('FREE_ANSWER', 5);
  13. define('HOT_SPOT', 6);
  14. define('HOT_SPOT_ORDER', 7);
  15. define('HOT_SPOT_DELINEATION', 8);
  16. require_once('exercise.class.php');
  17. require_once('question.class.php');
  18. require_once('answer.class.php');
  19. require_once('exercise.lib.php');
  20. if ( empty ( $exerciseResult ) ) {
  21. $exerciseResult = $_SESSION['exerciseResult'];
  22. }
  23. if ( empty ( $exerciseResultCoordinates ) ) {
  24. $exerciseResultCoordinates = $_REQUEST['exerciseResultCoordinates'];
  25. }
  26. $_SESSION['hotspot_coord']=array();
  27. $newquestionList=$_SESSION['newquestionList'];
  28. $questionList = $_SESSION['questionList'];
  29. $exerciseId=$_GET['exerciseId'];
  30. $exerciseType=$_GET['exerciseType'];
  31. $questionNum=$_GET['questionnum'];
  32. $nbrQuestions=$_GET['nbrQuestions'];
  33. //round-up the coordinates
  34. $coords = explode('/',$_GET['hotspot']);
  35. $user_array = '';
  36. foreach ($coords as $coord) {
  37. list($x,$y) = explode(';',$coord);
  38. $user_array .= round($x).';'.round($y).'/';
  39. }
  40. $user_array = substr($user_array,0,-1);
  41. if ( isset ( $_GET['choice'] ) )
  42. {
  43. $choice_value = $_GET['choice'];
  44. }
  45. // getting the options by js
  46. if (empty($choice_value) )
  47. {
  48. echo '<script type="text/javascript">'."
  49. // this works for only radio buttons
  50. var f= self.parent.window.document.frm_exercise;
  51. var choice_js='';
  52. var hotspot = new Array();
  53. var hotspotcoord = new Array();
  54. var counter=0;
  55. for( var i = 0; i < f.elements.length; i++ )
  56. {
  57. if (f.elements[i].type=='radio' && f.elements[i].checked)
  58. {
  59. //alert( f.elements[i].name);
  60. choice_js = f.elements[i].value;
  61. counter ++;
  62. }
  63. if (f.elements[i].type=='hidden' )
  64. {
  65. name = f.elements[i].name;
  66. if (name.substr(0,7)=='hotspot')
  67. hotspot.push(f.elements[i].value);
  68. if (name.substr(0,20)=='hotspot_coordinates')
  69. hotspotcoord.push(f.elements[i].value);
  70. //hotspot = f.elements[i].value;
  71. }
  72. }
  73. if (counter==0)
  74. {
  75. choice_js=-1; // this is an error
  76. }
  77. //alert(choice_js);
  78. ";
  79. echo 'window.location.href = "exercise_submit_modal.php?hotspotcoord="+ hotspotcoord + "&hotspot="+ hotspot + "&choice="+ choice_js + "&exerciseId='.$exerciseId.'&questionnum='.$questionNum.'&exerciseType='.$exerciseType.'";</script>';
  80. }
  81. $choice=array();
  82. $questionid= $questionList[$questionNum];
  83. // $choice_value => value of the user selection
  84. $choice[$questionid]=$choice_value;
  85. // initializing
  86. if(!is_array($exerciseResult))
  87. {
  88. $exerciseResult=array();
  89. }
  90. // if the user has answered at least one question
  91. if(is_array($choice))
  92. {
  93. if($exerciseType == 1)
  94. {
  95. // $exerciseResult receives the content of the form.
  96. // Each choice of the student is stored into the array $choice
  97. $exerciseResult=$choice;
  98. }
  99. else
  100. {
  101. // gets the question ID from $choice. It is the key of the array
  102. list($key)=array_keys($choice);
  103. // if the user didn't already answer this question
  104. if(!isset($exerciseResult[$key]))
  105. {
  106. // stores the user answer into the array
  107. $exerciseResult[$key]=$choice[$key];
  108. }
  109. }
  110. }
  111. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  112. api_session_register('exerciseResult');
  113. api_session_register('exerciseResultCoordinates');
  114. /*
  115. // if it is the last question (only for a sequential exercise)
  116. if($questionNum >= $nbrQuestions)
  117. {
  118. if($debug>0){echo str_repeat('&nbsp;',0).'Redirecting to exercise_result.php - Remove debug option to let this happen'."<br />\n";}
  119. // goes to the script that will show the result of the exercise
  120. // header("Location: exercise_result.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  121. // echo 'location result';
  122. }*/
  123. // gets the student choice for this question
  124. //print_r($choice); echo "<br>";
  125. // creates a temporary Question object
  126. if (in_array($questionid,$questionList))
  127. {
  128. $objQuestionTmp = Question :: read($questionid);
  129. $questionName=$objQuestionTmp->selectTitle();
  130. $questionDescription=$objQuestionTmp->selectDescription();
  131. $questionWeighting=$objQuestionTmp->selectWeighting();
  132. $answerType=$objQuestionTmp->selectType();
  133. $quesId =$objQuestionTmp->selectId(); //added by priya saini
  134. }
  135. $objAnswerTmp=new Answer($questionid);
  136. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  137. //echo 'answe_type '.$answerType;echo '<br />';
  138. if($answerType == FREE_ANSWER)
  139. $nbrAnswers = 1;
  140. $choice=$exerciseResult[$questionid];
  141. $destination=array();
  142. $comment='';
  143. $next=1;
  144. $_SESSION['hotspot_coord']=array();
  145. $_SESSION['hotspot_dest']=array();
  146. $overlap_color=$missing_color=$excess_color=false;
  147. $organs_at_risk_hit=0;
  148. if (!empty($choice_value))
  149. {
  150. for($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  151. $answer=$objAnswerTmp->selectAnswer($answerId);
  152. $answerComment=$objAnswerTmp->selectComment($answerId);
  153. $answerDestination=$objAnswerTmp->selectDestination($answerId);
  154. $answerCorrect=$objAnswerTmp->isCorrect($answerId);
  155. $answerWeighting=$objAnswerTmp->selectWeighting($answerId);
  156. //delineation
  157. $delineation_cord=$objAnswerTmp->selectHotspotCoordinates(1);
  158. $answer_delineation_destination=$objAnswerTmp->selectDestination(1);
  159. if ($dbg_local>0) { error_log(__LINE__.' answerId: '.$answerId.'('.$answerType.') - user delineation_cord: '.$delineation_cord.' - $answer_delineation_destination: '.$answer_delineation_destination,0);}
  160. switch($answerType) {
  161. // for unique answer
  162. case UNIQUE_ANSWER :
  163. $studentChoice=($choice_value == $answerId)?1:0;
  164. if($studentChoice) {
  165. $questionScore+=$answerWeighting;
  166. $totalScore+=$answerWeighting;
  167. $newquestionList[]=$questionid;
  168. }
  169. break;
  170. case HOT_SPOT_DELINEATION : $studentChoice=$choice[$answerId];
  171. if($studentChoice) {
  172. $questionScore+=$answerWeighting;
  173. $totalScore+=$answerWeighting;
  174. $newquestionList[]=$questionid;
  175. }
  176. if ($answerId===1) {
  177. $_SESSION['hotspot_coord'][1]=$delineation_cord;
  178. $_SESSION['hotspot_dest'][1]=$answer_delineation_destination;
  179. }
  180. break;
  181. }
  182. if($answerType != MATCHING || $answerCorrect)
  183. {
  184. if($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER)
  185. {
  186. //display_unique_or_multiple_answer($answerType, $studentChoice, $answer, $answerComment, $answerCorrect);
  187. //echo $questionScore;
  188. if ($studentChoice)
  189. {
  190. $destination=$answerDestination;
  191. $comment=$answerComment;
  192. }
  193. }
  194. elseif($answerType == HOT_SPOT_DELINEATION)
  195. {
  196. if ($next)
  197. {
  198. if ($dbg_local>0) { error_log(__LINE__.' - next',0);}
  199. $tbl_track_e_hotspot = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  200. // Save into db
  201. $sql = "INSERT INTO $tbl_track_e_hotspot (hotspot_user_id, hotspot_course_code, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate )
  202. VALUES ('".Database::escape_string($_user['user_id'])."', '".Database::escape_string($_course['id'])."', '".Database::escape_string($exeId)."', '".Database::escape_string($questionId)."', '".Database::escape_string($answerId)."', '".Database::escape_string($studentChoice)."', '".Database::escape_string($user_array)."')";
  203. $result = api_sql_query($sql,__FILE__,__LINE__);
  204. $user_answer = $user_array;
  205. //$_SESSION['exerciseResultCoordinates'][$questionId]=$exerciseResultCoordinates;
  206. // we compare only the delineation not the other points
  207. $answer_question= $_SESSION['hotspot_coord'][1];
  208. $answerDestination= $_SESSION['hotspot_dest'][1];
  209. $poly_user = convert_coordinates($user_answer,'/');
  210. $poly_answer = convert_coordinates($answer_question,'|');
  211. $max_coord = poly_get_max($poly_user,$poly_answer);
  212. $poly_user_compiled = poly_compile($poly_user,$max_coord);
  213. $poly_answer_compiled = poly_compile($poly_answer,$max_coord);
  214. $poly_results = poly_result($poly_answer_compiled,$poly_user_compiled,$max_coord);
  215. $overlap = $poly_results['both'];
  216. $poly_answer_area = $poly_results['s1'];
  217. $poly_user_area = $poly_results['s2'];
  218. $missing = $poly_results['s1Only'];
  219. $excess = $poly_results['s2Only'];
  220. //$overlap = round(polygons_overlap($poly_answer,$poly_user)); //this is an area in pixels
  221. if ($dbg_local>0) { error_log(__LINE__.' - Polygons results are '.print_r($poly_results,1),0);}
  222. if ($overlap < 1) {
  223. //shortcut to avoid complicated calculations
  224. $final_overlap = 0;
  225. $final_missing = 100;
  226. $final_excess = 100;
  227. } else {
  228. // the final overlap is the percentage of the initial polygon that is overlapped by the user's polygon
  229. $final_overlap = round(((float)$overlap / (float)$poly_answer_area)*100);
  230. if ($dbg_local>1) { error_log(__LINE__.' - Final overlap is '.$final_overlap,0);}
  231. // the final missing area is the percentage of the initial polygon that is not overlapped by the user's polygon
  232. $final_missing = 100 - $final_overlap;
  233. if ($dbg_local>1) { error_log(__LINE__.' - Final missing is '.$final_missing,0);}
  234. // 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
  235. $final_excess = round((((float)$poly_user_area-(float)$overlap)/(float)$poly_answer_area)*100);
  236. if ($dbg_local>1) { error_log(__LINE__.' - Final excess is '.$final_excess,0);}
  237. }
  238. $destination_items= explode('@@', $answerDestination);
  239. $threadhold_total = $destination_items[0];
  240. $threadhold_items=explode(';',$threadhold_total);
  241. $threadhold1 = $threadhold_items[0]; // overlap
  242. $threadhold2 = $threadhold_items[1]; // excess
  243. $threadhold3 = $threadhold_items[2]; //missing
  244. // if is delineation
  245. if ($answerId===1)
  246. {
  247. //setting colors
  248. if ($final_overlap>=$threadhold1)
  249. {
  250. $overlap_color=true; //echo 'a';
  251. }
  252. //echo $excess.'-'.$threadhold2;
  253. if ($final_excess<=$threadhold2)
  254. {
  255. $excess_color=true; //echo 'b';
  256. }
  257. //echo '--------'.$missing.'-'.$threadhold3;
  258. if ($final_missing<=$threadhold3)
  259. {
  260. $missing_color=true; //echo 'c';
  261. }
  262. // if pass
  263. if ($final_overlap>=$threadhold1 && $final_missing<=$threadhold2 && $final_excess<=$threadhold3)
  264. {
  265. $next=1; //go to the oars
  266. $result_comment=get_lang('Acceptable');
  267. }
  268. else
  269. {
  270. $next=0;
  271. $result_comment=get_lang('Unacceptable');
  272. $comment=$answerDestination=$objAnswerTmp->selectComment(1);
  273. $answerDestination=$objAnswerTmp->selectDestination(1);
  274. $destination_items= explode('@@', $answerDestination);
  275. $try_hotspot=$destination_items[1];
  276. $lp_hotspot=$destination_items[2];
  277. $select_question_hotspot=$destination_items[3];
  278. $url_hotspot=$destination_items[4];
  279. //echo 'show the feedback';
  280. }
  281. }
  282. elseif($answerId>1)
  283. {
  284. if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
  285. if ($dbg_local>0) { error_log(__LINE__.' - answerId is of type noerror',0);}
  286. //type no error shouldn't be treated
  287. $next = 1;
  288. continue;
  289. }
  290. if ($dbg_local>0) { error_log(__LINE__.' - answerId is >1 so we\'re probably in OAR',0);}
  291. //check the intersection between the oar and the user
  292. //echo 'user'; print_r($x_user_list); print_r($y_user_list);
  293. //echo 'official';print_r($x_list);print_r($y_list);
  294. //$result = get_intersection_data($x_list,$y_list,$x_user_list,$y_user_list);
  295. $inter= $result['success'];
  296. //$delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
  297. $delineation_cord=$objAnswerTmp->selectHotspotCoordinates($answerId);
  298. $poly_answer = convert_coordinates($delineation_cord,'|');
  299. $max_coord = poly_get_max($poly_user,$poly_answer);
  300. $poly_answer_compiled = poly_compile($poly_answer,$max_coord);
  301. $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled,$max_coord);
  302. if ($overlap == false) {
  303. //all good, no overlap
  304. $next = 1;
  305. continue;
  306. } else {
  307. if ($dbg_local>0) { error_log(__LINE__.' - Overlap is '.$overlap.': OAR hit',0);}
  308. $organs_at_risk_hit++;
  309. //show the feedback
  310. $next=0;
  311. $comment=$answerDestination=$objAnswerTmp->selectComment($answerId);
  312. $answerDestination=$objAnswerTmp->selectDestination($answerId);
  313. $destination_items= explode('@@', $answerDestination);
  314. $try_hotspot=$destination_items[1];
  315. $lp_hotspot=$destination_items[2];
  316. $select_question_hotspot=$destination_items[3];
  317. $url_hotspot=$destination_items[4];
  318. }
  319. }
  320. }
  321. else
  322. { // the first delineation feedback
  323. if ($dbg_local>0) { error_log(__LINE__.' first',0);}
  324. //we send the error
  325. }
  326. }
  327. }
  328. }
  329. if ($overlap_color) {
  330. $overlap_color='green';
  331. } else {
  332. $overlap_color='red';
  333. }
  334. if ($missing_color) {
  335. $missing_color='green';
  336. } else {
  337. $missing_color='red';
  338. }
  339. if ($excess_color) {
  340. $excess_color='green';
  341. } else {
  342. $excess_color='red';
  343. }
  344. $table_resume='<table class="data_table" >
  345. <tr class="row_odd" >
  346. <td></td>
  347. <td ><b>'.get_lang('Required').'</b></td>
  348. <td><b>'.get_lang('YourAnswer').'</b></td>
  349. </tr>
  350. <tr class="row_even">
  351. <td><b>'.get_lang('Overlap').'</b></td>
  352. <td>'.get_lang('Min').' '.$threadhold1.'</td>
  353. <td><div style="color:'.$overlap_color.'">'.$final_overlap.'</div></td>
  354. </tr>
  355. <tr class="row_even">
  356. <td><b>'.get_lang('Missing').'</b></td>
  357. <td>'.get_lang('Max').' '.$threadhold3.'</td>
  358. <td><div style="color:'.$missing_color.'">'.$final_missing.'</div></td>
  359. </tr>
  360. <tr>
  361. <td><b>'.get_lang('Excess').'</b></td>
  362. <td>'.get_lang('Max').' '.$threadhold2.'</td>
  363. <td><div style="color:'.$excess_color.'">'.$final_excess.'</div></td>
  364. </tr>
  365. </table>';
  366. }
  367. $_SESSION['newquestionList']=$newquestionList;
  368. if ($choice_value==-1)
  369. {
  370. $links. '<a href="#" onclick="self.parent.tb_remove();">'.get_lang('ChooseAnAnswer').'</a>';
  371. }
  372. if ($answerType!= HOT_SPOT_DELINEATION)
  373. {
  374. $item_list=explode('@@',$destination);
  375. //print_R($item_list);
  376. $try = $item_list[0];
  377. $lp = $item_list[1];
  378. $destinationid= $item_list[2];
  379. $url=$item_list[3];
  380. $table_resume='';
  381. }
  382. else
  383. {
  384. if ($next==0) {
  385. $try = $try_hotspot;
  386. $lp = $lp_hotspot;
  387. $destinationid= $select_question_hotspot;
  388. $url=$url_hotspot;
  389. } else {
  390. //show if no error
  391. //echo 'no error';
  392. $comment=$answerComment=$objAnswerTmp->selectComment($nbrAnswers);
  393. $answerDestination=$objAnswerTmp->selectDestination($nbrAnswers);
  394. //we send the error
  395. $destination_items= explode('@@', $answerDestination);
  396. $try=$destination_items[1];
  397. $lp=$destination_items[2];
  398. $destinationid=$destination_items[3];
  399. $url=$destination_items[4];
  400. }
  401. }
  402. //$pre_list_destination=explode(';',$list_dest);
  403. /*
  404. $destination_list=array();
  405. foreach($pre_list_destination as $value)
  406. {
  407. if ($value!='')
  408. $destination_list[]=$value;
  409. }*/
  410. //echo '<pre>';print_r($destination);
  411. $links='';
  412. // the link to retry the question
  413. if ($try==1)
  414. {
  415. $num_value_array= (array_keys($questionList, $questionid));
  416. $links.= Display :: return_icon('reload.gif', '', array ('style' => 'padding-left:0px;padding-right:5px;')).'<a onclick="SendEx('.$num_value_array[0].');" href="#">'.get_lang('TryAgain').'</a><br /><br />';
  417. }
  418. // the link to theory (a learning path)
  419. if (!empty($lp))
  420. {
  421. $lp_url= api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp;
  422. require_once('../newscorm/learnpathList.class.php');
  423. $list = new LearnpathList(api_get_user_id());
  424. $flat_list = $list->get_flat_list();
  425. $links.= Display :: return_icon('theory.gif', '', array ('style' => 'padding-left:0px;padding-right:5px;')).'<a target="_blank" href="'.$lp_url.'">'.get_lang('SeeTheory').'</a><br />';
  426. }
  427. $links.='<br />';
  428. // the link to an external website or link
  429. if (!empty($url)) {
  430. $links.= Display :: return_icon('link.gif', '', array ('style' => 'padding-left:0px;padding-right:5px;')).'<a target="_blank" href="'.$url.'">'.get_lang('VisitUrl').'</a><br /><br />';
  431. }
  432. // the link to finish the test
  433. if ($destinationid==-1)
  434. {
  435. $links.= Display :: return_icon('finish.gif', '', array ('style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;')).'<a onclick="SendEx(-1);" href="#">'.get_lang('EndActivity').'</a><br /><br />';
  436. }
  437. // the link to other question
  438. else
  439. {
  440. if (in_array($destinationid,$questionList))
  441. {
  442. $objQuestionTmp = Question :: read($destinationid);
  443. $questionName=$objQuestionTmp->selectTitle();
  444. $num_value_array= (array_keys($questionList, $destinationid));
  445. $links.= Display :: return_icon('quiz.gif', '', array ('style' => 'padding-left:0px;padding-right:5px;')).'<a onclick="SendEx('.$num_value_array[0].');" href="#">'.get_lang('GoToQuestion').' '.$num_value_array[0].'</a><br /><br />';
  446. }
  447. }
  448. echo '<script> function SendEx(num)
  449. {
  450. if (num==-1)
  451. {
  452. self.parent.window.location.href = "exercise_result.php";
  453. self.parent.tb_remove();
  454. }
  455. else
  456. {
  457. self.parent.window.location.href = "exercice_submit.php?tryagain=1&exerciseId='.$exerciseId.'&questionNum="+num+"&exerciseType='.$exerciseType.'";
  458. self.parent.tb_remove();
  459. }
  460. }
  461. </script>';
  462. api_protect_course_script();
  463. if ($links!='')
  464. {
  465. echo '<div id="ModalContent" style="padding-bottom:30px;padding-top:10px;padding-left:20px;padding-right:20px;">
  466. <a onclick="self.parent.tb_remove();" href="#" style="float:right; margin-top:-10px;" id="exercise_close_link">'.get_lang('Close').'</a>
  467. <h1><div style="color:#333;">'.get_lang('Feedback').'</div></h1>
  468. <p style="text-align:center">';
  469. if ($answerType == HOT_SPOT_DELINEATION)
  470. {
  471. $message='<p>'.get_lang('YourDelineation').'</p>';
  472. $message.=$table_resume;
  473. $message.='<br />'.get_lang('ResultIs').' '.$result_comment.'<br />';
  474. if ($organs_at_risk_hit>0)
  475. $message.='<p><b>'.get_lang('OARHit').'</b></p>';
  476. $message.='<p>'.$comment.'</p>';
  477. echo $message;
  478. }
  479. else
  480. {
  481. echo '<p>'.$comment.'</p>';
  482. }
  483. echo '<h3>'.$links.'</h3>';
  484. //echo '<a onclick="self.parent.tb_remove();" href="#" style="float:right;">'.get_lang('Close').'</a>';
  485. echo '</div>';
  486. $_SESSION['hot_spot_result']=$message;
  487. }
  488. else
  489. {
  490. $questionNum++;
  491. echo '<script>
  492. self.parent.window.location.href = "exercice_submit.php?exerciseId='.$exerciseId.'&questionNum='.$questionNum.'&exerciseType='.$exerciseType.'";
  493. //self.parent.tb_remove();
  494. </script>';
  495. }
  496. ?>