or
// http://www.fsf.org/licensing/licenses/agpl-3.0.html.
//
//
// change below values to adjust preferences
//
var $quizID;
var $mode;
var $rndquest;
var $rndchoice;
var $highscore;
var $showintro;
var $current_question = 1;
var $error = '';
var $in_question = 0;
var $in_section = '';
var $parseQuiz_cache = null;
var $quizobj;
function quiz($quizID, &$param) {
$this->quizID = $quizID;
$this->mode = $param['learning'];
$this->rndquest = $param['rndquest'];
$this->rndchoice = $param['rndchoice'];
$this->highscore = $param['highscores'];
$this->showintro = $param['showintro'];
$this->quizobj = plugin_load('syntax','quiz');
if (!($this->mode < 0 || $this->mode > 1 )) {
$this->error = 'Error: "' . $this->mode .'" is not a valid quiz mode. Valid modes are "test (0)" and "learning (1)"';
}
}
function do_quiz_here() {
if (isset($_POST['next_question'])) {
$next = $_POST['next_question']; // + 1;
if ((!isset($_POST['answers'][$next - 1])) && ($next > 1)) { $next = $next - 1; }
} else {
unset($_POST);
$next = 0;
}
return $this->print_question($next);
}
function print_question($question_num) {
$html = "";
$question_num = (int)$question_num;
$data = $this->parseQuiz();
if ($data !== false) {
$a = $data['questions'][$question_num];
$count_a = count($data['questions']);
if ($question_num <= $count_a) {
if ($question_num <= 0) {
$html .= '
'.$data['title'].'
';
$html .= $data['intro'];
if (($question_num != 0) && ($this->highscore )) $html .= $this->print_highscore();
$html .= '';
}
} else {
// mostra le risposte giuste
//display test results
$total_answers = count($_POST['answers']);
$total_correct_answers = 0;
$total_score = 0;
foreach ($_POST['answers'] as $_question_num => $_users_answer) {
if ($this->checkanswer($_question_num)) {
$total_correct_answers++;
$total_score = $total_score + $data['questions'][$_question_num]['score'];
}
}
$html .= ''.$data['title'].' - '.$this->quizobj->getLang('quiz_result').'
';
if ($this->showintro) $html .= $data['intro'];
$html .= '';
$html .= '
';
$percent = round(($total_correct_answers / $total_answers) * 100, 2);
$html .= sprintf($this->quizobj->getLang('quiz_resulttext'), $total_correct_answers, $total_answers, $percent);
$html .= '
';
$html .= $this->quizobj->getLang('quiz_score').$total_score . '
';
$html .= '
';
// descrizione del risultato per scaglioni
if ($data['scores']) {
foreach ($data['scores'] as $scores) {
if (($total_score >= $scores['range'][0]) && ($total_score <= $scores['range'][1])) {
$html .= '
';
$html .= $scores['text'];
$html .= '
';
}
}
}
$html .= '
';
$html .= '';
$html .= '
';
if ($this->mode == 1 && $question_num > 1) {
$html .= "
";
for ($i = 1; $i <= $count_a; $i++) {
$html .= $this->learning_mode_print_answer($i );
}
}
$scorelist = $this->updateScore($total_score, $percent);
$html .= '
';
}
} else {
$html .= $this->quiz_error();
}
return $html;
}
function learning_mode_print_answer($question_num) {
$ok = false;
if ($this->checkanswer($question_num, &$answers)) {
$ok = true;
$div = "correct";
} else {
$div = "wrong";
}
$a = $this->parseQuiz();
$a = $a['questions'][$question_num];
$html = "";
$html .= '';
$html .= '
';
$html .= '
';
$html .= $question_num.") ".$a['question']; //." ".$img;
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
if ($a['explanation'] != '') {
$html .= '
'.$this->quizobj->getLang('quiz_explanation').'' . $a['explanation'].'';
}
$html .= '
';
$html .= '
';
return $html ;
}
function checkanswer($question, $answer_array = null) {
$result = false;
if ($this->parseQuiz() !== false && $question > 0) {
$data = $this->parseQuiz();
$a = $data['questions'][$question];
$your_answer = '';
$correct_answer = '';
if ($a['type'] == 'single') {
$your_answer = $_POST['answers'][$question];
for($n=1; $n<=count($a['choices']); $n++) {
$answer_array[$n] = ($n == $your_answer?1:0);
}
$result = $a['choices'][$your_answer]['correct'];
} else {
if ($a['type'] == 'text') {
$your_answer = $_POST['answers'][$question];
$answer_array[1] = $your_answer;
$result = (strtolower($your_answer) == strtolower($a['answers'][0]));
} else {
if ($a['type'] == 'multi') {
$your_answers = $_POST['answers'][$question];
ksort($your_answers);
$result = true;
for($n=1; $n<=count($a['choices']); $n++) {
$answer_array[$n] = (isset($your_answers[$n])? 1:0);
if ( (($answer_array[$n] == 1) && !$a['choices'][$n]['correct']) ||
(($answer_array[$n] == 0) && $a['choices'][$n]['correct']) ) {
$result = false;
}
}
}
}
}
}
return $result;
}
function quiz_error() {
return $this->error;
}
function parseQuiz() {
if ($this->parseQuiz_cache === null) {
$page = $this->quizID;
$quizz = array();
$pos = strrpos(utf8_decode($page), ':');
$pageName = p_get_first_heading($page);
if($pageName == NULL) {
if($pos != FALSE) {
$pageName = utf8_substr($page, $pos+1, utf8_strlen($page));
} else {
$pageName = $page;
}
$pageName = str_replace('_', ' ', $pageName);
}
$quizz['title'] = $pageName;
$select= rawWiki($page);
$lines = explode("\n", $select);
$nr = count($lines);
$hRender = new Doku_Renderer_xhtml();
$nquest = 0;
$quizz['questions'] = array();
for($n=0; $n<$nr; $n++) {
if (substr($lines[$n], 0, 6) == "======") continue;
if (substr($lines[$n], 0, 5) == "=====") {
$temp = str_replace("=====",'',$lines[$n]);
$tmp_array = array();
$tmp_array['range']= explode("-", $temp);
for($k=$n+1; $k<$nr; $k++) {
if ((trim($lines[$k]) == '') || (substr($lines[$k], 0, 5) != "=====")) {
$tmp_array['text'] .= $lines[$k]."\n";
} else {
$n = $k - 1;
$k = $nr + 1;
}
};
$tmp_array['text'] = $hRender->render($tmp_array['text']);
$quizz['scores'][]= $tmp_array;
};
if ((trim($lines[$n]) == '') || (substr($lines[$n], 0, 4) != " * ")) {
if ($nquest == 0) $quizz['intro'] .= $lines[$n]."\n";
continue;
}
$nquest++;;
$tmp = $hRender->render(str_replace(" * ",'',$lines[$n]));
$tmp = substr($tmp, 4);
$tmp = substr($tmp, 0, -5);
$quizz['questions'][$nquest]['question'] = $tmp;
$nchoices = 0;
$quizz['questions'][$nquest]['choices'] = array();
$quizz['questions'][$nquest]['answers'] = array();
$quizz['questions'][$nquest]['score']= 1;
$quizz['questions'][$nquest]['explanation'] = '';
$temp_choices = array();
for($k=$n+1; $k<$nr; $k++) {
if (trim($lines[$k]) == '') continue;
if (substr($lines[$k], 0, 6) == " - ") {
$nchoices++;
$tmp = $hRender->render(str_replace(" - ",'',$lines[$k]));
$tmp = substr($tmp, 4);
$tmp = substr($tmp, 0, -5);
$quizz['questions'][$nquest]['choices'][$nchoices]['text'] = $tmp;
$quizz['questions'][$nquest]['choices'][$nchoices]['correct'] = false;
} elseif (substr($lines[$k], 0, 7) == " * [") {
$temp = str_replace(" * [",'',$lines[$k]);
$temp = ereg_replace("].*", "", $temp);
$quizz['questions'][$nquest]['answers'] = explode("|", $temp);
sort($quizz['questions'][$nquest]['answers']);
for($i=0; $irender(str_replace(" * ",'',$lines[$k]));
$tmp = substr($tmp, 4);
$tmp = substr($tmp, 0, -5);
$quizz['questions'][$nquest]['explanation'] = $tmp;
} else {
$n = $k - 1;
$k = $nr + 1;
}
}
if ($this->rndchoice) $this->kshuffle($quizz['questions'][$nquest]['choices']);
if (count($quizz['questions'][$nquest]['choices']) == 0) {
$quizz['questions'][$nquest]['type'] = 'text';
} elseif (count($quizz['questions'][$nquest]['answers']) > 1) {
$quizz['questions'][$nquest]['type'] = 'multi';
} else {
$quizz['questions'][$nquest]['type'] = 'single';
}
}
if ($this->rndquest) $this->kshuffle($quizz['questions']);
$quizz['intro'] = $hRender->render($quizz['intro']);
$this->parseQuiz_cache = $quizz;
}
return $this->parseQuiz_cache;
}
// sequenza casuale, fissa per sessione
function kshuffle(&$items) {
$temp = $items;
srand(hexdec(session_id()));
shuffle($temp);
for($k=1; $k<=count($temp); $k++) {
$items[$k] = $temp[$k-1];
}
}
// visualizza i migliori 10 punteggi
function print_highscore() {
// get score file contents
$pfile = metaFN(md5($this->quizID), '.quiz');
$scorelist = unserialize(@file_get_contents($pfile));
$temp = $scorelist['rows'];
usort($temp, array($this,'cmp'));
$temp = array_slice($temp, 0, 10);
$html = "";
$html .= '10 '.$this->quizobj->getLang('quiz_highscore').' ('.$scorelist['total'].')
';
$html .= '';
$html .= '';
$html .= ' User |
IP |
Score |
% |
Date | ';
$html .= '
';
foreach ($temp as $row) {
$html .= "";
$html .= ''.$row['user'].' | ';
$html .= ''.$row['ip'].' | ';
$html .= ''.$row['score'].' | ';
$html .= ''.$row['percent'].' | ';
$html .= ''.dformat($row['timestamp']).' | ';
$html .= "
";
}
$html .= "
";
return $html;
}
// Comparison function
function cmp($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
}
// aggiorna il file dei risultati
function updateScore($userscore, $percent) {
// get score file contents
$pfile = metaFN(md5($this->quizID), '.quiz');
$score = unserialize(@file_get_contents($pfile));
if(!isset($score['total'])) $score['total'] = 0;
if(!isset($score['rows'])) {
$score['rows'] = array();
}
if(!isset($score['ips'])) $score['ips'] = array();
if(!isset($score['users'])) $score['users'] = array();
$ip = clientIP(true);
$score['total']++;
$tmp = '(anonymous)';
if($_SERVER['REMOTE_USER']) $tmp = $_SERVER['REMOTE_USER'];
$score['users'][] =$tmp;
$score['ips'][] = $ip;
$score['rows'][$score['total']]['percent'] = $percent;
$score['rows'][$score['total']]['score'] = $userscore;
$score['rows'][$score['total']]['user'] = $tmp;
$score['rows'][$score['total']]['ip'] = $ip;
$score['rows'][$score['total']]['timestamp'] = time();
if($fh = fopen($pfile, 'w')) {
fwrite($fh, serialize($score));
fclose($fh);
}
return $score;
}
}
?>