Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin: suggest users #1

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/dokuwiki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: DokuWiki Default Tasks
on:
push:
pull_request:
schedule:
- cron: '56 15 18 * *'


jobs:
all:
uses: dokuwiki/github-action/.github/workflows/all.yml@main
52 changes: 0 additions & 52 deletions .github/workflows/phpTestLinux.yml

This file was deleted.

34 changes: 29 additions & 5 deletions _test/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,42 @@ public function testMarkRead()
public function testNextLesson() {
$hlp = new \helper_plugin_lms();

$result = $hlp->getNextLesson('nope');
$id = 'nope';
$this->setIdGlobals($id);
$result = $hlp->getNextLesson($id);
$this->assertEquals(false, $result, '$id is not a lesson');

$result = $hlp->getNextLesson('link');
$id = 'link';
$this->setIdGlobals($id);
$result = $hlp->getNextLesson($id);
$this->assertEquals(false, $result, '$id is last lesson');

$result = $hlp->getNextLesson('this');
$id = 'this';
$this->setIdGlobals($id);
$result = $hlp->getNextLesson($id);
$this->assertEquals('foo:bar', $result, 'next lesson no user context');

$hlp->markLesson('foo:bar', 'test', true);
$result = $hlp->getNextLesson('this', 'test');
$result = $hlp->getNextLesson($id, 'test');
$this->assertEquals('another_link', $result, 'skip seen lesson');
}

public function testPrevLesson() {
$hlp = new \helper_plugin_lms();

$id = 'nope';
$this->setIdGlobals($id);
$result = $hlp->getPrevLesson('nope');
$this->assertEquals(false, $result, '$id is not a lesson');

$id = 'this';
$this->setIdGlobals($id);
$result = $hlp->getPrevLesson('this');
$this->assertEquals(false, $result, '$id is first lesson');

$result = $hlp->getPrevLesson('another_link');
$id = 'another_link';
$this->setIdGlobals($id);
$result = $hlp->getPrevLesson($id);
$this->assertEquals('foo:bar', $result, 'prev lesson no user context');

$hlp->markLesson('foo:bar', 'test', true);
Expand Down Expand Up @@ -129,4 +141,16 @@ public function testParseControlPageMissing()

$this->assertEquals($expected, $result);
}

/**
* @param string $id
* @return void
*/
protected function setIdGlobals($id)
{
global $ID;
global $INFO;
$ID = $id;
$INFO['id'] = $ID;
}
}
35 changes: 32 additions & 3 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
*/
class action_plugin_lms extends \dokuwiki\Extension\ActionPlugin
{

/** @inheritDoc */
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 +82,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);
}
}
17 changes: 8 additions & 9 deletions admin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* DokuWiki Plugin lms (Admin Component)
*
Expand All @@ -13,7 +14,6 @@ public function forAdminOnly()
return false;
}


/** @inheritDoc */
public function handle()
{
Expand All @@ -27,34 +27,33 @@ 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>';
echo '<p>' . $form->toHTML() . '</p>';

if(!$INPUT->str('user')) return;
if (!$INPUT->str('user')) return;

/** @var helper_plugin_lms $hlp */
$hlp = $this->loadHelper('lms');
$list = $hlp->getLessons($INPUT->str('user'));

echo sprintf('<h2>'.$this->getLang('status').'</h2>', hsc($INPUT->str('user')));
echo sprintf('<h2>' . $this->getLang('status') . '</h2>', hsc($INPUT->str('user')));
echo '<table class="inline">';
foreach ($list as $id => $dt) {
echo '<tr>';
echo '<td>';
echo html_wikilink($id);
echo '</td>';
echo '<td>';
if($dt){
if ($dt) {
echo dformat($dt);
} else {
echo '---';
}
echo '</td>';
echo '</tr>';
}
echo '</table>';


}
}

2 changes: 1 addition & 1 deletion conf/default.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/**
* Default settings for the lms plugin
*
* @author Andreas Gohr <dokuwiki@cosmocode.de>
*/

$conf['controlpage'] = 'lms';

3 changes: 1 addition & 2 deletions conf/metadata.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

/**
* Options for the lms plugin
*
* @author Andreas Gohr <dokuwiki@cosmocode.de>
*/


$meta['controlpage'] = array('string');

19 changes: 17 additions & 2 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
class helper_plugin_lms extends \dokuwiki\Extension\Plugin
{

/**
* Return all lessons and info about the user's current completion status
*
Expand Down Expand Up @@ -173,6 +172,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 Expand Up @@ -207,4 +223,3 @@ protected function parseControlPage($cp)
return $pages;
}
}

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
});
});