2
// This file is part of Moodle - http://moodle.org/
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20
* @package tool_uploadcourse
21
* @copyright 2013 Frédéric Massart
22
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
defined('MOODLE_INTERNAL') || die();
26
require_once($CFG->libdir . '/weblib.php');
29
* Class output tracker.
31
* @package tool_uploadcourse
32
* @copyright 2013 Frédéric Massart
33
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
class tool_uploadcourse_tracker {
38
* Constant to output nothing.
43
* Constant to output HTML.
45
const OUTPUT_HTML = 1;
48
* Constant to output plain text.
50
const OUTPUT_PLAIN = 2;
53
* @var array columns to display.
55
protected $columns = array('line', 'result', 'id', 'shortname', 'fullname', 'idnumber', 'status');
58
* @var int row number.
63
* @var int chosen output mode.
65
protected $outputmode;
68
* @var object output buffer.
75
* @param int $outputmode desired output mode.
77
public function __construct($outputmode = self::NO_OUTPUT) {
78
$this->outputmode = $outputmode;
79
if ($this->outputmode == self::OUTPUT_PLAIN) {
80
$this->buffer = new progress_trace_buffer(new text_progress_trace());
89
public function finish() {
90
if ($this->outputmode == self::NO_OUTPUT) {
94
if ($this->outputmode == self::OUTPUT_HTML) {
95
echo html_writer::end_tag('table');
100
* Output the results.
102
* @param int $total total courses.
103
* @param int $created count of courses created.
104
* @param int $updated count of courses updated.
105
* @param int $deleted count of courses deleted.
106
* @param int $errors count of errors.
109
public function results($total, $created, $updated, $deleted, $errors) {
110
if ($this->outputmode == self::NO_OUTPUT) {
115
get_string('coursestotal', 'tool_uploadcourse', $total),
116
get_string('coursescreated', 'tool_uploadcourse', $created),
117
get_string('coursesupdated', 'tool_uploadcourse', $updated),
118
get_string('coursesdeleted', 'tool_uploadcourse', $deleted),
119
get_string('courseserrors', 'tool_uploadcourse', $errors)
122
if ($this->outputmode == self::OUTPUT_PLAIN) {
123
foreach ($message as $msg) {
124
$this->buffer->output($msg);
126
} else if ($this->outputmode == self::OUTPUT_HTML) {
127
$buffer = new progress_trace_buffer(new html_list_progress_trace());
128
foreach ($message as $msg) {
129
$buffer->output($msg);
136
* Output one more line.
138
* @param int $line line number.
139
* @param bool $outcome success or not?
140
* @param array $status array of statuses.
141
* @param array $data extra data to display.
144
public function output($line, $outcome, $status, $data) {
146
if ($this->outputmode == self::NO_OUTPUT) {
150
if ($this->outputmode == self::OUTPUT_PLAIN) {
153
$outcome ? 'OK' : 'NOK',
154
isset($data['id']) ? $data['id'] : '',
155
isset($data['shortname']) ? $data['shortname'] : '',
156
isset($data['fullname']) ? $data['fullname'] : '',
157
isset($data['idnumber']) ? $data['idnumber'] : ''
159
$this->buffer->output(implode("\t", $message));
160
if (!empty($status)) {
161
foreach ($status as $st) {
162
$this->buffer->output($st, 1);
165
} else if ($this->outputmode == self::OUTPUT_HTML) {
168
if (is_array($status)) {
169
$status = implode(html_writer::empty_tag('br'), $status);
172
$outcome = $OUTPUT->pix_icon('i/valid', '');
174
$outcome = $OUTPUT->pix_icon('i/invalid', '');
176
echo html_writer::start_tag('tr', array('class' => 'r' . $this->rownb % 2));
177
echo html_writer::tag('td', $line, array('class' => 'c' . $ci++));
178
echo html_writer::tag('td', $outcome, array('class' => 'c' . $ci++));
179
echo html_writer::tag('td', isset($data['id']) ? $data['id'] : '', array('class' => 'c' . $ci++));
180
echo html_writer::tag('td', isset($data['shortname']) ? $data['shortname'] : '', array('class' => 'c' . $ci++));
181
echo html_writer::tag('td', isset($data['fullname']) ? $data['fullname'] : '', array('class' => 'c' . $ci++));
182
echo html_writer::tag('td', isset($data['idnumber']) ? $data['idnumber'] : '', array('class' => 'c' . $ci++));
183
echo html_writer::tag('td', $status, array('class' => 'c' . $ci++));
184
echo html_writer::end_tag('tr');
193
public function start() {
194
if ($this->outputmode == self::NO_OUTPUT) {
198
if ($this->outputmode == self::OUTPUT_PLAIN) {
199
$columns = array_flip($this->columns);
200
unset($columns['status']);
201
$columns = array_flip($columns);
202
$this->buffer->output(implode("\t", $columns));
203
} else if ($this->outputmode == self::OUTPUT_HTML) {
205
echo html_writer::start_tag('table', array('class' => 'generaltable boxaligncenter flexible-wrap',
206
'summary' => get_string('uploadcoursesresult', 'tool_uploadcourse')));
207
echo html_writer::start_tag('tr', array('class' => 'heading r' . $this->rownb));
208
echo html_writer::tag('th', get_string('csvline', 'tool_uploadcourse'),
209
array('class' => 'c' . $ci++, 'scope' => 'col'));
210
echo html_writer::tag('th', get_string('result', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
211
echo html_writer::tag('th', get_string('id', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
212
echo html_writer::tag('th', get_string('shortname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
213
echo html_writer::tag('th', get_string('fullname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
214
echo html_writer::tag('th', get_string('idnumber'), array('class' => 'c' . $ci++, 'scope' => 'col'));
215
echo html_writer::tag('th', get_string('status'), array('class' => 'c' . $ci++, 'scope' => 'col'));
216
echo html_writer::end_tag('tr');