<?php
// This file is part of the Studyplan plugin for Moodle
//
// 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/>.
/**
 * Entry point for students to manage invitations to view their study plan
 * @package    local_treestudyplan
 * @copyright  2023 P.M. Kuipers
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once("../../config.php");

require_once($CFG->libdir.'/weblib.php');
require_once($CFG->dirroot.'/grade/querylib.php');

use local_treestudyplan;

$invitedurl = "/local/treestudyplan/invited.php";

$systemcontext = context_system::instance();

$PAGE->set_url("/local/treestudyplan/invitations.php", array());
require_login();

$PAGE->set_pagelayout('base');
$PAGE->set_context($systemcontext);
$PAGE->set_title(get_string('manage_invites', 'local_treestudyplan'));
$PAGE->set_heading(get_string('manage_invites', 'local_treestudyplan'));

// Load javascripts.
$PAGE->requires->js_call_amd('local_treestudyplan/page-invitemanager', 'init');
$PAGE->requires->js_call_amd('local_treestudyplan/buttonlinks', 'init');

// Retrieve list of courses that the student is enrolled in.
$sent = optional_param('sent', '', PARAM_INT);
if (!empty($sent)) {
    $invite = $DB->get_record('local_treestudyplan_invit', array('id' => $sent));
    \core\notification::success(get_string('invite_resent_msg', 'local_treestudyplan', $invite));

};

print $OUTPUT->header();

print "<p>".get_string('invite_description', 'local_treestudyplan')."</p>";

$invites = $DB->get_records('local_treestudyplan_invit', array('user_id' => $USER->id));

print "<h3>".get_string('invite_tablecaption', 'local_treestudyplan')."</h3>";
print "<table class='m-manage_invites'>";
print "<thead>";
print "<th>".get_string('invite_name', 'local_treestudyplan')."</th>";
print "<th>".get_string('invite_email', 'local_treestudyplan')."</th>";
print "<th>".get_string('invite_date', 'local_treestudyplan')."</th>";
print "<th>&nbsp;</th>";
print "</thead>";

print "<tbody>";
if (count($invites) > 0) {
    foreach ($invites as $invite) {
        $testlink  = $invitedurl."?key={$invite->invitekey}";
        print "<tr data-id='{$invite->id}'>";
        print "<td data-field='name'>{$invite->name}</td>";
        print "<td data-field='email'>{$invite->email}</td>";
        print "<td data-field='date'>".userdate($invite->idate, "%x")."</td>";
        print "<td data-field='control'>";

        print "<a class='m-action-view ' href='{$testlink}' title='"
              .get_string('invite_tooltip_testlink', 'local_treestudyplan')."'><i class='fa fa-eye'></i></a>";

        print "<a class='m-action-resend m-action-confirm'";
        print " data-confirmtext='".get_string('invite_confirm_resend', 'local_treestudyplan', $invite->name)."'";
        print " data-confirmbtn='".get_string('send', 'local_treestudyplan')."'";
        print " href='#' data-actionhref='edit-invite.php?resend={$invite->id}' title='"
                .get_string('invite_tooltip_resend', 'local_treestudyplan')."'";
        print " ><i class='fa fa-envelope'></i></a>";

        print "<a href='edit-invite.php?update={$invite->id}'><i class='fa fa-pencil' title='"
                .get_string('invite_tooltip_edit', 'local_treestudyplan')."'></i></a>";
        print "<a class='m-action-delete m-action-confirm'";
        print " data-confirmtext='".get_string('invite_confirm_delete', 'local_treestudyplan', $invite->name)."'";
        print " data-confirmbtn='".get_string('delete')."'";
        print " href='#' data-actionhref='edit-invite.php?delete={$invite->id}' title='"
                .get_string('invite_tooltip_delete', 'local_treestudyplan')."'";
        print " ><i class='fa fa-trash'></i></a>";

        print "</td>";
    }
} else {
    print "<tr><td colspan='6'>".get_string('invite_table_empty', 'local_treestudyplan')."</td></tr>";
}
print "</tbody></table>";

print "<a class='btn btn-info' href='/local/treestudyplan/edit-invite.php?add=true' class='btn btn-primary' id='add_invite'>";
print "<i class='fa fa-plus'></i> ".get_string('invite_button_new', 'local_treestudyplan')."</a>";

print $OUTPUT->footer();