attemptlib.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SLOODLE version of:
  4. * Back-end code for handling data about quizzes and the current user's attempt.
  5. *
  6. * The original Moodle 2.2 quiz won't let us pass our own results data in
  7. * ... except in the shape it expects to get it from its self-generated HTML form.
  8. * (It probably should, because the way it is it must be hard to unit-test.)
  9. *
  10. * We want to use different parameters than the ones used by the HTML version.
  11. *
  12. * Its internal quba class will let us pass those in, but we can't access it from here because it's protected.
  13. * So we sub-class quiz_attempt and create function that can accept data in the form we want to give it.
  14. *
  15. * @package mod
  16. * @subpackage sloodle
  17. * @copyright 2008 onwards Tim Hunt (original code) modified for SLOODLE by Edmund Edgar 2012
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  19. */
  20. defined('MOODLE_INTERNAL') || die();
  21. class sloodle_quiz_attempt extends quiz_attempt {
  22. // create and create_helper are duplicated here to persuade attempt::create to give us a sloodle_quiz_attempt
  23. // They are otherwise unchanged from the original quiz_attempt.
  24. /**
  25. * Static function to create a new quiz_attempt object given an attemptid.
  26. *
  27. * @param int $attemptid the attempt id.
  28. * @return quiz_attempt the new quiz_attempt object
  29. */
  30. public static function create($attemptid) {
  31. return self::create_helper(array('id' => $attemptid));
  32. }
  33. /**
  34. * Used by {create()} and {create_from_usage_id()}.
  35. * @param array $conditions passed to $DB->get_record('quiz_attempts', $conditions).
  36. */
  37. protected static function create_helper($conditions) {
  38. global $DB;
  39. $attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
  40. $quiz = quiz_access_manager::load_quiz_and_settings($attempt->quiz);
  41. $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
  42. $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);
  43. // Update quiz with override information
  44. $quiz = quiz_update_effective_access($quiz, $attempt->userid);
  45. return new sloodle_quiz_attempt($attempt, $quiz, $cm, $course);
  46. }
  47. /**
  48. * Process all the actions that were submitted as part of the current request.
  49. * Based on process_all_actions(), but targetted at a particular slot.
  50. *
  51. * @param int $timestamp the timestamp that should be stored as the modifed
  52. * time in the database for these actions. If null, will use the current time.
  53. */
  54. public function process_all_actions_for_slot($slot, $submitteddata, $timestamp) {
  55. global $DB;
  56. // Moodle's process_all_actions originally did:
  57. // $this->quba->process_all_actions($timestamp, $postdata);
  58. // ...extracted submitteddata, figured out which slots it wanted to deal with and for each one called:
  59. //$DB->set_debug(true);
  60. $this->quba->process_action($slot, $submitteddata, $timestamp);
  61. question_engine::save_questions_usage_by_activity($this->quba);
  62. $this->attempt->timemodified = $timestamp;
  63. if ($this->attempt->timefinish) {
  64. $this->attempt->sumgrades = $this->quba->get_total_mark();
  65. }
  66. $DB->update_record('quiz_attempts', $this->attempt);
  67. if (!$this->is_preview() && $this->attempt->timefinish) {
  68. quiz_save_best_grade($this->get_quiz(), $this->get_userid());
  69. }
  70. }
  71. }