savescores.php 3.6 KB

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