Skip to content

Commit

Permalink
Admin: autocomplete in user search
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Oct 18, 2023
1 parent 2a4994f commit c911f93
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
34 changes: 32 additions & 2 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handleStart');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAction');

$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAdminAjax');
}

/**
Expand Down Expand Up @@ -83,5 +83,35 @@ public function handleAction(Doku_Event $event, $param)
}
}

}
/**
* Check username input against all users with saved LMS data
* and return a list of matching names
*
* @param Doku_Event $event
* @return void
*/
public function handleAdminAjax(Doku_Event $event)
{
if ($event->data !== 'plugin_lms_autocomplete') return;
global $INPUT;

if (!checkSecurityToken()) return;

$event->preventDefault();
$event->stopPropagation();

/** @var helper_plugin_lms $hlp */
$hlp = $this->loadHelper('lms');

$knownUsers = $hlp->getKnownUsers();

$search = $INPUT->str('user');
$found = array_filter($knownUsers, function ($user) use ($search) {
return (strstr(strtolower($user), strtolower($search))) !== false ? $user : null;
});

header('Content-Type: application/json');

echo json_encode($found);
}
}
9 changes: 4 additions & 5 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function html()

echo '<h1>' . $this->getLang('menu') . '</h1>';

$form = new dokuwiki\Form\Form(['method' => 'POST']);
$form = new dokuwiki\Form\Form(['method' => 'POST', 'id' => 'lms__admin-autocomplete']);
$form->addTextInput('user', $this->getLang('username'));
$form->addButton('submit', '🔍');
echo '<p>'. $form->toHTML() .'</p>';
Expand All @@ -46,15 +46,14 @@ public function html()
echo html_wikilink($id);
echo '</td>';
echo '<td>';
if($dt){
if ($dt){
echo dformat($dt);
} else {
echo '---';
}
echo '</td>';
echo '</tr>';
}
echo '</table>';


}
}

17 changes: 17 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ protected function getUserFile($user)
return $conf['metadir'] . '_lms/' . $user . '.lms';
}

public function getKnownUsers()
{
global $conf;

$lmsData = $conf['metadir'] . '_lms/';

$s = scandir($lmsData);

$users = array_map(function ($file) {
if (!in_array($file, ['.', '..'])) {
return str_replace('.lms', '', $file);
}
}, $s);

return array_filter($users);
}

/**
* Get a list of links from the given control page
*
Expand Down
28 changes: 23 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ jQuery(function (){
if(!JSINFO['plugins']) return;
if(!JSINFO.plugins['lms']) return;

jQuery('a.wikilink1, a.wikilink2').each(function (idx, elem){
if (JSINFO.ACT !== 'admin') {
jQuery('a.wikilink1, a.wikilink2').each(function (idx, elem){

if(elem['title'] && JSINFO.plugins.lms.seen.includes(elem.title)) {
jQuery(elem).addClass('lms-seen');
}
if(elem['title'] && JSINFO.plugins.lms.seen.includes(elem.title)) {
jQuery(elem).addClass('lms-seen');
}

});
});
}

// mark whole sections seen
const navheaders = jQuery('nav div.content h1, nav div.content h2, nav div.content h3, nav div.content h4, nav div.content h5');
Expand All @@ -21,4 +23,20 @@ jQuery(function (){
}
});

/**
* admin interface: autocomplete users
*/
const $form = jQuery('.dokuwiki.mode_admin form#lms__admin-autocomplete');
if (!$form.length) return;

$form.find('input')
.autocomplete({
source: function (request, response) {
jQuery.getJSON(DOKU_BASE + 'lib/exe/ajax.php?call=plugin_lms_autocomplete', {
user: request.term,
sectok: $form.find('input[name="sectok"]').val()
}, response);
},
minLength: 0
});
});

0 comments on commit c911f93

Please sign in to comment.