<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.

/**
 * Block timetable is defined here.
 *
 * @package     block_timetable
 * @copyright   2020 Raphael Dannecker <raphael.dannecker@steinbeisschule-reutlingen.de>
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * timetable block.
 *
 * @package    block_timetable
 * @copyright  2020 Raphael Dannecker <raphael.dannecker@steinbeisschule-reutlingen.de>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class block_timetable extends block_base {

    /**
     * Initializes class member variables.
     */
    public function init() {
        // Needed by Moodle to differentiate between blocks.
        $this->title = get_string('pluginname', 'block_timetable');
    }

    /**
     * Returns the block contents.
     *
     * @return stdClass The block contents.
     */
    public function get_content() {
        global $USER, $PAGE;

        if ($this->content !== null) {
            return $this->content;
        }

        if (empty($this->instance)) {
            $this->content = '';
            return $this->content;
        }

        $this->content = new stdClass();
        $this->content->items = array();
        $this->content->icons = array();
        $this->content->footer = '';


        //$view1 = 'subject';
	//$view2 = 'teacher';
        if ($this->page->course->idnumber && $this->page->course->shortname) {
            $class = $this->page->course->shortname;
            //$this->content->text .= "Stundenplan der Klasse $class<br>";
            $ttable = new \mod_timetable\timetable('class',$class);
        } elseif ($USER->department == "Lehrer" || $USER->department == "Lehrer_fvs") {
            $teacher = $USER->username;
            $teacher = str_replace("l_", "", $teacher);
            $teacher = str_replace("-fvs", "", $teacher);
            //$this->content->text .= "Stundenplan von ".(substr($USER->firstname,0,1)).". {$USER->lastname}<br>";
            $ttable = new \mod_timetable\timetable('teacher',$teacher);
            //$view1 = 'class';
            //$view2 = 'subject';
            //$this->content->footer = '<div class="timetable_legend"><a href="javascript:M.block_timetable.view_class()">Klassen</a>  -  <a href="javascript:M.block_timetable.view_subject()">Fächer</a>  -  <a href="javascript:M.block_timetable.view_room()">Räume</a></div>';
        } elseif ($USER->department) {
            $class = $USER->department;
            $class = str_replace("_", "/", $class);
            //$this->content->text .= "Stundenplan der Klasse $class<br>";
            $ttable = new \mod_timetable\timetable('class',$class);
        } else {
            return $this->content;
        }

        $today = new \DateTime();
        $week = $today->format("W");
        $dayofweek = $today->format('w');
        if ($dayofweek > 5 + get_config('timetable', 'saturday') || ($dayofweek == 0)) {
            $week++;
	}

        //$ttable = new \mod_timetable\timetable('teacher','Da');
        $ttable->read_db($week);
/*
        for ($i=0; $i<3; $i++) {
            $ttable->read_db($i);
        }
*/
/*
        if (!empty($this->config->text)) {
            $this->content->text = $this->config->text;
        } else {
            $text = 'Please define the content text in /blocks/timetable/block_timetable.php.';
            $this->content->text = $text;
        }
*/
        $output = $PAGE->get_renderer('mod_timetable');
        $renderable = new \mod_timetable\output\timetable($ttable);
        $this->content->text = $output->render($renderable);

        return $this->content;
    }

    /**
     * Defines configuration data.
     *
     * The function is called immediatly after init().
     */
    public function specialization() {

        // Load user defined title and make sure it's never empty.
        if (empty($this->config->title)) {
            $this->title = get_string('pluginname', 'block_timetable');
        } else {
            $this->title = $this->config->title;
        }
    }

    /**
     * Enables global configuration of the block in settings.php.
     *
     * @return bool True if the global configuration is enabled.
     */
    function has_config() {
        return true;
    }

    /**
     * Sets the applicable formats for the block.
     *
     * @return string[] Array of pages and permissions.
     */
    public function applicable_formats() {
        return array('all' => true,
                     'site' => true,
                     'site-index' => true,
                     'course-view' => true,
                     'course-view-social' => false,
                     'mod' => true,
                     'mod-quiz' => false);
    }
}