Skip to content

Commit

Permalink
Merge branch 'wip-mdl-29244' of git://github.com/rajeshtaneja/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Aparup Banerjee committed Sep 27, 2011
2 parents df1ed7d + 8295d44 commit 59e2fd4
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 34 deletions.
3 changes: 2 additions & 1 deletion lang/en/filters.php
Expand Up @@ -54,7 +54,6 @@
$string['filtersettingsin'] = 'Filter settings in {$a}'; $string['filtersettingsin'] = 'Filter settings in {$a}';
$string['firstaccess'] = 'First access'; $string['firstaccess'] = 'First access';
$string['globalrolelabel'] = '{$a->label} is {$a->value}'; $string['globalrolelabel'] = '{$a->label} is {$a->value}';
$string['includenever'] = 'Never included';
$string['isactive'] = 'Active?'; $string['isactive'] = 'Active?';
$string['isafter'] = 'is after'; $string['isafter'] = 'is after';
$string['isanyvalue'] = 'is any value'; $string['isanyvalue'] = 'is any value';
Expand All @@ -64,6 +63,8 @@
$string['isequalto'] = 'is equal to'; $string['isequalto'] = 'is equal to';
$string['isnotdefined'] = 'isn\'t defined'; $string['isnotdefined'] = 'isn\'t defined';
$string['isnotequalto'] = 'isn\'t equal to'; $string['isnotequalto'] = 'isn\'t equal to';
$string['neveraccessed'] = 'Never accessed';
$string['nevermodified'] = 'Never modified';
$string['newfilter'] = 'New filter'; $string['newfilter'] = 'New filter';
$string['nofiltersenabled'] = 'No filter plugins have been enabled on this site.'; $string['nofiltersenabled'] = 'No filter plugins have been enabled on this site.';
$string['off'] = 'Off'; $string['off'] = 'Off';
Expand Down
135 changes: 135 additions & 0 deletions user/filters/checkbox.php
@@ -0,0 +1,135 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* moodlelib.php - Moodle main library
*
* Main library file of miscellaneous general-purpose Moodle functions.
* Other main libraries:
* - weblib.php - functions that produce web output
* - datalib.php - functions that access the database
*
* @package user
* @copyright 2011 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/user/filters/lib.php');

/**
* Generic filter based for checkbox and can be used for disabling items
*/
class user_filter_checkbox extends user_filter_type {
/**
* list of all the fields which needs to be disabled, if checkbox is checked
* @var array
*/
protected $disableelements = array();

/**
* name of user table field/fields on which data needs to be compared
* @var mixed
*/
protected $field;

/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param boolean $advanced advanced form element flag
* @param mixed $field user table field/fields name for comparison
* @param array $disableelements name of fields which should be disabled if this checkbox is checked.
*/
public function __construct($name, $label, $advanced, $field, $disableelements=null) {
parent::__construct($name, $label, $advanced);
$this->field = $field;
if (!empty($disableelements)) {
if (!is_array($disableelements)) {
$this->disableelements = array($disableelements);
} else {
$this->disableelements = $disableelements;
}
}
}

/**
* Adds controls specific to this filter in the form.
* @param moodleform $mform a MoodleQuickForm object in which element will be added
*/
public function setupForm(MoodleQuickForm &$mform) {
$objs = array();

$objs[] = $mform->createElement('checkbox', $this->_name, null, '');
$grp = $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);

if ($this->_advanced) {
$mform->setAdvanced($this->_name.'_grp');
}
//Check if disable if options are set. if yes then set rules
if (!empty($this->disableelements) && is_array($this->disableelements)) {
foreach ($this->disableelements as $disableelement) {
$mform->disabledIf($disableelement, $this->_name, 'checked');
}
}
}

/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
* @return mixed array filter data or false when filter not set
*/
public function check_data($formdata) {
$field = $this->_name;
//Check if disable if options are set. if yes then don't add this.
if (!empty($this->disableelements) && is_array($this->disableelements)) {
foreach ($this->disableelements as $disableelement) {
if (array_key_exists($disableelement, $formdata)) {
return false;
}
}
}
if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
return array('value' => (string)$formdata->$field);
}
return false;
}

/**
* Returns the condition to be used with SQL where
* @param array $data filter settings
* @return array sql string and $params
*/
public function get_sql_filter($data) {
$field = $this->field;
if (is_array($field)) {
$res = " {$field[0]} = {$field[1]} ";
} else {
$res = " {$field} = 0 ";
}
return array($res, array());
}

/**
* Returns a human friendly description of the filter used as label.
* @param array $data filter settings
* @return string active filter label
*/
public function get_label($data) {
return $this->_label;
}
}
2 changes: 1 addition & 1 deletion user/filters/courserole.php
Expand Up @@ -22,7 +22,7 @@ function user_filter_courserole($name, $label, $advanced) {
*/ */
function get_roles() { function get_roles() {
$context = get_context_instance(CONTEXT_SYSTEM); $context = get_context_instance(CONTEXT_SYSTEM);
$roles = array(0=> get_string('anyrole','filters')) + get_assignable_roles($context); $roles = array(0=> get_string('anyrole','filters')) + get_default_enrol_roles($context);
return $roles; return $roles;
} }


Expand Down
33 changes: 4 additions & 29 deletions user/filters/date.php
Expand Up @@ -35,7 +35,6 @@ function setupForm(&$mform) {
$objs[] =& $mform->createElement('static', $this->_name.'_break', null, '<br/>'); $objs[] =& $mform->createElement('static', $this->_name.'_break', null, '<br/>');
$objs[] =& $mform->createElement('checkbox', $this->_name.'_eck', null, get_string('isbefore', 'filters')); $objs[] =& $mform->createElement('checkbox', $this->_name.'_eck', null, get_string('isbefore', 'filters'));
$objs[] =& $mform->createElement('date_selector', $this->_name.'_edt', null); $objs[] =& $mform->createElement('date_selector', $this->_name.'_edt', null);
$objs[] = & $mform->createElement('checkbox', $this->_name.'_never', null, get_string('includenever', 'filters'));


$grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);


Expand All @@ -49,8 +48,6 @@ function setupForm(&$mform) {
$mform->disabledIf($this->_name.'_edt[day]', $this->_name.'_eck', 'notchecked'); $mform->disabledIf($this->_name.'_edt[day]', $this->_name.'_eck', 'notchecked');
$mform->disabledIf($this->_name.'_edt[month]', $this->_name.'_eck', 'notchecked'); $mform->disabledIf($this->_name.'_edt[month]', $this->_name.'_eck', 'notchecked');
$mform->disabledIf($this->_name.'_edt[year]', $this->_name.'_eck', 'notchecked'); $mform->disabledIf($this->_name.'_edt[year]', $this->_name.'_eck', 'notchecked');

$mform->disabledIf($this->_name.'_never', $this->_name.'_eck', 'notchecked');
} }


/** /**
Expand All @@ -63,7 +60,6 @@ function check_data($formdata) {
$sdt = $this->_name.'_sdt'; $sdt = $this->_name.'_sdt';
$eck = $this->_name.'_eck'; $eck = $this->_name.'_eck';
$edt = $this->_name.'_edt'; $edt = $this->_name.'_edt';
$never = $this->_name.'_never';


if (!array_key_exists($sck, $formdata) and !array_key_exists($eck, $formdata)) { if (!array_key_exists($sck, $formdata) and !array_key_exists($eck, $formdata)) {
return false; return false;
Expand All @@ -80,11 +76,6 @@ function check_data($formdata) {
} else { } else {
$data['before'] = 0; $data['before'] = 0;
} }
if (array_key_exists($never, $formdata)) {
$data['never'] = $formdata->$never;
} else {
$data['never'] = 0;
}


return $data; return $data;
} }
Expand All @@ -97,21 +88,14 @@ function check_data($formdata) {
function get_sql_filter($data) { function get_sql_filter($data) {
$after = (int)$data['after']; $after = (int)$data['after'];
$before = (int)$data['before']; $before = (int)$data['before'];
$never = (int)$data['never'];


$field = $this->_field; $field = $this->_field;


if (empty($after) and empty($before)) { if (empty($after) and empty($before)) {
return array('', array()); return array('', array());
} }


$res = ''; $res = " $field >= 0 " ;

if (!empty($never)) {
$res .= " $field >= 0 " ;
} else {
$res .= " $field > 0 " ;
}


if ($after) { if ($after) {
$res .= " AND $field >= $after"; $res .= " AND $field >= $after";
Expand All @@ -131,28 +115,19 @@ function get_sql_filter($data) {
function get_label($data) { function get_label($data) {
$after = $data['after']; $after = $data['after'];
$before = $data['before']; $before = $data['before'];
$never = $data['never'];
$field = $this->_field; $field = $this->_field;


$a = new stdClass(); $a = new stdClass();
$a->label = $this->_label; $a->label = $this->_label;
$a->after = userdate($after); $a->after = userdate($after);
$a->before = userdate($before); $a->before = userdate($before);


if ($never) {
$strnever = ' ('.get_string('includenever', 'filters').')';
} else {
$strnever = '';
}

if ($after and $before) { if ($after and $before) {
return get_string('datelabelisbetween', 'filters', $a).$strnever; return get_string('datelabelisbetween', 'filters', $a);

} else if ($after) { } else if ($after) {
return get_string('datelabelisafter', 'filters', $a).$strnever;; return get_string('datelabelisafter', 'filters', $a);

} else if ($before) { } else if ($before) {
return get_string('datelabelisbefore', 'filters', $a).$strnever;; return get_string('datelabelisbefore', 'filters', $a);
} }
return ''; return '';
} }
Expand Down
8 changes: 5 additions & 3 deletions user/filters/lib.php
Expand Up @@ -10,7 +10,7 @@
require_once($CFG->dirroot.'/user/filters/yesno.php'); require_once($CFG->dirroot.'/user/filters/yesno.php');
require_once($CFG->dirroot.'/user/filters/cohort.php'); require_once($CFG->dirroot.'/user/filters/cohort.php');
require_once($CFG->dirroot.'/user/filters/user_filter_forms.php'); require_once($CFG->dirroot.'/user/filters/user_filter_forms.php');

require_once($CFG->dirroot.'/user/filters/checkbox.php');


/** /**
* User filtering wrapper class. * User filtering wrapper class.
Expand All @@ -36,7 +36,8 @@ function user_filtering($fieldnames=null, $baseurl=null, $extraparams=null) {
if (empty($fieldnames)) { if (empty($fieldnames)) {
$fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1, $fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1,
'confirmed'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1, 'cohort'=>1, 'confirmed'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1, 'cohort'=>1,
'firstaccess'=>1, 'lastaccess'=>1, 'lastlogin'=>1, 'timemodified'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1); 'firstaccess'=>1, 'lastaccess'=>1, 'neveraccessed'=>1, 'timemodified'=>1,
'nevermodified'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1);
} }


$this->_fields = array(); $this->_fields = array();
Expand Down Expand Up @@ -114,8 +115,9 @@ function get_field($fieldname, $advanced) {
case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess'); case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin'); case 'neveraccessed': return new user_filter_checkbox('neveraccessed', get_string('neveraccessed', 'filters'), $advanced, 'firstaccess', array('lastaccess_sck', 'lastaccess_eck', 'firstaccess_eck', 'firstaccess_sck'));
case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified'); case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified');
case 'nevermodified': return new user_filter_checkbox('nevermodified', get_string('nevermodified', 'filters'), $advanced, array('timemodified', 'timecreated'), array('timemodified_sck', 'timemodified_eck'));
case 'cohort': return new user_filter_cohort($advanced); case 'cohort': return new user_filter_cohort($advanced);
case 'auth': case 'auth':
$plugins = get_plugin_list('auth'); $plugins = get_plugin_list('auth');
Expand Down

0 comments on commit 59e2fd4

Please sign in to comment.