1<?php
2/**
3 * Mikio Syntax Plugin: Quiz Item
4 *
5 * @author  James Collins <james.collins@outlook.com.au>
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @link    http://github.com/nomadjimbob/mikioplugin
8 */
9
10if (!defined('DOKU_INC')) {
11    die();
12}
13if (!defined('DOKU_PLUGIN')) {
14    define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
15}
16require_once dirname(__FILE__).'/core.php';
17
18class syntax_plugin_mikioplugin_quizitem extends syntax_plugin_mikioplugin_core
19{
20    public $tag                 = 'quiz-item';
21    public $requires_tag        = 'quiz';
22    public $hasEndTag           = false;
23    public $options             = array(
24        'type'          => array('type'     => 'choice',
25                                 'data'     => array('choice', 'multiple'),
26                                 'default'  => 'choice'),
27        'question'      => array('type'     => 'text',          'default'   => ''),
28        'options'       => array('type'     => 'text',          'default'   => ''),
29        'answer'        => array('type'     => 'text',          'default'   => ''),
30        'scores'        => array('type'     => 'text',          'default'   => ''),
31        'text'          => array('type'     => 'text',          'default'   => ''),
32    );
33
34    public function render_lexer_special(Doku_Renderer $renderer, $data)
35    {
36        $classes = $this->buildClass($data);
37
38        $data['question'] = $this->applyMarkdownEffects($data['question']);
39        $data['text'] = $this->applyMarkdownEffects($data['text']);
40
41        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-item' . $classes . '" data-question="' . $data['question'] . '" ' . ($data['answer'] != '' ? 'data-answer="' . $data['answer'] . '"' : '') . '>';
42        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-question">' . $data['question'] . '</div>';
43        if($data['text'] != '') { $renderer->doc .= '<p>' . $data['text'] . '</p>';
44        }
45        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-options">';
46
47        switch(strtolower($data['type'])) {
48        case 'choice':
49            $name = rand(10000, 99999);
50
51            $options = explode('|', $data['options']);
52            $scores = explode('|', $data['scores']);
53            foreach($options as $key => $option) {
54                $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-option"><label><input type="radio" name="' . $name . '" value="' . $option . '" ' . (isset($scores[$key]) && $scores[$key] != "" ? 'data-score="' . $scores[$key] . '" ' : '') . '/>' . $this->applyMarkdownEffects($option) . '</label></div>';
55            }
56            break;
57        case 'multiple':
58            $name = rand(10000, 99999);
59
60            $options = explode('|', $data['options']);
61            $scores = explode('|', $data['scores']);
62            $inGroup = false;
63            $groupKey = 0;
64
65            foreach($options as $key => $option) {
66                $endGroup = false;
67
68                if($inGroup === false && substr($option, 0, 1) === '[') {
69                    $inGroup = true;
70                    $option = substr($option, 1);
71                } else if($inGroup === true && substr($option, -1) === ']') {
72                    $endGroup = true;
73                    $option = substr($option, 0, -1);
74                }
75
76                if($inGroup === true) {
77                    $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-option"><label><input type="radio" name="' . $name . '-group-' . $groupKey . '" value="' . $option . '" ' . (isset($scores[$key]) && $scores[$key] != "" ? 'data-score="' . $scores[$key] . '" ' : '') . '/>' . $this->applyMarkdownEffects($option) . '</label></div>';
78                } else {
79                    $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-option"><label><input type="checkbox" name="' . $name . '-' . $key . '" value="' . $option . '" ' . (isset($scores[$key]) && $scores[$key] != "" ? 'data-score="' . $scores[$key] . '" ' : '') . '/>' . $this->applyMarkdownEffects($option) . '</label></div>';
80                }
81
82                if($endGroup === true) {
83                    $inGroup = false;
84                    $groupKey++;
85                }
86            }
87            break;
88        }
89
90        $renderer->doc .= '</div>';
91        $renderer->doc .= '</div>';
92    }
93}
94?>
95