savescores.php 3.5 KB

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