1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\EventHandler; 5use dokuwiki\Extension\Event; 6 7/** 8 * DokuWiki Plugin lms (Action Component) 9 * 10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 11 * @author Andreas Gohr <dokuwiki@cosmocode.de> 12 */ 13class action_plugin_lms extends ActionPlugin 14{ 15 /** @inheritDoc */ 16 public function register(EventHandler $controller) 17 { 18 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handleStart'); 19 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAction'); 20 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAdminAjax'); 21 } 22 23 /** 24 * Initiatilize seen info for current user 25 * 26 * @param Event $event event object by reference 27 * @param mixed $param optional parameter passed when event was registered 28 * @return void 29 */ 30 public function handleStart(Event $event, $param) 31 { 32 global $JSINFO; 33 global $INPUT; 34 35 $user = $INPUT->server->str('REMOTE_USER'); 36 if (!$user) return; 37 38 /** @var helper_plugin_lms $hlp */ 39 $hlp = $this->loadHelper('lms'); 40 41 $JSINFO['plugins']['lms']['seen'] = array_keys($hlp->getUserLessons($user)); 42 } 43 44 45 /** 46 * Run user actions 47 * 48 * @param Event $event event object by reference 49 * @param mixed $param optional parameter passed when event was registered 50 * @return void 51 */ 52 public function handleAction(Event $event, $param) 53 { 54 global $INPUT; 55 global $ID; 56 57 $user = $INPUT->server->str('REMOTE_USER'); 58 if (!$user) return; 59 60 $act = act_clean($event->data); 61 if ($act !== 'lms') return; 62 63 $event->data = 'redirect'; 64 65 $action = $INPUT->str('lms'); 66 if (!$action) return; 67 68 if (!checkSecurityToken()) return; 69 70 /** @var helper_plugin_lms $hlp */ 71 $hlp = $this->loadHelper('lms'); 72 switch ($action) { 73 case 'seen': 74 $hlp->markLesson($ID, $user, true); 75 break; 76 case 'check': 77 $hlp->markLesson($ID, $user, true); 78 $next = $hlp->getNextLesson($ID, $user); 79 if ($next) { 80 $ID = $next; 81 } 82 break; 83 case 'unseen': 84 $hlp->markLesson($ID, $user, false); 85 break; 86 } 87 } 88 89 /** 90 * Check username input against all users with saved LMS data 91 * and return a list of matching names 92 * 93 * @param Event $event 94 * @return void 95 */ 96 public function handleAdminAjax(Event $event) 97 { 98 if ($event->data !== 'plugin_lms_autocomplete') return; 99 global $INPUT; 100 101 if (!checkSecurityToken()) return; 102 103 $event->preventDefault(); 104 $event->stopPropagation(); 105 106 /** @var helper_plugin_lms $hlp */ 107 $hlp = $this->loadHelper('lms'); 108 109 $knownUsers = $hlp->getKnownUsers(); 110 111 $search = $INPUT->str('user'); 112 $found = array_filter($knownUsers, function ($user) use ($search) { 113 return (strstr(strtolower($user), strtolower($search))) !== false ? $user : null; 114 }); 115 116 header('Content-Type: application/json'); 117 118 echo json_encode($found); 119 } 120} 121