Skip to content

Commit

Permalink
moodle-tool_mergeusers - #39 - search UI
Browse files Browse the repository at this point in the history
Related to issue #39.

* New UI for searching and confirming users to merge.
* Progress bar added.

All based on John Hoopes contribution.
  • Loading branch information
jpahullo committed May 6, 2014
1 parent 5a44284 commit 8764274
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 140 deletions.
101 changes: 61 additions & 40 deletions index.php
@@ -1,4 +1,5 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -27,20 +28,20 @@
* @author John Hoopes <hoopes@wisc.edu>, University of Wisconsin - Madison
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require('../../../config.php');

global $CFG;
global $PAGE;
global $SESSION;

// Report all PHP errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');

require_once($CFG->libdir.'/blocklib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/accesslib.php');
require_once($CFG->libdir.'/weblib.php');
require_once($CFG->libdir . '/blocklib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/accesslib.php');
require_once($CFG->libdir . '/weblib.php');

require_once('./index_form.php');
require_once(__DIR__ . '/lib/autoload.php');
Expand All @@ -52,67 +53,78 @@

// Get possible posted params
$option = optional_param('option', NULL, PARAM_TEXT);
if (!$option) {
if (optional_param('clearselection', false, PARAM_TEXT)) {
$option = 'clearselection';
} else if (optional_param('mergeusers', false, PARAM_TEXT)) {
$option = 'mergeusers';
}
}

// Define the form
$mergeuserform = new mergeuserform();
$renderer = $PAGE->get_renderer('tool_mergeusers');

$data = $mergeuserform->get_data();

$mut = new MergeUserTool(); //may abort execution if database not supported, for security
$mus = new MergeUserSearch(); // Search tool for searching for users and verifying them
//may abort execution if database not supported, for security
$mut = new MergeUserTool();
// Search tool for searching for users and verifying them
$mus = new MergeUserSearch();

if(!empty($option)){ // if there was a custom option submitted (by custom form) then use that option instead of main form's data

switch($option){
// If there was a custom option submitted (by custom form) then use that option
// instead of main form's data
if (!empty($option)) {
switch ($option) {
// one or two users are selected: save them into session.
case 'saveselection':

//get and verify the userids from the selection form usig the verify_user function (second field is column)
list($olduser, $oumessage) = $mus->verify_user(optional_param('olduser', NULL, PARAM_INT), 'id');
list($newuser, $numessage) = $mus->verify_user(optional_param('newuser', NULL, PARAM_INT), 'id');

if($olduser === NULL && $newuser === NULL){
if ($olduser === NULL && $newuser === NULL) {
$renderer->mu_error(get_string('no_saveselection', 'tool_mergeusers'));
exit(); // end execution for error
}

if(empty($SESSION->mut)){
if (empty($SESSION->mut)) {
$SESSION->mut = new stdClass();
}

// Store saved selection in session for display on index page, requires logic to not overwrite existing session
// data, unless a "new" old, or "new" new is specified

// If session old user already has a user and we have a "new" old user, replace the sesson's old user
if(empty($SESSION->mut->olduser) || !empty($olduser) ){
if (empty($SESSION->mut->olduser) || !empty($olduser)) {
$SESSION->mut->olduser = $olduser;
}

// If session new user already has a user and we have a "new" new user, replace the sesson's new user
if(empty($SESSION->mut->newuser) || !empty($newuser) ){
if (empty($SESSION->mut->newuser) || !empty($newuser)) {
$SESSION->mut->newuser = $newuser;
}

// Redirect back to index/search page for new selections or review selections
$redirecturl = new moodle_url('/admin/tool/mergeusers/index.php');
redirect($redirecturl, NULL, 0);
$step = (!empty($SESSION->mut->olduser) && !empty($SESSION->mut->newuser)) ?
$renderer::INDEX_PAGE_CONFIRMATION_STEP :
$renderer::INDEX_PAGE_SEARCH_STEP;

echo $renderer->index_page($mergeuserform, $step);
break;
case 'clearselection':

// remove any of the selected users to merge, and search for them again.
case 'clearselection':
$SESSION->mut = NULL;

// Redirect back to index/search page for new selections or review selections
$redirecturl = new moodle_url('/admin/tool/mergeusers/index.php');
redirect($redirecturl, NULL, 0);

break;
case 'mergeusers':

// proceed with the merging and show results.
case 'mergeusers':
// Verify users once more just to be sure. Both users should already be verified, but just an extra layer of security
list($fromuser, $oumessage) = $mus->verify_user($SESSION->mut->olduser->id, 'id');
list($touser, $numessage) = $mus->verify_user($SESSION->mut->newuser->id, 'id');
if($fromuser === NULL || $touser === NULL){
if ($fromuser === NULL || $touser === NULL) {
$renderer->mu_error($oumessage . '<br />' . $numessage);
break; // break execution for error
}
Expand All @@ -127,48 +139,57 @@

// render results page
echo $renderer->results_page($touser, $fromuser, $success, $log, $logid);
break;

// we have both users to merge selected, but we want to change any of them.
case 'searchusers':
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_STEP);
break;
default:

$renderer->mu_error(get_string('invalid_option', 'tool_mergeusers'));
// we have both users to merge selected, and in the search step, we
// want to proceed with the merging of the currently selected users.
case 'continueselection':
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_CONFIRMATION_STEP);
break;

// ops!
default:
$renderer->mu_error(get_string('invalid_option', 'tool_mergeusers'));
break;
}

}else if ($data) { // Any submitted data?
// Any submitted data?
} else if ($data) {
// If there is a search argument use this instead of advanced form
if(!empty($data->searchgroup['searcharg'])){
if (!empty($data->searchgroup['searcharg'])) {

$search_users = $mus->search_users($data->searchgroup['searcharg'], $data->searchgroup['searchfield']);
$user_select_table = new UserSelectTable($search_users, $renderer);

echo $renderer->index_page($mergeuserform, $user_select_table);
}else if(!empty($data->oldusergroup['olduserid']) && !empty($data->newusergroup['newuserid']) ){ // only run this step if there are both a new and old userids
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_AND_SELECT_STEP, $user_select_table);

// only run this step if there are both a new and old userids
} else if (!empty($data->oldusergroup['olduserid']) && !empty($data->newusergroup['newuserid'])) {
//get and verify the userids from the selection form usig the verify_user function (second field is column)
list($olduser, $oumessage) = $mus->verify_user($data->oldusergroup['olduserid'], $data->oldusergroup['olduseridtype']);
list($newuser, $numessage) = $mus->verify_user($data->newusergroup['newuserid'], $data->newusergroup['newuseridtype']);

if($olduser === NULL || $newuser === NULL){
if ($olduser === NULL || $newuser === NULL) {
$renderer->mu_error($oumessage . '<br />' . $numessage);
exit(); // end execution for error
}
// Add users to session for review step
if(empty($SESSION->mut)){
if (empty($SESSION->mut)) {
$SESSION->mut = new stdClass();
}
$SESSION->mut->olduser = $olduser;
$SESSION->mut->newuser = $newuser;

echo $renderer->index_page($mergeuserform);
}else{
echo $renderer->index_page($mergeuserform);
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_AND_SELECT_STEP);
} else {
// simply show search form.
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_STEP);
}

} else {
} else {
// no form submitted data
echo $renderer->index_page($mergeuserform);
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_STEP);
}

11 changes: 8 additions & 3 deletions lang/ca/tool_mergeusers.php
Expand Up @@ -61,6 +61,11 @@

//New strings

// Progress bar
$string['choose_users'] = 'Escull els usuaris a fusionar';
$string['review_users'] = 'Confirma els usuaris a fusionar';
$string['results'] = 'Resultats de la fusió';

// Form Strings
$string['form_header'] = 'Cerca els usuaris a fusionar';
$string['form_description'] = '<p>A continuació pots cercar els usuaris a fusionar.
Expand Down Expand Up @@ -90,9 +95,9 @@
$string['userreviewtable_legend'] = '<b>Usuaris a fusionar</b>';

// Error string
$string['error_return'] = 'Return to search form';
$string['no_saveselection'] = 'You did not select either an old or new user.';
$string['invalid_option'] = 'Invalid form option';
$string['error_return'] = 'Retorna al formulari de cerca';
$string['no_saveselection'] = 'No has seleccionat cap usuari.';
$string['invalid_option'] = 'Opció incorrecta';

// Settings page
$string['suspenduser_setting'] = 'Suspendre usuari a eliminar';
Expand Down
5 changes: 5 additions & 0 deletions lang/en/tool_mergeusers.php
Expand Up @@ -83,6 +83,11 @@

//New strings

// Progress bar
$string['choose_users'] = 'Choose users to merge';
$string['review_users'] = 'Confirm users to merge';
$string['results'] = 'Merging results and log';

// Form Strings
$string['form_header'] = 'Find users to merge';
$string['form_description'] = '<p>You may search for users here if you don\'t
Expand Down
8 changes: 6 additions & 2 deletions lang/es/tool_mergeusers.php
Expand Up @@ -62,6 +62,11 @@

//New strings

// Progress bar
$string['choose_users'] = 'Escoge los usuarios a fusionar';
$string['review_users'] = 'Confirma los usuarios a fusionar';
$string['results'] = 'Resultados de la fusión';

// Form Strings
$string['form_header'] = 'Busca los usuarios a fusionar';
$string['form_description'] = '<p>A continuación puedes buscar los usuarios a fusionar.
Expand Down Expand Up @@ -92,8 +97,7 @@

// Error string
$string['error_return'] = 'Vuelve al formulario de búsqueda';
$string['no_saveselection'] = 'No has seleccionado ni usuario a eliminar ni a mantener.
Selecciona al menos uno.';
$string['no_saveselection'] = 'No has seleccionado ningún usuario.';
$string['invalid_option'] = 'Opción inválida';

// Settings page
Expand Down
7 changes: 7 additions & 0 deletions lang/fr/tool_mergeusers.php
Expand Up @@ -67,6 +67,13 @@
configurer que les fusions sont faites sans transactions.
Vérifiez les paramètres en fonction de vos besoins.';

//New strings

// Progress bar
$string['choose_users'] = 'Sélectionnez les utilisateurs de fusionner';
$string['review_users'] = 'Confirmez utilisateurs de fusionner';
$string['results'] = 'Résultats de la fusion';

// Settings page
$string['transactions_setting'] = 'Seules les transactions sont autorisées';
$string['transactions_setting_desc'] = 'Si cette option est activée,
Expand Down
18 changes: 0 additions & 18 deletions lib/userreviewtable.php
Expand Up @@ -87,11 +87,9 @@ public function __construct($renderer)

/**
* Build the user select table using the extension of html_table
*
*/
protected function buildtable()
{

// Reset any existing data
$this->data = array();

Expand Down Expand Up @@ -136,22 +134,6 @@ protected function buildtable()
$newuserrow[] = '';
}
$this->data[] = $newuserrow;

// If both are not empty this means we can show merge button
if (!empty($this->olduser) && !empty($this->newuser)) {
$this->showmergebutton = true;
}
}
}

/**
* Simple get function so that the showmergebutton var is protected from outside
*
* @return bool whether or not to show the button
*/
public function show_button()
{
return $this->showmergebutton;
}

}

0 comments on commit 8764274

Please sign in to comment.