Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-26735 Questions file links broken when quiz_attempts.id and .uniq…

…ueid differ.
  • Loading branch information...
commit f11672d84a171de6a917cb0dab8f89ec48a4b140 1 parent c096042
@timhunt timhunt authored
Showing with 30 additions and 19 deletions.
  1. +28 −17 mod/quiz/attemptlib.php
  2. +2 −2 mod/quiz/lib.php
View
45 mod/quiz/attemptlib.php
@@ -462,32 +462,43 @@ function __construct($attempt, $quiz, $cm, $course) {
}
/**
- * Static function to create a new quiz_attempt object given an attemptid.
- *
- * @param integer $attemptid the attempt id.
- * @return quiz_attempt the new quiz_attempt object
+ * Used by {create()} and {create_from_usage_id()}.
+ * @param array $conditions passed to $DB->get_record('quiz_attempts', $conditions).
*/
- static public function create($attemptid) {
+ static protected function create_helper($conditions) {
global $DB;
- if (!$attempt = quiz_load_attempt($attemptid)) {
- throw new moodle_exception('invalidattemptid', 'quiz');
- }
- if (!$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz))) {
- throw new moodle_exception('invalidquizid', 'quiz');
- }
- if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
- throw new moodle_exception('invalidcoursemodule');
- }
- if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
- throw new moodle_exception('invalidcoursemodule');
- }
+ $attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
+ $quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST);
+ $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
+ $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);
+
// Update quiz with override information
$quiz = quiz_update_effective_access($quiz, $attempt->userid);
return new quiz_attempt($attempt, $quiz, $cm, $course);
}
+ /**
+ * Static function to create a new quiz_attempt object given an attemptid.
+ *
+ * @param int $attemptid the attempt id.
+ * @return quiz_attempt the new quiz_attempt object
+ */
+ static public function create($attemptid) {
+ return self::create_helper(array('id' => $attemptid));
+ }
+
+ /**
+ * Static function to create a new quiz_attempt object given a usage id.
+ *
+ * @param int $usageid the attempt usage id.
+ * @return quiz_attempt the new quiz_attempt object
+ */
+ static public function create_from_unique_id($usageid) {
+ return self::create_helper(array('uniqueid' => $usageid));
+ }
+
// Functions for loading more data =====================================================
/**
* Load the state of a number of questions that have already been loaded.
View
4 mod/quiz/lib.php
@@ -1770,11 +1770,11 @@ function quiz_pluginfile($course, $cm, $context, $filearea, $args, $forcedownloa
* @return bool false if file not found, does not return if found - justsend the file
*/
function quiz_question_pluginfile($course, $context, $component,
- $filearea, $attemptid, $questionid, $args, $forcedownload) {
+ $filearea, $uniqueid, $questionid, $args, $forcedownload) {
global $USER, $CFG;
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
- $attemptobj = quiz_attempt::create($attemptid);
+ $attemptobj = quiz_attempt::create_from_unique_id($uniqueid);
require_login($attemptobj->get_courseid(), false, $attemptobj->get_cm());
$questionids = array($questionid);
$attemptobj->load_questions($questionids);
Please sign in to comment.
Something went wrong with that request. Please try again.