forked from jpahullo/moodle-tool_mergeusers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
174 lines (133 loc) · 6.79 KB
/
Copy pathindex.php
File metadata and controls
174 lines (133 loc) · 6.79 KB
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
<?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 <Nicolas.Dunand@unil.ch>
* @author Mike Holzer
* @author Forrest Gaston
* @author Juan Pablo Torres Herrera
* @author Jordi Pujol-Ahulló, Sred, Universitat Rovira i Virgili
* @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 $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('moodle/site:config', context_system::instance());
admin_externalpage_setup('tool_mergeusers_merge');
// Get possible posted params
$option = optional_param('option', NULL, PARAM_TEXT);
// 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
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){
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;
}
// 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 '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':
// 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;
default:
$renderer->mu_error(get_string('invalid_option', 'tool_mergeusers'));
break;
}
}else if ($data) { // Any submitted 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);
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
//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);
}else{
echo $renderer->index_page($mergeuserform);
}
} else {
// no form submitted data
echo $renderer->index_page($mergeuserform);
}