Dan Marsden
4 years ago
committed by
GitHub
8 changed files with 791 additions and 91 deletions
@ -0,0 +1,97 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* This file contains the form used to upload a csv attendance file to automatically update attendance records. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2019 Jonathan Chan <jonathan.chan@sta.uwi.edu> |
||||
|
* @copyright based on work by 2012 NetSpot {@link http://www.netspot.com.au} |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ |
||||
|
namespace mod_attendance\form\import; |
||||
|
|
||||
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); |
||||
|
|
||||
|
use core_text; |
||||
|
use moodleform; |
||||
|
require_once($CFG->libdir.'/formslib.php'); |
||||
|
|
||||
|
/** |
||||
|
* Class for displaying the csv upload form. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2019 Jonathan Chan <jonathan.chan@sta.uwi.edu> |
||||
|
* @copyright based on work by 2012 NetSpot {@link http://www.netspot.com.au} |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class marksessions extends moodleform { |
||||
|
|
||||
|
/** |
||||
|
* Called to define this moodle form |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function definition() { |
||||
|
global $COURSE; |
||||
|
|
||||
|
$mform = $this->_form; |
||||
|
$params = $this->_customdata; |
||||
|
|
||||
|
$mform->addElement('header', 'uploadattendance', get_string('uploadattendance', 'attendance')); |
||||
|
|
||||
|
$fileoptions = array('subdirs' => 0, |
||||
|
'maxbytes' => $COURSE->maxbytes, |
||||
|
'accepted_types' => 'csv', |
||||
|
'maxfiles' => 1); |
||||
|
|
||||
|
$mform->addElement('filepicker', 'attendancefile', get_string('uploadafile'), null, $fileoptions); |
||||
|
$mform->addRule('attendancefile', get_string('uploadnofilefound'), 'required', null, 'client'); |
||||
|
$mform->addHelpButton('attendancefile', 'attendancefile', 'attendance'); |
||||
|
|
||||
|
$encodings = core_text::get_encodings(); |
||||
|
$mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings); |
||||
|
$mform->addHelpButton('encoding', 'encoding', 'grades'); |
||||
|
|
||||
|
$radio = array(); |
||||
|
$radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab'); |
||||
|
$radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma'); |
||||
|
$radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon'); |
||||
|
$radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon'); |
||||
|
$mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false); |
||||
|
$mform->addHelpButton('separator', 'separator', 'grades'); |
||||
|
$mform->setDefault('separator', 'comma'); |
||||
|
|
||||
|
$mform->addElement('hidden', 'id', $params['id']); |
||||
|
$mform->setType('id', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'sessionid', $params['sessionid']); |
||||
|
$mform->setType('sessionid', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'grouptype', $params['grouptype']); |
||||
|
$mform->setType('grouptype', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'confirm', 0); |
||||
|
$mform->setType('confirm', PARAM_BOOL); |
||||
|
$this->add_action_buttons(true, get_string('uploadattendance', 'attendance')); |
||||
|
} |
||||
|
/** |
||||
|
* Display an error on the import form. |
||||
|
* |
||||
|
* @param string $msg |
||||
|
*/ |
||||
|
public function set_import_error($msg) { |
||||
|
$mform = $this->_form; |
||||
|
|
||||
|
$mform->setElementError('attendancefile', $msg); |
||||
|
} |
||||
|
} |
@ -0,0 +1,132 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* This file contains the form used to upload a csv attendance file to automatically update attendance records. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2020 Catalyst IT |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ |
||||
|
namespace mod_attendance\form\import; |
||||
|
|
||||
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); |
||||
|
|
||||
|
use core_text; |
||||
|
use moodleform; |
||||
|
require_once($CFG->libdir.'/formslib.php'); |
||||
|
|
||||
|
/** |
||||
|
* Mark attendance sessions confirm csv upload. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2020 Catalyst IT |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class marksessions_confirm extends moodleform { |
||||
|
|
||||
|
/** |
||||
|
* Called to define this moodle form |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function definition() { |
||||
|
$params = $this->_customdata; |
||||
|
$importer = $this->_customdata['importer']; |
||||
|
|
||||
|
$mform = $this->_form; |
||||
|
$mform->addElement('hidden', 'confirm', 1); |
||||
|
$mform->setType('confirm', PARAM_BOOL); |
||||
|
|
||||
|
$foundheaders = $importer->list_found_headers(); |
||||
|
|
||||
|
// Add user mapping. |
||||
|
$mform->addElement('select', 'userfrom', get_string('userimportfield', 'attendance'), $foundheaders); |
||||
|
$mform->addHelpButton('userfrom', 'userimportfield', 'attendance'); |
||||
|
// This allows the user to choose which field in the user database the identifying column will map to. |
||||
|
$useroptions = array( |
||||
|
'userid' => get_string('userid', 'attendance'), |
||||
|
'username' => get_string('username'), |
||||
|
'idnumber' => get_string('idnumber'), |
||||
|
'email' => get_string('email') |
||||
|
); |
||||
|
$mform->addElement('select', 'userto', get_string('userimportto', 'attendance'), $useroptions); |
||||
|
|
||||
|
// Check if we can set an easy default value. |
||||
|
foreach (array_keys($useroptions) as $o) { |
||||
|
if (in_array($o, $foundheaders)) { |
||||
|
$mform->setDefault('userto', $o); |
||||
|
$mform->setDefault('userfrom', $o); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
$mform->addHelpButton('userto', 'userimportto', 'attendance'); |
||||
|
|
||||
|
// Below options need a "none" option in the headers. |
||||
|
$foundheaders[- 1] = get_string('notset', 'mod_attendance'); |
||||
|
ksort($foundheaders); |
||||
|
|
||||
|
// Add scan time mapping. |
||||
|
$mform->addElement('select', 'scantime', get_string('scantime', 'attendance'), $foundheaders); |
||||
|
$mform->addHelpButton('scantime', 'scantime', 'attendance'); |
||||
|
$mform->setDefault('scantime', -1); |
||||
|
|
||||
|
// Add status mapping. |
||||
|
$mform->addElement('select', 'status', get_string('importstatus', 'attendance'), $foundheaders); |
||||
|
$mform->addHelpButton('status', 'importstatus', 'attendance'); |
||||
|
$mform->disabledif('status', 'scantime', 'noteq', -1); |
||||
|
$mform->disabledif('scantime', 'status', 'noteq', -1); |
||||
|
|
||||
|
// Try to set a useful default value for scantime or status. |
||||
|
$key = array_search('status', $foundheaders); |
||||
|
|
||||
|
if ($key !== false) { |
||||
|
// Status is passed in CSV - set that as default. |
||||
|
$mform->setDefault('status', $key); |
||||
|
$mform->setDefault('scantime', -1); |
||||
|
} else { |
||||
|
$keyscan = array_search('scantime', $foundheaders); |
||||
|
if ($keyscan !== false) { |
||||
|
// The Scantime var exists in the csv. |
||||
|
$mform->setDefault('status', -1); |
||||
|
$mform->setDefault('scantime', $keyscan); |
||||
|
} else { |
||||
|
$mform->setDefault('status', -1); |
||||
|
$mform->setDefault('scantime', -1); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
foreach (array_keys($useroptions) as $o) { |
||||
|
if (in_array($o, $foundheaders)) { |
||||
|
$mform->setDefault('userto', $o); |
||||
|
$mform->setDefault('userfrom', $o); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
$mform->addElement('hidden', 'id', $params['id']); |
||||
|
$mform->setType('id', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'sessionid', $params['sessionid']); |
||||
|
$mform->setType('sessionid', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'grouptype', $params['grouptype']); |
||||
|
$mform->setType('grouptype', PARAM_INT); |
||||
|
$mform->addElement('hidden', 'importid', $importer->get_importid()); |
||||
|
$mform->setType('importid', PARAM_INT); |
||||
|
$mform->setConstant('importid', $importer->get_importid()); |
||||
|
|
||||
|
$this->add_action_buttons(true, get_string('uploadattendance', 'attendance')); |
||||
|
} |
||||
|
} |
@ -0,0 +1,302 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Import attendance sessions class. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2020 Catalyst IT |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
namespace mod_attendance\import; |
||||
|
|
||||
|
defined('MOODLE_INTERNAL') || die(); |
||||
|
|
||||
|
use csv_import_reader; |
||||
|
use mod_attendance_notifyqueue; |
||||
|
use mod_attendance_structure; |
||||
|
use stdClass; |
||||
|
|
||||
|
/** |
||||
|
* Import attendance sessions. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2020 Catalyst IT |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class marksessions { |
||||
|
|
||||
|
/** @var string $error The errors message from reading the xml */ |
||||
|
protected $error = ''; |
||||
|
|
||||
|
/** @var array $sessions The sessions info */ |
||||
|
protected $sessions = array(); |
||||
|
|
||||
|
/** @var array $mappings The mappings info */ |
||||
|
protected $mappings = array(); |
||||
|
|
||||
|
/** @var int The id of the csv import */ |
||||
|
protected $importid = 0; |
||||
|
|
||||
|
/** @var csv_import_reader|null $importer */ |
||||
|
protected $importer = null; |
||||
|
|
||||
|
/** @var array $foundheaders */ |
||||
|
protected $foundheaders = array(); |
||||
|
|
||||
|
/** @var bool $useprogressbar Control whether importing should use progress bars or not. */ |
||||
|
protected $useprogressbar = false; |
||||
|
|
||||
|
/** @var \core\progress\display_if_slow|null $progress The progress bar instance. */ |
||||
|
protected $progress = null; |
||||
|
|
||||
|
/** @var mod_attendance_structure $att - the mod_attendance_structure class */ |
||||
|
private $att; |
||||
|
|
||||
|
/** |
||||
|
* Store an error message for display later |
||||
|
* |
||||
|
* @param string $msg |
||||
|
*/ |
||||
|
public function fail($msg) { |
||||
|
$this->error = $msg; |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the CSV import id |
||||
|
* |
||||
|
* @return string The import id. |
||||
|
*/ |
||||
|
public function get_importid() { |
||||
|
return $this->importid; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the list of headers found in the import. |
||||
|
* |
||||
|
* @return array The found headers (names from import) |
||||
|
*/ |
||||
|
public function list_found_headers() { |
||||
|
return $this->foundheaders; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Read the data from the mapping form. |
||||
|
* |
||||
|
* @param array $data The mapping data. |
||||
|
*/ |
||||
|
protected function read_mapping_data($data) { |
||||
|
if ($data) { |
||||
|
return array( |
||||
|
'user' => $data->userfrom, |
||||
|
'scantime' => $data->scantime, |
||||
|
'status' => $data->status |
||||
|
); |
||||
|
} else { |
||||
|
return array( |
||||
|
'user' => 0, |
||||
|
'scantime' => 1, |
||||
|
'status' => 2 |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the a column from the imported data. |
||||
|
* |
||||
|
* @param array $row The imported raw row |
||||
|
* @param int $index The column index we want |
||||
|
* @return string The column data. |
||||
|
*/ |
||||
|
protected function get_column_data($row, $index) { |
||||
|
if ($index < 0) { |
||||
|
return ''; |
||||
|
} |
||||
|
return isset($row[$index]) ? $row[$index] : ''; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Constructor - parses the raw text for sanity. |
||||
|
* |
||||
|
* @param string $text The raw csv text. |
||||
|
* @param mod_attendance_structure $att The current assignment |
||||
|
* @param string $encoding The encoding of the csv file. |
||||
|
* @param string $delimiter The specified delimiter for the file. |
||||
|
* @param string $importid The id of the csv import. |
||||
|
* @param array $mappingdata The mapping data from the import form. |
||||
|
* @param bool $useprogressbar Whether progress bar should be displayed, to avoid html output on CLI. |
||||
|
*/ |
||||
|
public function __construct($text = null, $att, $encoding = null, $delimiter = null, $importid = 0, |
||||
|
$mappingdata = null, $useprogressbar = false) { |
||||
|
global $CFG, $USER; |
||||
|
|
||||
|
require_once($CFG->libdir . '/csvlib.class.php'); |
||||
|
|
||||
|
$type = 'marksessions'; |
||||
|
|
||||
|
$this->att = $att; |
||||
|
|
||||
|
if (! $importid) { |
||||
|
if ($text === null) { |
||||
|
return; |
||||
|
} |
||||
|
$this->importid = csv_import_reader::get_new_iid($type); |
||||
|
|
||||
|
$this->importer = new csv_import_reader($this->importid, $type); |
||||
|
|
||||
|
if (! $this->importer->load_csv_content($text, $encoding, $delimiter)) { |
||||
|
$this->fail(get_string('invalidimportfile', 'attendance')); |
||||
|
$this->importer->cleanup(); |
||||
|
echo $text; |
||||
|
return; |
||||
|
} |
||||
|
} else { |
||||
|
$this->importid = $importid; |
||||
|
|
||||
|
$this->importer = new csv_import_reader($this->importid, $type); |
||||
|
} |
||||
|
|
||||
|
if (! $this->importer->init()) { |
||||
|
$this->fail(get_string('invalidimportfile', 'attendance')); |
||||
|
$this->importer->cleanup(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
$this->foundheaders = $this->importer->get_columns(); |
||||
|
|
||||
|
$this->useprogressbar = $useprogressbar; |
||||
|
|
||||
|
$sesslog = array(); |
||||
|
|
||||
|
$validusers = $this->att->get_users($this->att->pageparams->grouptype, 0); |
||||
|
$users = array(); |
||||
|
|
||||
|
// Re-key validusers based on the identifier used by import. |
||||
|
if (!empty($mappingdata) && $mappingdata->userto !== 'id') { |
||||
|
foreach ($validusers as $u) { |
||||
|
if (!empty($u->{$mappingdata->userto})) { |
||||
|
$users[$u->{$mappingdata->userto}] = $u; |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
$users = $validusers; |
||||
|
} |
||||
|
|
||||
|
$statuses = $this->att->get_statuses(); |
||||
|
$statusmap = array(); |
||||
|
foreach ($statuses as $st) { |
||||
|
$statusmap[$st->acronym] = $st->id; |
||||
|
} |
||||
|
|
||||
|
$sessioninfo = $this->att->get_session_info($this->att->pageparams->sessionid); |
||||
|
|
||||
|
while ($row = $this->importer->next()) { |
||||
|
// This structure mimics what the UI form returns. |
||||
|
if (empty($mappingdata)) { |
||||
|
// Precheck - just return for now - would be nice to look at adding preview option in future. |
||||
|
return; |
||||
|
} |
||||
|
$mapping = $this->read_mapping_data($mappingdata); |
||||
|
|
||||
|
// Get user. |
||||
|
$extuser = $this->get_column_data($row, $mapping['user']); |
||||
|
if (empty($users[$extuser])) { |
||||
|
$a = new \stdClass(); |
||||
|
$a->extuser = $extuser; |
||||
|
$a->userfield = $mappingdata->userto; |
||||
|
\mod_attendance_notifyqueue::notify_problem(get_string('error:usernotfound', 'attendance', $a)); |
||||
|
continue; |
||||
|
} |
||||
|
$userid = $users[$extuser]->id; |
||||
|
if (isset($sesslog[$userid])) { |
||||
|
\mod_attendance_notifyqueue::notify_problem(get_string('error:userduplicate', 'attendance', $extuser)); |
||||
|
continue; |
||||
|
} |
||||
|
$sesslog[$userid] = new stdClass(); |
||||
|
$sesslog[$userid]->studentid = $userid; |
||||
|
$sesslog[$userid]->statusset = $statuses; |
||||
|
$sesslog[$userid]->remarks = ''; |
||||
|
$sesslog[$userid]->sessionid = $this->att->pageparams->sessionid; |
||||
|
$sesslog[$userid]->timetaken = time(); |
||||
|
$sesslog[$userid]->takenby = $USER->id; |
||||
|
|
||||
|
$scantime = $this->get_column_data($row, $mapping['scantime']); |
||||
|
if (!empty($scantime)) { |
||||
|
$t = strtotime($scantime); |
||||
|
if ($t === false) { |
||||
|
$a = new \stdClass(); |
||||
|
$a->extuser = $extuser; |
||||
|
$a->scantime = $scantime; |
||||
|
\mod_attendance_notifyqueue::notify_problem(get_string('error:timenotreadable', 'attendance', $a)); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
$sesslog[$userid]->statusid = attendance_session_get_highest_status($this->att, $sessioninfo, $t); |
||||
|
} else { |
||||
|
$status = $this->get_column_data($row, $mapping['status']); |
||||
|
if (!empty($statusmap[$status])) { |
||||
|
$sesslog[$userid]->statusid = $statusmap[$status]; |
||||
|
} else { |
||||
|
$a = new \stdClass(); |
||||
|
$a->extuser = $extuser; |
||||
|
$a->status = $status; |
||||
|
\mod_attendance_notifyqueue::notify_problem(get_string('error:statusnotfound', 'attendance', $a)); |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
$this->sessions = $sesslog; |
||||
|
|
||||
|
$this->importer->close(); |
||||
|
if (empty($sesslog)) { |
||||
|
$this->fail(get_string('invalidimportfile', 'attendance')); |
||||
|
return; |
||||
|
} else { |
||||
|
raise_memory_limit(MEMORY_EXTRA); |
||||
|
|
||||
|
// We are calling from browser, display progress bar. |
||||
|
if ($this->useprogressbar === true) { |
||||
|
$this->progress = new \core\progress\display_if_slow(get_string('processingfile', 'attendance')); |
||||
|
$this->progress->start_html(); |
||||
|
} else { |
||||
|
$this->progress = new \core\progress\none(); |
||||
|
} |
||||
|
$this->progress->start_progress('', count($this->sessions)); |
||||
|
$this->progress->end_progress(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get parse errors. |
||||
|
* |
||||
|
* @return array of errors from parsing the xml. |
||||
|
*/ |
||||
|
public function get_error() { |
||||
|
return $this->error; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Create sessions using the CSV data. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function import() { |
||||
|
$this->att->save_log($this->sessions); |
||||
|
\mod_attendance_notifyqueue::notify_success(get_string('sessionsupdated', 'mod_attendance')); |
||||
|
} |
||||
|
} |
@ -0,0 +1,122 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Mark attendance sessions using a csv import. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @author Dan Marsden |
||||
|
* @copyright 2020 Catalyst IT |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
define('NO_OUTPUT_BUFFERING', true); |
||||
|
|
||||
|
require(__DIR__ . '/../../../config.php'); |
||||
|
require_once($CFG->dirroot . '/mod/attendance/lib.php'); |
||||
|
require_once($CFG->dirroot . '/mod/attendance/locallib.php'); |
||||
|
|
||||
|
$pageparams = new mod_attendance_take_page_params(); |
||||
|
|
||||
|
$id = required_param('id', PARAM_INT); |
||||
|
$pageparams->sessionid = required_param('sessionid', PARAM_INT); |
||||
|
$pageparams->grouptype = optional_param('grouptype', null, PARAM_INT); |
||||
|
$pageparams->page = optional_param('page', 1, PARAM_INT); |
||||
|
$importid = optional_param('importid', null, PARAM_INT); |
||||
|
|
||||
|
$cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST); |
||||
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); |
||||
|
$att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); |
||||
|
|
||||
|
// Check this is a valid session for this attendance. |
||||
|
$session = $DB->get_record('attendance_sessions', array('id' => $pageparams->sessionid, 'attendanceid' => $att->id), |
||||
|
'*', MUST_EXIST); |
||||
|
|
||||
|
require_login($course, true, $cm); |
||||
|
$context = context_module::instance($cm->id); |
||||
|
require_capability('mod/attendance:takeattendances', $context); |
||||
|
|
||||
|
$pageparams->init($course->id); |
||||
|
|
||||
|
$PAGE->set_context($context); |
||||
|
$url = new moodle_url('/mod/attendance/import/marksessions.php'); |
||||
|
$PAGE->set_url($url); |
||||
|
$PAGE->set_title($course->shortname. ": ".$att->name); |
||||
|
$PAGE->set_heading($course->fullname); |
||||
|
$PAGE->set_cacheable(true); |
||||
|
$PAGE->navbar->add($att->name); |
||||
|
|
||||
|
$att = new mod_attendance_structure($att, $cm, $course, $PAGE->context, $pageparams); |
||||
|
|
||||
|
// Form processing and displaying is done here. |
||||
|
$output = $PAGE->get_renderer('mod_attendance'); |
||||
|
|
||||
|
$formparams = ['id' => $cm->id, |
||||
|
'sessionid' => $pageparams->sessionid, |
||||
|
'grouptype' => $pageparams->grouptype]; |
||||
|
$form = null; |
||||
|
if (optional_param('needsconfirm', 0, PARAM_BOOL)) { |
||||
|
$form = new \mod_attendance\form\import\marksessions($url->out(false), $formparams); |
||||
|
} else if (optional_param('confirm', 0, PARAM_BOOL)) { |
||||
|
$importer = new \mod_attendance\import\marksessions(null, $att, null, null, $importid); |
||||
|
$formparams['importer'] = $importer; |
||||
|
$form = new \mod_attendance\form\import\marksessions_confirm(null, $formparams); |
||||
|
} else { |
||||
|
$form = new \mod_attendance\form\import\marksessions($url->out(false), $formparams); |
||||
|
} |
||||
|
|
||||
|
if ($form->is_cancelled()) { |
||||
|
redirect(new moodle_url('/mod/attendance/take.php', |
||||
|
array('id' => $cm->id, |
||||
|
'sessionid' => $pageparams->sessionid, |
||||
|
'grouptype' => $pageparams->grouptype))); |
||||
|
return; |
||||
|
} else if ($data = $form->get_data()) { |
||||
|
if ($data->confirm) { |
||||
|
$importid = $data->importid; |
||||
|
$importer = new \mod_attendance\import\marksessions(null, $att, null, null, $importid, $data, true); |
||||
|
$error = $importer->get_error(); |
||||
|
if ($error) { |
||||
|
$form = new \mod_attendance\form\import\marksessions($url->out(false), $formparams); |
||||
|
$form->set_import_error($error); |
||||
|
} else { |
||||
|
echo $output->header(); |
||||
|
$sessions = $importer->import(); |
||||
|
mod_attendance_notifyqueue::show(); |
||||
|
$url = new moodle_url('/mod/attendance/manage.php', array('id' => $att->cmid)); |
||||
|
echo $output->continue_button($url); |
||||
|
echo $output->footer(); |
||||
|
die(); |
||||
|
} |
||||
|
} else { |
||||
|
$text = $form->get_file_content('attendancefile'); |
||||
|
$encoding = $data->encoding; |
||||
|
$delimiter = $data->separator; |
||||
|
$importer = new \mod_attendance\import\marksessions($text, $att, $encoding, $delimiter, 0, null, true); |
||||
|
$formparams['importer'] = $importer; |
||||
|
$confirmform = new \mod_attendance\form\import\marksessions_confirm(null, $formparams); |
||||
|
$form = $confirmform; |
||||
|
$pagetitle = get_string('confirmcolumnmappings', 'attendance'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Output for the file upload form starts here. |
||||
|
echo $output->header(); |
||||
|
echo $output->heading(get_string('attendanceforthecourse', 'attendance') . ' :: ' . format_string($course->fullname)); |
||||
|
echo $output->box(get_string('marksessionimportcsvhelp', 'attendance')); |
||||
|
mod_attendance_notifyqueue::show(); |
||||
|
$form->display(); |
||||
|
echo $output->footer(); |
Loading…
Reference in new issue