251 lines
8 KiB
PHP
251 lines
8 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
namespace mod_timetable\task;
|
||
|
|
||
|
/**
|
||
|
* An example of a scheduled task.
|
||
|
*/
|
||
|
class import_data extends \core\task\scheduled_task {
|
||
|
|
||
|
/**
|
||
|
* Return the task's name as shown in admin screens.
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
|
||
|
public function get_name() {
|
||
|
return get_string('import_data', 'mod_timetable');
|
||
|
}
|
||
|
|
||
|
private function read_lesson() {
|
||
|
global $DB,$CFG;
|
||
|
$lastmtime = get_config('timetable', 'lastmtime');
|
||
|
$filenamel = get_config('timetable', 'fname_lesson');
|
||
|
|
||
|
echo "Lastmtime = $lastmtime\n";
|
||
|
if ($filenamel && (file_exists ($filenamel))) {
|
||
|
$mtime = filemtime ( $filenamel );
|
||
|
echo "mtime = $mtime\n";
|
||
|
if ($mtime<=$lastmtime) return;
|
||
|
echo "after return\n";
|
||
|
try {
|
||
|
$transaction = $DB->start_delegated_transaction();
|
||
|
echo "before delete\n";
|
||
|
$DB->delete_records_select("timetable_lesson", "id>0");
|
||
|
echo "after delete\n";
|
||
|
$handle = @fopen($filenamel, "r");
|
||
|
echo "after handle\n";
|
||
|
while (($buffer = fgets($handle, 4096)) !== false) {
|
||
|
// echo $buffer;
|
||
|
$buffer = utf8_encode(rtrim($buffer));
|
||
|
$data = explode("\t", $buffer);
|
||
|
if (count($data) != 10) throw new Exception('Wrong size of elements');
|
||
|
$dataobject = (object)array('teacher' => $data[0],
|
||
|
'day' => $data[1],
|
||
|
'period' => $data[2],
|
||
|
'subject' => $data[3],
|
||
|
'room' => $data[4],
|
||
|
'lessonid'=> $data[5],
|
||
|
'flag' => $data[6],
|
||
|
'class' => $data[7],
|
||
|
'week' => $data[8],
|
||
|
'unknown' => $data[9]);
|
||
|
|
||
|
$DB->insert_record("timetable_lesson", $dataobject);
|
||
|
}
|
||
|
$transaction->allow_commit();
|
||
|
fclose($handle);
|
||
|
$lastmtime = $mtime;
|
||
|
set_config('lastmtime', $lastmtime, 'timetable');
|
||
|
} catch(Exception $e) {
|
||
|
$transaction->rollback($e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function read_substitution() {
|
||
|
global $DB,$CFG;
|
||
|
$filehash = get_config('timetable', 'filehash_substitution');
|
||
|
$filename = get_config('timetable', 'fname_substitution');
|
||
|
|
||
|
if (!$filename || !file_exists($filename)) return;
|
||
|
if (($handle = fopen($filename, 'r')) == FALSE) return;
|
||
|
$hash = hash_file('md5', $filename);
|
||
|
if ($hash == $filehash) return;
|
||
|
$max_id = 0;
|
||
|
$max_changetime = 0;
|
||
|
if ($result = $DB->get_record_sql("select max(id), max(changetime) from {$CFG->prefix}timetable_substitution")) {
|
||
|
$max_id = $result->{'max(id)'};
|
||
|
$max_changetime = $result->{'max(changetime)'};
|
||
|
}
|
||
|
echo "Max id = $max_id\n";
|
||
|
echo "Max changetime = $max_changetime\n";
|
||
|
try {
|
||
|
$transaction = $DB->start_delegated_transaction();
|
||
|
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
|
||
|
$dataobject = (object)array('id' => $data[0],
|
||
|
'date' => $data[1],
|
||
|
'period' => $data[2],
|
||
|
'absence' => $data[3],
|
||
|
'lesson' => $data[4],
|
||
|
'teachera' => $data[5],
|
||
|
'teacherb' => $data[6],
|
||
|
'subjecta' => utf8_encode($data[7]),
|
||
|
'statistica' => $data[8],
|
||
|
'subjectb' => utf8_encode($data[9]),
|
||
|
'statisticb' => $data[10],
|
||
|
'rooma' => $data[11],
|
||
|
'roomb' => $data[12],
|
||
|
'statisticflag' => $data[13],
|
||
|
'classa' => utf8_encode($data[14]),
|
||
|
'reason' => utf8_encode($data[15]),
|
||
|
'text' => utf8_encode($data[16]),
|
||
|
'type' => $data[17],
|
||
|
'classb' => utf8_encode($data[18]),
|
||
|
'substitutiontype' => $data[19],
|
||
|
'changetime' => $data[20],
|
||
|
'unknown' => $data[21]);
|
||
|
//echo var_dump($dataobject);
|
||
|
foreach (get_object_vars($dataobject) as $key => $value) {
|
||
|
if ($value=='') unset($dataobject->{$key});
|
||
|
}
|
||
|
if ($dataobject->id > $max_id) {
|
||
|
$DB->insert_record_raw("timetable_substitution", $dataobject,false, false, true);
|
||
|
echo "Object (id={$data[0]}) inserted\n";
|
||
|
} elseif ($dataobject->changetime >= $max_changetime) {
|
||
|
$DB->update_record("timetable_substitution", $dataobject);
|
||
|
echo "Object (id={$data[0]}) updated\n";
|
||
|
}
|
||
|
}
|
||
|
$transaction->allow_commit();
|
||
|
fclose($handle);
|
||
|
set_config('filehash_substitution', $hash, 'timetable');
|
||
|
} catch(Exception $e) {
|
||
|
$transaction->rollback($e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function read_teacher() {
|
||
|
global $DB,$CFG;
|
||
|
$filehash = get_config('timetable', 'filehash_teacher');
|
||
|
$filename = get_config('timetable', 'fname_teacher');
|
||
|
echo "Filename: $filename\n";
|
||
|
if (!$filename || !file_exists($filename)) return;
|
||
|
if (($handle = fopen($filename, 'r')) == FALSE) return;
|
||
|
$hash = hash_file('md5', $filename);
|
||
|
if ($hash == $filehash) return;
|
||
|
try {
|
||
|
$transaction = $DB->start_delegated_transaction();
|
||
|
echo "before delete\n";
|
||
|
$DB->delete_records_select("timetable_teacher", "id>0");
|
||
|
echo "after delete\n";
|
||
|
$handle = @fopen($filename, "r");
|
||
|
echo "after handle\n";
|
||
|
while (($buffer = fgets($handle, 4096)) !== false) {
|
||
|
echo $buffer;
|
||
|
$buffer = utf8_encode(rtrim($buffer));
|
||
|
$data = explode("\t", $buffer);
|
||
|
if (!(array_key_exists(3,$data))) {
|
||
|
$data[3] = '';
|
||
|
} else {
|
||
|
echo "firstname exists: $data[3]\n";
|
||
|
}
|
||
|
//if (count($data) < 3) throw new Exception('Wrong size of elements');
|
||
|
$dataobject = (object)array('teacher' => $data[0],
|
||
|
'surname' => $data[1],
|
||
|
'firstname' => $data[3]);
|
||
|
|
||
|
$DB->insert_record("timetable_teacher", $dataobject);
|
||
|
}
|
||
|
$transaction->allow_commit();
|
||
|
fclose($handle);
|
||
|
set_config('filehash_teacher', $hash, 'timetable');
|
||
|
} catch(Exception $e) {
|
||
|
$transaction->rollback($e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function read_room() {
|
||
|
global $DB,$CFG;
|
||
|
$filehash = get_config('timetable', 'filehash_room');
|
||
|
$filename = get_config('timetable', 'fname_room');
|
||
|
|
||
|
if (!$filename || !file_exists($filename)) return;
|
||
|
if (($handle = fopen($filename, 'r')) == FALSE) return;
|
||
|
$hash = hash_file('md5', $filename);
|
||
|
if ($hash == $filehash) return;
|
||
|
try {
|
||
|
$transaction = $DB->start_delegated_transaction();
|
||
|
echo "before delete\n";
|
||
|
$DB->delete_records_select("timetable_room", "id>0");
|
||
|
echo "after delete\n";
|
||
|
$handle = @fopen($filename, "r");
|
||
|
echo "after handle\n";
|
||
|
while (($buffer = fgets($handle, 4096)) !== false) {
|
||
|
// echo $buffer;
|
||
|
$buffer = utf8_encode(rtrim($buffer));
|
||
|
$data = explode("\t", $buffer);
|
||
|
if (count($data) < 2) throw new Exception('Wrong size of elements');
|
||
|
$dataobject = (object)array('room' => $data[0],
|
||
|
'description' => $data[1]);
|
||
|
|
||
|
$DB->insert_record("timetable_room", $dataobject);
|
||
|
}
|
||
|
$transaction->allow_commit();
|
||
|
fclose($handle);
|
||
|
set_config('filehash_room', $hash, 'timetable');
|
||
|
} catch(Exception $e) {
|
||
|
$transaction->rollback($e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function read_class() {
|
||
|
global $DB,$CFG;
|
||
|
$filehash = get_config('timetable', 'filehash_class');
|
||
|
$filename = get_config('timetable', 'fname_class');
|
||
|
|
||
|
if (!$filename || !file_exists($filename)) return;
|
||
|
if (($handle = fopen($filename, 'r')) == FALSE) return;
|
||
|
$hash = hash_file('md5', $filename);
|
||
|
if ($hash == $filehash) return;
|
||
|
try {
|
||
|
$transaction = $DB->start_delegated_transaction();
|
||
|
echo "before delete\n";
|
||
|
$DB->delete_records_select("timetable_class", "id>0");
|
||
|
echo "after delete\n";
|
||
|
$handle = @fopen($filename, "r");
|
||
|
echo "after handle\n";
|
||
|
while (($buffer = fgets($handle, 4096)) !== false) {
|
||
|
// echo $buffer;
|
||
|
$buffer = utf8_encode(rtrim($buffer));
|
||
|
$data = explode("\t", $buffer);
|
||
|
if (!(array_key_exists(1,$data))) $data[1] = $data[0];
|
||
|
|
||
|
if (count($data) < 2) throw new Exception('Wrong size of elements');
|
||
|
$dataobject = (object)array('class' => $data[0],
|
||
|
'description' => $data[1]);
|
||
|
|
||
|
$DB->insert_record("timetable_class", $dataobject);
|
||
|
}
|
||
|
$transaction->allow_commit();
|
||
|
fclose($handle);
|
||
|
set_config('filehash_class', $hash, 'timetable');
|
||
|
} catch(Exception $e) {
|
||
|
$transaction->rollback($e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Execute the task.
|
||
|
*/
|
||
|
public function execute() {
|
||
|
global $DB,$CFG;
|
||
|
|
||
|
$this->read_lesson();
|
||
|
$this->read_substitution();
|
||
|
$this->read_teacher();
|
||
|
$this->read_room();
|
||
|
$this->read_class();
|
||
|
}
|
||
|
|
||
|
}
|