savescores.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Saving the scores.
  5. * @package chamilo.exercise
  6. */
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $courseInfo = api_get_course_info();
  9. $_user = api_get_user_info();
  10. $this_section = SECTION_COURSES;
  11. $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path']."/document";
  12. $test = $_REQUEST['test'];
  13. $full_file_path = $documentPath.$test;
  14. my_delete($full_file_path.$_user['user_id'].".t.html");
  15. $TABLETRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  16. $TABLE_LP_ITEM_VIEW = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  17. $score = $_REQUEST['score'];
  18. $origin = $_REQUEST['origin'];
  19. $learnpath_item_id = intval($_REQUEST['learnpath_item_id']);
  20. $lpViewId = isset($_REQUEST['lp_view_id']) ? intval($_REQUEST['lp_view_id']) : null;
  21. $course_id = $courseInfo['real_id'];
  22. $jscript2run = '';
  23. /**
  24. * Save the score for a HP quiz. Can be used by the learnpath tool as well
  25. * for HotPotatoes quizzes. When coming from the learning path, we
  26. * use the session variables telling us which item of the learning path has to
  27. * be updated (score-wise)
  28. * @param string File is the exercise name (the file name for a HP)
  29. * @param integer Score to save inside the tracking tables (HP and learnpath)
  30. * @return void
  31. */
  32. function save_scores($file, $score)
  33. {
  34. global $origin;
  35. $TABLETRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  36. $_user = api_get_user_info();
  37. // if tracking is disabled record nothing
  38. $weighting = 100; // 100%
  39. $date = api_get_utc_datetime();
  40. $c_id = api_get_course_int_id();
  41. if ($_user['user_id']) {
  42. $user_id = $_user['user_id'];
  43. } else {
  44. // anonymous
  45. $user_id = "NULL";
  46. }
  47. $params = [
  48. 'exe_name' => $file,
  49. 'exe_user_id' => $user_id,
  50. 'exe_date' => $date,
  51. 'c_id' => $c_id,
  52. 'exe_result' => $score,
  53. 'exe_weighting' => $weighting,
  54. ];
  55. Database::insert($TABLETRACK_HOTPOTATOES, $params);
  56. if ($origin == 'learnpath') {
  57. //if we are in a learning path, save the score in the corresponding
  58. //table to get tracking in there as well
  59. global $jscript2run;
  60. //record the results in the learning path, using the SCORM interface (API)
  61. $jscript2run .= "<script>
  62. $(document).ready(function() {
  63. //API_obj = window.frames.window.content.API;
  64. //API_obj = $('content_id').context.defaultView.content.API; //works only in FF
  65. //API_obj = window.parent.frames.window.top.API;
  66. API_obj = window.top.API;
  67. API_obj.void_save_asset('$score', '$weighting', 0, 'completed');
  68. });
  69. </script>";
  70. }
  71. }
  72. // Save the Scores
  73. save_scores($test, $score);
  74. // Back
  75. if ($origin != 'learnpath') {
  76. $url = "exercise.php"; // back to exercises
  77. $jscript2run .= '<script>'."window.open('$url', '_top', '')".'</script>';
  78. echo $jscript2run;
  79. } else {
  80. $htmlHeadXtra[] = $jscript2run;
  81. Display::display_reduced_header();
  82. if (!empty($course_id) && !empty($learnpath_item_id) && !empty($lpViewId)) {
  83. $sql = "UPDATE $TABLE_LP_ITEM_VIEW SET
  84. status = 'completed'
  85. WHERE
  86. c_id = $course_id AND
  87. lp_item_id= $learnpath_item_id AND
  88. lp_view_id = $lpViewId
  89. ";
  90. Database::query($sql);
  91. Display::display_confirmation_message(get_lang('HotPotatoesFinished'));
  92. } else {
  93. Display::display_error_message(get_lang('Error'));
  94. }
  95. Display::display_footer();
  96. }