<?php
if(isset($_SERVER['SCRIPT_FILENAME']))
{
	// If SCRIPT_FILENAME is set, use that so the symlinked directories the developmen environment uses are handled correctly
	$root = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
	error_log("Using {$root}/config.php");
	require_once($root."/config.php");
}
else
{
	// If not, assume the cwd is not symlinked and proceed as we are used to
	require_once("../../config.php");
}

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

use local_treestudyplan;

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

//admin_externalpage_setup('major');
$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  = $INVITED_URL."?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->date, "%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'><i class='fa fa-plus'></i> ".get_string('invite_button_new', 'local_treestudyplan')."</a>";





print $OUTPUT->footer();