From a6334e6c8686d0aee83a4a9aec5d024808ea6b4f Mon Sep 17 00:00:00 2001 From: L_DA Date: Tue, 30 Jun 2020 19:26:24 +0000 Subject: [PATCH] import of substitutions in MoodleDB (rd) --- .gitignore | 0 README.md | 0 block_timetable.php | 1 + classes/task/import_data.php | 95 ++++++++++++++++++++++++++---------- classes/timetable.php | 8 +-- composer.json | 0 db/access.php | 0 db/install.xml | 31 +++++++++++- db/tasks.php | 0 db/upgrade.php | 40 +++++++++++++++ edit_form.php | 0 lang/de/block_timetable.php | 0 lang/en/block_timetable.php | 4 +- settings.php | 5 ++ version.php | 2 +- 15 files changed, 154 insertions(+), 32 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 README.md mode change 100644 => 100755 block_timetable.php mode change 100644 => 100755 classes/task/import_data.php mode change 100644 => 100755 composer.json mode change 100644 => 100755 db/access.php mode change 100644 => 100755 db/install.xml mode change 100644 => 100755 db/tasks.php mode change 100644 => 100755 db/upgrade.php mode change 100644 => 100755 edit_form.php mode change 100644 => 100755 lang/de/block_timetable.php mode change 100644 => 100755 lang/en/block_timetable.php mode change 100644 => 100755 settings.php mode change 100644 => 100755 version.php diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/block_timetable.php b/block_timetable.php old mode 100644 new mode 100755 index a81762f..a1511d4 --- a/block_timetable.php +++ b/block_timetable.php @@ -107,6 +107,7 @@ class block_timetable extends block_base { } $week = $monday->format("W"); + $week++; $lastday = new DateTime($monday->format('Y-m-d\TH:i:sP')); $lastday->add(new DateInterval("P".($numdayweek-1)."D")); $this->content->text .= $monday->format('d.m') . " - " . $lastday->format('d.m'); diff --git a/classes/task/import_data.php b/classes/task/import_data.php old mode 100644 new mode 100755 index 87e4743..1bb00c4 --- a/classes/task/import_data.php +++ b/classes/task/import_data.php @@ -20,15 +20,77 @@ class import_data extends \core\task\scheduled_task { * Execute the task. */ public function execute() { - global $DB; + global $DB,$CFG; + $filehash = get_config('block_timetable', 'filehash'); $lastmtime = get_config('block_timetable', 'lastmtime'); - $filename = get_config('timetable', 'fnamelesson'); - echo $filename; - if (!$filename) return; + $filenamel = get_config('timetable', 'fnamelesson'); + $filenames = get_config('timetable', 'fnamesubst'); + + if ($filenames && (file_exists ($filenames))) { + if (($handle = fopen($filenames, 'r')) !== FALSE) { + $hash = hash_file('md5', $filenames); + if ($hash != $filehash) { + $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], + 'classesa' => utf8_encode($data[14]), + 'reason' => utf8_encode($data[15]), + 'text' => utf8_encode($data[16]), + 'type' => $data[17], + 'classesb' => 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', $hash, 'block_timetable'); + } catch(Exception $e) { + $transaction->rollback($e); + } + } + } + } + + + //$filename = "/srv/stundenplan/lesson.txt"; echo "Lastmtime = $lastmtime\n"; - if (file_exists ( $filename )) { - $mtime = filemtime ( $filename ); + if ($filenamel && (file_exists ($filenamel))) { + $mtime = filemtime ( $filenamel ); echo "mtime = $mtime\n"; if ($mtime<=$lastmtime) return; echo "after return\n"; @@ -37,31 +99,13 @@ class import_data extends \core\task\scheduled_task { echo "before delete\n"; $DB->delete_records_select("timetable_lesson", "id>0"); echo "after delete\n"; - $handle = @fopen($filename, "r"); + $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); -// echo "Num elements: ".count($data)."\n"; if (count($data) != 10) throw new Exception('Wrong size of elements'); -// echo "after count data \n"; -// echo "data 0 =$data[0]\n"; - - /* - $dataobject = new stdClass(); - //$dataobject = new object; - $dataobject->teacher = $data[0]; - $dataobject->day = $data[1]; - $dataobject->period = $data[2]; - $dataobject->subject = $data[3]; - $dataobject->room = $data[4]; - $dataobject->lessonid= $data[5]; - $dataobject->flag = $data[6]; - $dataobject->class = $data[7]; - $dataobject->week = $data[8]; - $dataobject->unknown = $data[9]; - */ $dataobject = (object)array('teacher' => $data[0], 'day' => $data[1], 'period' => $data[2], @@ -73,7 +117,6 @@ class import_data extends \core\task\scheduled_task { 'week' => $data[8], 'unknown' => $data[9]); -// echo "after dataobject\n"; $DB->insert_record("timetable_lesson", $dataobject); } $transaction->allow_commit(); diff --git a/classes/timetable.php b/classes/timetable.php index bfe6fe9..e040de9 100755 --- a/classes/timetable.php +++ b/classes/timetable.php @@ -46,8 +46,8 @@ class timetable { global $USER, $DB, $CFG; $this->week = $week; - //$sql = "$this->type = '$this->name' and not (mid(week, $this->week, 1) = '0')"; - $sql = "$this->type = '$this->name' and (mid(week, $this->week, 1) = '1')"; + $sql = "$this->type = '$this->name' and not (mid(week, $this->week, 1) = '0')"; + //$sql = "$this->type = '$this->name' and (mid(week, $this->week, 1) = '1')"; if ($result = $DB->get_records_select('timetable_lesson',$sql)) { foreach ($result as $lesson) { $this->data[$lesson->day][$lesson->period][] = $lesson; @@ -84,7 +84,9 @@ class timetable { foreach ($this->data[$k][$i] as $lesson) { if ($content) $content .= "
"; if ($lesson->flag) $content .= ""; - $content .= $lesson->{$view}; + //if (substr($lesson->week,$this->week,1) == '1') $content .= $lesson->{$view}; + if ($lesson->week[$this->week-1] == '1') $content .= $lesson->{$view}; + if ($lesson->week[$this->week-1] == 'x') $content .= "---"; if ($lesson->flag) $content .= ""; $flag += $lesson->flag; } diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 diff --git a/db/access.php b/db/access.php old mode 100644 new mode 100755 diff --git a/db/install.xml b/db/install.xml old mode 100644 new mode 100755 index 070c946..d1a4aad --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -30,5 +30,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/db/tasks.php b/db/tasks.php old mode 100644 new mode 100755 diff --git a/db/upgrade.php b/db/upgrade.php old mode 100644 new mode 100755 index 81f331a..1ac5418 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -53,6 +53,46 @@ function xmldb_block_timetable_upgrade($oldversion) { } + if ($oldversion < 2020063001) { + + // Define table timetable_substitution to be created. + $table = new xmldb_table('timetable_substitution'); + + // Adding fields to table timetable_substitution. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('date', XMLDB_TYPE_CHAR, '8', null, null, null, null); + $table->add_field('period', XMLDB_TYPE_INTEGER, '2', null, null, null, null); + $table->add_field('absence', XMLDB_TYPE_INTEGER, '6', null, null, null, null); + $table->add_field('lesson', XMLDB_TYPE_INTEGER, '6', null, null, null, null); + $table->add_field('teachera', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('teacherb', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('subjecta', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('statistica', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('subjectb', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('statisticb', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('rooma', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('roomb', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('statisticflag', XMLDB_TYPE_CHAR, '10', null, null, null, null); + $table->add_field('classesa', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('reason', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('text', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('classesb', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('substitutiontype', XMLDB_TYPE_CHAR, '1', null, null, null, null); + $table->add_field('changetime', XMLDB_TYPE_INTEGER, '12', null, null, null, null); + $table->add_field('unknown', XMLDB_TYPE_CHAR, '10', null, null, null, null); + + // Adding keys to table timetable_substitution. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + + // Conditionally launch create table for timetable_substitution. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Timetable savepoint reached. + upgrade_block_savepoint(true, 2020063001, 'timetable'); + } return $result; diff --git a/edit_form.php b/edit_form.php old mode 100644 new mode 100755 diff --git a/lang/de/block_timetable.php b/lang/de/block_timetable.php old mode 100644 new mode 100755 diff --git a/lang/en/block_timetable.php b/lang/en/block_timetable.php old mode 100644 new mode 100755 index 320917c..7c16744 --- a/lang/en/block_timetable.php +++ b/lang/en/block_timetable.php @@ -32,8 +32,10 @@ $string['timetable:addinstance'] = 'Add a timetable block'; $string['timetable:myaddinstance'] = 'Add a timetable block to my moodle'; $string['pluginname'] = 'Timetable'; $string['import_data'] = 'import_data'; -$string['labelfnamelesson'] = 'import file'; +$string['labelfnamelesson'] = 'lesson import file'; $string['descfnamelesson'] = 'Absolute path and Filename of untis import file lesson.txt'; +$string['labelfnamesubst'] = 'substitution import file'; +$string['descfnamesubst'] = 'Absolute path and Filename of untis import file GPU014.TXT'; $string['labelnumperiod'] = 'max period'; $string['descnumperiod'] = 'max period per day'; $string['labelsaturday'] = 'saturday'; diff --git a/settings.php b/settings.php old mode 100644 new mode 100755 index b7586fe..c64a1c8 --- a/settings.php +++ b/settings.php @@ -38,6 +38,11 @@ $settings->add(new admin_setting_configtext('timetable/fnamelesson', get_string('descfnamelesson', 'block_timetable'), '')); +$settings->add(new admin_setting_configtext('timetable/fnamesubst', + get_string('labelfnamesubst', 'block_timetable'), + get_string('descfnamesubst', 'block_timetable'), + '')); + $settings->add(new admin_setting_configtext('timetable/numperiod', get_string('labelnumperiod', 'block_timetable'), get_string('descnumperiod', 'block_timetable'), diff --git a/version.php b/version.php old mode 100644 new mode 100755 index 4af4a6a..c224f52 --- a/version.php +++ b/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020062702; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2020063001; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2012112900; // Requires this Moodle version $plugin->component = 'block_timetable'; // Full name of the plugin (used for diagnostics)