<?php
$date = date('Y-m-d H:i:s');
require_once("../../incs/utilities.php");

$display = "";
$display2 = "";
$gen_error = "<p>Sorry. There has been an error somewhere. <br/><br/>It looks like the link used to get here is not valid.</p>";

$qc = (isset($_GET["qc"])) ? $_GET["qc"] : null; 
$pos = (isset($_GET["pos"])) ? $_GET["pos"] : null; 
$sub = (isset($_GET["s"])) ? $_GET["s"] : null; 
$ft_pt = (isset($_GET["ft"])) ? $_GET["ft"] : null; 
$ay = (isset($_GET["ay"])) ? $_GET["ay"] : null; 
$oo = (isset($_GET["oo"])) ? $_GET["oo"] : null; //oo = On Offer. If 'N' then display 'Not on Offer Message Only'
$order = (isset($_GET["order"])) ? $_GET["order"] : null; 

if((empty($_GET)) || ($qc == "") || ($qc == "x") || ($pos == "") || ($pos == "x") || ($ay == "") || ($ay == "x")){   
	echo $gen_error;exit();
}
if (!isset($qc)||empty($qc)||strlen($qc)>7||!ctype_alpha($qc)){
	echo $gen_error;exit;
}
if(!ctype_digit($pos)||strlen($pos)>1||strlen($ay)!=4||strlen($sub)>7||strlen($order)>3){
	echo $gen_error;exit;
}
if(isset($ft_pt)&&($ft_pt!='f'&&$ft_pt!='p'&&$ft_pt!='x')){
	echo $gen_error;exit;
}
if(isset($oo) && $oo != 'n'){
	echo $gen_error;exit;
}
if($oo == 'n'){
    $display = '<h2><u>NOTE:</u> This programme is not on offer for the academic year you are looking at currently.</h2>';
}

$var = httpCurlWithPW(API_URL."curriculum?ft=".$ft_pt."&ay=".$ay."&pos=".$pos."&qc=".$qc."&order=".$order, "GET");	

if($var == '204'){
	$json_output = [];
} else {
	$json_output = json_decode($var, true);
}
 
if(!empty($json_output)){
	
$display2 .= "<table id=\"hor-minimalist-b\">
	<thead>
    <tr>
	<th style=\"cursor:pointer\" onclick=\"location.href='https://reg.ucc.ie/curriculum/modules/calendar_resources/calendar_config/find.php?qc=".$qc."&amp;pos=".$pos."&amp;s=".$sub."&amp;ft=".$ft_pt."&amp;ay=".$ay."&amp;order=mc'\" scope=\"col\">Module Code <span class=\"glyphicon glyphicon-sort\"></span></th>
	<th style=\"cursor:pointer\" onclick=\"location.href='https://reg.ucc.ie/curriculum/modules/calendar_resources/calendar_config/find.php?qc=".$qc."&amp;pos=".$pos."&amp;s=".$sub."&amp;ft=".$ft_pt."&amp;ay=".$ay."&amp;order=mt'\" scope=\"col\">Module Title <span class=\"glyphicon glyphicon-sort\"></span></th>
	<th style=\"cursor:pointer\" onclick=\"location.href='https://reg.ucc.ie/curriculum/modules/calendar_resources/calendar_config/find.php?qc=".$qc."&amp;pos=".$pos."&amp;s=".$sub."&amp;ft=".$ft_pt."&amp;ay=".$ay."&amp;order=sem'\" scope=\"col\">Semester <span class=\"glyphicon glyphicon-sort\"></span></th>
	<th style=\"cursor:pointer\" onclick=\"location.href='https://reg.ucc.ie/curriculum/modules/calendar_resources/calendar_config/find.php?qc=".$qc."&amp;pos=".$pos."&amp;s=".$sub."&amp;ft=".$ft_pt."&amp;ay=".$ay."&amp;order=ce'\" scope=\"col\" style=\"text-align: center\">Core/Elective <span class=\"glyphicon glyphicon-sort\"></span></th>
	<th style=\"cursor:pointer\" onclick=\"location.href='https://reg.ucc.ie/curriculum/modules/calendar_resources/calendar_config/find.php?qc=".$qc."&amp;pos=".$pos."&amp;s=".$sub."&amp;ft=".$ft_pt."&amp;ay=".$ay."&amp;order=cw'\" scope=\"col\" style=\"text-align: center\">Credit Weighting <span class=\"glyphicon glyphicon-sort\"></span></th>
    </tr>
	</thead>";

	foreach ($json_output as $key => $value) {
		$ay = $value["acad_year"];
 		$qc = $value["qual_code"];
 		$qd = $value["qual_desc"];
 		$pos = $value["period_study"];
 		$sub = $value["subject"];
 		$subtitle = $value["subject_title"];
 		$ft_pt = $value["ft_pt"];
 		$mc = $value["module_code"];
 		$mt = $value["module_title"];
 		$sem = $value["semesters"];
 		$ce = $value["core_elective"];
			if(($ce == "Y") || ($ce == "y")){
			$ce = "Core";
			}
			elseif(($ce == "N") || $ce == "n"){
			$ce = "Elective";
			}
 		$cw = $value["credit_weight"];
						//echo $module."<br/>";
						
		$display2 .= "<tr><td><strong>".$mc."</strong></td>
		<td>".$mt."</td>
		<td>".$sem."</td>
		<td style=\"text-align: center\">".$ce."</td>
		<td style=\"text-align: center\">".$cw."</td></tr>
		";
	}
					
$display .= "<h3>Academic Year: ".$ay."<br/>Programme: ".$qd."<br/>Year of Study: ".$pos."</h3>";
$display .= "<a style=\"float: right;\" href=\"#\" onclick=\"window.print();return false;\"><img src=\"./images/print.png\"/></a><br/>";

} else {
	$display .= "<p>There is no data available for this selection.</p>";
}

?>

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>UCC College Calendar - Semester Information</title>
	<style>
		body {text-align: left; padding: 20px; margin-top: 20px; max-width: 90%; margin-right: auto; margin-left: auto; line-height: 100%; color:#265a87;}
		img, a {border: 0px;}
		#hor-minimalist-b{font-size:14px;background:#fff;width:100%;border-collapse:collapse;text-align:left;}
		#hor-minimalist-b th{text-align: left;font-size:14px;font-weight:bold;color:#265a87;border-bottom:2px solid #6678b1;padding:10px 8px;}
		#hor-minimalist-b td{text-align: left;border-bottom:1px solid #ccc; color:#265a87;padding:6px 8px;}
		#hor-minimalist-b tbody tr:hover td{color:#265a87;}
	</style>
	<link href="./css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <p><?php echo $display; ?></p>
  <br/>
	<?php echo $display2; ?>
	</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</html>