Commit f6430648 authored by Martyn Smith's avatar Martyn Smith Committed by Martyn Smith
Browse files

Userlist element for forms

parent 05336dc4
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage form-element
* @author Martyn Smith <martyn@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* Provides a basic text field input.
*
* @todo this is just lies ...
* @param array $element The element to render
* @param Form $form The form to render the element for
* @return string The HTML for the element
*/
function form_render_userlist($element, Form $form) {
$smarty = smarty();
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';
$value = $form->get_value($element);
if (is_array($value) && count($value)) {
$members = get_records_select('usr','id IN (' . join(',',$value) . ')', null, '', 'id,firstname,lastname,prefname');
foreach($members as &$member) {
// @todo should be using the pref/full name function here
$member = $member->firstname;
}
$smarty->assign('options',$members);
$smarty->assign('value', join(',',$form->get_value($element)));
}
else if (!is_array($value)) {
// @todo use the default
}
$smarty->assign('name', $element['name']);
return $smarty->fetch('form/userlist.tpl');
}
function form_get_value_userlist($element, Form $form) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
if (isset($global[$name])) {
$value = $global[$name];
if ($value == '') {
return array();
}
if (preg_match('/^(\d+(,\d+)*)$/',$value)) {
return array_map('intval', explode(',', $value));
}
throw new FormException("Invalid value for userlist form element '$name' = '$value'");
}
return null;
}
function form_is_empty_userlist($value, $element) {
if (is_array($value) && count($value)) {
return false;
}
return true;
}
?>
<script type="text/javascript">
var {{$name}}_d;
var {{$name}}_searchfunc = function (q) {
processingStart();
replaceChildNodes('{{$name}}_messages');
{{$name}}_d = loadJSONDoc('{{$WWWROOT}}json/usersearch.php', {'query':q, 'limit': 100});
{{$name}}_d.addCallbacks(
function (users) {
processingStop();
if(users.error) {
displayMessage(users.message,'error');
return;
}
var members = new Array();
forEach($('{{$name}}_members').childNodes, function(node) {
members[node.value] = 1;
});
replaceChildNodes('{{$name}}_potential');
forEach(users.results, function(user) {
if (members[user.id]) {
return;
}
appendChildNodes('{{$name}}_potential',OPTION({'value':user.id},user.name));
});
if(users.count > users.limit) {
replaceChildNodes('{{$name}}_messages', DIV(null, 'Only showing first ' + users.limit + ' results of ' + users.count));
}
},
function (err) {
processingStop();
displayMessage(get_string('errorloadingusers'),'error');
}
);
}
addLoadEvent(function () {
removeElement($('{{$name}}_potential').childNodes[0]);
removeElement($('{{$name}}_members').childNodes[0]);
{{$name}}_searchfunc('');
connect('{{$name}}_search', 'onkeypress', function (k) {
if (k.key().code == 13) {
{{$name}}_searchfunc($('{{$name}}_search').value);
k.stop();
}
});
});
function {{$name}}_moveopts(from,to) {
var from = $('{{$name}}_' + from);
var to = $('{{$name}}_' + to);
var list = new Array();
forEach(from.childNodes, function(opt) {
if (!opt.selected) {
return;
}
list.push(opt);
});
forEach(list, function(node) {
to.appendChild(node);
node.selected = false;
});
var members = new Array();
forEach($('{{$name}}_members').childNodes, function(node) {
if (typeof(node) == 'object' && typeof(node.value) == 'string') {
members.push(node.value);
}
});
$('{{$name}}').value=members.join(',');
};
</script>
<table>
<tr>
<td colspan="3">
<select id="{{$name}}_groups">
<option value="all">All Users</option>
<option value="all">Test Community</option>
<option value="all">My Group</option>
</select>
</td>
</tr>
<tr>
<td colspan="3" id="{{$name}}_messages">
</td>
</tr>
<tr>
<td style="width: 15em;">
<select id="{{$name}}_potential" size="10" multiple="true" style="width: 100%;"><option></option></select>
</td>
<td>
<button type="button" onClick="{{$name}}_moveopts('potential','members')">--&gt;</button>
<button type="button" onClick="{{$name}}_moveopts('members','potential')">&lt;--</button>
</td>
<td style="width: 15em;">
<select size="10" multiple="true" id="{{$name}}_members" style="width: 100%;"><option></option>
{{foreach from=$options key=id item=user}}
<option value="{{$id|escape}}">{{$user|escape}}</option>
{{/foreach}}
</select>
</td>
</tr>
<tr>
<td colspan="3">
<label for="{{$name}}_search">Search:</label><input id="{{$name}}_search" type="text">
</td>
</tr>
</table>
<input type="hidden" id="{{$name}}" name="{{$name}}" value="{{$value}}">
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment