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->setHiddenField('do', 'admin'); 126 $form->setHiddenField('page', 'acknowledge_report'); 127 $form->addTextInput('user', $this->getLang('overviewUser')); 128 $form->addButton('', '>'); 129 echo $form->toHTML(); 130 131 $form = new Form(['method' => 'GET']); 132 $form->setHiddenField('do', 'admin'); 133 $form->setHiddenField('page', 'acknowledge_report'); 134 $form->addTextInput('pg', $this->getLang('pattern'))->val($ID); 135 $form->addButton('', '>'); 136 echo $form->toHTML(); 137 echo '</nav>'; 138 } 139 140 /** 141 * Print the given acknowledge data 142 * 143 * @param array $data 144 * @return int number of acknowledged entries 145 */ 146 protected function htmlTable($data) 147 { 148 echo '<table>'; 149 echo '<tr>'; 150 echo '<th>' . $this->getLang('overviewPage') . '</th>'; 151 echo '<th>' . $this->getLang('overviewUser') . '</th>'; 152 echo '<th>' . $this->getLang('overviewMod') . '</th>'; 153 echo '<th>' . $this->getLang('overviewTime') . '</th>'; 154 echo '<th>' . $this->getLang('overviewCurrent') . '</th>'; 155 echo '</tr>'; 156 157 $count = 0; 158 foreach ($data as $item) { 159 $current = $item['ack'] >= $item['lastmod']; 160 if ($current) $count++; 161 162 echo '<tr>'; 163 echo '<td>' . $this->pageLink($item['page']) . '</td>'; 164 echo '<td>' . $this->userLink($item['user']) . '</td>'; 165 echo '<td>' . html_wikilink( 166 ':' . $item['page'], 167 ($item['lastmod'] ? dformat($item['lastmod']) : '?') 168 ) . '</td>'; 169 echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>'; 170 echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>'; 171 echo '</tr>'; 172 } 173 echo '</table>'; 174 175 return $count; 176 } 177 178 protected function homeLink() 179 { 180 global $ID; 181 182 $url = wl( 183 $ID, 184 [ 185 'do' => 'admin', 186 'page' => 'acknowledge_report', 187 ] 188 ); 189 190 return '<a href="' . $url . '">' . $this->getLang('home') . '</a>'; 191 } 192 193 /** 194 * Link to the user overview 195 * 196 * @param string $user 197 * @return string 198 */ 199 protected function userLink($user) 200 { 201 global $ID; 202 203 $url = wl( 204 $ID, 205 [ 206 'do' => 'admin', 207 'page' => 'acknowledge_report', 208 'user' => $user, 209 ] 210 ); 211 212 return '<a href="' . $url . '">' . hsc($user) . '</a>'; 213 } 214 215 /** 216 * Link to the page overview 217 * 218 * @param string $page 219 * @return string 220 */ 221 protected function pageLink($page) 222 { 223 global $ID; 224 225 $url = wl( 226 $ID, 227 [ 228 'do' => 'admin', 229 'page' => 'acknowledge_report', 230 'pg' => $page, 231 ] 232 ); 233 234 return '<a href="' . $url . '">' . hsc($page) . '</a>'; 235 } 236 237 /** @inheritdoc */ 238 public function getTOC() 239 { 240 global $ID; 241 return [ 242 html_mktocitem( 243 wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']), 244 $this->getLang('menu'), 245 0, 246 '' 247 ), 248 html_mktocitem( 249 wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']), 250 $this->getLang('menu_assign'), 251 0, 252 '' 253 ), 254 ]; 255 } 256} 257