1<?php 2 3use dokuwiki\Extension\AdminPlugin; 4use dokuwiki\Form\Form; 5use dokuwiki\Extension\AuthPlugin; 6 7/** 8 * DokuWiki Plugin acknowledge (Admin Component) 9 * 10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 11 * @author Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de> 12 */ 13class admin_plugin_acknowledge_report extends AdminPlugin 14{ 15 /** @inheritdoc */ 16 public function forAdminOnly() 17 { 18 return false; 19 } 20 21 /** @inheritdoc */ 22 public function handle() 23 { 24 } 25 26 /** @inheritdoc */ 27 public function html() 28 { 29 global $INPUT; 30 31 echo '<div class="plugin_acknowledgement_admin">'; 32 echo '<h1>' . $this->getLang('menu') . '</h1>'; 33 $this->htmlForms(); 34 $user = $INPUT->str('user'); 35 $pg = $INPUT->str('pg'); 36 if ($pg) { 37 $this->htmlPageStatus($pg, $user); 38 } elseif ($user) { 39 $this->htmlUserStatus($user); 40 } else { 41 $this->htmlLatest(); 42 } 43 echo '</div>'; 44 } 45 46 /** 47 * Show which users have or need ot acknowledge a specific page 48 * 49 * @param string $pattern A page assignment pattern 50 * @param string $user Optional user 51 */ 52 protected function htmlPageStatus($pattern, $user = '') 53 { 54 global $lang; 55 global $INPUT; 56 57 /** @var helper_plugin_acknowledge $helper */ 58 $helper = plugin_load('helper', 'acknowledge'); 59 60 $status = $INPUT->str('status'); 61 $pages = $helper->getPagesMatchingPattern($pattern); 62 $acknowledgements = []; 63 64 foreach ($pages as $pattern) { 65 $acknowledgements = array_merge($acknowledgements, $helper->getPageAcknowledgements($pattern, $user, $status, 1000)); 66 if (count($acknowledgements) > 1000) { 67 // don't show too many 68 msg($this->getLang('toomanyresults'), 0); 69 break; 70 } 71 } 72 73 if (!$acknowledgements) { 74 echo '<p>' . $lang['nothingfound'] . '</p>'; 75 return; 76 } 77 78 $this->htmlTable($acknowledgements); 79 } 80 81 /** 82 * Show what a given user should sign and has 83 * 84 * @param string $user 85 */ 86 protected function htmlUserStatus($user) 87 { 88 /** @var AuthPlugin $auth */ 89 global $auth; 90 global $lang; 91 global $INPUT; 92 93 $user = $auth->cleanUser($user); 94 $userinfo = $auth->getUserData($user, true); 95 if (!$userinfo) { 96 echo '<p>' . $lang['nothingfound'] . '</p>'; 97 return; 98 } 99 100 /** @var helper_plugin_acknowledge $helper */ 101 $helper = plugin_load('helper', 'acknowledge'); 102 103 $status = $INPUT->str('status'); 104 105 if ($status === 'current') { 106 $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'current'); 107 } elseif ($status === 'due') { 108 $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'due'); 109 } else { 110 $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'all'); 111 } 112 $count = $this->htmlTable($assignments); 113 echo '<p>' . sprintf($this->getLang('count'), hsc($user), $count, count($assignments)) . '</p>'; 114 } 115 116 /** 117 * Show the latest 100 acknowledgements 118 */ 119 protected function htmlLatest() 120 { 121 /** @var helper_plugin_acknowledge $helper */ 122 $helper = plugin_load('helper', 'acknowledge'); 123 $acks = $helper->getAcknowledgements(); 124 $this->htmlTable($acks); 125 echo '<p>' . $this->getLang('overviewHistory') . '</p>'; 126 } 127 128 /** 129 * @return void 130 */ 131 protected function htmlForms() 132 { 133 echo '<nav>'; 134 echo $this->homeLink(); 135 136 $form = new Form(['method' => 'GET']); 137 $form->id('acknowledge__user-autocomplete'); 138 $form->setHiddenField('do', 'admin'); 139 $form->setHiddenField('page', 'acknowledge_report'); 140 $form->addTextInput('pg', $this->getLang('pattern')); 141 $form->addTextInput('user', $this->getLang('overviewUser')) 142 ->attr('type', 'search'); 143 $form->addDropdown( 144 'status', 145 [ 146 'all' => $this->getLang('all'), 147 'current' => $this->getLang('current'), 148 'due' => $this->getLang('due'), 149 ], 150 $this->getLang('status') 151 ); 152 $form->addButton('', '>'); 153 echo $form->toHTML(); 154 echo '</nav>'; 155 } 156 157 /** 158 * Print the given acknowledge data 159 * 160 * @param array $data 161 * @return int number of acknowledged entries 162 */ 163 protected function htmlTable($data) 164 { 165 echo '<table>'; 166 echo '<tr>'; 167 echo '<th>#</th>'; 168 echo '<th>' . $this->getLang('overviewPage') . '</th>'; 169 echo '<th>' . $this->getLang('overviewUser') . '</th>'; 170 echo '<th>' . $this->getLang('overviewMod') . '</th>'; 171 echo '<th>' . $this->getLang('overviewTime') . '</th>'; 172 echo '<th>' . $this->getLang('overviewCurrent') . '</th>'; 173 echo '</tr>'; 174 175 $count = 0; 176 $i = 0; 177 foreach ($data as $item) { 178 $current = $item['ack'] >= $item['lastmod']; 179 if ($current) $count++; 180 $i++; 181 echo '<tr>'; 182 echo "<td>$i</td>"; 183 echo '<td>' . $this->pageLink($item['page']) . '</td>'; 184 echo '<td>' . $this->userLink($item['user']) . '</td>'; 185 echo '<td>' . html_wikilink( 186 ':' . $item['page'], 187 ($item['lastmod'] ? dformat($item['lastmod']) : '?') 188 ) . '</td>'; 189 echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>'; 190 echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>'; 191 echo '</tr>'; 192 } 193 echo '</table>'; 194 195 return $count; 196 } 197 198 protected function homeLink() 199 { 200 global $ID; 201 202 $url = wl( 203 $ID, 204 [ 205 'do' => 'admin', 206 'page' => 'acknowledge_report', 207 ] 208 ); 209 210 return '<a href="' . $url . '">' . $this->getLang('home') . '</a>'; 211 } 212 213 /** 214 * Link to the user overview 215 * 216 * @param string $user 217 * @return string 218 */ 219 protected function userLink($user) 220 { 221 global $ID; 222 223 $url = wl( 224 $ID, 225 [ 226 'do' => 'admin', 227 'page' => 'acknowledge_report', 228 'user' => $user, 229 ] 230 ); 231 232 return '<a href="' . $url . '">' . hsc($user) . '</a>'; 233 } 234 235 /** 236 * Link to the page overview 237 * 238 * @param string $page 239 * @return string 240 */ 241 protected function pageLink($page) 242 { 243 global $ID; 244 245 $url = wl( 246 $ID, 247 [ 248 'do' => 'admin', 249 'page' => 'acknowledge_report', 250 'pg' => $page, 251 ] 252 ); 253 254 return '<a href="' . $url . '">' . hsc($page) . '</a>'; 255 } 256 257 /** @inheritdoc */ 258 public function getTOC() 259 { 260 global $ID; 261 return [ 262 html_mktocitem( 263 wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']), 264 $this->getLang('menu'), 265 0, 266 '' 267 ), 268 html_mktocitem( 269 wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']), 270 $this->getLang('menu_assign'), 271 0, 272 '' 273 ), 274 ]; 275 } 276} 277