<?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("./lib.php");
require_once($CFG->libdir.'/weblib.php');
require_once($CFG->dirroot.'/local/treestudyplan/classes/reportinvite_form.php');

$add    = optional_param('add', '', PARAM_ALPHANUM);     // module name
$update = optional_param('update', 0, PARAM_INT);
$resend = optional_param('resend', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);

$systemcontext = context_system::instance();

$PAGE->set_url("/local/treestudyplan/edit-invite.php");
$PAGE->set_pagelayout('base');
$PAGE->set_context($systemcontext);

if($update > 0)
{
	$PAGE->set_title(get_string('invite_desc_edit', 'local_treestudyplan'));
	$PAGE->set_heading(get_string('invite_desc_edit', 'local_treestudyplan'));
}
else
{
	$PAGE->set_title(get_string('invite_desc_new', 'local_treestudyplan'));
	$PAGE->set_heading(get_string('invite_desc_new', 'local_treestudyplan'));
}


// Check if user has capability to manage study plan units
require_login();

print $OUTPUT->header();

if(!empty($add))
{
	$data = array(
	 'add' => 1,
	);

}
else if(!empty($update))
{
	$data = $DB->get_record("local_treestudyplan_invit", array('id' => $update));
	$data->update = $update;
	if(empty($data) || $data->user_id != $USER->id)
	{
		print_error('invalidaction');
		exit;
	}

}
else if(!empty($resend))
{
	$data = $DB->get_record("local_treestudyplan_invit", array('id' => $resend));
	$data->resend = $resend;
	if(empty($data) || $data->user_id != $USER->id)
	{
		print_error('invalidaction');
		exit;
	}

	// Do some resending of an invitation
	local_treestudyplan_send_invite($data->id);	
	
	

	redirect("$CFG->wwwroot/local/treestudyplan/invitations.php?sent={$resend}");
	print $OUTPUT->footer();
	exit;
}
else if(!empty($delete))
{
	$data = $DB->get_record("local_treestudyplan_invit", array('id' => $delete));
	$data->delete = $delete;
	if(empty($data) || $data->user_id != $USER->id)
	{
		print_error('invalidaction');
		exit;
	}

	$DB->delete_records('local_treestudyplan_invit', ['id' => $data->delete]);
		
	redirect("$CFG->wwwroot/local/treestudyplan/invitations.php");
	print $OUTPUT->footer();
	exit;
}
else {
	print_error('invalidaction');
}

$mform = new reportinvite_form();
$mform->set_data($data);

if($mform->is_cancelled())
{
	redirect("$CFG->wwwroot/local/treestudyplan/invitations.php");
}
else if ($data = $mform->get_data())
{
	if(!empty($data->update))
	{
		
		$id = $data->update;
		$data->id = $id;

		$DB->update_record('local_treestudyplan_invit', $data);

		
		redirect("$CFG->wwwroot/local/treestudyplan/invitations.php");

	}
	else if(!empty($data->add))
	{

		$id = $DB->insert_record("local_treestudyplan_invit",$data, true);
		
		// Send invitaion mail
		local_treestudyplan_send_invite($id);	

		redirect("$CFG->wwwroot/local/treestudyplan/invitations.php?sent={$id}");
	}
	else if(!empty($data->resend))
	{

	}
	else if(!empty($data->delete))
	{

	}
	else 
	{
		print_error("invaliddata");
	}

	exit;
	
}
else
{
	$data = null;
	if($unitid > 0)
	{
		
	}
	$mform->display();
}



print $OUTPUT->footer();