MDL-13114 tool_uploadcourse: ID number errors handling
[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();
26
27/**
28 * Class output tracker.
29 *
30 * @package tool_uploadcourse
31 * @copyright 2013 Frédéric Massart
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_uploadcourse_tracker {
35
36 /**
37 * Constant to output nothing.
38 */
39 const NO_OUTPUT = 0;
40
41 /**
42 * Constant to output HTML.
43 */
44 const OUTPUT_HTML = 1;
45
46 /**
47 * Constant to output plain text.
48 */
49 const OUTPUT_PLAIN = 2;
50
51 /**
52 * @var array columns to display.
53 */
54 protected $columns = array('line', 'result', 'id', 'shortname', 'fullname', 'idnumber', 'status');
55
56 /**
57 * @var int row number.
58 */
59 protected $rownb = 0;
60
61 /**
62 * @var int chosen output mode.
63 */
64 protected $outputmode;
65
66 /**
67 * @var object output buffer.
68 */
69 protected $buffer;
70
71 /**
72 * Constructor.
73 *
74 * @param int $outputmode desired output mode.
75 */
76 public function __construct($outputmode = self::NO_OUTPUT) {
77 $this->outputmode = $outputmode;
78 if ($this->outputmode == self::OUTPUT_PLAIN) {
79 $this->buffer = new progress_trace_buffer(new text_progress_trace());
80 }
81 }
82
83 /**
84 * Finish the output.
85 *
86 * @return void
87 */
88 public function finish() {
89 if ($this->outputmode == self::NO_OUTPUT) {
90 return;
91 }
92
93 if ($this->outputmode == self::OUTPUT_HTML) {
94 echo html_writer::end_tag('table');
95 }
96 }
97
98 /**
99 * Output the results.
100 *
101 * @param int $total total courses.
102 * @param int $created count of courses created.
103 * @param int $updated count of courses updated.
104 * @param int $deleted count of courses deleted.
105 * @param int $errors count of errors.
106 * @return void
107 */
108 public function results($total, $created, $updated, $deleted, $errors) {
109 global $OUTPUT;
110
111 if ($this->outputmode == self::NO_OUTPUT) {
112 return;
113 }
114
115 $message = array(
116 get_string('coursestotal', 'tool_uploadcourse', $total),
117 get_string('coursescreated', 'tool_uploadcourse', $created),
118 get_string('coursesupdated', 'tool_uploadcourse', $updated),
119 get_string('coursesdeleted', 'tool_uploadcourse', $deleted),
120 get_string('courseserrors', 'tool_uploadcourse', $errors)
121 );
122
123 if ($this->outputmode == self::OUTPUT_PLAIN) {
124 } else if ($this->outputmode == self::OUTPUT_HTML) {
125 echo html_writer::end_tag('table');
126 }
127 }
128
129 /**
130 * Output one more line.
131 *
132 * @param int $line line number.
133 * @param bool $outcome success or not?
134 * @param array $status array of statuses.
135 * @param array $data extra data to display.
136 * @return void
137 */
138 public function output($line, $outcome, $status, $data) {
139 global $OUTPUT;
140 if ($this->outputmode == self::NO_OUTPUT) {
141 return;
142 }
143
144 if ($this->outputmode == self::OUTPUT_PLAIN) {
145 $message = array(
146 $line,
147 $outcome ? 'OK' : 'NOK',
148 isset($data['id']) ? $data['id'] : '',
149 isset($data['shortname']) ? $data['shortname'] : '',
150 isset($data['fullname']) ? $data['fullname'] : '',
151 isset($data['idnumber']) ? $data['idnumber'] : ''
152 );
153 $this->buffer->output(implode("\t", $message));
154 if (!empty($status)) {
155 foreach ($status as $st) {
156 $this->buffer->output($st, 1);
157 }
158 }
159 } else if ($this->outputmode == self::OUTPUT_HTML) {
160 $ci = 0;
161 $this->rownb++;
162 if (is_array($status)) {
163 $status = implode(html_writer::empty_tag('br'), $status);
164 }
165 if ($outcome) {
166 $outcome = $OUTPUT->pix_icon('i/valid', '');
167 } else {
168 $outcome = $OUTPUT->pix_icon('i/invalid', '');
169 }
170 echo html_writer::start_tag('tr', array('class' => 'r' . $this->rownb));
171 echo html_writer::tag('td', $line, array('class' => 'c' . $ci++));
172 echo html_writer::tag('td', $outcome, array('class' => 'c' . $ci++));
173 echo html_writer::tag('td', $status, array('class' => 'c' . $ci++));
174 echo html_writer::tag('td', isset($data['id']) ? $data['id'] : '', array('class' => 'c' . $ci++));
175 echo html_writer::tag('td', isset($data['shortname']) ? $data['shortname'] : '', array('class' => 'c' . $ci++));
176 echo html_writer::tag('td', isset($data['fullname']) ? $data['fullname'] : '', array('class' => 'c' . $ci++));
177 echo html_writer::tag('td', isset($data['idnumber']) ? $data['idnumber'] : '', array('class' => 'c' . $ci++));
178 echo html_writer::end_tag('tr');
179 }
180 }
181
182 /**
183 * Start the output.
184 *
185 * @return void
186 */
187 public function start() {
188 if ($this->outputmode == self::NO_OUTPUT) {
189 return;
190 }
191
192 if ($this->outputmode == self::OUTPUT_PLAIN) {
193 $columns = array_flip($this->columns);
194 unset($columns['status']);
195 $columns = array_flip($columns);
196 $this->buffer->output(implode("\t", $columns));
197 } else if ($this->outputmode == self::OUTPUT_HTML) {
198 $ci = 0;
199 echo html_writer::start_tag('table', array('class' => 'generaltable boxaligncenter flexible-wrap',
200 'summary' => get_string('uploadcoursesresult', 'tool_uploadcourse')));
201 echo html_writer::start_tag('tr', array('class' => 'heading r' . $this->rownb));
202 echo html_writer::tag('th', get_string('csvline', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
203 echo html_writer::tag('th', get_string('outcome', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
204 echo html_writer::tag('th', get_string('status'), array('class' => 'c' . $ci++, 'scope' => 'col'));
205 echo html_writer::tag('th', get_string('id', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
206 echo html_writer::tag('th', get_string('shortname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
207 echo html_writer::tag('th', get_string('fullname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
208 echo html_writer::tag('th', get_string('idnumber'), array('class' => 'c' . $ci++, 'scope' => 'col'));
209 echo html_writer::end_tag('tr');
210 }
211 }
212
213}