xref: /plugin/mikioplugin/syntax/quizitem.php (revision 2e607ed9d4bf870226f4a35db3bcd9ef9681d429)
1<?php
2/**
3 * Mikio Syntax Plugin: Quiz Item
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 */
9
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(dirname(__FILE__).'/core.php');
13
14class syntax_plugin_mikioplugin_quizitem extends syntax_plugin_mikioplugin_core {
15    public $tag                 = 'quiz-item';
16    public $requires_tag        = 'quiz';
17    public $hasEndTag           = false;
18    public $options             = array(
19        'type'          => array('type'     => 'choice',
20                                 'data'     => array('choice', 'multiple'),
21                                 'default'  => 'choice'),
22        'question'      => array('type'     => 'text',          'default'   => ''),
23        'options'       => array('type'     => 'text',          'default'   => ''),
24        'answer'        => array('type'     => 'text',          'default'   => ''),
25        'scores'        => array('type'     => 'text',          'default'   => ''),
26        'text'          => array('type'     => 'text',          'default'   => ''),
27    );
28
29    // public function getAllowedTypes() {  return array(); }
30
31    public function render_lexer_special(Doku_Renderer $renderer, $data) {
32        $classes = $this->buildClass($data);
33
34        $data['question'] = $this->applyMarkdownEffects($data['question']);
35        $data['text'] = $this->applyMarkdownEffects($data['text']);
36
37        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-item' . $classes . '" data-question="' . $data['question'] . '" ' . ($data['answer'] != '' ? 'data-answer="' . $data['answer'] . '"' : '') . '>';
38        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-question">' . $data['question'] . '</div>';
39        if($data['text'] != '') $renderer->doc .= '<p>' . $data['text'] . '</p>';
40        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'quiz-options">';
41
42        switch(strtolower($data['type'])) {
43            case 'choice':
44                $name = rand(10000, 99999);
45
46                $options = explode('|', $data['options']);
47                $scores = explode('|', $data['scores']);
48                foreach($options as $key => $option) {
49                    $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>';
50                }
51                break;
52            case 'multiple':
53                $name = rand(10000, 99999);
54
55                $options = explode('|', $data['options']);
56                $scores = explode('|', $data['scores']);
57                $inGroup = false;
58                $groupKey = 0;
59
60                foreach($options as $key => $option) {
61                    $endGroup = false;
62
63                    if($inGroup === false && substr($option, 0, 1) === '[') {
64                        $inGroup = true;
65                        $option = substr($option, 1);
66                    } else if($inGroup === true && substr($option, -1) === ']') {
67                        $endGroup = true;
68                        $option = substr($option, 0, -1);
69                    }
70
71                    if($inGroup === true) {
72                        $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>';
73                    } else {
74                        $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>';
75                    }
76
77                    if($endGroup === true) {
78                        $inGroup = false;
79                        $groupKey++;
80                    }
81                }
82                break;
83        }
84
85        $renderer->doc .= '</div>';
86        $renderer->doc .= '</div>';
87    }
88}
89?>
90