-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.php
195 lines (162 loc) · 8.06 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?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/>.
/**
* Version information
*
* @package tool
* @subpackage mergeusers
* @author Nicolas Dunand <[email protected]>
* @author Mike Holzer
* @author Forrest Gaston
* @author Juan Pablo Torres Herrera
* @author Jordi Pujol-Ahulló, Sred, Universitat Rovira i Virgili
* @author John Hoopes <[email protected]>, 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('./index_form.php');
require_once(__DIR__ . '/lib/autoload.php');
require_login();
require_capability('tool/mergeusers:mergeusers', context_system::instance());
admin_externalpage_setup('tool_mergeusers_merge');
// 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();
//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 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) {
$renderer->mu_error(get_string('no_saveselection', 'tool_mergeusers'));
exit(); // end execution for error
}
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)) {
$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)) {
$SESSION->mut->newuser = $newuser;
}
$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;
// 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;
// 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) {
$renderer->mu_error($oumessage . '<br />' . $numessage);
break; // break execution for error
}
// Merge the users
$log = array();
$success = true;
list($success, $log, $logid) = $mut->merge($touser->id, $fromuser->id);
// reset mut session
$SESSION->mut = null;
// 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;
// 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;
}
// Any submitted data?
} else if ($data) {
// If there is a search argument use this instead of advanced form
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, $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) {
$renderer->mu_error($oumessage . '<br />' . $numessage);
exit(); // end execution for error
}
// Add users to session for review step
if (empty($SESSION->mut)) {
$SESSION->mut = new stdClass();
}
$SESSION->mut->olduser = $olduser;
$SESSION->mut->newuser = $newuser;
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 {
// no form submitted data
echo $renderer->index_page($mergeuserform, $renderer::INDEX_PAGE_SEARCH_STEP);
}