* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace mod_timetable; defined('MOODLE_INTERNAL') || die(); function super_unique($array) { $result = array_map("unserialize", array_unique(array_map("serialize", $array))); foreach ($result as $key => $value) { if ( is_array($value) ) { $result[$key] = super_unique($value); } } return $result; } function remove_dup($matriz) { $aux_ini=array(); $entrega=array(); for($n=0;$n * @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; //echo var_dump($substitution); } } $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(''); } else { $tablerow->cells[] = new \html_table_cell(''); } $tablerow->cells[] = new \html_table_cell(''.$this->monday->format('d.m') . " - " . $this->lastday->format('d.m').''); if ($week<2) { $tablerow->cells[] = new \html_table_cell(''); } 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 .= ''; $result .= ''; } else { $result .= ''; //$result .= ''; } $result .= ' | '; $result .= ''; $result .= ''.$this->monday->format('d.m') . " - " . $this->lastday->format('d.m').''; $result .= ""; $result .= ' | '; if ($week<2) { //$result .= ''; $result .= ''; } 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 = "
".$this->get_caption($week)."
"; //$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 .= "
"; if ($lesson->flag) $content .= ""; //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 .= ""; $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 = "$content"; //$content .= "$subtxt"; } } $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'] = ""; $mylesson['flag'] = ""; if ($lesson->flag) $mylesson['flag'] = $lesson->flag; if ($lesson->week[$this->week-1] == 'x') { $mylesson['status'] = 1; $mylesson['flag'] = 1; /* if (array_key_exists('substitution', $this->data[$day+1][$period+1])) { foreach ($this->data[$day+1][$period+1]['substitution'] as $substitution) { if ($lesson->class == $substitution->classa && $lesson->teacher == $substitution->teachera && $lesson->subject == $substitution->subjecta && $lesson->room == $substitution->rooma) { $mylesson['class'] = $substitution->classb; $mylesson['teacher'] = $substitution->teacherb; $mylesson['room'] = $substitution->roomb; $mylesson['subject'] = $substitution->subjectb; $mylesson['status'] = ''; } } } */ } /* if ($this->type == 'class') $mylesson['class'] = ""; if ($this->type == 'teacher') $mylesson['teacher'] = ""; if ($this->type == 'room') $mylesson['room'] = ""; */ if ($lesson->week[$this->week-1] != '0' && !lesson_in_lessons($mylesson, $days[$day]['lessons'])) { //$days[$day]['lessons'][] = $mylesson; if (count($days[$day]['lessons']) && $days[$day]['lessons'][0]['status']) { $days[$day]['lessons'][0] = $mylesson; } else { $days[$day]['lessons'][] = $mylesson; } } //if ($lesson->week[$this->week-1] == '1') $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; if ($days[$day]['substitution']) $days[$day]['substitution'] .= ' / '; $days[$day]['substitution'] .= $substitution->text; $mylesson = array(); $mylesson['class'] = $substitution->classb; $mylesson['teacher'] = $substitution->teacherb; $mylesson['room'] = $substitution->roomb; $mylesson['subject'] = $substitution->subjectb; if ($substitution->subjectb && (($this->type == 'teacher' && $this->name == $substitution->teacherb) || // ($this->type == 'class' && $this->name == $substitution->classb) || ($this->type == 'class' && strpos($substitution->classb, $this->name)) || ($this->type == 'room' && $this->name == $substitution->roomb)) ) { $mylesson['status'] = ""; $mylesson['flag'] = 1; if (!lesson_in_lessons($mylesson, $days[$day]['lessons'])) { if (count($days[$day]['lessons']) && $days[$day]['lessons'][0]['status']) { $days[$day]['lessons'][0] = $mylesson; } else { $days[$day]['lessons'][] = $mylesson; } } //if ($lesson->week[$this->week-1] != '0') $days[$day]['lessons'][] = $mylesson; } elseif (($this->type == 'teacher' && $this->name == $substitution->teachera) || // ($this->type == 'class' && $this->name == $substitution->classa) || ($this->type == 'class' && strpos($substitution->classa, $this->name)) || ($this->type == 'room' && $this->name == $substitution->rooma)) { $mylesson['class'] = $substitution->classa; $mylesson['teacher'] = $substitution->teachera; $mylesson['room'] = $substitution->rooma; $mylesson['subject'] = $substitution->subjecta; $mylesson['status'] = 1; $mylesson['flag'] = 1; //$days[$day]['lessons'][] = $mylesson; if (!lesson_in_lessons($mylesson, $days[$day]['lessons'])) $days[$day]['lessons'][] = $mylesson; } } } //$days[$day]['lessons'] = array_unique($days[$day]['lessons']); //$days[$day]['lessons'] = super_unique($days[$day]['lessons']); //$days[$day]['lessons'] = remove_dup($days[$day]['lessons']); } $periods[$period] = array(); $periods[$period]['days'] = $days; $periods[$period]['number'] = $period+1; } $resultdata['periods'] = $periods; //echo var_dump($data); return $resultdata; } }