Initial commit of timetable-Module (rd)

This commit is contained in:
root 2020-06-26 11:30:19 +00:00
commit 784cb23da3
14 changed files with 723 additions and 0 deletions

View file

@ -0,0 +1,91 @@
<?php
namespace block_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', 'block_timetable');
}
/**
* Execute the task.
*/
public function execute() {
global $DB;
$lastmtime = get_config('block_timetable', 'lastmtime');
$filename = get_config('timetable', 'fnamelesson');
echo $filename;
if (!$filename) return;
//$filename = "/srv/stundenplan/lesson.txt";
echo "Lastmtime = $lastmtime\n";
if (file_exists ( $filename )) {
$mtime = filemtime ( $filename );
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($filename, "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],
'subject' => $data[3],
'room' => $data[4],
'lessonid'=> $data[5],
'flag' => $data[6],
'class' => $data[7],
'week' => $data[8],
'unknown' => $data[9]);
// echo "after dataobject\n";
$DB->insert_record("timetable_lesson", $dataobject);
}
$transaction->allow_commit();
$lastmtime = $mtime;
set_config('lastmtime', $lastmtime, 'block_timetable');
} catch(Exception $e) {
$transaction->rollback($e);
}
}
// Apply fungus cream.
// Apply chainsaw.
// Apply olive oil.
}
}

98
classes/timetable.php Executable file
View file

@ -0,0 +1,98 @@
<?php
/**
* File containing timetable class.
*
* @package block_online_users
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_timetable;
defined('MOODLE_INTERNAL') || die();
/**
* Class used to list and count online users
*
* @package block_online_users
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @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 int $name Name of class or room or teacher
*/
public function __construct($type, $name) {
$this->type = $type;
$this->name = $name;
$this->periodmax=0;
$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->week = $week;
$sql = "$this->type = '$this->name' and not (mid(week, $this->week, 1) = '0')";
if ($result = $DB->get_records_select('timetable_lesson',$sql)) {
foreach ($result as $lesson) {
$this->data[$lesson->day][$lesson->period][] = $lesson;
if ($this->periodmax < $lesson->period) $this->periodmax = $lesson->period;
}
}
}
/**
* Count the number of online users
*
* @return int
*/
public function print_table($view) {
global $DB;
//var_dump($this->data[1]);
$numperiod = $period_max = get_config('timetable', 'numperiod');
if ($this->periodmax>$numperiod) $numperiod = $this->periodmax;
$table = new \html_table();
$table->attributes['class'] = 'minicalendar calendartable generaltable';
$table->head = array('','Mo','Di', 'Mi' , 'Do', 'Fr');
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 <= 5; $k++) {
$flag = 0;
if (array_key_exists($i,$this->data[$k])) {
$content = "";
foreach ($this->data[$k][$i] as $lesson) {
if ($content) $content .= "<br>";
$content .= $lesson->{$view};
$flag += $lesson->flag;
}
} else {
$content = "-";
}
$cell = new \html_table_cell($content);
if ($flag) $cell->style = 'color: red;';
$tablerow->cells[] = $cell;
}
$table->data[] = $tablerow;
}
return \html_writer::table($table);
}
}