initial commit (da)

This commit is contained in:
Raphael Dannecker 2020-09-03 21:25:42 +02:00
commit 634dceef57
26 changed files with 2592 additions and 0 deletions

View file

@ -0,0 +1,25 @@
<?php
// Standard GPL and phpdocs
namespace mod_timetable\output;
defined('MOODLE_INTERNAL') || die;
use plugin_renderer_base;
class renderer extends plugin_renderer_base {
/**
* Defer to template.
*
* @param index_page $page
*
* @return string html for the page
*/
public function render_index_page($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_timetable/timetable', $data);
}
public function render_search_form($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_timetable/searchform', $data);
}
}

View file

@ -0,0 +1,28 @@
<?php
// Standard GPL and phpdocs
namespace mod_timetable\output;
use renderable;
use renderer_base;
use templatable;
use stdClass;
class searchform implements renderable, templatable {
/** @var string $sometext Some text to show how to pass data to a template. */
var $sometext = null;
public function __construct($sometext) {
$this->sometext = $sometext;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->sometext = $this->sometext;
return $data;
}
}

View file

@ -0,0 +1,61 @@
<?php
// Standard GPL and phpdocs
namespace mod_timetable\output;
use renderable;
use renderer_base;
use templatable;
use stdClass;
class timetable implements renderable, templatable {
/** @var string $sometext Some text to show how to pass data to a template. */
var $sometext = null;
public function __construct($timetable) {
$this->timetable = $timetable;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
/*
$data = new stdClass();
$numperiod = get_config('timetable', 'numperiod');
if ($this->timetable->periodmax > $numperiod) $numperiod = $this->timetable->periodmax;
$numdayweek = 5 + get_config('timetable', 'saturday');
if ($numdayweek == 6) $data->saturday = 1;
$periods = array();
for ($period=0; $period<$numperiod; $period++) {
$days = array();
for ($day=0; $day<$numdayweek; $day++) {
$days[$day] = new stdClass;
//$days[$day]->lessons = "$period, $day";
$days[$day]->lessons = array();
if (array_key_exists($period+1,$this->timetable->data[$day+1])
&& array_key_exists('lesson',$this->timetable->data[$day+1][$period+1])) {
foreach ($this->timetable->data[$day+1][$period+1]['lesson'] as $lesson) {
$mylesson = new stdClass;
$mylesson->class = $lesson->class;
$mylesson->teacher = $lesson->teacher;
$mylesson->room = $lesson->room;
$mylesson->subject = $lesson->subject;
$mylesson->status = $lesson->week[$this->timetable->week-1];
$mylesson->flag = $lesson->flag;
$days[$day]->lessons[] = $mylesson;
}
}
}
$periods[$period] = new stdClass;
$periods[$period]->days = $days;
$periods[$period]->number = $period+1;
}
$data->periods = $periods;
//echo var_dump($data); */
return $this->timetable->prepare_output();
}
}

251
classes/task/import_data.php Executable file
View file

@ -0,0 +1,251 @@
<?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();
}
}

301
classes/timetable.php Executable file
View file

@ -0,0 +1,301 @@
<?php
/**
* File containing timetable class.
*
* @package mod_timetable
* @copyright 2020 Raphael Dannecker <raphael.dannecker@steinbeisschule-reutlingen.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_timetable;
defined('MOODLE_INTERNAL') || die();
/**
* Class timetable
*
* @package mod_timetable
* @copyright 2020 Raphael Dannecker <raphael.dannecker@steinbeisschule-reutlingen.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class timetable {
/** @var array the data of timetable */
public $data;
/**
* Class constructor
*
* @param string $type The type of the timetable [class, room, teacher]
* @param string $name Name of class or room or teacher
*/
public function __construct($type, $name) {
$this->type = $type;
$this->name = $name;
$this->periodmax=0;
$this->clear_data();
}
public function clear_data() {
$this->data = array();
for ($i=1; $i<=6; $i++) $this->data[$i] = array();
}
/**
*
* @param int $week The week of timetable
*/
public function read_db($week) {
global $USER, $DB, $CFG;
$this->clear_data();
$this->numdayweek = 5 + get_config('timetable', 'saturday');
$this->week = $week;
$this->monday = new \DateTime();
$this->monday->setISODate($this->monday->format("Y"), $week);
/*
$this->monday = new \DateTime('29-07-2020'); // Only for debugging!!!
if ($week>0) {
$this->monday->add(new \DateInterval("P".($week)."W"));
} elseif ($week<0) {
$this->monday->sub(new \DateInterval("P".(-$week)."W"));
}
*/
$dayofweek = $this->monday->format('w');
if ($dayofweek > $this->numdayweek) {
$this->monday->add(new \DateInterval("P".(8-$dayofweek)."D"));
} elseif ($dayofweek < 1) {
$this->monday->add(new \DateInterval("P1D"));
} else {
$this->monday->sub(new \DateInterval("P".($dayofweek-1)."D"));
}
$this->week = $this->monday->format("W");
$this->lastday = new \DateTime($this->monday->format('Y-m-d\TH:i:sP'));
$this->lastday->add(new \DateInterval("P".($this->numdayweek-1)."D"));
//$this->content->text .= $monday->format('d.m') . " - " . $lastday->format('d.m');
$sql = "$this->type = '$this->name' and not (mid(week, $this->week, 1) = '-')";
//$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) {
//if (!exists($this->data[$lesson->day][$lesson->period])) $this->data[$lesson->day][$lesson->period] = array();
$this->data[$lesson->day][$lesson->period]['lesson'][] = $lesson;
if ($this->periodmax < $lesson->period) $this->periodmax = $lesson->period;
}
}
$day = new \DateTime($this->monday->format('Y-m-d\TH:i:sP'));
for ($i=0; $i<$this->numdayweek; $i++) {
$sql = "date = '".$day->format('Ymd')."' AND ({$this->type}a LIKE '%{$this->name}%' OR {$this->type}b LIKE '%{$this->name}%')";
if ($result = $DB->get_records_select('timetable_substitution',$sql)) {
foreach ($result as $substitution) {
$this->data[$i+1][$substitution->period]['substitution'][] = $substitution;
if ($this->periodmax < $lesson->period) $this->periodmax = $lesson->period;
}
}
$day->add(new \DateInterval("P1D"));
}
if ($this->type == 'teacher') {
if ($result = $DB->get_record("timetable_$this->type",[$this->type => $this->name])) {
$this->description = "$result->surname, $result->firstname";
}
} else {
if ($result = $DB->get_record("timetable_$this->type",[$this->type => $this->name])) {
$this->description = $result->description;
}
}
}
/**
* Count the number of online users
*
* @return int
*/
public function print_navigation($week) {
/**
* Count the number of online users
*
* @return int
*/
$table = new \html_table();
$table->attributes['class'] = 'minicalendar calendartable calendar-controls';
//$table->head = array('','Mo','Di', 'Mi' , 'Do', 'Fr');
$tablerow = new \html_table_row();
if ($week>0) {
$tablerow->cells[] = new \html_table_cell('<a href="#slide-'.($week-1).'" class="arrow_link previous" title="Vorherige Woche" data-week="-1"><span class="arrow">◄</span></a>');
} else {
$tablerow->cells[] = new \html_table_cell('');
}
$tablerow->cells[] = new \html_table_cell('<a href="#">'.$this->monday->format('d.m') . " - " . $this->lastday->format('d.m').'</a>');
if ($week<2) {
$tablerow->cells[] = new \html_table_cell('<a href="#slide-'.($week+1).'" class="arrow_link next" title="Nächste Woche" data-month="8" data-week="1"><span class="arrow">►</span></a>');
} else {
$tablerow->cells[] = new \html_table_cell('');
}
$table->data[] = $tablerow;
return \html_writer::table($table);
}
public function get_caption($week) {
$result = "";
if ($week>0) {
//$result .= '<a href="#slide-'.($week-1).'" class="arrow_link previous" title="Vorherige Woche" data-week="-1"><span class="arrow">◄</span></a>';
$result .= '<a href="javascript:M.block_timetable.scroll_week('.($week-1).');" class="arrow_link previous" title="Vorherige Woche" data-week="1"><span class="arrow">◄<</span></a>';
} else {
$result .= '<a href="#" class="previous" style="visibility:hidden" data-week="-1"></A>';
//$result .= '<a href="#" class="arrow_link previous" data-week="-1"><span class="arrow" style="display:none"></span></a>';
}
$result .= '<span class="hide"> | </span>';
$result .= '<span class="current">';
$result .= '<a href="#">'.$this->monday->format('d.m') . " - " . $this->lastday->format('d.m').'</a>';
$result .= "</span>";
$result .= '<span class="hide"> | </span>';
if ($week<2) {
//$result .= '<a href="#slide-'.($week+1).'" class="arrow_link next" title="Nächste Woche" data-month="8" data-week="1"><span class="arrow">►</span></a>';
$result .= '<a href="javascript:M.block_timetable.scroll_week('.($week+1).');" class="arrow_link next" title="Nächste Woche" data-month="8" data-week="1"><span class="arrow">►</span></a>';
} else {
$result .= '';
}
return $result;
}
public function print_table($view,$week) {
global $DB;
//var_dump($this->data[1]);
$numperiod = get_config('timetable', 'numperiod');
$numdayweek = 5 + get_config('timetable', 'saturday');
if ($this->periodmax>$numperiod) $numperiod = $this->periodmax;
$table = new \html_table();
//$table->attributes['class'] = "minicalendar calendartable generaltable timetable_view_$view";
$table->attributes['class'] = "minicalendar calendartable generaltable timetable_view_$view";
$table->caption = "<div class='calendar-controls'>".$this->get_caption($week)."</div>";
//$table->style = 'display: none;';
$table->head = array('','Mo','Di', 'Mi' , 'Do', 'Fr');
if ($numdayweek == 6) $table->head[] = 'Sa';
for ($i = 1; $i<=$numperiod; $i++) {
$tablerow = new \html_table_row();
$cell = new \html_table_cell($i);
$cell->style = 'font-weight: bold;';
// $cell->attributes['class'] = 'header';
$tablerow->cells[] = $cell;
for ($k = 1; $k <= $numdayweek; $k++) {
$flag = 0;
if (array_key_exists($i,$this->data[$k]) && array_key_exists('lesson',$this->data[$k][$i])) {
$content = "";
foreach ($this->data[$k][$i]['lesson'] as $lesson) {
if ($content) $content .= "<br>";
if ($lesson->flag) $content .= "<span style='color:red;'>";
//if (substr($lesson->week,$this->week,1) == '1') $content .= $lesson->{$view};
if ($lesson->week[$this->week-1] == '1') {
if ($lesson->{$view}) {
$content .= $lesson->{$view};
} else {
$content .= "+++";
}
}
if ($lesson->week[$this->week-1] == 'x') $content .= "---";
if ($lesson->flag) $content .= "</span>";
$flag += $lesson->flag;
}
} else {
$content = "-";
}
$subtxt = "";
if (array_key_exists($i,$this->data[$k]) && array_key_exists('substitution', $this->data[$k][$i])) {
foreach ($this->data[$k][$i]['substitution'] as $substitution) {
$subtxt .= $substitution->text;
}
if ($subtxt) {
$content = "<a href='#' data-toggle='tooltip' title='$subtxt'>$content</a>";
//$content .= "<span class='tooltiptext'>$subtxt</span>";
}
}
$cell = new \html_table_cell($content);
// if ($flag) $cell->style = 'color: red;';
//if (array_key_exists($i, $this->data[$k] ) && array_key_exists('substitution', $this->data[$k][$i])) $cell->attributes['class'] = 'tooltip';
$tablerow->cells[] = $cell;
}
$table->data[] = $tablerow;
}
return \html_writer::table($table);
}
public function prepare_output() {
$resultdata = array();
$numperiod = get_config('timetable', 'numperiod');
if ($this->periodmax > $numperiod) $numperiod = $this->periodmax;
$numdayweek = 5 + get_config('timetable', 'saturday');
if ($numdayweek == 6) $resultdata['saturday'] = 1; else $resultdata['saturday'] = 0;
$resultdata['type'] = $this->type;
$resultdata['name'] = $this->name;
$resultdata['description'] = $this->description;
$resultdata['date'] = $this->monday->format('d.m') . " - " . $this->lastday->format('d.m');
$resultdata['week'] = $this->week;
$resultdata['prevweek'] = $this->week-1;
$resultdata['nextweek'] = $this->week+1;
$resultdata['id'] = \substr(\md5(\rand()), 0, 7);
$periods = array();
for ($period=0; $period<$numperiod; $period++) {
$days = array();
for ($day=0; $day<$numdayweek; $day++) {
$days[$day] = array();
$days[$day]['substitution'] = "";
$days[$day]['lessons'] = array();
if (array_key_exists($period+1,$this->data[$day+1])
&& array_key_exists('lesson',$this->data[$day+1][$period+1])) {
foreach ($this->data[$day+1][$period+1]['lesson'] as $lesson) {
$mylesson = array();
$mylesson['class'] = $lesson->class;
$mylesson['teacher'] = $lesson->teacher;
$mylesson['room'] = $lesson->room;
$mylesson['subject'] = $lesson->subject;
$mylesson['status'] = "";
if ($lesson->week[$this->week-1] == 'x') $mylesson['status'] = 1;
$mylesson['flag'] = "";
if ($lesson->flag) $mylesson['flag'] = $lesson->flag;
if ($this->type == 'class') $mylesson['class'] = "";
if ($this->type == 'teacher') $mylesson['teacher'] = "";
if ($this->type == 'room') $mylesson['room'] = "";
$days[$day]['lessons'][] = $mylesson;
/*
- für diese Woche wurden keine Daten ausgegeben
0 in dieser Woche findet der Unterricht nicht statt
1 in dieser Woche findet der Unterricht statt
x in dieser Woche entfällt der Unterricht
if ($lesson->week[$this->week-1] == 'x')
*/
}
}
if (array_key_exists($period+1,$this->data[$day+1])
&& array_key_exists('substitution', $this->data[$day+1][$period+1])) {
foreach ($this->data[$day+1][$period+1]['substitution'] as $substitution) {
//echo "In substitution loop".$substitution->text;
$days[$day]['substitution'] .= $substitution->text;
}
}
}
$periods[$period] = array();
$periods[$period]['days'] = $days;
$periods[$period]['number'] = $period+1;
}
$resultdata['periods'] = $periods;
//echo var_dump($data);
return $resultdata;
}
}