Is it possible to add multiple user filters?

Is it possible to add multiple user filters?

by Elliott Benzle -
Number of replies: 13

I need my report to have multiple user filters. For example, filter by both city and country. Is this something the plugin does? or will I need to write code for each of the new filters? 

I notice that once I add a filter, I can no longer add the same type again. Is this the way it is supposed to work?

Thanks

Average of ratings: -
In reply to Elliott Benzle

Re: Is it possible to add multiple user filters?

by Kimber Warden -

I have the same question. I would like to be able to add a User field search for firstname and lastname, but I can only figure out how to have one instance of a filter type.

In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by Peter Bowen -

Not directly within the module.

However, I found a nice workaround - under the \blocks\configurable_reports\components\filters directory is the code for each filter. If you make a copy of the filter, rename the directory and rename the class in line 31 and the element in line 37, you can create as many filters as you like. (I have about 5 filters which specifically search for different fields)

Cheers
Peter

Average of ratings: Useful (1)
In reply to Peter Bowen

Re: Is it possible to add multiple user filters?

by anthony leduc -

Hi,

I'm very interested for your workaround.

Can you put some screenshot please ?

 

Cheers,

Anthony

In reply to Peter Bowen

Re: Is it possible to add multiple user filters?

by Kimber Warden -

Peter, you're my hero!

For anyone else wanting to do this, there are a few other places you'll have to change the name. Just do a find and replace fsearchuserfield with fsearchuserfieldtwo. Also, add this around line 139 of lang/en/block_configurable_reports.php:

$string['fsearchuserfieldtwo'] = "Second user field search box";

Lastly, replace "FILTER_USERS" with "FILTER_USERS2" in lines 54 and 56 of configurable_reports/components/filters/fsearchuserfieldtwo/plugin.class.php.

When you're writing your SQL filter, use "%%FILTER_USERS2:u.firstname%%" to filter by first name (for example).

Average of ratings: Useful (4)
In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by anthony leduc -

And yesss...it works for me too wink

Thank u both.

I save the thread now ^^

Average of ratings: Useful (1)
In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by Peter Bowen -

OK - so I was being lazy on the language block - nice pick up there. (I am the only one creating reports for my site, so it doesn't have to look pretty)

I am thinking about using this as a course search as well - although my head is being done in on it for a Friday arfternoon - this would be able to inject code to avoid non visible courses. :D

I will post code if I can nut it.

In reply to Peter Bowen

Re: Is it possible to add multiple user filters?

by Ferry van der Vorst -

Hi.

 

I am trying to achieve the multiple filters option with the latest version of the plugin, but can't get this to work.

 

Would you be able to provide me a sample, so I can replicate?

 

Many thanks.

Average of ratings: Useful (1)
In reply to Peter Bowen

Re: Is it possible to add multiple user filters?

by Edmund Evangelista -

Hi Peter,

Have you tried changing the text of "Search Text" filter into a more descriptive one like Academic Year?


Attachment x.jpg
In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by Vani Bheemreddy -

Hi Kimber,

I now see it is easy to create a second instance of the existing filter- user filter in your example. But I am wondering how I can create other filters like activity and action filters in configurable reports similar to the ones on Reports >Log. Hope you can answer this one too. 

What are the places where the plugin.class.php file be changed if I were to add activity filter and action filter. Activity includes Forum, Chat etc and Action includes View/Delete and other actions.

Which plugin.class file from the existing list of files(categories/course categories/courses/fcoursefield/startendtime) can be used as base for creating activity and action filters?

Thanks in advance for any help. Have a great day!!


In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by Rob Cole -

Can you please give the line above and below

$string['fsearchuserfieldtwo'] = "Second user field search box";

so that I know I am adding it to the right place? Thanks!

In reply to Kimber Warden

Re: Is it possible to add multiple user filters?

by Edmund Evangelista -

Hi Kimber, I tried to copy a filter and save it on another filter, modified the class, but I am still getting an error once I have loaded the report page. Can you provide a screenshot of your modified plugin php code? I am using Moodle 3.1 and I find the lines of code you mentioned a bit different with mine. Attached below is the actual code of filter "users", can you point out which particular lines should I modify if I will make another one and name it as "users2"?

Attachment x.jpg
In reply to Peter Bowen

Re: Is it possible to add multiple user filters?

by J K -

Hey Peter, this looks really interesting! I'm trying to have several selected users view a report, but the Permissions option only allows one user.

Could you please elaborate a bit on your post ? "However, I found a nice workaround - under the \blocks\configurable_reports\components\filters directory is the code for each filter. If you make a copy of the filter, rename the directory and rename the class in line 31 and the element in line 37, you can create as many filters as you like. (I have about 5 filters which specifically search for different fields)"


I'm using Moodle 2.6 and unsure about the following:-

  • which filter to copy - user or users?
  • the changes you mention in lines 31 & 37 refer to what file exactly? - I had a look in the file (plug.class.php in user folder, but see the attached lines (screenshot attached)


Any help would be greatly appreciated!


Jason

Attachment Capture.PNG
In reply to J K

Re: Is it possible to add multiple user filters?

by Peter Bowen -

Hmm - must have missed this post...

This is about using filters to reduce the number of results in your query, not restricting who can see the query.

The lines you need to modify are:

class plugin_filtername extends plugin_base{

You need to replace filtername with the name of your new filter

The within the private function execute_sql, in all places, replace the filter_filtername with the name of your new filter.


For example, I have one that searches for department, so I have....

require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');

class plugin_department extends plugin_base{
   
    function init(){
        $this->form = true;
        $this->unique = true;
        $this->fullname = get_string('department','block_configurable_reports');
        $this->reporttypes = array('users', 'sql');
    }
   
    function summary($data){
        return $data->field;
    }
   
    function execute($finalelements,$data){
        if($this->report->type == 'sql') {
            return $this->execute_sql($finalelements, $data);
        }

        return $this->execute_users($finalelements, $data);


    }
   
    private function execute_sql($finalelements, $data) {
        $filter_department = optional_param('filter_department_'.$data->field,0,PARAM_RAW);
        $filter = clean_param(base64_decode($filter_department),PARAM_CLEAN);

        if($filter_department &&
           preg_match("/%%FILTER_DEPARTMENTsad[^%]+)%%/i",$finalelements, $output)){
            $replace = ' AND '.$output[1].' = '. "'$filter'";
            return str_replace('%%FILTER_DEPARTMENT:'.$output[1].'%%',$replace,$finalelements);
        }

        return $finalelements;
    }

    private function execute_users($finalelements, $data) {
        global $DB, $CFG;
       
        $filter_department = optional_param('filter_department_'.$data->field,0,PARAM_RAW);       
        if($filter_department){
            // addslashes is done in clean param
            $filter = clean_param(base64_decode($filter_department),PARAM_CLEAN);