MDL-10181, user management improvements, adding bulk user operations, code is not finished

This commit is contained in:
toyomoyo 2007-08-07 07:26:58 +00:00
parent e46f4d110d
commit c3260b0714
25 changed files with 1471 additions and 0 deletions

72
admin/user_bulk.php Normal file
View File

@ -0,0 +1,72 @@
<?php //$Id$
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/dmllib.php');
require_once($CFG->dirroot.'/user/filters/text.php');
require_once($CFG->dirroot.'/user/filters/select.php');
require_once($CFG->dirroot.'/user/filters/courserole.php');
require_once($CFG->dirroot.'/user/filters/globalrole.php');
require_once($CFG->dirroot.'/user/filters/profilefield.php');
require_once($CFG->dirroot.'/user/filters/yesno.php');
require_once($CFG->dirroot.'/admin/user_bulk_form.php');
define("MAX_USERS_PER_PAGE", 5000);
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
// array of bulk operations
$actions = array();
$actions[1] = get_string('confirm');
if (has_capability('moodle/site:readallmessages', $sitecontext) && !empty($CFG->messaging)) {
$actions[2] = get_string('messageselectadd');
}
$actions[3] = get_string('delete');
// create the bulk operations form
$user_bulk_form =& new user_bulk_form(null, $actions);
// check if an action should be performed and do so
switch ($user_bulk_form->getAction()) {
case 1:
include($CFG->dirroot . '/admin/user_bulk_confirm.php');
return;
case 2:
include($CFG->dirroot . '/admin/user_bulk_message.php');
return;
case 3:
include($CFG->dirroot . '/admin/user_bulk_delete.php');
return;
}
// prepare user filter types
$filters[] = new user_filter_text('username', get_string('username'), 'username');
$filters[] = new user_filter_text('realname', get_string('fullname'), sql_fullname());
$filters[] = new user_filter_text('email', get_string('email'), 'email');
$filters[] = new user_filter_text('city', get_string('city'), 'city');
$filters[] = new user_filter_select('country', get_string('country'), 'country', get_list_of_countries());
$filters[] = new user_filter_yesno('confirmed', get_string('confirm'), 'confirmed');
$filters[] = new user_filter_profilefield('profile', get_string('profile'));
$filters[] = new user_filter_courserole('course', get_string('courserole', 'filters'));
$filters[] = new user_filter_globalrole('system', get_string('globalrole', 'role'));
// create the user filter form
$user_filter_form =& new user_filter_form(null, $filters);
// do output
admin_externalpage_setup('editusers');
admin_externalpage_print_header();
// put the user filter form first
$user_filter_form->display();
// get the SQL filter
$where =& $user_filter_form->getSQLFilter('id<>1 AND NOT deleted');
$ausercount = count_records_select('user', $where);
// limit the number of options
if($ausercount <= MAX_USERS_PER_PAGE) {
$user_bulk_form->setAvailableUsersSQL($where);
} else {
echo get_string('toomanytoshow');
}
// display the bulk user form
$user_bulk_form->display();
admin_externalpage_print_footer();

View File

@ -0,0 +1,49 @@
<?php //$Id$
/**
* script for bulk user delete operations
*/
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
require_capability('moodle/user:update', $sitecontext);
// clean-up users list
$primaryadmin = get_admin();
$userlist = array();
foreach ($SESSION->bulk_susers as $k => $v) {
$user = get_record('user', 'id', $v, null, null, null, null, 'id,firstname,lastname,username,secret,confirmed,mnethostid,auth');
if (!empty($user) && $user->id != $primaryadmin->id && !$user->confirmed && !is_mnet_remote_user($user)) {
$userlist[$k] = $user;
}
}
if (empty($userlist)) {
redirect($CFG->wwwroot . '/admin/user_bulk.php');
}
admin_externalpage_setup('editusers');
admin_externalpage_print_header();
if (empty($confirm)) {
$usernames = array();
foreach ($userlist as $user) {
$usernames[] =& fullname($user, true);
}
$usernames = implode(', ', $usernames);
$optionsyes['confirm'] = 1;
$optionsyes['sesskey'] = sesskey();
print_heading(get_string('confirmation', 'admin'));
notice_yesno(get_string('confirmcheckfull', '', $usernames), 'user_bulk_confirm.php', 'user_bulk.php', $optionsyes, NULL, 'post', 'get');
} else {
foreach ($userlist as $k => $user) {
$auth = get_auth_plugin($user->auth);
$result = $auth->user_confirm(addslashes($user->username), addslashes($user->secret));
if ($result != AUTH_CONFIRM_OK && $result != AUTH_CONFIRM_ALREADY) {
notify(get_string('usernotconfirmed', '', fullname($user, true)));
}
}
redirect($CFG->wwwroot . '/admin/user_bulk.php', get_string('changessaved'));
}
admin_externalpage_print_footer();

View File

@ -0,0 +1,59 @@
<?php //$Id$
/**
* script for bulk user delete operations
*/
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
require_capability('moodle/user:delete', $sitecontext);
// clean-up users list
$primaryadmin = get_admin();
$userlist = array();
foreach ($SESSION->bulk_susers as $k => $v) {
$user = get_record('user', 'id', $v, null, null, null, null, 'id,firstname,lastname,email');
if (!empty($user) && $user->id != $primaryadmin->id) {
$userlist[$k] = $user;
}
}
if (empty($userlist)) {
redirect($CFG->wwwroot . '/admin/user_bulk.php');
}
admin_externalpage_setup('editusers');
admin_externalpage_print_header();
if (empty($confirm)) {
$usernames = array();
foreach ($userlist as $user) {
$usernames[] =& fullname($user, true);
}
$usernames = implode(', ', $usernames);
$optionsyes['confirm'] = 1;
$optionsyes['sesskey'] = sesskey();
print_heading(get_string('confirmation', 'admin'));
notice_yesno(get_string('deletecheckfull', '', $usernames), 'user_bulk_delete.php', 'user_bulk.php', $optionsyes, NULL, 'post', 'get');
} else {
foreach ($userlist as $k => $user) {
$user->username = addslashes($user->email . time()); // Remember it just in case
$user->deleted = 1;
$user->email = ''; // Clear this field to free it up
$user->timemodified = time();
$user->idnumber = ''; // Clear this field to free it up
if (update_record('user', $user)) {
// not sure if this is needed. unenrol_student($user->id); // From all courses
delete_records('role_assignments', 'userid', $user->id); // unassign all roles
// remove all context assigned on this user?
// notify(get_string('deletedactivity', '', fullname($user, true)) );
unset($SESSION->bulk_susers[$k]);
} else {
notify(get_string('deletednot', '', fullname($user, true)));
}
}
redirect($CFG->wwwroot . '/admin/user_bulk.php', get_string('changessaved'));
}
admin_externalpage_print_footer();

166
admin/user_bulk_form.php Normal file
View File

@ -0,0 +1,166 @@
<?php //$Id$
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir . '/datalib.php');
class user_bulk_form extends moodleform {
/**
* Quickform select object for the available users.
*/
var $ausers;
/**
* Quickform select object for the selected users.
*/
var $susers;
// form definition
function definition() {
$mform =& $this->_form;
$mform->addElement('header', 'users', get_string('usersinlist', 'bulkusers'));
$this->ausers =& $mform->createElement('select', 'ausers', get_string('filtered', 'bulkusers'), null, 'size="15"');
$this->ausers->setMultiple(true);
$this->susers =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), null, 'size="15"');
$this->susers->setMultiple(true);
$objs = array();
$objs[] = &$this->ausers;
$objs[] = &$this->susers;
$mform->addElement('group', 'usersgrp', get_string('users'), $objs, ' ', false);
$objs = array();
$objs[] =& $mform->createElement('submit', 'addone', get_string('addsel', 'bulkusers'));
$objs[] =& $mform->createElement('submit', 'removeone', get_string('removesel', 'bulkusers'));
$objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'bulkusers'));
$objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'bulkusers'));
$mform->addElement('group', 'buttonsgrp', null, $objs, array(' ', '<br />'), false);
$objs = array();
$objs[] =& $mform->createElement('select', 'action', get_string('withselected'), @$this->_customdata);
$objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));;
$mform->addElement('group', 'actionsgrp', get_string('actions'), $objs, ' ', false);
$renderer =& $mform->defaultRenderer();
$template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
$renderer->setGroupElementTemplate($template, 'usersgrp');
}
function definition_after_data() {
global $SESSION;
$this->_updateSelection($this->get_data());
$this->setSelectedUsers($SESSION->bulk_susers);
if(empty($SESSION->bulk_susers)) {
$this->_form->removeElement('actionsgrp');
}
}
/**
* Updates the user selection based on the data submited to the form.
* @param object $data object with data received by the form
*/
function _updateSelection($data) {
global $SESSION;
if(!is_array(@$SESSION->bulk_susers)) {
$SESSION->bulk_susers = array();
}
// if the forms was not submited, then quit
if(is_null($data)){
return;
}
if(@$data->addall) {
if(!empty($SESSION->bulk_ausers)) {
$SESSION->bulk_susers = array_merge($SESSION->bulk_susers, $SESSION->bulk_ausers);
}
} else if(@$data->addone) {
if(!empty($data->ausers)) {
$SESSION->bulk_susers = array_merge($SESSION->bulk_susers, array_values($data->ausers));
}
} else if(@$data->removeone) {
if(!empty($data->susers)) {
$SESSION->bulk_susers = array_diff($SESSION->bulk_susers, array_values($data->susers));
}
} else if(@$data->removeall) {
$SESSION->bulk_susers = array();
}
$SESSION->bulk_susers = array_unique($SESSION->bulk_susers);
}
/**
* Sets the available users list, based on their ids
* @param array $ausers array of user ids
*/
function setAvailableUsers($ausers) {
$sqlwhere = null;
if(!empty($ausers)) {
$sqlwhere = 'id IN (' . implode(',', $ausers) . ')';
}
$this->setAvailableUsersSQL($sqlwhere);
}
/**
* Sets the available users list, based on a SQL where condition
* @param string $sqlwhere filter for the users
*/
function setAvailableUsersSQL($sqlwhere=null) {
global $SESSION;
if(is_null($sqlwhere) || ($users =& $this->getUserData($sqlwhere))===false) {
$users = array();
}
$SESSION->bulk_ausers =& array_keys($users);
$this->ausers->load($users);
}
/**
* Sets the selected users list, based on their ids
* @param array $ausers array of user ids
*/
function setSelectedUsers($susers) {
$sqlwhere = null;
if(!empty($susers)) {
$sqlwhere = 'id IN (' . implode(',', $susers) . ')';
}
$this->setSelectedUsersSQL($sqlwhere);
}
/**
* Sets the selected users list, based on a SQL where condition
* @param string $sqlwhere filter for the users
*/
function setSelectedUsersSQL($sqlwhere=null) {
global $SESSION;
if(is_null($sqlwhere) || ($users =& $this->getUserData($sqlwhere))===false) {
$users = array();
}
$SESSION->bulk_susers =& array_keys($users);
$this->susers->load($users);
}
/**
* Returns information about the users.
* @param string $sqlwhere filter for the users
*/
function getUserData($sqlwhere) {
return get_records_select_menu('user', $sqlwhere, 'fullname', 'id,' . sql_fullname() . ' AS fullname');
}
/**
* Returns an array of ids of selected users.
* @return array of selected users' ids
*/
function getSelectedUsers() {
global $SESSION;
return $SESSION->bulk_susers;
}
/**
* Returns an int code of the action to be performed.
* @return int code of the action or false if no action should be performed
*/
function getAction() {
$data =& $this->get_data();
if(!$this->is_submitted() || empty($data->doaction)){
return false;
}
return $data->action;
}
}

View File

@ -0,0 +1,47 @@
<?php //$Id$
require_once('../config.php');
require_once($CFG->dirroot.'/message/lib.php');
require_once($CFG->libdir.'/adminlib.php');
$users = $SESSION->bulk_susers;
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
require_login();
require_capability('moodle/site:readallmessages', $sitecontext);
// fix for MDL-10112
if (empty($CFG->messaging)) {
error("Messaging is disabled on this site");
}
require_once('user_message_form.php');
$extradata['userlist'] =& $users;
$noteform =& new user_message_form('user_bulk_message.php', $extradata);
// if no users or action canceled, return to users page
if (empty($users) || $noteform->is_cancelled()) {
redirect($CFG->wwwroot . '/admin/user_bulk.php');
}
$formdata =& $noteform->get_data();
// if we have the message and the command, then send it
if ($noteform->is_submitted() && !empty($formdata->send)) {
if(empty($formdata->messagebody)) {
notify(get_string('allfieldsrequired'));
} else {
foreach ($users as $u) {
if ($user = get_record('user', 'id', $u)) {
message_post_message($USER, $user, addslashes($formdata->messagebody), $formdata->format, 'direct');
}
}
redirect($CFG->wwwroot . '/admin/user_bulk.php');
}
}
admin_externalpage_setup('editusers');
admin_externalpage_print_header();
if ($noteform->is_submitted() && !empty($formdata->preview)) {
echo '<h3>'.get_string('previewhtml').'</h3>';
echo '<div class="messagepreview">'. format_text(stripslashes($formdata->messagebody),$formdata->format). '</div>';
}
$noteform->display();
admin_externalpage_print_footer();

View File

@ -0,0 +1,32 @@
<?php //$Id$
require_once($CFG->libdir.'/formslib.php');
class user_message_form extends moodleform {
function definition() {
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string('message', 'message'));
$mform->addElement('textarea', 'messagebody', get_string('messagebody'), array('rows'=>15, 'cols'=>30));
$mform->setType('messagebody', PARAM_CLEANHTML);
$mform->addRule('messagebody', '', 'required', null, 'client');
$mform->setHelpButton('messagebody', array('writing', 'reading', 'questions', 'richtext'), false, 'editorhelpbutton');
$mform->addElement('format', 'format', get_string('format'));
$objs = array();
foreach($this->_customdata['userlist'] as $k=>$u) {
$user = get_record('user', 'id', $u);
$objs[] =& $mform->createElement('static', null, null, '<input type="checkbox" name="userid['. $k .']" checked="checked" value="'.$u . '" />'. fullname($user));
}
$mform->addElement('group', 'users', 'Users', $objs, '<br />', false);
$objs = array();
$objs[] = &$mform->createElement('submit', 'send', 'Send');
$objs[] = &$mform->createElement('submit', 'preview', 'Preview');
$objs[] = &$mform->createElement('cancel');
$mform->addElement('group', 'buttonar', '', $objs, ' ', false);
$mform->closeHeaderBefore('buttonar');
}
}

View File

@ -0,0 +1,9 @@
<?php //$Id$
$string['usersinlist'] = 'Users in list';
$string['addall'] = 'Add all to selection';
$string['addsel'] = 'Add to selection';
$string['removeall'] = 'Remove all from selection';
$string['removesel'] = 'Remove from selection';
$string['filtered'] = 'Filtered';
$string['selected'] = 'Selected';
$string['action'] = 'Action';

24
lang/en_utf8/filters.php Normal file
View File

@ -0,0 +1,24 @@
<?php //$Id$
$string['actfilterhdr'] = 'Active filters';
$string['addfilter'] = 'Add filter';
$string['anyvalue'] = 'any value';
$string['anyrole'] = 'any role';
$string['anyfield'] = 'any field';
$string['newfilter'] = 'New filter';
$string['setfilter'] = 'Set filter';
$string['removeall'] = 'Remove all';
$string['removeselected'] = 'Remove selected';
$string['anycourse'] = 'any course';
$string['anycategory'] = 'any category';
$string['contains'] = 'contains';
$string['doesnotcontain'] = 'doesn\'t contain';
$string['isequalto'] = 'is equal to';
$string['isnotequalto'] = 'isn\'t equal to';
$string['startswith'] = 'starts with';
$string['endswith'] = 'ends with';
$string['isempty'] = 'is empty';
$string['isnotdefined'] = 'isn\'t defined';
$string['isdefined'] = 'is defined';
$string['isanyvalue'] = 'is any value';
$string['categoryrole'] = 'Category role';
$string['courserole'] = 'Course role';

View File

@ -0,0 +1,3 @@
<h1>Course role filter</h1>
<p>This filter allows you to filter users based the role they have assigned in the course specified by its shortname from a specified course category (if the shortname textbox is empty, the category is "any category" and the role is "any role" then the filter is disabled).
</p>

View File

@ -0,0 +1,2 @@
<h1>Global role filter</h1>
<p>This filter allows you to filter users based the global role they have assigned.</p>

View File

@ -0,0 +1,14 @@
<h1>Profile filter</h1>
<p>This filter allows you to filter users based on values of profile fields.
The filter can be applied on a single profile field or an all profile fields.
The filter has the following options:</p>
<ul>
<li>contains - this option allows only users for which the specified field contains the text entered (if no text is entered, then the filter is disabled)</li>
<li>doesn't contain - this option allows only users for which the specified field does not contain the text entered (if no text is entered, then the filter is disabled)</li>
<li>is equal to - this option allows only users for which the specified field is equal to the text entered (if no text is entered, then the filter is disabled)</li>
<li>starts with - this option allows only users for which the specified field starts with the text entered (if no text is entered, then the filter is disabled)</li>
<li>ends with - this option allows only users for which the specified field ends with the text entered (if no text is entered, then the filter is disabled)</li>
<li>is empty - this option allows only users for which the specified field is equal to an empty string, but it is defined (the text entered is ignored)</li>
<li>is not defined - this option allows only users for which the specified field is not defined (the text entered is ignored)</li>
<li>is defined - this option allows only users for which the specified field is defined (the text entered is ignored)</li>
</ul>

View File

@ -0,0 +1,3 @@
<h1>Radios filter</h1>
<p>This filter allows you to filter information based on the option you select.
Depending on the filter settings, the first option might be the "any value" option which disables the filter.</p>

View File

@ -0,0 +1,8 @@
<h1>Select filter</h1>
<p>This filter allows you to filter information based on a drop down list.
The filter has the following options:</p>
<ul>
<li>is any value - this option disables the filter (i.e. all information is accepted by this filter)</li>
<li>is equal to - this option allows only information that is equal to the value selected from the list</li>
<li>is not equal to - this option allows only information that is different from the value selected from the list</li>
</ul>

View File

@ -0,0 +1,11 @@
<h1>Text filter</h1>
<p>This filter allows you to filter information based on a free form text.
The filter has the following options:</p>
<ul>
<li>contains - this option allows only information that contains the text entered (if no text is entered, then the filter is disabled)</li>
<li>doesn't contain - this option allows only information that does not contain the text entered (if no text is entered, then the filter is disabled)</li>
<li>is equal to - this option allows only information that is equal to the text entered (if no text is entered, then the filter is disabled)</li>
<li>starts with - this option allows only information that starts with the text entered (if no text is entered, then the filter is disabled)</li>
<li>ends with - this option allows only information that ends with the text entered (if no text is entered, then the filter is disabled)</li>
<li>is empty - this option allows only information that is equal to an empty string (the text entered is ignored)</li>
</ul>

View File

@ -228,6 +228,7 @@ $string['complete'] = 'Complete';
$string['completereport'] = 'Complete report';
$string['configuration'] = 'Configuration';
$string['confirm'] = 'Confirm';
$string['confirmcheckfull'] = 'Are you absolutely sure you want to confirm $a ?';
$string['confirmed'] = 'Your registration has been confirmed';
$string['confirmednot'] = 'Your registration has not yet been confirmed!';
$string['continue'] = 'Continue';

View File

@ -66,6 +66,7 @@ $string['errorbadroleshortname'] = 'Incorrect role name';
$string['errorexistsrolename'] = 'Role name already exists';
$string['errorexistsroleshortname'] = 'Role name already exists';
$string['existingusers'] = '$a existing users';
$string['globalrole'] = 'Global role';
$string['globalroleswarning'] = 'WARNING! Any roles you assign from this page will apply to the assigned users throughout the entire site, including the front page and all the courses.';
$string['inherit'] = 'Inherit';
$string['legacy:admin'] = 'LEGACY ROLE: Administrator';

132
user/filters/courserole.php Normal file
View File

@ -0,0 +1,132 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* User filter based on roles in a course identified by its shortname.
*/
class user_filter_courserole extends user_filter_type {
/**
* User role (0 = any role)
*/
var $_roleid;
/**
* Course category in which to search the course (0 = all categories).
*/
var $_categoryid;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param string $value the shortname of the course (used for filtering data)
* @param int $categoryid id of the category
* @param int $roleid id of the role
*/
function user_filter_courserole($name, $label, $field='id', $value=null, $categoryid=0, $roleid=0) {
parent::user_filter_type($name, $label, $field, $value);
$this->_roleid = $roleid;
$this->_categoryid = $categoryid;
}
/**
* Returns an array of available roles
* @return array of availble roles
*/
function getRoles() {
$context =& get_context_instance(CONTEXT_SYSTEM);
$roles =& array_merge(array(0=> get_string('anyrole','filters')), get_assignable_roles($context));
return $roles;
}
/**
* Returns an array of course categories
* @return array of course categories
*/
function getCourseCategories() {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
return array_merge(array(0=> get_string('anycategory', 'filters')), $displaylist);
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$objs = array();
$objs[] =& $mform->createElement('select', $this->_name . '_rl', null, $this->getRoles());
$objs[] =& $mform->createElement('select', $this->_name . '_ct', null, $this->getCourseCategories());
$objs[] =& $mform->createElement('text', $this->_name, null);
$grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
$grp->setHelpButton(array('courserole','','filters'));
$mform->setDefault($this->_name, $this->_value);
$mform->setDefault($this->_name . '_rl', $this->_roleid);
$mform->setDefault($this->_name . '_ct', $this->_categoryid);
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$role = $field . '_rl';
$category = $field . '_ct';
$this->_value = (string)@$formdata->$field;
$this->_roleid = (int)@$formdata->$role;
$this->_categoryid = (int)@$formdata->$category;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
global $CFG;
if(empty($this->_value) && empty($this->_roleid) && empty($this->_categoryid)) {
return null;
}
$timenow = time();
$where = 'WHERE b.contextlevel=50 AND timestart<' . $timenow .' AND (timeend=0 OR timeend>'. $timenow . ')';
if($this->_roleid) {
$where.= ' AND roleid='. $this->_roleid;
}
if($this->_categoryid) {
$where .= ' AND category=' . $this->_categoryid;
}
if($this->_value) {
$where .= ' AND shortname="' . $this->_value . '"';
}
return $this->_field . " IN (SELECT userid FROM {$CFG->prefix}role_assignments a ".
"INNER JOIN {$CFG->prefix}context b ON a.contextid=b.id ".
"INNER JOIN {$CFG->prefix}course c ON b.instanceid=c.id ".
$where . ')';
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
if ($this->_roleid) {
$roles =& $this->getRoles();
$rolename = '"' . $roles[$this->_roleid]. '"';
} else {
$rolename = get_string('anyrole','filters');
}
if ($this->_categoryid) {
$categories=& $this->getCourseCategories();
$categoryname = '"' . $categories[$this->_categoryid]. '"';
} else {
$categoryname = get_string('anycategory', 'filters');
}
if ($this->_value) {
$coursename = '"' . stripslashes($this->_value). '"';
} else {
$coursename = get_string('anycourse','filters');
}
return $this->_label . ' is ' . $rolename. ' in ' . $coursename . ' from ' . $categoryname;
}
}

View File

@ -0,0 +1,73 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* User filter based on global roles.
*/
class user_filter_globalrole extends user_filter_type {
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param int $value id of the role (used for filtering data); 0 = any role
*/
function user_filter_globalrole($name, $label, $field='id', $value=0) {
parent::user_filter_type($name, $label, $field, $value);
}
/**
* Returns an array of available roles
* @return array of availble roles
*/
function getRoles() {
$context =& get_context_instance(CONTEXT_SYSTEM);
$roles =& array_merge(array(0=> get_string('anyrole','filters')), get_assignable_roles($context));
return $roles;
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$obj =& $mform->addElement('select', $this->_name, $this->_label, $this->getRoles());
$obj->setHelpButton(array('globalrole','','filters'));
$mform->setDefault($this->_name, $this->_value);
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$this->_value = (string)@$formdata->$field;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
global $CFG;
if(empty($this->_value)) {
return null;
}
$timenow = time();
$where = 'WHERE contextlevel=10 AND roleid='. $this->_value . ' AND timestart<' . $timenow .' AND (timeend=0 OR timeend>'. $timenow . ')';
return $this->_field . " IN (SELECT userid FROM {$CFG->prefix}role_assignments a ".
"INNER JOIN {$CFG->prefix}context b ON a.contextid=b.id ".
$where . ')';
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
$roles =& $this->getRoles();
return $this->_label . ' is ' . $roles[$this->_value];
}
}

76
user/filters/lib.php Normal file
View File

@ -0,0 +1,76 @@
<?php //$Id$
/**
* Library of functions and constants for user filters
*/
require_once($CFG->dirroot . '/user/filters/user_filter_form.php');
/**
* The base user filter.
*/
class user_filter_type {
/**
* The name of this filter instance.
*/
var $_name;
/**
* The label of this filter instance.
*/
var $_label;
/**
* The field database used for filtering data.
*/
var $_field;
/**
* The value used for filtering data.
*/
var $_value;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param string $value the value used for filtering data
*/
function user_filter_type($name, $label, $field, $value=null) {
$this->_name = $name;
$this->_label = $label;
$this->_field = $field;
$this->_value = $value;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
return $this->_field . '="' . $this->_value . '"';
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$this->_value = (string)@$formdata->$field;
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$mform->addElement('text', $this->_name, $this->_label);
$mform->setDefault($this->_name, $this->_value);
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
return $this->_label . ' is "' . $this->_value . '"';
}
}

View File

@ -0,0 +1,179 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* User filter based on values of custom profile fields.
*/
class user_filter_profilefield extends user_filter_type {
/**
* operator used for comparison of data
*/
var $_operator;
/**
* profile field to look at (0 - all fields)
*/
var $_profile_field;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param string $value the value of the profile field (used for filtering data)
* @param int $profile_field id of the profile field to look in
* @param int $operator code of the comparison operator
*/
function user_filter_profilefield($name, $label, $field='id', $value=null, $profile_field=0, $operator=0) {
parent::user_filter_type($name, $label, $field, $value);
$this->_operator = $operator;
$this->_profile_field = $profile_field;
}
/**
* Returns an array of comparison operators
* @return array of comparison operators
*/
function getOperators() {
return array(
get_string('contains', 'filters'),
get_string('doesnotcontain','filters'),
get_string('isequalto','filters'),
get_string('startswith','filters'),
get_string('endswith','filters'),
get_string('isempty','filters'),
get_string('isnotdefined','filters'),
get_string('isdefined','filters'),
);
}
/**
* Returns an array of custom profile fields
* @return array of profile fields
*/
function getProfileFields() {
$fields =& get_records_select('user_info_field', '', 'shortname', 'id,shortname');
if(empty($fields)) {
return null;
}
$res[0] = get_string('anyfield','filters');
foreach($fields as $k=>$v) {
$res[$k] = $v->shortname;
}
return $res;
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$profile_fields =& $this->getProfileFields();
if(empty($profile_fields)) {
return;
}
$objs = array();
$objs[] =& $mform->createElement('select', $this->_name . '_fld', null, $profile_fields);
$objs[] =& $mform->createElement('select', $this->_name . '_op', null, $this->getOperators());
$objs[] =& $mform->createElement('text', $this->_name, null);
$grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
$grp->setHelpButton(array('profilefield','','filters'));
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$operator = $field . '_op';
$profile_field = $field . '_fld';
$this->_value = (string)@$formdata->$field;
$this->_operator = (int)@$formdata->$operator;
$this->_profile_field = (int)@$formdata->$profile_field;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
global $CFG;
$where = '';
$op = ' IN ';
switch($this->_operator) {
case 0: // contains
if(empty($this->_value)) {
return null;
}
$where = 'data ' . sql_ilike(). ' "%' . $this->_value . '%"';
break;
case 1: // does not contain
if(empty($this->_value)) {
return null;
}
$where = 'data NOT ' . sql_ilike(). ' "%' . $this->_value . '%"';
break;
case 2: // equal to
if(empty($this->_value)) {
return null;
}
$where = 'data="' . $this->_value . '"';
break;
case 3: // starts with
if(empty($this->_value)) {
return null;
}
$where = 'data ' . sql_ilike(). ' "' . $this->_value . '%"';
break;
case 4: // ends with
if(empty($this->_value)) {
return null;
}
$where = 'data ' . sql_ilike(). ' "%' . $this->_value . '"';
break;
case 5: // empty
$where = 'data=""';
break;
case 6: // is not defined
$op = ' NOT IN ';
break;
case 7: // is defined
break;
}
if(!empty($this->_profile_field)) {
if(!empty($where)) {
$where = ' AND ' . $where;
}
$where = 'fieldid=' . $this->_profile_field . $where;
}
if(!empty($where)) {
$where = ' WHERE ' . $where;
}
return $this->_field . $op . "(SELECT userid FROM {$CFG->prefix}user_info_data" . $where . ')';
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
$res = '';
$operators =& $this->getOperators();
$profilefields =& $this->getProfileFields();
switch($this->_operator) {
case 0: // contains
case 1: // doesn't contain
case 2: // equal to
case 3: // starts with
case 4: // ends with
$res = $this->_label . ': '. $profilefields[$this->_profile_field] . ' ' . $operators[$this->_operator]. ' "' . stripslashes($this->_value) . '"';
break;
case 5: // empty
case 6: // is not defined
case 7: // is defined
$res = $this->_label . ': '. $profilefields[$this->_profile_field] . ' ' . $operators[$this->_operator];
break;
}
return $res;
}
}

80
user/filters/radios.php Normal file
View File

@ -0,0 +1,80 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* Generic filter with radio buttons for integer fields.
*/
class user_filter_radios extends user_filter_type {
/**
* options for the radio buttons
*/
var $_options;
/**
* id of the option which disables the filter
*/
var $_offoption;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param array $options associative array used to generate the radio buttons
* @param boolean $offoption true if a "don't care" option should be generated
* @param int $value the value used for filtering data
*/
function user_filter_radios($name, $label, $field, $options, $offoption=true, $value=null) {
parent::user_filter_type($name, $label, $field, $value);
$this->_options = $options;
if($offoption) {
$this->_offoption = @min(array_keys($options)) - 1;
} else {
$this->_offoption = null;
}
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$objs = array();
if(!is_null($this->_offoption)) {
$objs[] =& $mform->createElement('radio', $this->_name, null, get_string('anyvalue', 'filters'), $this->_offoption);
}
foreach($this->_options as $k=>$v) {
$objs[] =& $mform->createElement('radio', $this->_name, null, $v, $k);
}
$grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
$mform->setDefault($this->_name, -1);
$grp->setHelpButton(array('radios','','filters'));
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$this->_value = (int)@$formdata->$field;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
if($this->_value === $this->_offoption) {
return null;
}
return $this->_field . '=' . $this->_value;
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
return $this->_label . ' is ' . $this->_options[$this->_value];
}
}

99
user/filters/select.php Normal file
View File

@ -0,0 +1,99 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* Generic filter based on a list of values.
*/
class user_filter_select extends user_filter_type {
/**
* operator used for comparison of data
*/
var $_operator;
/**
* options for the list values
*/
var $_options;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param string $value the value used for filtering data
* @param int $operator code of the comparison operator
*/
function user_filter_select($name, $label, $field, $options, $value=null, $operator=null) {
parent::user_filter_type($name, $label, $field, $value);
$this->_operator = $operator;
$this->_options = $options;
}
/**
* Returns an array of comparison operators
* @return array of comparison operators
*/
function getOperators() {
return array(
get_string('isanyvalue','filters'),
get_string('isequalto','filters'),
get_string('isnotequalto','filters'),
);
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$objs = array();
$objs[] =& $mform->createElement('select', $this->_name . '_op', null, $this->getOperators());
$objs[] =& $mform->createElement('select', $this->_name, null, $this->_options);
$grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
$grp->setHelpButton(array('select','','filters'));
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$operator = $field . '_op';
$this->_value = (string)@$formdata->$field;
$this->_operator = (int)@$formdata->$operator;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
switch($this->_operator) {
default:
return null;
case 1: // equal to
$res = '="' . $this->_value . '"';
break;
case 2: // not equal to
$res = '<>"' . $this->_value . '"';
break;
}
return $this->_field . $res;
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
$operators = $this->getOperators();
switch($this->_operator) {
case 1: // equal to
case 2: // not equal to
$res = $this->_label . ' ' . $operators[$this->_operator]. ' "' . $this->_options[stripslashes($this->_value)] . '"';
break;
}
return $res;
}
}

134
user/filters/text.php Normal file
View File

@ -0,0 +1,134 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/lib.php');
/**
* Generic filter for text fields.
*/
class user_filter_text extends user_filter_type {
/**
* operator used for comparison of data
*/
var $_operator;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param string $value the value used for filtering data
* @param int $operator code of the comparison operator
*/
function user_filter_text($name, $label, $field, $value=null, $operator=0) {
parent::user_filter_type($name, $label, $field, $value);
$this->_operator = $operator;
}
/**
* Returns an array of comparison operators
* @return array of comparison operators
*/
function getOperators() {
return array(
get_string('contains', 'filters'),
get_string('doesnotcontain','filters'),
get_string('isequalto','filters'),
get_string('startswith','filters'),
get_string('endswith','filters'),
get_string('isempty','filters'),
);
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$objs = array();
$objs[] =& $mform->createElement('select', $this->_name . '_op', null, $this->getOperators());
$objs[] =& $mform->createElement('text', $this->_name, null);
$grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
$grp->setHelpButton(array('text','','filters'));
$mform->setDefault($this->_name, $this->_value);
$mform->setDefault($this->_name . '_op', $this->_operator);
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
*/
function checkData($formdata) {
$field = $this->_name;
$operator = $field . '_op';
$this->_value = (string)@$formdata->$field;
$this->_operator = (int)@$formdata->$operator;
}
/**
* Returns the condition to be used with SQL where
* @return string the filtering condition or null if the filter is disabled
*/
function getSQLFilter() {
switch($this->_operator) {
default:
return null;
case 0: // contains
if(empty($this->_value)) {
return null;
}
$res = ' ' . sql_ilike(). ' "%' . $this->_value . '%"';
break;
case 1: // does not contain
if(empty($this->_value)) {
return null;
}
$res = ' NOT ' . sql_ilike(). ' "%' . $this->_value . '%"';
break;
case 2: // equal to
if(empty($this->_value)) {
return null;
}
$res = '="' . $this->_value . '"';
break;
case 3: // starts with
if(empty($this->_value)) {
return null;
}
$res = ' ' . sql_ilike(). ' "' . $this->_value . '%"';
break;
case 4: // ends with
if(empty($this->_value)) {
return null;
}
$res = ' ' . sql_ilike(). ' "%' . $this->_value . '"';
break;
case 5: // empty
if(empty($this->_value)) {
return null;
}
$res = '=""';
break;
}
return $this->_field . $res;
}
/**
* Returns a human friendly description of the filter.
* @return string filter description
*/
function getDescription() {
$operators = $this->getOperators();
switch($this->_operator) {
case 0: // contains
case 1: // doesn't contain
case 2: // equal to
case 3: // starts with
case 4: // ends with
$res = $operators[$this->_operator]. ' "' . stripslashes($this->_value) . '"';
break;
case 5: // empty
$res = $operators[$this->_operator];
break;
}
return $this->_label . ' ' . $res;
}
}

View File

@ -0,0 +1,178 @@
<?php //$Id$
require_once($CFG->libdir.'/formslib.php');
//require_once($CFG->libdir . '/form/submit.php'); // (abautu) is it necesary? i don't think so
class user_filter_form extends moodleform {
/**
* array of filter type objects
*/
var $_filtersTypes = array();
// form definition
function definition() {
global $SESSION;
// add controls for each filter type in the new filter group
$mform =& $this->_form;
$this->_filtersTypes =& $this->_customdata;
$mform->addElement('header', 'newfilter', get_string('newfilter','filters'));
foreach($this->_filtersTypes as $ft) {
$ft->setupForm($mform);
}
$objs = array();
$objs[] = &$mform->createElement('submit', 'add ', get_string('addfilter','filters'));
$objs[] = &$mform->createElement('submit', 'set', get_string('setfilter','filters'));
$mform->addElement('group', 'addfilterbar', '', $objs, ' ', false);
// add controls for each active filter in the active filters group
$mform->addElement('header', 'actfilterhdr', get_string('actfilterhdr','filters'));
$objs = array();
$objs[] = &$mform->createElement('cancel', 'remove', get_string('removeselected','filters'));
$objs[] = &$mform->createElement('cancel', 'cancel', get_string('removeall','filters'));
$mform->addElement('group', 'actfiltergrp', '', $objs, ' ', false);
// insert the controls before the buttons
if(!empty($SESSION->user_filter_descriptions)) {
foreach($SESSION->user_filter_descriptions as $k => $f) {
$obj = &$mform->createElement('checkbox', 'filter['.$k.']', null, $f);
$mform->insertElementBefore($obj, 'actfiltergrp');
}
}
}
/**
* Removes an active filter from the form and filters list.
* @param int $key id of filter to remove
*/
function _removeFilter($key) {
global $SESSION;
unset($SESSION->user_filter_clauses[$key]);
unset($SESSION->user_filter_descriptions[$key]);
$this->_form->removeElement('filter['.$key.']');
}
/**
* Removes all active filters from the form and filters list.
*/
function _removeFilters() {
global $SESSION;
if(!empty($SESSION->user_filter_clauses)) {
foreach($SESSION->user_filter_clauses as $key=>$f) {
$this->_removeFilter($key);
}
}
}
/**
* Stores an active filter to the form and filters list.
* @param int $key id of filter to remove
* @param string $description human friendly description of the filter
* @param string $clauses SQL where condition
*/
function _insertFilter($key, $description, $clause) {
global $SESSION;
$SESSION->user_filter_clauses[$key] = $clause;
$SESSION->user_filter_descriptions[$key] = $description;
$mform =& $this->_form;
$obj = &$mform->createElement('checkbox', 'filter['.$key.']', null, $description);
$mform->insertElementBefore($obj, 'actfiltergrp');
}
/**
* Updates form and filters list based on data received
* @param object $data object with data received by the form
*/
function _updateFilters($data) {
global $SESSION;
// if the forms was not submited, then quit
if(is_null($data)){
return;
}
// if cancel was pressed, then remove all filters
if(!empty($data->cancel)) {
$this->_removeFilters();
return;
}
// if remove was pressed, then remove selected filters
if(!empty($data->remove)) {
if(!empty($data->filter)) {
foreach($data->filter as $k=>$f) {
$this->_removeFilter($k);
}
}
return;
}
// if set was pressed, then remove all filters before adding new ones
if(!empty($data->set)) {
$this->_removeFilters();
}
// in any other case, add the selected filter
// first build the filter out of each active filter type
$clauses = array();
$descriptions = array();
foreach($this->_filtersTypes as $ft) {
$ft->checkData($data);
$sqlFilter = $ft->getSQLFilter();
// ignore disabled filters
if(!empty($sqlFilter)) {
$clauses[] = $sqlFilter;
$descriptions[] = $ft->getDescription();
}
}
// if no filters are active, then quit
if(empty($clauses)) {
return;
}
// join the filter parts and their descriptions together
$clauses = implode(' AND ', $clauses);
$descriptions = implode(', ', $descriptions);
// check if this filter is a duplicate; if so, then quit
$lastkey = -1;
if(!empty($SESSION->user_filter_descriptions)) {
foreach($SESSION->user_filter_descriptions as $k=>$c) {
if($c == $descriptions) {
return;
}
$lastkey = $k;
}
}
// append the new filter
$this->_insertFilter($lastkey + 1, $descriptions, $clauses);
}
function definition_after_data() {
global $SESSION;
$mform =& $this->_form;
// update the filters
$this->_updateFilters($this->get_data());
// remove the active filters section if no filters are defined
if(empty($SESSION->user_filter_descriptions)) {
$mform->removeElement('actfiltergrp');
$mform->removeElement('actfilterhdr');
}
}
/**
* Returns the complete SQL where condition coresponding to the active filters and the extra conditions
* @param mixed $extra array of SQL where conditions to be conected by ANDs or a string SQL where condition, which will be connected to the active filters conditions by AND
* @return string SQL where condition
*/
function getSQLFilter($extra='') {
global $SESSION;
if(is_array($extra)) {
$extra = implode(' AND ', $extra);
}
// join sql filters with ORs and put inside paranteses
if(!empty($SESSION->user_filter_clauses)) {
if(!empty($extra)) {
$extra .= ' AND ';
}
$extra .= '((' . implode(') OR (',$SESSION->user_filter_clauses) . '))';
}
return $extra;
}
}

19
user/filters/yesno.php Normal file
View File

@ -0,0 +1,19 @@
<?php //$Id$
require_once($CFG->dirroot . '/user/filters/radios.php');
/**
* Generic yes/no filter with radio buttons for integer fields.
*/
class user_filter_yesno extends user_filter_radios {
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param string $field the field used for filtering data
* @param int $value the value used for filtering data
*/
function user_filter_yesno($name, $label, $field, $value=-1) {
parent::user_filter_radios($name, $label, $field, array(0=>get_string('no'), 1=>get_string('yes')), true, $value);
}
}