* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ require_once(dirname(__FILE__) . '/QuizSession.php'); class StoredQuizSession extends QuizSession { var $_answer; // answer to the question var $_sidE; // escaped session ID function __construct() { $this->_sid = $_POST['qs_id']; $this->_answer = trim($_POST['qs_answer']); $this->_stored = TRUE; $this->_sidE = $this->_sid; } // checks if the current sessions exists function exists() { global $connect; $q = $connect->prepare('SELECT quiz_id, term1, term2' . ' FROM ' . QDTP . 'quiz_master' . ' WHERE session_id=?'); $q->bind_param('s', $this->_sidE); if (! $q->execute()) { echo $q->error; return FALSE; } $r = $q->get_result()->fetch_array(); $this->_qid = $r['quiz_id']; $this->_term1 = $r['term1']; $this->_term2 = $r['term2']; return TRUE; } // validates the session including validating the answer function validate() { global $connect; if (! $this->exists()) { trigger_error('Invalid Session'); return FALSE; } $q = $connect->prepare('SELECT answer FROM ' . QDTP . 'quiz WHERE id=?'); $q->bind_param('i', $this->_qid); $q->execute(); $r = $q->get_result()->fetch_array(); // do some calculations if ($this->_term1 != 'NULL') { switch ($r['answer']) { case '+': $ans = $this->_term1 + $this->_term2; break; case '-': $ans = $this->_term1 - $this->_term2; break; case '*': $ans = $this->_term1 * $this->_term2; break; } return $ans == $this->_answer; } return $r['answer'] == $this->_answer; } // deletes the current session function cleanup() { global $connect; $q = $connect->prepare('DELETE FROM ' . QDTP . 'quiz_master' . ' WHERE session_id=?'); $q->bind_param('s', $this->_sidE); $q->execute(); if ($q->errno) trigger_error($q->error, E_USER_ERROR); } }