exercise_result.php 13 KB

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