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/>.
18 * Import attendance sessions class.
20 * @package mod_attendance
21 * @author Chris Wharton <chriswharton@catalyst.net.nz>
22 * @copyright 2017 Catalyst IT
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace mod_attendance\import
;
28 defined('MOODLE_INTERNAL') ||
die();
30 use csv_import_reader
;
31 use mod_attendance_notifyqueue
;
32 use mod_attendance_structure
;
36 * Import attendance sessions.
38 * @package mod_attendance
39 * @author Chris Wharton <chriswharton@catalyst.net.nz>
40 * @copyright 2017 Catalyst IT
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 /** @var string $error The errors message from reading the xml */
46 protected $error = '';
48 /** @var array $sessions The sessions info */
49 protected $sessions = array();
51 /** @var array $mappings The mappings info */
52 protected $mappings = array();
54 /** @var int The id of the csv import */
55 protected $importid = 0;
57 /** @var csv_import_reader|null $importer */
58 protected $importer = null
;
60 /** @var array $foundheaders */
61 protected $foundheaders = array();
63 /** @var bool $useprogressbar Control whether importing should use progress bars or not. */
64 protected $useprogressbar = false
;
66 /** @var \core\progress\display_if_slow|null $progress The progress bar instance. */
67 protected $progress = null
;
70 * Store an error message for display later
74 public function fail($msg) {
80 * Get the CSV import id
82 * @return string The import id.
84 public function get_importid() {
85 return $this->importid
;
89 * Get the list of headers required for import.
91 * @return array The headers (lang strings)
93 public static function list_required_headers() {
95 get_string('course', 'attendance'),
96 get_string('groups', 'attendance'),
97 get_string('sessiondate', 'attendance'),
98 get_string('from', 'attendance'),
99 get_string('to', 'attendance'),
100 get_string('description', 'attendance'),
101 get_string('repeaton', 'attendance'),
102 get_string('repeatevery', 'attendance'),
103 get_string('repeatuntil', 'attendance'),
104 get_string('studentscanmark', 'attendance'),
105 get_string('passwordgrp', 'attendance'),
106 get_string('randompassword', 'attendance'),
107 get_string('subnet', 'attendance'),
108 get_string('automark', 'attendance'),
109 get_string('autoassignstatus', 'attendance'),
110 get_string('absenteereport', 'attendance'),
111 get_string('preventsharedip', 'attendance'),
112 get_string('preventsharediptime', 'attendance')
117 * Get the list of headers found in the import.
119 * @return array The found headers (names from import)
121 public function list_found_headers() {
122 return $this->foundheaders
;
126 * Read the data from the mapping form.
128 * @param array $data The mapping data.
130 protected function read_mapping_data($data) {
133 'course' => $data->header0
,
134 'groups' => $data->header1
,
135 'sessiondate' => $data->header2
,
136 'from' => $data->header3
,
137 'to' => $data->header4
,
138 'description' => $data->header5
,
139 'repeaton' => $data->header6
,
140 'repeatevery' => $data->header7
,
141 'repeatuntil' => $data->header8
,
142 'studentscanmark' => $data->header9
,
143 'passwordgrp' => $data->header10
,
144 'randompassword' => $data->header11
,
145 'subnet' => $data->header12
,
146 'automark' => $data->header13
,
147 'autoassignstatus' => $data->header14
,
148 'absenteereport' => $data->header15
,
149 'preventsharedip' => $data->header16
,
150 'preventsharediptime' => $data->header17
,
163 'studentscanmark' => 9,
165 'randompassword' => 11,
168 'autoassignstatus' => 14,
169 'absenteereport' => 15,
170 'preventsharedip' => 16,
171 'preventsharediptime' => 17
177 * Get the a column from the imported data.
179 * @param array $row The imported raw row
180 * @param int $index The column index we want
181 * @return string The column data.
183 protected function get_column_data($row, $index) {
187 return isset($row[$index]) ?
$row[$index] : '';
191 * Constructor - parses the raw text for sanity.
193 * @param string $text The raw csv text.
194 * @param string $encoding The encoding of the csv file.
195 * @param string $delimiter The specified delimiter for the file.
196 * @param string $importid The id of the csv import.
197 * @param array $mappingdata The mapping data from the import form.
198 * @param bool $useprogressbar Whether progress bar should be displayed, to avoid html output on CLI.
200 public function __construct($text = null
, $encoding = null
, $delimiter = null
, $importid = 0,
201 $mappingdata = null
, $useprogressbar = false
) {
204 require_once($CFG->libdir
. '/csvlib.class.php');
206 $pluginconfig = get_config('attendance');
211 if ($text === null
) {
214 $this->importid
= csv_import_reader
::get_new_iid($type);
216 $this->importer
= new csv_import_reader($this->importid
, $type);
218 if (! $this->importer
->load_csv_content($text, $encoding, $delimiter)) {
219 $this->fail(get_string('invalidimportfile', 'attendance'));
220 $this->importer
->cleanup();
224 $this->importid
= $importid;
226 $this->importer
= new csv_import_reader($this->importid
, $type);
229 if (! $this->importer
->init()) {
230 $this->fail(get_string('invalidimportfile', 'attendance'));
231 $this->importer
->cleanup();
235 $this->foundheaders
= $this->importer
->get_columns();
236 $this->useprogressbar
= $useprogressbar;
241 while ($row = $this->importer
->next()) {
242 // This structure mimics what the UI form returns.
243 $mapping = $this->read_mapping_data($mappingdata);
245 $session = new stdClass();
246 $session->course
= $this->get_column_data($row, $mapping['course']);
247 if (empty($session->course
)) {
248 \mod_attendance_notifyqueue
::notify_problem(get_string('error:sessioncourseinvalid', 'attendance'));
252 // Handle multiple group assignments per session. Expect semicolon separated group names.
253 $groups = $this->get_column_data($row, $mapping['groups']);
254 if (! empty($groups)) {
255 $session->groups
= explode(';', $groups);
256 $session->sessiontype
= \mod_attendance_structure
::SESSION_GROUP
;
258 $session->sessiontype
= \mod_attendance_structure
::SESSION_COMMON
;
261 // Expect standardised date format, eg YYYY-MM-DD.
262 $sessiondate = strtotime($this->get_column_data($row, $mapping['sessiondate']));
263 if ($sessiondate === false
) {
264 \mod_attendance_notifyqueue
::notify_problem(get_string('error:sessiondateinvalid', 'attendance'));
267 $session->sessiondate
= $sessiondate;
269 // Expect standardised time format, eg HH:MM.
270 $from = $this->get_column_data($row, $mapping['from']);
272 \mod_attendance_notifyqueue
::notify_problem(get_string('error:sessionstartinvalid', 'attendance'));
275 $from = explode(':', $from);
276 $session->sestime
['starthour'] = $from[0];
277 $session->sestime
['startminute'] = $from[1];
279 $to = $this->get_column_data($row, $mapping['to']);
281 \mod_attendance_notifyqueue
::notify_problem(get_string('error:sessionendinvalid', 'attendance'));
284 $to = explode(':', $to);
285 $session->sestime
['endhour'] = $to[0];
286 $session->sestime
['endminute'] = $to[1];
288 // Wrap the plain text description in html tags.
289 $session->sdescription
['text'] = '<p>' . $this->get_column_data($row, $mapping['description']) . '</p>';
290 $session->sdescription
['format'] = FORMAT_HTML
;
291 $session->sdescription
['itemid'] = 0;
292 $session->passwordgrp
= $this->get_column_data($row, $mapping['passwordgrp']);
293 $session->subnet
= $this->get_column_data($row, $mapping['subnet']);
294 // Set session subnet restriction. Use the default activity level subnet if there isn't one set for this session.
295 if (empty($session->subnet
)) {
296 $session->usedefaultsubnet
= '1';
298 $session->usedefaultsubnet
= '';
301 if ($mapping['studentscanmark'] == -1) {
302 $session->studentscanmark
= $pluginconfig->studentscanmark_default
;
304 $session->studentscanmark
= $this->get_column_data($row, $mapping['studentscanmark']);
306 if ($mapping['randompassword'] == -1) {
307 $session->randompassword
= $pluginconfig->randompassword_default
;
309 $session->randompassword
= $this->get_column_data($row, $mapping['randompassword']);
311 if ($mapping['automark'] == -1) {
312 $session->automark
= $pluginconfig->automark_default
;
314 $session->automark
= $this->get_column_data($row, $mapping['automark']);
316 if ($mapping['autoassignstatus'] == -1) {
317 $session->autoassignstatus
= $pluginconfig->autoassignstatus
;
319 $session->autoassignstatus
= $this->get_column_data($row, $mapping['autoassignstatus']);
321 if ($mapping['absenteereport'] == -1) {
322 $session->absenteereport
= $pluginconfig->absenteereport_default
;
324 $session->absenteereport
= $this->get_column_data($row, $mapping['absenteereport']);
326 if ($mapping['preventsharedip'] == -1) {
327 $session->preventsharedip
= $pluginconfig->preventsharedip
;
329 $session->preventsharedip
= $this->get_column_data($row, $mapping['preventsharedip']);
331 if ($mapping['preventsharediptime'] == -1) {
332 $session->preventsharediptime
= $pluginconfig->preventsharediptime
;
334 $session->preventsharediptime
= $this->get_column_data($row, $mapping['preventsharediptime']);
337 $session->statusset
= 0;
339 $sessions[] = $session;
341 $this->sessions
= $sessions;
343 $this->importer
->close();
344 if ($this->sessions
== null
) {
345 $this->fail(get_string('invalidimportfile', 'attendance'));
348 // We are calling from browser, display progress bar.
349 if ($this->useprogressbar
=== true
) {
350 $this->progress
= new \core\progress\
display_if_slow(get_string('processingfile', 'attendance'));
351 $this->progress
->start_html();
353 // Avoid html output on CLI scripts.
354 $this->progress
= new \core\progress
\none
();
356 $this->progress
->start_progress('', count($this->sessions
));
357 raise_memory_limit(MEMORY_EXTRA
);
358 $this->progress
->end_progress();
365 * @return array of errors from parsing the xml.
367 public function get_error() {
372 * Create sessions using the CSV data.
376 public function import() {
379 // Count of sessions added.
382 foreach ($this->sessions
as $session) {
384 // Check course shortname matches.
385 if ($DB->record_exists('course', array(
386 'shortname' => $session->course
389 $course = $DB->get_record('course', array(
390 'shortname' => $session->course
393 // Check course has activities.
394 if ($DB->record_exists('attendance', array(
395 'course' => $course->id
397 // Translate group names to group IDs. They are unique per course.
398 if ($session->sessiontype
=== \mod_attendance_structure
::SESSION_GROUP
) {
399 foreach ($session->groups
as $groupname) {
400 $gid = groups_get_group_by_name($course->id
, $groupname);
401 if ($gid === false
) {
402 \mod_attendance_notifyqueue
::notify_problem(get_string('sessionunknowngroup',
403 'attendance', $groupname));
408 $session->groups
= $groupids;
411 // Get activities in course.
412 $activities = $DB->get_recordset('attendance', array(
413 'course' => $course->id
416 foreach ($activities as $activity) {
417 // Build the session data.
418 $cm = get_coursemodule_from_instance('attendance', $activity->id
, $course->id
);
419 if (!empty($cm->deletioninprogress
)) {
420 // Don't do anything if this attendance is in recycle bin.
423 $att = new mod_attendance_structure($activity, $cm, $course);
424 $sessions = attendance_construct_sessions_data_for_add($session, $att);
426 foreach ($sessions as $index => $sess) {
427 // Check for duplicate sessions.
428 if ($this->session_exists($sess)) {
429 mod_attendance_notifyqueue
::notify_message(get_string('sessionduplicate', 'attendance', (array(
430 'course' => $session->course
,
431 'activity' => $cm->name
433 unset($sessions[$index]);
438 if (! empty($sessions)) {
439 $att->add_sessions($sessions);
442 $activities->close();
444 mod_attendance_notifyqueue
::notify_problem(get_string('error:coursehasnoattendance',
445 'attendance', $session->course
));
448 mod_attendance_notifyqueue
::notify_problem(get_string('error:coursenotfound', 'attendance', $session->course
));
452 $message = get_string('sessionsgenerated', 'attendance', $okcount);
454 mod_attendance_notifyqueue
::notify_message($message);
456 mod_attendance_notifyqueue
::notify_success($message);
459 // Trigger a sessions imported event.
460 $event = \mod_attendance\event\sessions_imported
::create(array(
462 'context' => \context_system
::instance(),
472 * Check if an identical session exists.
474 * @param stdClass $session
477 private function session_exists(stdClass
$session) {
480 $check = clone $session;
482 // Remove the properties that aren't useful to check.
483 unset($check->description
);
484 unset($check->descriptionitemid
);
485 unset($check->timemodified
);
486 $check = (array) $check;
488 if ($DB->record_exists('attendance_sessions', $check)) {