forked from jpahullo/moodle-tool_mergeusers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
344 lines (307 loc) · 12.9 KB
/
renderer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?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/>.
/**
* @author Jordi Pujol-Ahulló <[email protected]>
* @author John Hoopes <[email protected]>, University of Wisconsin - Madison
* @copyright 2013 Servei de Recursos Educatius (http://www.sre.urv.cat)
*/
defined('MOODLE_INTERNAL') || die();
require_once __DIR__ . '/select_form.php';
require_once __DIR__ . '/review_form.php';
require_once($CFG->dirroot . '/'.$CFG->admin.'/tool/mergeusers/lib.php');
/**
* Renderer for the merge user plugin.
*
* @package tool
* @subpackage mergeuser
* @copyright 2013 Jordi Pujol-Ahulló, SREd, Universitat Rovira i Virgili
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_mergeusers_renderer extends plugin_renderer_base
{
/** On index page, show only the search form. */
const INDEX_PAGE_SEARCH_STEP = 1;
/** On index page, show both search and select forms. */
const INDEX_PAGE_SEARCH_AND_SELECT_STEP = 2;
/** On index page, show only the list of users to merge. */
const INDEX_PAGE_CONFIRMATION_STEP = 3;
/** On index page, show the merging results. */
const INDEX_PAGE_RESULTS_STEP = 4;
/**
* Renderers a progress bar.
* @param array $items An array of items
* @return string
*/
public function progress_bar(array $items)
{
foreach ($items as &$item) {
$text = $item['text'];
unset($item['text']);
if (array_key_exists('link', $item)) {
$link = $item['link'];
unset($item['link']);
$item = html_writer::link($link, $text, $item);
} else {
$item = html_writer::tag('span', $text, $item);
}
}
return html_writer::tag('div', join(get_separator(), $items), array('class' => 'merge_progress clearfix'));
}
/**
* Returns the HTML for the progress bar, according to the current step.
* @param int $step current step
* @return string HTML for the progress bar.
*/
public function build_progress_bar($step)
{
$steps = array(
array('text' => '1. ' . get_string('choose_users', 'tool_mergeusers')),
array('text' => '2. ' . get_string('review_users', 'tool_mergeusers')),
array('text' => '3. ' . get_string('results', 'tool_mergeusers')),
);
switch ($step) {
case self::INDEX_PAGE_SEARCH_STEP:
case self::INDEX_PAGE_SEARCH_AND_SELECT_STEP:
$steps[0]['class'] = 'bold';
break;
case self::INDEX_PAGE_CONFIRMATION_STEP:
$steps[1]['class'] = 'bold';
break;
case self::INDEX_PAGE_RESULTS_STEP:
$steps[2]['class'] = 'bold';
}
return $this->progress_bar($steps);
}
/**
* Shows form for merging users.
* @param moodleform $mform form for merging users.
* @param int $step step to show in the index page.
* @param UserSelectTable $ust table for users to merge after searching
* @return string html to show on index page.
*/
public function index_page(moodleform $mform, $step, UserSelectTable $ust = null)
{
$output = $this->header();
$output .= $this->heading_with_help(get_string('mergeusers', 'tool_mergeusers'), 'header', 'tool_mergeusers');
$output .= $this->build_progress_bar($step);
switch ($step) {
case self::INDEX_PAGE_SEARCH_STEP:
$output .= $this->moodleform($mform);
break;
case self::INDEX_PAGE_SEARCH_AND_SELECT_STEP:
$output .= $this->moodleform($mform);
// Render user select table if available
if ($ust !== null) {
$this->page->requires->js_init_call('M.tool_mergeusers.init_select_table', array());
$output .= $this->render_user_select_table($ust);
}
break;
case self::INDEX_PAGE_CONFIRMATION_STEP:
break;
}
$output .= $this->render_user_review_table($step);
$output .= $this->footer();
return $output;
}
/**
* Renders user select table
* @param UserSelectTable $ust the user select table
*
* @return string $tablehtml html string rendering
*/
public function render_user_select_table(UserSelectTable $ust)
{
return $this->moodleform(new selectuserform($ust));
}
/**
* Builds and renders a user review table
*
* @return string $reviewtable HTML of the review table section
*/
public function render_user_review_table($step)
{
return $this->moodleform(
new reviewuserform(
new UserReviewTable($this),
$this,
$step === self::INDEX_PAGE_CONFIRMATION_STEP));
}
/**
* Displays merge users tool error message
*
* @param string $message The error message
* @param bool $showreturn Shows a return button to the index page
*
*/
public function mu_error($message, $showreturn = true)
{
$errorhtml = '';
echo $this->header();
$errorhtml .= $this->output->box($message, 'generalbox align-center');
if ($showreturn) {
$returnurl = new moodle_url('/admin/tool/mergeusers/index.php');
$returnbutton = new single_button($returnurl, get_string('error_return', 'tool_mergeusers'));
$errorhtml .= $this->output->render($returnbutton);
}
echo $errorhtml;
echo $this->footer();
}
/**
* Shows the result of a merging action.
* @param object $to stdClass with at least id and username fields.
* @param object $from stdClass with at least id and username fields.
* @param bool $success true if merging was ok; false otherwise.
* @param array $data logs of actions done if success, or list of errors on failure.
* @param id $logid id of the record with the whole detail of this merging action.
* @return string html with the results.
*/
public function results_page($to, $from, $success, array $data, $logid)
{
if ($success) {
$resulttype = 'ok';
$dbmessage = 'dbok';
$notifytype = 'notifysuccess';
} else {
$transactions = (tool_mergeusers_transactionssupported()) ?
'_transactions' :
'_no_transactions';
$resulttype = 'ko';
$dbmessage = 'dbko' . $transactions;
$notifytype = 'notifyproblem';
}
$output = $this->header();
$output .= $this->heading(get_string('mergeusers', 'tool_mergeusers'));
$output .= $this->build_progress_bar(self::INDEX_PAGE_RESULTS_STEP);
$output .= html_writer::empty_tag('br');
$output .= html_writer::start_tag('div', array('class' => 'result'));
$output .= html_writer::start_tag('div', array('class' => 'title'));
$output .= get_string('merging', 'tool_mergeusers');
if (!is_null($to) && !is_null($from)) {
$output .= ' ' . get_string('usermergingheader', 'tool_mergeusers', $from) . ' ' .
get_string('into', 'tool_mergeusers') . ' ' .
get_string('usermergingheader', 'tool_mergeusers', $to);
}
$output .= html_writer::empty_tag('br') . html_writer::empty_tag('br');
$output .= get_string('logid', 'tool_mergeusers', $logid);
$output .= html_writer::empty_tag('br');
$output .= get_string('log' . $resulttype, 'tool_mergeusers');
$output .= html_writer::end_tag('div');
$output .= html_writer::empty_tag('br');
$output .= html_writer::start_tag('div', array('class' => 'resultset' . $resulttype));
foreach ($data as $item) {
$output .= $item . html_writer::empty_tag('br');
}
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
$output .= html_writer::tag('div', html_writer::empty_tag('br'));
$output .= $this->notification(html_writer::tag('center', get_string($dbmessage, 'tool_mergeusers')), $notifytype);
$output .= html_writer::tag('center', $this->single_button(new moodle_url('/admin/tool/mergeusers/index.php'), get_string('continue'), 'get'));
$output .= $this->footer();
return $output;
}
/**
* Helper method dealing with the fact we can not just fetch the output of moodleforms
*
* @param moodleform $mform
* @return string HTML
*/
protected function moodleform(moodleform $mform)
{
ob_start();
$mform->display();
$o = ob_get_contents();
ob_end_clean();
return $o;
}
/**
* This method produces the HTML to show the details of a user.
* @param int $userid user.id
* @param object $user an object with firstname and lastname attributes.
* @return string the corresponding HTML.
*/
public function show_user($userid, $user)
{
return html_writer::link(
new moodle_url('/user/view.php',
array('id' => $userid, 'sesskey' => sesskey())),
fullname($user) .
' (' . $user->username . ') ' .
' <' . $user->email . '>' .
' ' . $user->idnumber);
}
/**
* Produces the page with the list of logs.
* TODO: make pagination.
* @global type $CFG
* @param array $logs array of logs.
* @return string the corresponding HTML.
*/
public function logs_page($logs)
{
global $CFG;
$output = $this->header();
$output .= $this->heading(get_string('viewlog', 'tool_mergeusers'));
$output .= html_writer::start_tag('div', array('class' => 'result'));
if (empty($logs)) {
$output .= get_string('nologs', 'tool_mergeusers');
} else {
$output .= html_writer::tag('div', get_string('loglist', 'tool_mergeusers'), array('class' => 'title'));
$flags = array();
$flags[] = $this->pix_icon('i/invalid', get_string('eventusermergedfailure', 'tool_mergeusers')); //failure icon
$flags[] = $this->pix_icon('i/valid', get_string('eventusermergedsuccess', 'tool_mergeusers')); //ok icon
$output .= html_writer::link(new moodle_url('/admin/tool/mergeusers/view.php', ['export' => 1]),
get_string('exportlogs', 'tool_mergeusers'));
$table = new html_table();
$table->align = array('center', 'center', 'center', 'center', 'center', 'center');
$table->head = array(
get_string('olduseridonlog', 'tool_mergeusers'),
get_string('newuseridonlog', 'tool_mergeusers'),
get_string('mergedbyuseridonlog', 'tool_mergeusers'),
get_string('date'),
get_string('status'),
''
);
$rows = array();
foreach ($logs as $i => $log) {
$row = new html_table_row();
$row->cells = array(
($log->from)
? $this->show_user($log->fromuserid, $log->from)
: get_string('deleted', 'tool_mergeusers', $log->fromuserid),
($log->to)
? $this->show_user($log->touserid, $log->to)
: get_string('deleted', 'tool_mergeusers', $log->touserid),
($log->mergedby)
? $this->show_user($log->mergedbyuserid, $log->mergedby)
: get_string('nomergedby', 'tool_mergeusers'),
userdate($log->timemodified, get_string('strftimedaydatetime', 'langconfig')),
$flags[$log->success],
html_writer::link(
new moodle_url('/' . $CFG->admin . '/tool/mergeusers/log.php',
array('id' => $log->id, 'sesskey' => sesskey())),
get_string('more'),
array('target' => '_blank')),
);
$rows[] = $row;
}
$table->data = $rows;
$output .= html_writer::table($table);
}
$output .= html_writer::end_tag('div');
$output .= $this->footer();
return $output;
}
}