exercise_result.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. /* INIT SECTION */
  18. require_once 'exercise.class.php';
  19. require_once 'exercise.lib.php';
  20. require_once 'question.class.php';
  21. require_once 'answer.class.php';
  22. // Name of the language file that needs to be included
  23. $language_file='exercice';
  24. require_once '../inc/global.inc.php';
  25. if ($_GET['origin']=='learnpath') {
  26. require_once '../newscorm/learnpath.class.php';
  27. require_once '../newscorm/learnpathItem.class.php';
  28. require_once '../newscorm/scorm.class.php';
  29. require_once '../newscorm/scormItem.class.php';
  30. require_once '../newscorm/aicc.class.php';
  31. require_once '../newscorm/aiccItem.class.php';
  32. }
  33. require_once api_get_path(LIBRARY_PATH).'exercise_show_functions.lib.php';
  34. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  35. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  36. $this_section=SECTION_COURSES;
  37. /* ACCESS RIGHTS */
  38. // notice for unauthorized people.
  39. api_protect_course_script(true);
  40. // Database table definitions
  41. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  42. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  43. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  44. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  45. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  46. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  47. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  48. $main_admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  49. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  50. if($debug>0){error_log('Entered exercise_result.php: '.print_r($_POST,1));}
  51. // general parameters passed via POST/GET
  52. if ( empty ( $origin ) ) { $origin = Security::remove_XSS($_REQUEST['origin']);}
  53. if ( empty ( $learnpath_id ) ) { $learnpath_id = intval($_REQUEST['learnpath_id']);}
  54. if ( empty ( $learnpath_item_id ) ) { $learnpath_item_id = intval($_REQUEST['learnpath_item_id']);}
  55. if ( empty ( $learnpath_item_view_id ) ) { $learnpath_item_view_id = intval($_REQUEST['learnpath_item_view_id']);}
  56. if ( empty ( $formSent ) ) { $formSent = $_REQUEST['formSent'];}
  57. if ( empty ( $exerciseResult ) ) { $exerciseResult = $_SESSION['exerciseResult'];}
  58. if ( empty ( $exerciseResultCoordinates)){ $exerciseResultCoordinates = $_SESSION['exerciseResultCoordinates'];}
  59. if ( empty ( $questionId ) ) { $questionId = $_REQUEST['questionId'];}
  60. if ( empty ( $choice ) ) { $choice = $_REQUEST['choice'];}
  61. if ( empty ( $questionNum ) ) { $questionNum = $_REQUEST['questionNum'];}
  62. if ( empty ( $nbrQuestions ) ) { $nbrQuestions = $_REQUEST['nbrQuestions'];}
  63. if ( empty ( $questionList ) ) { $questionList = $_SESSION['questionList'];}
  64. if ( empty ( $objExercise ) ) { $objExercise = $_SESSION['objExercise'];}
  65. if ( empty ( $exerciseType ) ) { $exerciseType = $_REQUEST['exerciseType'];}
  66. //@todo There should be some doc about this settings
  67. $_configuration['live_exercise_tracking'] = false;
  68. if ($_configuration['live_exercise_tracking']) define('ENABLED_LIVE_EXERCISE_TRACKING',1);
  69. if ($_configuration['live_exercise_tracking'] && $exerciseType == 1){
  70. $_configuration['live_exercise_tracking'] = false;
  71. }
  72. $arrques = array();
  73. $arrans = array();
  74. // set admin name as person who sends the results e-mail (lacks policy about whom should really send the results)
  75. $query = "SELECT user_id FROM $main_admin_table LIMIT 1"; //get all admins from admin table
  76. $admin_id = Database::result(Database::query($query),0,"user_id");
  77. $uinfo = api_get_user_info($admin_id);
  78. $from = $uinfo['mail'];
  79. $from_name = api_get_person_name($uinfo['firstname'], $uinfo['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  80. $str = $_SERVER['REQUEST_URI'];
  81. $url = api_get_path(WEB_CODE_PATH).'exercice/exercice.php?'.api_get_cidreq().'&show=result';
  82. // if the above variables are empty or incorrect, we don't have any result to show, so stop the script
  83. if(!is_array($exerciseResult) || !is_array($questionList) || !is_object($objExercise)) {
  84. 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));}
  85. header('Location: exercice.php');
  86. exit();
  87. }
  88. $gradebook = '';
  89. if (isset($_SESSION['gradebook'])) {
  90. $gradebook= $_SESSION['gradebook'];
  91. }
  92. if (!empty($gradebook) && $gradebook=='view') {
  93. $interbreadcrumb[]= array ('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('ToolGradebook'));
  94. }
  95. $nameTools=get_lang('Exercice');
  96. $interbreadcrumb[]=array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  97. //$htmlHeadXtra[] = $objExercise->show_lp_javascript();
  98. if ($origin != 'learnpath') {
  99. //so we are not in learnpath tool
  100. Display::display_header($nameTools,get_lang('Exercise'));
  101. } else {
  102. header('Content-Type: text/html; charset='.api_get_system_encoding());
  103. $document_language = api_get_language_isocode();
  104. /* HTML HEADER */
  105. ?>
  106. <!DOCTYPE html
  107. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  108. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  109. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $document_language; ?>" lang="<?php echo $document_language; ?>">
  110. <head>
  111. <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charset; ?>" />
  112. <link rel="stylesheet" type="text/css" href="<?php echo api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/default.css'; ?>" />
  113. </head>
  114. <body dir="<?php echo api_get_text_direction(); ?>">
  115. <?php
  116. }
  117. //Hide results
  118. $show_results = false;
  119. $show_only_score = false;
  120. if ($objExercise->results_disabled == 0) {
  121. $show_results = true;
  122. }
  123. if ($objExercise->results_disabled == 2) {
  124. $show_only_score = true;
  125. }
  126. /* DISPLAY AND MAIN PROCESS */
  127. // I'm in a preview mode as course admin. Display the action menu.
  128. if (api_is_course_admin() && $origin != 'learnpath') {
  129. echo '<div class="actions">';
  130. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'), array(), 32).'</a>';
  131. 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>';
  132. echo '</div>';
  133. }
  134. $exerciseTitle=text_filter($objExercise->selectTitle());
  135. $feedback_type = $objExercise->feedbacktype;
  136. //show exercise title
  137. if($origin == 'learnpath') { ?>
  138. <form method="get" action="exercice.php?<?php echo api_get_cidreq() ?>">
  139. <input type="hidden" name="origin" value="<?php echo $origin; ?>" />
  140. <input type="hidden" name="learnpath_id" value="<?php echo $learnpath_id; ?>" />
  141. <input type="hidden" name="learnpath_item_id" value="<?php echo $learnpath_item_id; ?>" />
  142. <input type="hidden" name="learnpath_item_view_id" value="<?php echo $learnpath_item_view_id; ?>" />
  143. <?php
  144. }
  145. $i=$totalScore=$totalWeighting=0;
  146. if($debug>0){error_log ("ExerciseResult: ".print_r($exerciseResult,1)); error_log("QuestionList: ".print_r($questionList,1));}
  147. // Create an empty exercise
  148. if (api_is_allowed_to_session_edit()) {
  149. $exeId= create_event_exercice($objExercise->selectId());
  150. }
  151. $counter=0;
  152. $user_info = api_get_user_info(api_get_user_id());
  153. if ($show_results || $show_only_score) {
  154. echo $exercise_header = $objExercise->show_exercise_result_header(api_get_person_name($user_info['firstName'], $user_info['lastName']));
  155. }
  156. // Loop over all question to show results for each of them, one by one
  157. foreach ($questionList as $questionId) {
  158. // destruction of the Question object
  159. unset($objQuestionTmp);
  160. $counter++;
  161. // gets the student choice for this question
  162. $choice = $exerciseResult[$questionId];
  163. // creates a temporary Question object
  164. $objQuestionTmp = Question :: read($questionId);
  165. // initialize question information
  166. $questionName = $objQuestionTmp->selectTitle();
  167. $questionDescription = $objQuestionTmp->selectDescription();
  168. $questionWeighting = $objQuestionTmp->selectWeighting();
  169. $answerType = $objQuestionTmp->selectType();
  170. $quesId = $objQuestionTmp->selectId();
  171. //this variable commes from exercise_submit_modal.php
  172. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$quesId];
  173. if ($show_results) {
  174. // show titles
  175. if ($origin != 'learnpath') {
  176. echo $objQuestionTmp->return_header($objExercise->feedbacktype);
  177. if ($answerType == HOT_SPOT) {
  178. ?>
  179. <tr>
  180. <td valign="top" colspan="2">
  181. <table width="552" border="1" bordercolor="#A4A4A4" style="border-collapse: collapse;">
  182. <tr>
  183. <td width="152" valign="top">
  184. <i><?php echo get_lang("CorrectAnswer"); ?></i><br /><br />
  185. </td>
  186. <td width="100" valign="top">
  187. <i><?php echo get_lang('HotspotHit'); ?></i><br /><br />
  188. </td>
  189. <?php if ($objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  190. <td width="300" valign="top">
  191. <i><?php echo get_lang("Comment"); ?></i><br /><br />
  192. </td>
  193. <?php } else { ?>
  194. <td>&nbsp;</td>
  195. <?php } ?>
  196. </tr>
  197. <?php
  198. }
  199. }
  200. }
  201. // We're inside *one* question. Go through each possible answer for this question
  202. $result = $objExercise->manage_answer($exeId, $questionId, $choice,'exercise_result', $exerciseResultCoordinates, true, false, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result);
  203. $totalScore += $result['score'];
  204. $totalWeighting += $result['weight'];
  205. } // end foreach() block that loops over all questions
  206. if ($origin != 'learnpath') {
  207. if ($show_results || $show_only_score) {
  208. echo '<div id="question_score">';
  209. echo get_lang('YourTotalScore')." ";
  210. if ($objExercise->selectPropagateNeg() == 0 && $totalScore < 0) {
  211. $totalScore = 0;
  212. }
  213. echo show_score($totalScore, $totalWeighting, false);
  214. echo '</div>';
  215. }
  216. /* <button type="submit" class="save"><?php echo get_lang('Finish');?></button> */
  217. }
  218. // Tracking of results
  219. // Updates the empty exercise
  220. $safe_lp_id = $learnpath_id==''?0:(int)$learnpath_id;
  221. $safe_lp_item_id = $learnpath_item_id==''?0:(int)$learnpath_item_id;
  222. $safe_lp_item_view_id = $learnpath_item_view_id==''?0:(int)$learnpath_item_view_id;
  223. $quizDuration = (!empty($_SESSION['quizStartTime']) ? time() - $_SESSION['quizStartTime'] : 0);
  224. if (api_is_allowed_to_session_edit() ) {
  225. 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);
  226. }
  227. if ($origin != 'learnpath') {
  228. Display :: display_normal_message(get_lang('ExerciseFinished').'<br /><a href="exercice.php" />'.get_lang('Back').'</a>',false);
  229. } else {
  230. Display :: display_normal_message(get_lang('ExerciseFinished').'<br /><br />',false);
  231. $lp_mode = $_SESSION['lp_mode'];
  232. $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;
  233. $href = ($lp_mode == 'fullscreen')?' window.opener.location.href="'.$url.'" ':' top.location.href="'.$url.'" ';
  234. echo '<script language="javascript" type="text/javascript">'.$href.'</script>'."\n";
  235. //record the results in the learning path, using the SCORM interface (API)
  236. echo '<script language="javascript" type="text/javascript">window.parent.API.void_save_asset('.$totalScore.','.$totalWeighting.');</script>'."\n";
  237. echo '</body></html>';
  238. }
  239. if ($origin != 'learnpath') {
  240. //we are not in learnpath tool
  241. Display::display_footer();
  242. }
  243. // Email configuration settings
  244. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  245. $user_info = UserManager::get_user_info_by_id(api_get_user_id());
  246. $firstName = $user_info['firstname'];
  247. $lastName = $user_info['lastname'];
  248. $mail = $user_info['email'];
  249. $coursecode = api_get_course_id();
  250. $courseName = $_SESSION['_course']['name'];
  251. $to = '';
  252. $teachers = array();
  253. if(api_get_setting('use_session_mode')=='true' && !empty($_SESSION['id_session'])) {
  254. $teachers = CourseManager::get_coach_list_from_course_code($coursecode,$_SESSION['id_session']);
  255. } else {
  256. $teachers = CourseManager::get_teacher_list_from_course_code($coursecode);
  257. }
  258. $num = count($teachers);
  259. if($num>1) {
  260. $to = array();
  261. foreach($teachers as $teacher) {
  262. $to[] = $teacher['email'];
  263. }
  264. }elseif($num>0){
  265. foreach($teachers as $teacher) {
  266. $to = $teacher['email'];
  267. }
  268. } else {
  269. //this is a problem (it means that there is no admin for this course)
  270. }
  271. // we are able to send emails to the teachers?
  272. if (api_get_course_setting('email_alert_manager_on_new_quiz') == 1 ) {
  273. // only for "simple tests"
  274. if ($origin != 'learnpath') {
  275. //has a unique answer?
  276. $mycharset = api_get_system_encoding();
  277. $msg = '<html><head>
  278. <link rel="stylesheet" href="'.api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/default.css" type="text/css">
  279. <meta content="text/html; charset='.$mycharset.'" http-equiv="content-type"></head>';
  280. if (count($arrques)>0) {
  281. $msg .= '
  282. <body><br />
  283. <p>'.get_lang('OpenQuestionsAttempted').' :
  284. </p>
  285. <p>'.get_lang('AttemptDetails').' : <br />
  286. </p>
  287. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">
  288. <tr>
  289. <td width="229" valign="top"><h2>&nbsp;&nbsp;'.get_lang('CourseName').'</h2></td>
  290. <td width="469" valign="top"><h2>#course#</h2></td>
  291. </tr>
  292. <tr>
  293. <td width="229" valign="top" class="outerframe">&nbsp;&nbsp;'.get_lang('TestAttempted').'</span></td>
  294. <td width="469" valign="top" class="outerframe">#exercise#</td>
  295. </tr>
  296. <tr>
  297. <td valign="top">&nbsp;&nbsp;<span class="style10">'.get_lang('StudentName').'</span></td>
  298. '.(api_is_western_name_order() ? '<td valign="top" >#firstName# #lastName#</td>' : '<td valign="top" >#lastName# #firstName#</td>').'
  299. </tr>
  300. <tr>
  301. <td valign="top" >&nbsp;&nbsp;'.get_lang('StudentEmail').' </td>
  302. <td valign="top"> #mail#</td>
  303. </tr></table>
  304. <p><br />'.get_lang('OpenQuestionsAttemptedAre').' :</p>
  305. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">';
  306. for($i=0;$i<sizeof($arrques);$i++) {
  307. $msg.='
  308. <tr>
  309. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;<span class="style10">'.get_lang('Question').'</span></td>
  310. <td width="473" valign="top" bgcolor="#F3F3F3"><span class="style16"> #questionName#</span></td>
  311. </tr>
  312. <tr>
  313. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;<span class="style10">'.get_lang('Answer').' </span></td>
  314. <td valign="top" bgcolor="#F3F3F3"><span class="style16"> #answer#</span></td>
  315. </tr>';
  316. $msg1= str_replace("#exercise#",$exerciseTitle,$msg);
  317. $msg= str_replace("#firstName#",$firstName,$msg1);
  318. $msg1= str_replace("#lastName#",$lastName,$msg);
  319. $msg= str_replace("#mail#",$mail,$msg1);
  320. $msg1= str_replace("#questionName#",$arrques[$i],$msg);
  321. $msg= str_replace("#answer#",$arrans[$i],$msg1);
  322. $msg1= str_replace("#i#",$i,$msg);
  323. $msg= str_replace("#course#",$courseName,$msg1);
  324. }
  325. $msg.='</table><br>
  326. <span class="style16">'.get_lang('ClickToCommentAndGiveFeedback').',<br />
  327. <a href="#url#">#url#</a></span></body></html>';
  328. $msg1= str_replace("#url#",$url,$msg);
  329. $mail_content = $msg1;
  330. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  331. $email_admin = api_get_setting('emailAdministrator');
  332. $subject = get_lang('OpenQuestionsAttempted');
  333. $result = @api_mail_html('', $to, $subject, $mail_content, $sender_name, $email_admin, array('charset'=>$mycharset));
  334. } else {
  335. $msg .= '<body>
  336. <p>'.get_lang('ExerciseAttempted').' <br />
  337. </p>
  338. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">
  339. <tr>
  340. <td width="229" valign="top"><h2>&nbsp;&nbsp;'.get_lang('CourseName').'</h2></td>
  341. <td width="469" valign="top"><h2>#course#</h2></td>
  342. </tr>
  343. <tr>
  344. <td width="229" valign="top" class="outerframe">&nbsp;&nbsp;'.get_lang('TestAttempted').'</span></td>
  345. <td width="469" valign="top" class="outerframe">#exercise#</td>
  346. </tr>
  347. <tr>
  348. <td valign="top">&nbsp;&nbsp;<span class="style10">'.get_lang('StudentName').'</span></td>
  349. '.(api_is_western_name_order() ? '<td valign="top" >#firstName# #lastName#</td>' : '<td valign="top" >#lastName# #firstName#</td>').'
  350. </tr>
  351. <tr>
  352. <td valign="top" >&nbsp;&nbsp;'.get_lang('StudentEmail').' </td>
  353. <td valign="top"> #mail#</td>
  354. </tr></table>';
  355. $msg= str_replace("#exercise#",$exerciseTitle,$msg);
  356. $msg= str_replace("#firstName#",$firstName,$msg);
  357. $msg= str_replace("#lastName#",$lastName,$msg);
  358. $msg= str_replace("#mail#",$mail,$msg);
  359. $msg= str_replace("#course#",$courseName,$msg);
  360. $msg.='<br />
  361. <span class="style16">'.get_lang('ClickToCommentAndGiveFeedback').',<br />
  362. <a href="#url#">#url#</a></span></body></html>';
  363. $msg= str_replace("#url#",$url,$msg);
  364. $mail_content = $msg;
  365. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  366. $email_admin = api_get_setting('emailAdministrator');
  367. $subject = get_lang('ExerciseAttempted');
  368. $result = @api_mail_html('', $to, $subject, $mail_content, $sender_name, $email_admin, array('charset'=>$mycharset));
  369. }
  370. }
  371. }