<?php
/*
  CourseMS
  https://sourceforge.net/projects/coursems

  Copyright (c) 2007 Jacques Malan

  This version of the code is released under the GNU General Public License
*/
	include('includes/application_top.php');
	if (!isset($_SESSION['learner'])) {
		header("Location: login.php");
	}
	if(time() - $_SESSION['timestamp'] > Session_time_out) { //subtract new timestamp from the old one
		echo"<script>alert('Du har automatiskt loggats ut p.g.a för lång inaktivitet.');</script>";
		unset($_SESSION['learner'], $_SESSION['timestamp']);
		$_SESSION['logged_in'] = false;
		header("Location: login.php"); //redirect to index.php
		exit;
	} 
	else {
		$_SESSION['timestamp'] = time(); //set new timestamp
	}
	
	$user = $_SESSION['learner'];
	$user->set_profile();
//print_r(($user));	
	include(INCLUDES . 'header.php');
	include(INCLUDES . 'front_header.php');
	
	if (isset($_POST['UppdateraMeddelande'])){
		$id = $_GET['id'];
		$sql_array = array("id" => $id,
						   "Message" => $_POST['Message']);

		tep_db_perform(TABLE_REGISTRATIONS, $sql_array, 'update', "id='$id'");
		header("Location: view_profile.php");
	}


	// algorith to determine this user's courses

	$schedule_array = array();
	$query = "select ca.id, ca.course_id, ca.start_date, ca.dates, ca.start_time, ca.end_time, ca.resources, ca.reg_online, ca.lockDate, ca.lock_time, c.center, c.type, c.name, c.kursavgift, r.id'registration_id', r.status, r.notering, r.Message from calendar as ca left join registrations as r on (r.calendar_id = ca.id) left join courses as c on (ca.course_id = c.id) where r.user_id = '$user->id' order by ca.start_date, c.center, c.type, ca.start_time, c.name";
	$result = tep_db_query($query);
	$courses_error = false;
	if (tep_db_num_rows($result) > 0) {
		while ($row = tep_db_fetch_array($result)) {
			if (date("Y-m-d", mktime(0, 0, 0, substr($row['start_date'],5,2), substr($row['start_date'],-2,2), substr($row['start_date'],0,4))) >= date("Y-m-d")){
				$past = false;
			}
			else{
				$past = true;
			}

			//if ((substr($row['start_date'],5,2) == date("m") && (substr($row['start_date'],-2,2)+$row['duration']-1) >= date("d")) || (substr($row['start_date'],5,2) > date("m") && substr($row['start_date'],0,4) >= date("Y"))) {
				$schedule_array[] = array('id' => $row['id'],
							  'date' => $row['start_date'],
							  'course_id' => $row['course_id'],
							  'center' => $row['center'],
							  'type' => $row['type'],
							  'name' => $row['name'],
							  'dates' => unserialize($row['dates']),
							  'start_time' => $row['start_time'],
							  'end_time' => $row['end_time'],
							  'resources' => unserialize($row['resources']),
							  'reg_online' => $row['reg_online'],
							  'status' => $row['status'],
							  'registration_id' => $row['registration_id'],
							  'message' => $row['Message'],
							  'lockDate' => $row['lockDate'],
							  'lock_time' => $row['lock_time'],
							  'kursavgift' => $row['kursavgift'],
							  'notering' => $row['notering'],
							  'past' => $past);
		}
	}
	else
	{
		$courses_error = true; //no courses have been registered for.
	}


	// template for page build

	?>
<style>
table#t01, td#t01{
    border: 1px solid black;
}
</style>
	<TABLE width="100%" cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#ffffff">
		<tr>
			<td colspan=3><IMG height=10 src="images/blank.gif" width=1></td>
		</tr>
		<tr>
			<td width="3%" rowspan=2><IMG height=45 src="images/line.gif" width=24></td>
			<td class="title" height="27" colspan=3>&nbsp;Användarehanterare</td>
		</tr>
		<tr>
			<td width="2%"><IMG src="images/left.gif"></td>
			<td height="18" class="subtitle" valign="bottom" width="12%">Dina uppgifter</td>
			<td width="83%"><IMG src="images/right.gif"></td>
		</tr>
		<tr>
			<td colspan="3">&nbsp;</td>
		</tr>
	</TABLE>
	<TABLE width="100%" cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#ffffff">
		<tr>
			<td align="middle">
				<!-- main content starts here -->
				<table width="100%" align="center" cellspacing="1" cellpadding="3">
					<tr>
						<td>
							<table width='100%'>
								<!-- CONTENT -->
								<tr>
									<td>
	<!--									<table width="100%">
											<tr>
												<td class="right">
													<span class="subtitle2"><?php echo $user->username; ?>&nbsp;&nbsp;&nbsp;</span>(<a href="edit_profile.php">Ändra uppgifter</a>)
												</td>
												<td class="left">
													<?php
													if ($user->instructor == '1') {
														$instructor = new instructor(tep_get_username($_GET['id']));
														$instructor->instructor_attributes();
														?><img src="<?php echo RELATIVE_UPLOAD_DIR."/".$instructor->photo;?>" height="90" width="70"><?php
													}
													else
														echo '&nbsp';
													?>
												</td>
											</tr>
										</table>
	-->								</td>
								</tr>
								<tr>
									<td>

									</td>
								</tr>
								<tr>
									<td align="left">
										<h4>Kontakt uppgifter: (<a href="edit_profile.php"> Klicka här för att ändra uppgifter</a>)</h4>
									</td>
								</tr>
								<tr>
									<td>
										<table width="100%">
											<tr>
												<td width="30%" class="left">
													Användarnamn:
												</td>
												<td class="right">
													<?php echo $user->username; ?>
												</td>
											</tr>
											
											<tr>
												<td width="30%" class="left">
													Namn:
												</td>
												<td class="right">
													<?php echo $user->firstname.' '.$user->lastname; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Födelseår:
												</td>
												<td class="right">
													<?php echo $user->date_of_birth; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Alias:
												</td>
												<td class="right">
													<?php echo $user->alias; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Telefon hem:
												</td>
												<td class="right">
													<?php echo $user->home_telephone; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Telefon mobil:
												</td>
												<td class="right">
													<?php echo $user->mobile_telephone; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Gatuadress:
												</td>
												<td class="right">
													<?php echo $user->address_line1; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Postnummer:
												</td>
												<td class="right">
													<?php echo $user->postcode; ?>
												</td>
											</tr>
											
											<tr>
												<td class="left">
													Postort:
												</td>
												<td class="right">
													<?php echo $user->city; ?>
												</td>
											</tr>
											
										</table>
									</td>
								</tr>
								<tr>
					
								<!-- The schedule of this user -->
								<tr>
									<td>
										<table width="100%">
											<tr>
												<td class="hright">
													<h2><center>Kurser som du "<?php echo $user->firstname,' ',$user->lastname; ?>" är anmäld till</center></h2>
												</td>
											</tr>
										</table>
									</td>
								</tr>
								<tr>
										
											<?php

											foreach ($schedule_array as $x=>$y) {
												$kursen_tids_låst = false;
												$nuDatum = Date('Y-m-d'); $nutid = Date('H:i');
												$låsdatum = $y['lockDate']; $låstid = $y['lock_time'];
												if(($nuDatum > $låsdatum) || (($nuDatum == $låsdatum) &&($nutid>=$låstid)) ){
													$kursen_tids_låst = true;
												}
//echo'nutid:',$nuDatum,' ',$nutid,'  låsdatum:',$låsdatum,'  låstid:',$låstid,'  Kursen är tidslås:',$kursen_tids_låst,'<br>';
												?>
												<table id="t01" width="100%">
												<tr>
												<td class="hright">
													Kurs
												</td>
												<td class="hright">
													Datum, Tid
												</td>
												<td class="hright">
													Avgift
												</td>
												<td class="hright">
													Plats
												</td>
												<td class="hright">
													Status
												</td>
												<td class="hright">
													Kursinfo
												</td>
												<td class="hright">
													Avboka
												</td>
											</tr>
												<tr>
													<td class="right"><?php echo '<a href="view_course.php?id='.$y['course_id'].'">'.$y['name']; ?>	</a></td>
													<td class="right"><span class="heading"><?php if ($y['past']) { echo '<font color="#D0103A">'; } echo $y['date'],'&nbsp&nbsp',substr($y['start_time'],0,5),'-',substr($y['end_time'],0,5); ?></td>
													<td class="right"><?php echo $y['kursavgift']; ?></td>
													<td class="right"><?php echo tep_get_name(TABLE_CENTERS, $y['center']); ?> </span></td>
													<td class="right"><?php
														if ($y['past'])
															echo 'Färdig';
														else
															echo tep_get_name(TABLE_STATUS, $y['status']);
														?>
													<td class="right">
														<a href="course_registrations.php?id=<?php echo $y['id']; ?>">Visa information</a>
													</td>
													<td class="right">
													<?php if($kursen_tids_låst==false){?>
														<a href="course_cancel.php?id=<?php echo $y['id'],'&registration_id=',$y['registration_id']; ?>" onclick="return confirm('Vill du verkligen avboka?')">Avboka</a> 
													<?php }
													else{
														echo'Anmälan är låst';
													} ?>
													</td>	
												</tr>
												<tr>
													<td class="right" colspan="1"> Meddelande: </td>
													<td class="right" colspan="6"> 
													<?php 
																if($kursen_tids_låst==false){
																	echo '<form action=view_my_courses.php?id=',$y['registration_id'],' method=post>';
																	echo '<textarea name="Message" cols="90" class="textbox1" placeholder="Om du vill lämna ett meddelande så kan du göra det här!">',$y['message'],'</textarea>';
																	echo '<input type=submit name=UppdateraMeddelande value=Uppdatera></form>'; 
																}
																else{
																	echo '<textarea readonly name="Message" cols="90"  class="textbox1" >',$y['message'],'</textarea>'; 
																	echo '<input type=submit name=UppdateraMeddelande value=Låst>';
																}
															?>
													</td>	
												</tr>
												<tr>
													<td class="right" colspan="1"> Notering från Admin:</td>
													<td class="right" colspan="6"><?php echo $y['notering']; ?></td>
												</tr>
				
												</table>
												<br>
													<?php
											}

											if ($courses_error) { ?>
												<tr>
													<td colspan="8" class="right">
														<font color="red">Du är inte anmäld till någon kurs.</font>
													</td>
												</tr>
												<?php
											}
											?>
										<!-- if user is an instructor list courses they are to instruct on here -->
										<?php
										if ($user->instructor==1) {
										?>



											<table width="100%">
												<tr>
													<td colspan="5" class="hright">Instructor Schedule</td>
												</tr>
												<tr>
													<td class="hright">Course ID</td>
													<td class="hright">Dates</td>
													<td class="hright">Course</td>
													<td class="hright">Other Instructors</td>
													<td class="hright">Cancelled</td>
												</tr>
												<?php
												//add courses instructor instructs on in here
												$instructor->get_instructor_courses();

												foreach ($instructor->courses as $a=>$b) {

													$scheduled = new scheduled(tep_get_course_id($b['id']));
													$scheduled->set_calendar_vars($b['id']);
													if ($scheduled->cancelled) {
														$class='h3right';
													}
													else
													{
														$class='right';
													}
													$dates = unserialize($b['dates']);
													?>
													<tr>
														<td class="<?php echo $class; ?>"><?php echo $b['id']; ?></td>
														<td class="<?php echo $class; ?>">
														<?php
															foreach ($dates as $c=>$d) {
																echo tep_swap_dates($d).'<br>';
															}
														?>
														</td>
														<td class="<?php echo $class; ?>"><a href="course_registrations.php?id=<?php echo $b['id']; ?>"><?php echo tep_get_course_name(tep_get_course_id($b['id'])); ?></a></td>
														<td class="<?php echo $class; ?>"><?php
															// TODO: Add instructor list here
														?>
														</td>
														<td class=<?php echo $class; ?>>
																<?php echo $scheduled->cancelled; ?>
														</td>

													</tr>
													<?php
												}
												?>

											</table>
										<?php
										}
										?>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
				<br><br>
				<table width="96%" align="center" cellspacing="0" cellpadding="2" border="0">
					<tr>
						<td colspan=3 align=right></td>
					</tr>
				</table>
				<br><br>
			</td>
		</tr>
	</TABLE>
<?php
	include(INCLUDES . 'rightmenu.php');
	include(INCLUDES . 'footer.php');
?>