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