<?php

/**
 * File containing lesson 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 lesson
 *
 * @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 lesson {

  /** @var array the data of lesson */
  public $data;

  /**
   * Class constructor
   *
   * @param object $lesson Lesson
   */
  public function __construct($lesson) {
    $this->lessonid= $lesson->lessonid;
    $this->teacher = $lesson->teacher;
    $this->subject = $lesson->subject;
    $this->class   = $lesson->class;
    $this->room    = $lesson->room;
    $this->period  = $lesson->period;
    $this->day     = $lesson->day;
    $this->text    = "";
    $this->substitution = "";
    $this->teachera = "";
    $this->subjecta = "";
    $this->classa   = "";
    $this->rooma    = "";
    //$this->teacherb = "";
    //$this->subjectb = "";
    //$this->classb   = "";
    //$this->roomb    = "";
    $this->teacherchanged = false;
    $this->subjectchanged = false;
    $this->classchanged = false;
    $this->roomchanged = false;
    $this->reason = "";
    $this->type     = 0;
  }

  /**
   *
   * @param object $substitution Substitution
   */
  public function process_substitution($substitution) {
    if (//$this->lessonid == $substitution->lesson && 
        $this->teacher  == $substitution->teacherb && 
        $this->subject  == $substitution->subjectb && 
        preg_match("/(^|~)".preg_quote($this->class,"/")."($|~)/",  $substitution->classb) && 
        preg_match("/(^|~)".preg_quote($this->room,"/")."($|~)/",   $substitution->roomb) ) {
      // Append change info
      $this->teachera = $substitution->teachera;
      $this->subjecta = $substitution->subjecta;
      $this->classa   = $substitution->classa;
      $this->rooma    = $substitution->rooma;
      $this->teacherchanged = ($this->teacher != $this->teachera) ? true : false;
      $this->subjectchanged = ($this->subject != $this->subjecta) ? true : false;
      $this->classchanged = (!preg_match("/(^|~)".preg_quote($this->class,"/")."($|~)/" , $this->classa )) ? true : false;
      $this->roomchanged  = (!preg_match("/(^|~)".preg_quote($this->room,"/")."($|~)/"  , $this->rooma  )) ? true : false;
      if ($substitution->text) $this->text .= $substitution->text;
      $this->type = $substitution->type;
      $this->substitution = "1";
      return 1;
    } elseif (//$this->lessonid == $substitution->lesson && 
              $this->teacher  == $substitution->teachera && 
              $this->subject  == $substitution->subjecta && 
              preg_match("/(^|~)".preg_quote($this->class,"/")."($|~)/",  $substitution->classa) && 
              preg_match("/(^|~)".preg_quote($this->room,"/")."($|~)/",   $substitution->rooma) ) {
      // Append change info
      $this->teacherb = $substitution->teacherb;
      $this->subjectb = $substitution->subjectb;
      $this->classb   = $substitution->classb;
      $this->roomb    = $substitution->roomb;
      $this->teacherchanged = ($this->teacher != $this->teacherb) ? true : false;
      $this->subjectchanged = ($this->subject != $this->subjectb) ? true : false;
      $this->classchanged = (!preg_match("/(^|~)".preg_quote($this->class,"/")."($|~)/" , $this->classb )) ? true : false;
      $this->roomchanged  = (!preg_match("/(^|~)".preg_quote($this->room,"/")."($|~)/"  , $this->roomb  )) ? true : false;
      if ($substitution->text) $this->text .= $substitution->text;
      // TODO Entfall, falls teacherb!=teacher bzw. class not in classb bzw. roomb!=room TODO
      $this->type = $substitution->type;
      $this->substitution = "1";
      return 1;
    } else {
      return 0;
    }
  }

  /**
   *
   * @param object $absence Absence
   */
  public function process_absence($absence) {
    //	  echo "Process_absence: $absence->endperiod";
    if ((($absence->type == 'L' && $absence->name == $this->teacher) ||
         ($absence->type == 'K' && $absence->name == $this->class)   ||
         ($absence->type == 'R' && $absence->name == $this->room)) &&
         $absence->startperiod <= $this->period &&
         $absence->endperiod   >= $this->period) {
      $this->text .= $absence->text;
      $this->reason = $absence->reason;
      $this->startperiod = $absence->startperiod;
      $this->endperiod = $absence->endperiod;
      return 1;
    } else {
      return 0;
    }
  }

  public function integrate($lesson) {
    if($this->lessonid == $lesson->lessonid &&
      //$this->teacher  == $lesson->teacher  &&
      $this->subject  == $lesson->subject  &&
      $this->room     == $lesson->room     &&
      $this->text     == $lesson->text) {
      if ($this->class != $lesson->class) {
        $class1parts = explode("/", $this->class);
        $class2parts = explode("/", $lesson->class);
        if (count($class1parts)>1 && count($class2parts)==2) {
          $this->class .= "/".$class2parts[1];
        } else
          $this->class .= $lesson->class;
      }
      if ($this->teacher != $lesson->teacher) {
        $this->teacher .= "/".$lesson->teacher;
      }
      return true;
    } else 
      return false;
  }
}