<?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/>.

/**
 * Prints an instance of mod_timetable.
 *
 * @package     mod_timetable
 * @copyright   2020 Raphael Dannecker <raphael.dannecker@steinbeisschule-reutlingen.de>
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require(__DIR__.'/../../config.php');
require_once(__DIR__.'/lib.php');

//use mod_timetable\timetable;


// Course_module ID, or
$id = optional_param('id', 0, PARAM_INT);

// ... module instance id.
$t  = optional_param('t', 0, PARAM_INT);

if ($id) {
    $cm             = get_coursemodule_from_id('timetable', $id, 0, false, MUST_EXIST);
    $course         = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
    $moduleinstance = $DB->get_record('timetable', array('id' => $cm->instance), '*', MUST_EXIST);
} else if ($t) {
    $moduleinstance = $DB->get_record('timetable', array('id' => $n), '*', MUST_EXIST);
    $course         = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
    $cm             = get_coursemodule_from_instance('timetable', $moduleinstance->id, $course->id, false, MUST_EXIST);
} else {
    print_error(get_string('missingidandcmid', 'mod_timetable'));
}

require_login($course, true, $cm);

$modulecontext = context_module::instance($cm->id);

/*
$event = \mod_timetable\event\course_module_viewed::create(array(
    'objectid' => $moduleinstance->id,
    'context' => $modulecontext
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('timetable', $moduleinstance);
$event->trigger();
 */

$PAGE->set_url('/mod/timetable/view.php', array('id' => $cm->id));
$PAGE->set_title(format_string($moduleinstance->name));
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_context($modulecontext);
//$PAGE->requires->jquery();
//$PAGE->requires->js_call_amd('mod_timetable/search', 'initialise', $params);
$PAGE->requires->js_call_amd('mod_timetable/search', 'initialize');

$output = $PAGE->get_renderer('mod_timetable');
echo $output->header();

//echo "<table><tr><td>Test</td></tr></table>";
$renderable = new \mod_timetable\output\searchform('Some text');
echo $output->render($renderable);

/*$ttable = new \mod_timetable\timetable('teacher','Da');

$ttable->read_db(0);
$renderable = new \mod_timetable\output\timetable($ttable);
echo $output->render($renderable);*/

echo $output->footer();