1<?php
2/**
3 * Mikio Syntax Plugin: Quiz
4  *
5 * @link        http://github.com/nomadjimbob/mikioplugin
6 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author      James Collins <james.collins@outlook.com.au>
8 */
9if (!defined('DOKU_INC')) die();
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(dirname(__FILE__).'/core.php');
12
13class syntax_plugin_mikioplugin_quiz extends syntax_plugin_mikioplugin_core {
14    public $tag                 = 'quiz';
15    public $hasEndTag           = true;
16    public $options             = array(
17        'resettable'   => array('type'     => 'boolean',          'default'   => 'false'),
18        'full'   => array('type'     => 'boolean',          'default'   => 'false'),
19        'reset-text'   => array('type'     => 'text',          'default'   => 'Retry'),
20        'submit-text'   => array('type'     => 'text',          'default'   => 'Submit'),
21        'submit-type'   => array('type' => 'text', 'default' => ''),
22        'prev-text'   => array('type'     => 'text',          'default'   => 'Prev'),
23        'next-text'   => array('type'     => 'text',          'default'   => 'Next'),
24        'correct-text'   => array('type'     => 'text',          'default'   => 'Correct'),
25        'incorrect-text'   => array('type'     => 'text',          'default'   => 'Incorrect'),
26        'status-text'   => array('type'     => 'text',          'default'   => 'Question $1 of $2'),
27        'result-correct-text'   => array('type'     => 'text',          'default'   => 'You got $1 out of $2 correct'),
28        'result-score-text'   => array('type'     => 'text',          'default'   => 'Score: $1'),
29        'result-score-total-text'   => array('type'     => 'text',          'default'   => 'Total score: $1'),
30    );
31
32    public function __construct() {
33        $this->addCommonOptions('type');
34        $this->options['type']['default'] = 'secondary';
35    }
36
37    public function render_lexer_enter(Doku_Renderer $renderer, $data) {
38        $classes = $this->buildClass($data);
39        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz ' . $classes . '" data-status="' . $this->applyMarkdownEffects($data['status-text']) . '" data-result-correct="' . $this->applyMarkdownEffects($data['result-correct-text']) . '" data-result-score="' . $this->applyMarkdownEffects($data['result-score-text']) . '" data-result-score-total="' . $this->applyMarkdownEffects($data['result-score-total-text']) . '" data-correct="' . $this->applyMarkdownEffects($data['correct-text']) . '" data-incorrect="' . $this->applyMarkdownEffects($data['incorrect-text']) . '"' . ($data['full'] == true ? ' data-full="true"' : '') . '>';
40    }
41
42
43    public function render_lexer_exit(Doku_Renderer $renderer, $data) {
44        $classes = $this->buildClass($data);
45
46        if($data['submit-type'] == '') {
47            if(substr($data['type'], 0, 8) == 'outline-') {
48                $data['type'] = substr($data['type'], 8);
49            } else {
50                $data['type'] = 'outline-'.$data['type'];
51            }
52        } else {
53            $data['type'] = $data['submit-type'];
54        }
55
56        $oppositeClasses = $this->buildClass($data);
57        /*
58        ['type']
59        outline-
60        */
61
62        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-result"></div>';
63        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-status">';
64        if($data['full'] == false) {
65            $renderer->doc .= '<span class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-status-text"></span>';
66            $renderer->doc .= '<button class="' . $this->elemClass . ' ' . $this->classPrefix . 'button ' . $this->classPrefix . 'quiz-button-prev ' . $classes . '">&laquo; ' . $data['prev-text'] . '</button>';
67        }
68        $renderer->doc .= '<button class="' . $this->elemClass . ' ' . $this->classPrefix . 'button ' . $this->classPrefix . 'quiz-button-submit ' . $oppositeClasses . '">' . $data['submit-text'] . '</button>';
69        if($data['resettable'] == true) {
70            $renderer->doc .= '<button class="' . $this->elemClass . ' ' . $this->classPrefix . 'button ' . $this->classPrefix . 'quiz-button-reset ' . $oppositeClasses . '">' . $data['reset-text'] . '</button>';
71        }
72        if($data['full'] == false) {
73            $renderer->doc .= '<button class="' . $this->elemClass . ' ' . $this->classPrefix . 'button ' . $this->classPrefix . 'quiz-button-next ' . $classes . '">' . $data['next-text'] . ' &raquo;</button>';
74        }
75        $renderer->doc .= '</div>';
76        $renderer->doc .= '</div>';
77    }
78}
79?>