Search code examples
phppluginsmoodle

Is there way to add a userselector to the settings page of an activity module in moodle?


So, basically what the title says.

Here is what I would like to have in my settings.php page: what I want

And here is what I have as a workaround:

    $warning = 'Please make sure you have no unsaved changes.';
    $link = "</form><a href=".new moodle_url('/mod/game/clear.php')." class='btn btn-danger';>Empty all results</a> <strong style='color: red;'>".$warning."</strong>";

    $clear_url = new moodle_url('/mod/game/clear_user_results.php');

    $link .= "</br>
    <form action='".$clear_url."' class='mt-3' method='POST'>
        <div class='input-group rounded'>
            <input type='text' class='form-control rounded' placeholder='Search a user' name='userSearch' aria-label='Search' aria-describedby='search-addon'/>
            <button class='input-group-text border-0' type='submit'>
                <i class='fa fa-search'></i>
            </button>
        </div>
    </form>
      ";

    
    $settings->add(new admin_setting_heading('modemptydb', get_string('modemptydb', 'game'), $link));

what I got

It works but I'm having to enter the exact name for the user whereas in the first picture it also shows me other results matching the query.

Is there a way to achieve this?

I've tried both of these but they didn't work:

    $settings->add(new core_role_admins_existing_selector());
    $settings->add(new core_role_check_users_selector());

Solution

  • This is how I did it:

        $userselector = new core_role_check_users_selector('reportuser', array());
        $userselector->set_rows(20);
    
        $OUTPUT->box_start('generalbox boxwidthnormal boxaligncenter', 'chooseuser');
        $selector = "<div class='container'>";
        $selector .= "<form method='POST' action='".$clear_url."'>";
        $selector .= $userselector->display(true);
        $selector .= '<p id="chooseusersubmit"><input type="submit" value="' . get_string('seeuserresults', 'game') . '" ' .
                'class="btn btn-primary"/></p>';
                '</form>
                </div>';
        $selector .= $OUTPUT->box_end();
    
        $settings->add(new admin_setting_heading('modemptydb', get_string('modemptydb', 'game'), $link.$selector));