exercise_result.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise result
  5. * This script gets informations from the script "exercise_submit.php",
  6. * through the session, and calculates the score of the student for
  7. * that exercise.
  8. * Then it shows the results on the screen.
  9. * @package chamilo.exercise
  10. * @author Olivier Brouckaert, main author
  11. * @author Roan Embrechts, some refactoring
  12. * @author Julio Montoya Armas switchable fill in blank option added
  13. * @version $Id: exercise_result.php 22201 2009-07-17 19:57:03Z cfasanando $
  14. *
  15. * @todo split more code up in functions, move functions to library?
  16. */
  17. /**
  18. * Code
  19. */
  20. /* INIT SECTION */
  21. require_once 'exercise.class.php';
  22. require_once 'exercise.lib.php';
  23. require_once 'question.class.php';
  24. require_once 'answer.class.php';
  25. // Name of the language file that needs to be included
  26. $language_file='exercice';
  27. require_once '../inc/global.inc.php';
  28. if ($_GET['origin']=='learnpath') {
  29. require_once '../newscorm/learnpath.class.php';
  30. require_once '../newscorm/learnpathItem.class.php';
  31. require_once '../newscorm/scorm.class.php';
  32. require_once '../newscorm/scormItem.class.php';
  33. require_once '../newscorm/aicc.class.php';
  34. require_once '../newscorm/aiccItem.class.php';
  35. }
  36. require_once api_get_path(LIBRARY_PATH).'exercise_show_functions.lib.php';
  37. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  38. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  39. $this_section=SECTION_COURSES;
  40. /* ACCESS RIGHTS */
  41. // notice for unauthorized people.
  42. api_protect_course_script(true);
  43. // Database table definitions
  44. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  45. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  46. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  47. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  48. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  49. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  50. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  51. $main_admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  52. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  53. if($debug>0){error_log('Entered exercise_result.php: '.print_r($_POST,1));}
  54. // general parameters passed via POST/GET
  55. if ( empty ( $origin ) ) { $origin = Security::remove_XSS($_REQUEST['origin']);}
  56. if ( empty ( $learnpath_id ) ) { $learnpath_id = intval($_REQUEST['learnpath_id']);}
  57. if ( empty ( $learnpath_item_id ) ) { $learnpath_item_id = intval($_REQUEST['learnpath_item_id']);}
  58. if ( empty ( $learnpath_item_view_id ) ) { $learnpath_item_view_id = intval($_REQUEST['learnpath_item_view_id']);}
  59. if ( empty ( $formSent ) ) { $formSent = $_REQUEST['formSent'];}
  60. if ( empty ( $exerciseResult ) ) { $exerciseResult = $_SESSION['exerciseResult'];}
  61. if ( empty ( $exerciseResultCoordinates)){ $exerciseResultCoordinates = $_SESSION['exerciseResultCoordinates'];}
  62. if ( empty ( $questionId ) ) { $questionId = $_REQUEST['questionId'];}
  63. if ( empty ( $choice ) ) { $choice = $_REQUEST['choice'];}
  64. if ( empty ( $questionNum ) ) { $questionNum = $_REQUEST['questionNum'];}
  65. if ( empty ( $nbrQuestions ) ) { $nbrQuestions = $_REQUEST['nbrQuestions'];}
  66. if ( empty ( $questionList ) ) { $questionList = $_SESSION['questionList'];}
  67. if ( empty ( $objExercise ) ) { $objExercise = $_SESSION['objExercise'];}
  68. if ( empty ( $exerciseType ) ) { $exerciseType = $_REQUEST['exerciseType'];}
  69. //@todo There should be some doc about this settings
  70. $_configuration['live_exercise_tracking'] = false;
  71. if ($_configuration['live_exercise_tracking']) define('ENABLED_LIVE_EXERCISE_TRACKING',1);
  72. if ($_configuration['live_exercise_tracking'] && $exerciseType == 1){
  73. $_configuration['live_exercise_tracking'] = false;
  74. }
  75. $arrques = array();
  76. $arrans = array();
  77. // set admin name as person who sends the results e-mail (lacks policy about whom should really send the results)
  78. $query = "SELECT user_id FROM $main_admin_table LIMIT 1"; //get all admins from admin table
  79. $admin_id = Database::result(Database::query($query),0,"user_id");
  80. $uinfo = api_get_user_info($admin_id);
  81. $from = $uinfo['mail'];
  82. $from_name = api_get_person_name($uinfo['firstname'], $uinfo['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  83. $str = $_SERVER['REQUEST_URI'];
  84. $url = api_get_path(WEB_CODE_PATH).'exercice/exercice.php?'.api_get_cidreq().'&show=result';
  85. // if the above variables are empty or incorrect, we don't have any result to show, so stop the script
  86. if (!is_array($exerciseResult) || !is_array($questionList) || !is_object($objExercise)) {
  87. if ($debug) {error_log('Exit exercise result'); error_log('$exerciseResult: '.print_r($exerciseResult,1)); error_log('$questionList:'.print_r($questionList,1));error_log('$objExercise:'.print_r($objExercise,1));}
  88. header('Location: exercice.php');
  89. exit();
  90. }
  91. $gradebook = '';
  92. if (isset($_SESSION['gradebook'])) {
  93. $gradebook= $_SESSION['gradebook'];
  94. }
  95. if (!empty($gradebook) && $gradebook=='view') {
  96. $interbreadcrumb[]= array ('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('ToolGradebook'));
  97. }
  98. $nameTools=get_lang('Exercice');
  99. $interbreadcrumb[]=array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  100. //$htmlHeadXtra[] = $objExercise->show_lp_javascript();
  101. if ($origin != 'learnpath') {
  102. //so we are not in learnpath tool
  103. Display::display_header($nameTools,get_lang('Exercise'));
  104. } else {
  105. header('Content-Type: text/html; charset='.api_get_system_encoding());
  106. $document_language = api_get_language_isocode();
  107. /* HTML HEADER */
  108. ?>
  109. <!DOCTYPE html
  110. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  111. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  112. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $document_language; ?>" lang="<?php echo $document_language; ?>">
  113. <head>
  114. <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charset; ?>" />
  115. <link rel="stylesheet" type="text/css" href="<?php echo api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/default.css'; ?>" />
  116. </head>
  117. <body dir="<?php echo api_get_text_direction(); ?>">
  118. <?php
  119. }
  120. //Hide results
  121. $show_results = false;
  122. $show_only_score = false;
  123. if ($objExercise->results_disabled == 0) {
  124. $show_results = true;
  125. }
  126. if ($objExercise->results_disabled == 2) {
  127. $show_only_score = true;
  128. }
  129. /* DISPLAY AND MAIN PROCESS */
  130. // I'm in a preview mode as course admin. Display the action menu.
  131. if (api_is_course_admin() && $origin != 'learnpath') {
  132. echo '<div class="actions">';
  133. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'), array(), 32).'</a>';
  134. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon('edit.png', get_lang('ModifyExercise'), array(), 32).'</a>';
  135. echo '</div>';
  136. }
  137. $exerciseTitle=text_filter($objExercise->selectTitle());
  138. $feedback_type = $objExercise->feedbacktype;
  139. //show exercise title
  140. if($origin == 'learnpath') { ?>
  141. <form method="get" action="exercice.php?<?php echo api_get_cidreq() ?>">
  142. <input type="hidden" name="origin" value="<?php echo $origin; ?>" />
  143. <input type="hidden" name="learnpath_id" value="<?php echo $learnpath_id; ?>" />
  144. <input type="hidden" name="learnpath_item_id" value="<?php echo $learnpath_item_id; ?>" />
  145. <input type="hidden" name="learnpath_item_view_id" value="<?php echo $learnpath_item_view_id; ?>" />
  146. <?php
  147. }
  148. $i = $totalScore = $totalWeighting=0;
  149. if ($debug>0){error_log ("ExerciseResult: ".print_r($exerciseResult,1)); error_log("QuestionList: ".print_r($questionList,1));}
  150. $safe_lp_id = $learnpath_id==''?0:(int)$learnpath_id;
  151. $safe_lp_item_id = $learnpath_item_id==''?0:(int)$learnpath_item_id;
  152. $safe_lp_item_view_id = $learnpath_item_view_id==''?0:(int)$learnpath_item_view_id;
  153. //We check if the user attempts before sending to the exercise_result.php
  154. if ($objExercise->selectAttempts() > 0) {
  155. $attempt_count = get_attempt_count(api_get_user_id(), $objExercise->id, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);
  156. if ($attempt_count >= $objExercise->selectAttempts()) {
  157. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exerciseTitle, $objExercise->selectAttempts()), false);
  158. if ($origin != 'learnpath') {
  159. //we are not in learnpath tool
  160. Display::display_footer();
  161. }
  162. exit;
  163. }
  164. }
  165. // Create an empty exercise
  166. if (api_is_allowed_to_session_edit()) {
  167. $exeId = create_event_exercice($objExercise->selectId());
  168. }
  169. $counter=0;
  170. $user_info = api_get_user_info(api_get_user_id());
  171. if ($show_results || $show_only_score) {
  172. echo $exercise_header = $objExercise->show_exercise_result_header(api_get_person_name($user_info['firstName'], $user_info['lastName']));
  173. }
  174. // Loop over all question to show results for each of them, one by one
  175. foreach ($questionList as $questionId) {
  176. // destruction of the Question object
  177. unset($objQuestionTmp);
  178. $counter++;
  179. // gets the student choice for this question
  180. $choice = $exerciseResult[$questionId];
  181. // creates a temporary Question object
  182. $objQuestionTmp = Question :: read($questionId);
  183. // initialize question information
  184. $questionName = $objQuestionTmp->selectTitle();
  185. $questionDescription = $objQuestionTmp->selectDescription();
  186. $questionWeighting = $objQuestionTmp->selectWeighting();
  187. $answerType = $objQuestionTmp->selectType();
  188. $quesId = $objQuestionTmp->selectId();
  189. //this variable commes from exercise_submit_modal.php
  190. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$quesId];
  191. if ($show_results) {
  192. // show titles
  193. if ($origin != 'learnpath') {
  194. echo $objQuestionTmp->return_header($objExercise->feedbacktype);
  195. if ($answerType == HOT_SPOT) {
  196. ?>
  197. <tr>
  198. <td valign="top" colspan="2">
  199. <table width="552" border="1" bordercolor="#A4A4A4" style="border-collapse: collapse;">
  200. <tr>
  201. <td width="152" valign="top">
  202. <i><?php echo get_lang("CorrectAnswer"); ?></i><br /><br />
  203. </td>
  204. <td width="100" valign="top">
  205. <i><?php echo get_lang('HotspotHit'); ?></i><br /><br />
  206. </td>
  207. <?php if ($objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  208. <td width="300" valign="top">
  209. <i><?php echo get_lang("Comment"); ?></i><br /><br />
  210. </td>
  211. <?php } else { ?>
  212. <td>&nbsp;</td>
  213. <?php } ?>
  214. </tr>
  215. <?php
  216. }
  217. }
  218. }
  219. // We're inside *one* question. Go through each possible answer for this question
  220. $result = $objExercise->manage_answer($exeId, $questionId, $choice,'exercise_result', $exerciseResultCoordinates, true, false, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result);
  221. $totalScore += $result['score'];
  222. $totalWeighting += $result['weight'];
  223. } // end foreach() block that loops over all questions
  224. if ($origin != 'learnpath') {
  225. if ($show_results || $show_only_score) {
  226. echo '<div id="question_score">';
  227. echo get_lang('YourTotalScore')." ";
  228. if ($objExercise->selectPropagateNeg() == 0 && $totalScore < 0) {
  229. $totalScore = 0;
  230. }
  231. echo show_score($totalScore, $totalWeighting, false);
  232. echo '</div>';
  233. }
  234. /* <button type="submit" class="save"><?php echo get_lang('Finish');?></button> */
  235. }
  236. // Tracking of results
  237. // Updates the empty exercise
  238. $quizDuration = (!empty($_SESSION['quizStartTime']) ? time() - $_SESSION['quizStartTime'] : 0);
  239. if (api_is_allowed_to_session_edit() ) {
  240. update_event_exercice($exeId, $objExercise->selectId(), $totalScore, $totalWeighting, api_get_session_id(), $safe_lp_id,$safe_lp_item_id,$safe_lp_item_view_id, $quizDuration);
  241. }
  242. if ($origin != 'learnpath') {
  243. Display :: display_normal_message(get_lang('ExerciseFinished').'<br /><a href="exercice.php" />'.get_lang('Back').'</a>',false);
  244. } else {
  245. Display :: display_normal_message(get_lang('ExerciseFinished').'<br /><br />',false);
  246. $lp_mode = $_SESSION['lp_mode'];
  247. $url = '../newscorm/lp_controller.php?cidReq='.api_get_course_id().'&action=view&lp_id='.$learnpath_id.'&lp_item_id='.$learnpath_item_id.'&exeId='.$exeId.'&fb_type='.$objExercise->feedbacktype;
  248. $href = ($lp_mode == 'fullscreen')?' window.opener.location.href="'.$url.'" ':' top.location.href="'.$url.'" ';
  249. echo '<script language="javascript" type="text/javascript">'.$href.'</script>'."\n";
  250. //record the results in the learning path, using the SCORM interface (API)
  251. echo '<script language="javascript" type="text/javascript">window.parent.API.void_save_asset('.$totalScore.','.$totalWeighting.');</script>'."\n";
  252. echo '</body></html>';
  253. }
  254. if ($origin != 'learnpath') {
  255. //we are not in learnpath tool
  256. Display::display_footer();
  257. }
  258. // Send notification..
  259. if (!api_is_allowed_to_edit(null,true)) {
  260. $objExercise->send_notification($arrques, $arrans, $origin);
  261. }