1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin lms (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
10 */
11class syntax_plugin_lms_lms extends SyntaxPlugin
12{
13    /** @var helper_plugin_lms */
14    protected $hlp;
15
16    /** @var string current user */
17    protected $user;
18
19    /**
20     * Constructor
21     */
22    public function __construct()
23    {
24        global $INPUT;
25        $this->hlp = $this->loadHelper('lms');
26        $this->user = $INPUT->server->str('REMOTE_USER');
27    }
28
29    /** @inheritDoc */
30    public function getType()
31    {
32        return 'substition';
33    }
34
35    /** @inheritDoc */
36    public function getPType()
37    {
38        return 'normal';
39    }
40
41    /** @inheritDoc */
42    public function getSort()
43    {
44        return 150;
45    }
46
47    /** @inheritDoc */
48    public function connectTo($mode)
49    {
50        $this->Lexer->addSpecialPattern('~~LMS~~', $mode, 'plugin_lms_lms');
51    }
52
53    /** @inheritDoc */
54    public function handle($match, $state, $pos, Doku_Handler $handler)
55    {
56        $data = [];
57
58        return $data;
59    }
60
61    /** @inheritDoc */
62    public function render($mode, Doku_Renderer $renderer, $data)
63    {
64        if ($mode !== 'xhtml') {
65            return false;
66        }
67        $renderer->nocache();
68        if (!$this->user) return true;
69
70        global $INFO;
71        $seen = $this->hlp->getLesson($INFO['id'], $this->user);
72        if ($seen === false) return true; // we're not on a lesson page
73
74        $renderer->doc .= '<div class="lms-nav">';
75        $renderer->doc .= $this->prevButton();
76        $renderer->doc .= $this->toggleButton($seen);
77        $renderer->doc .= $this->nextButton();
78        $renderer->doc .= '</div>';
79
80        return true;
81    }
82
83    /**
84     * Build a navigation link based on the given command
85     *
86     * @param string $cmd
87     * @return string
88     */
89    protected function makeLink($id, $cmd)
90    {
91        $args = [
92            'do' => 'lms',
93            'lms' => $cmd,
94            'sectok' => getSecurityToken(),
95        ];
96
97        $attr = [
98            'href' => wl($id, $args, false, '&'),
99            'class' => "lms-btn lms-btn-$cmd",
100            'title' => $this->getLang($cmd),
101        ];
102
103        $svg = inlineSVG(__DIR__ . '/../img/' . $cmd . '.svg');
104        $span = '<span class="a11y">' . hsc($this->getLang($cmd)) . '</span>';
105
106        return '<a ' . buildAttributes($attr) . '>' . $span . $svg . '</a>';
107    }
108
109    /**
110     * Toggle seen status
111     *
112     * @param bool|null $seen current seen status
113     * @return string
114     */
115    public function toggleButton($seen = null)
116    {
117        global $INFO;
118
119        if ($seen === null) {
120            $seen = $this->hlp->getLesson($INFO['id'], $this->user);
121        }
122
123        if ($seen) {
124            return $this->makeLink($INFO['id'], 'unseen');
125        }
126        return $this->makeLink($INFO['id'], 'seen');
127    }
128
129    /**
130     * Navigate to next lesson
131     *
132     * @return string
133     */
134    public function nextButton()
135    {
136        global $INFO;
137        $next = $this->hlp->getNextLesson($INFO['id']);
138        if (!$next) return '';
139        return $this->makeLink($next, 'next');
140    }
141
142    /**
143     * Navigate to previous lesson
144     *
145     * @return string
146     */
147    public function prevButton()
148    {
149        global $INFO;
150        $prev = $this->hlp->getPrevLesson($INFO['id']);
151        if (!$prev) return '';
152        return $this->makeLink($prev, 'prev');
153    }
154}
155