|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 00030 00031 00041 function quiz_upgrade_very_old_question_sessions($attempt) { 00042 global $DB; 00043 // The old quiz model only allowed a single response per quiz attempt so that there will be 00044 // only one state record per question for this attempt. 00045 00046 // We set the timestamp of all states to the timemodified field of the attempt. 00047 $DB->execute("UPDATE {question_states} SET timestamp = ? WHERE attempt = ?", 00048 array($attempt->timemodified, $attempt->uniqueid)); 00049 00050 // For each state we create an entry in the question_sessions table, with both newest and 00051 // newgraded pointing to this state. 00052 // Actually we only do this for states whose question is actually listed in $attempt->layout. 00053 // We do not do it for states associated to wrapped questions like for example the questions 00054 // used by a random question 00055 $session = new stdClass(); 00056 $session->attemptid = $attempt->uniqueid; 00057 $session->sumpenalty = 0; 00058 $session->manualcomment = ''; 00059 $session->manualcommentformat = FORMAT_HTML; 00060 $session->flagged = 0; 00061 00062 $questionlist = str_replace(',0', '', quiz_clean_layout($attempt->layout, true)); 00063 if (!$questionlist) { 00064 return; 00065 } 00066 list($usql, $question_params) = $DB->get_in_or_equal(explode(',', $questionlist)); 00067 $params = array_merge(array($attempt->uniqueid), $question_params); 00068 00069 if ($states = $DB->get_records_select('question_states', 00070 "attempt = ? AND question $usql", $params)) { 00071 foreach ($states as $state) { 00072 if ($DB->record_exists('question_sessions', 00073 array('attemptid' => $attempt->uniqueid, 'questionid' => $state->question))) { 00074 // It was possible for the code to get here when some of the necessary 00075 // question_sessions were already in the database. That lead to a 00076 // unique key violation, so we manually detect and avoid that. 00077 continue; 00078 } 00079 $session->newgraded = $state->id; 00080 $session->newest = $state->id; 00081 $session->questionid = $state->question; 00082 $DB->insert_record('question_sessions', $session, false); 00083 } 00084 } 00085 00086 // It was possible to have old question_states records for this attempt but 00087 // pointing to questionids that were no longer in quiz_attempt->layout. 00088 // That makes no sense, and will break things later in the upgrade, so delete 00089 // those now. 00090 list($qidsql, $params) = $DB->get_in_or_equal(explode(',', $questionlist), 00091 SQL_PARAMS_QM, 'param', false); 00092 $params[] = $attempt->uniqueid; 00093 $DB->delete_records_select('question_states', "question $usql AND attempt = ?", $params); 00094 }