on-demand release 4.0dev+
[moodle.git] / admin / tool / uploadcourse / classes / tracker.php
CommitLineData
5b2f7718
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Output tracker.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
e0164eb3 26require_once($CFG->libdir . '/weblib.php');
5b2f7718
FM
27
28/**
29 * Class output tracker.
30 *
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
34 */
35class tool_uploadcourse_tracker {
36
37 /**
38 * Constant to output nothing.
39 */
40 const NO_OUTPUT = 0;
41
42 /**
43 * Constant to output HTML.
44 */
45 const OUTPUT_HTML = 1;
46
47 /**
48 * Constant to output plain text.
49 */
50 const OUTPUT_PLAIN = 2;
51
52 /**
53 * @var array columns to display.
54 */
55 protected $columns = array('line', 'result', 'id', 'shortname', 'fullname', 'idnumber', 'status');
56
57 /**
58 * @var int row number.
59 */
60 protected $rownb = 0;
61
62 /**
63 * @var int chosen output mode.
64 */
65 protected $outputmode;
66
67 /**
68 * @var object output buffer.
69 */
70 protected $buffer;
71
72 /**
73 * Constructor.
74 *
75 * @param int $outputmode desired output mode.
76 */
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());
81 }
82 }
83
84 /**
85 * Finish the output.
86 *
87 * @return void
88 */
89 public function finish() {
90 if ($this->outputmode == self::NO_OUTPUT) {
91 return;
92 }
93
94 if ($this->outputmode == self::OUTPUT_HTML) {
95 echo html_writer::end_tag('table');
96 }
97 }
98
99 /**
100 * Output the results.
101 *
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.
107 * @return void
108 */
109 public function results($total, $created, $updated, $deleted, $errors) {
5b2f7718
FM
110 if ($this->outputmode == self::NO_OUTPUT) {
111 return;
112 }
113
114 $message = array(
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)
120 );
121
122 if ($this->outputmode == self::OUTPUT_PLAIN) {
5dcd7e28
FM
123 foreach ($message as $msg) {
124 $this->buffer->output($msg);
125 }
5b2f7718 126 } else if ($this->outputmode == self::OUTPUT_HTML) {
5dcd7e28
FM
127 $buffer = new progress_trace_buffer(new html_list_progress_trace());
128 foreach ($message as $msg) {
129 $buffer->output($msg);
130 }
131 $buffer->finished();
5b2f7718
FM
132 }
133 }
134
135 /**
136 * Output one more line.
137 *
138 * @param int $line line number.
853b6cff 139 * @param bool $outcome success or not?
5b2f7718
FM
140 * @param array $status array of statuses.
141 * @param array $data extra data to display.
142 * @return void
143 */
144 public function output($line, $outcome, $status, $data) {
145 global $OUTPUT;
146 if ($this->outputmode == self::NO_OUTPUT) {
147 return;
148 }
149
150 if ($this->outputmode == self::OUTPUT_PLAIN) {
151 $message = array(
152 $line,
853b6cff 153 $outcome ? 'OK' : 'NOK',
5b2f7718
FM
154 isset($data['id']) ? $data['id'] : '',
155 isset($data['shortname']) ? $data['shortname'] : '',
156 isset($data['fullname']) ? $data['fullname'] : '',
157 isset($data['idnumber']) ? $data['idnumber'] : ''
158 );
159 $this->buffer->output(implode("\t", $message));
160 if (!empty($status)) {
161 foreach ($status as $st) {
162 $this->buffer->output($st, 1);
163 }
164 }
165 } else if ($this->outputmode == self::OUTPUT_HTML) {
166 $ci = 0;
167 $this->rownb++;
168 if (is_array($status)) {
169 $status = implode(html_writer::empty_tag('br'), $status);
170 }
853b6cff 171 if ($outcome) {
5b2f7718 172 $outcome = $OUTPUT->pix_icon('i/valid', '');
61380ed2 173 } else {
853b6cff 174 $outcome = $OUTPUT->pix_icon('i/invalid', '');
5b2f7718 175 }
1e3e4efd 176 echo html_writer::start_tag('tr', array('class' => 'r' . $this->rownb % 2));
5b2f7718
FM
177 echo html_writer::tag('td', $line, array('class' => 'c' . $ci++));
178 echo html_writer::tag('td', $outcome, array('class' => 'c' . $ci++));
5b2f7718
FM
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++));
1e3e4efd 183 echo html_writer::tag('td', $status, array('class' => 'c' . $ci++));
5b2f7718
FM
184 echo html_writer::end_tag('tr');
185 }
186 }
187
188 /**
189 * Start the output.
190 *
191 * @return void
192 */
193 public function start() {
194 if ($this->outputmode == self::NO_OUTPUT) {
195 return;
196 }
197
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) {
204 $ci = 0;
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));
3f57c87e
FM
208 echo html_writer::tag('th', get_string('csvline', 'tool_uploadcourse'),
209 array('class' => 'c' . $ci++, 'scope' => 'col'));
1e3e4efd 210 echo html_writer::tag('th', get_string('result', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
5b2f7718
FM
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'));
1e3e4efd 215 echo html_writer::tag('th', get_string('status'), array('class' => 'c' . $ci++, 'scope' => 'col'));
5b2f7718
FM
216 echo html_writer::end_tag('tr');
217 }
218 }
219
220}