<?php
$page_title = "Add Session | CES Admin";
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/common_functions.php"); //common functions in the course registartion system
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/config.php"); //holds global config variables
check_if_authenticated('admin');//check if user is logged in
if(isset($_POST['cancel']) || isset($_POST['back'])){ //form has been canceled
header('location:../courses/');
exit;
}//if
$db = mysqli_connect($mysql_server, $user, $pass, $database);
if(isset($_POST['submit'])){
//convert the timestamps to 24 hour clock as that is what mysql needs
$start_dt_hour = convert_hour($_POST['start_dt_hour'], $_POST['start_dt_meridiem']);
$end_dt_hour = convert_hour($_POST['end_dt_hour'], $_POST['end_dt_meridiem']);
$telehealth = (isset($_POST['session_type']) && $_POST['session_type'] == 'telehealth')? 1 : 0;
$online = (isset($_POST['session_type']) && $_POST['session_type'] == 'online')? 1 : 0;
$webex = (isset($_POST['session_type']) && $_POST['session_type'] == 'webex')? 1 : 0;
$location = (isset($_POST['location_id']))? $_POST['location_id'] : NULL;
if($online == 1 || $webex == 0){
$location = NULL;
}
//now combine all the date fields into one timestamp
$start_dt = $_POST['start_dt_year'] . "-" . $_POST['start_dt_month'] . "-" . $_POST['start_dt_day'] . " " . $start_dt_hour . ":" . $_POST['start_dt_minute'] . ":00";
$end_dt = $_POST['end_dt_year'] . "-" . $_POST['end_dt_month'] . "-" . $_POST['end_dt_day'] . " " . $end_dt_hour . ":" . $_POST['end_dt_minute'] . ":00";
if(strtotime($start_dt) >= strtotime($end_dt)){
$error_1 = true;
}//if
elseif(!checkdate($_POST['start_dt_month'], $_POST['start_dt_day'], $_POST['start_dt_year'])){
$error_2 = true;
}//elseif
elseif(!checkdate($_POST['end_dt_month'], $_POST['end_dt_day'], $_POST['end_dt_year'])){
$error_3 = true;
}//elseif
else{
$location_id = (isset($_POST['location_id']) && $online == 0 && $webex == 0)? $_POST['location_id'] : 'NULL';
$url = (isset($_POST['url']) && $online == 1)? '"'.$_POST['url'].'"' : 'NULL';
$webex_meeting_key = (isset($_POST['webex_meeting_key']) && $webex == 1)? str_replace(" ", "", trim($_POST['webex_meeting_key'])) : 'NULL';
$webex_url = (isset($_POST['webex_url']) && $webex == 1)? '"'.$_POST['webex_url'].'"' : 'NULL';
$webex_password = (isset($_POST['webex_password']) && $webex == 1)? '"'.$_POST['webex_password'].'"' : 'NULL';
$ces_sessions_insert = "INSERT INTO ces_sessions (course_id, start_dt, end_dt, telehealth, location_id, display, online, url, webex, webex_meeting_key, webex_url, webex_password) VALUES ('$_POST[course_id]', '$start_dt', '$end_dt', '$telehealth', $location_id, '$_POST[display]', '$online', $url, '$webex', $webex_meeting_key, $webex_url, $webex_password)";
if(mysqli_query($db, $ces_sessions_insert)){ //if insert into the first table is successful continue
$last_session_id = mysqli_fetch_row(mysqli_query($db, "SELECT LAST_INSERT_ID()")); //get the primary key of the session that we just inserted so we can use it in the next insert
header("location:edit.php?session_id=".$last_session_id[0]."&m=success");
exit;
}//if
else{ //else the update did not happen so output an error
$sql_error = true;
}//end else
}//else
}//if
include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/php/top-admin.php"); //get the HTML heading common to all pages in the CES admin module
?>
<article class="ces-admin">
<h2>CES Admin - Add Session</h2>
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/php/nav-admin.php");
//opens connection to database
$course_id = "";
if(isset($_GET['course_id']) || isset($_POST['course_id'])){
if(isset($_GET['course_id'])){
$course_id = $_GET['course_id'];
}
elseif(isset($_POST['course_id'])){
$course_id = $_POST['course_id'];
}
else{
$course_id = false;
}
$course_title_row = mysqli_fetch_row(mysqli_query($db, "SELECT course_name FROM ces_courses WHERE course_id = $course_id"));
$course_title = $course_title_row[0];
echo "<h5>Course: ".$course_title."</h5>";
}
if(isset($error_1)){
echo "<p class='alert alter-error'>Error - Start Date (" . $start_dt . ") can not be greater than or equal to the End Date (" . $end_dt . ").<br>
Please correct the dates</p>";
}
if(isset($error_2)){
echo "<p class='alert alter-error'>Error - The start date (" . $start_dt . ") you entered is not a valid date.<br>";
echo "Please correct the date.</p>";
}
if(isset($error_3)){
echo "<p class='alert alter-error'>Error - The end date (" . $end_dt . ") you entered is not a valid date.<br>";
echo "Please correct the date.</p>";
}
if(isset($sql_error)){
echo "<p class='alert alter-error'>";
print_sql_error('Error - session submission failed when trying to do:', $ces_sessions_insert);
echo "</p>";
}
echo "\n<form method='post' action='" . $_SERVER['PHP_SELF'] . "'>";
echo "<input type='hidden' name='course_id' value=" . $course_id . ">"; //so we can keep track of which record we're modifying
?>
<div class="form-group">
<label>Start Date:</label>
<div class="ces-session-start-date">
<?php //output the date select boxes
month_name_select('start_dt_month', date('m'));
days_in_month_select('start_dt_day', date('j'));
year_select('start_dt_year', date('Y'));
echo " ";
hours_in_day_select('start_dt_hour', date('g'));
echo ":";
minutes_in_hour_select('start_dt_minute', '');
meridiem_select('start_dt_meridiem', date('A'));
?>
</div>
</div>
<div class="form-group">
<label>End Date:</label>
<div class="ces-session-end-date">
<?php //output the date select boxes
month_name_select('end_dt_month', date('m'));
days_in_month_select('end_dt_day', date('j'));
year_select('end_dt_year', date('Y'));
echo " ";
hours_in_day_select('end_dt_hour', date('g'));
echo ":";
minutes_in_hour_select('end_dt_minute', '');
meridiem_select('end_dt_meridiem', date('A'));
?>
</div>
</div>
<div class="form-group">
<label>Session Type:</label>
<select class='form-control width-25' name="session_type" id="session-type" onchange="showInput()">
<option value="in-person" selected>In Person</option>
<option value="telehealth">Telehealth</option>
<option value="online">Online</option>
<option value="webex">WebEx</option>
</select>
</div>
<div class="form-group" id="location">
<label>Location:</label>
<?php
$location_sql = mysqli_query($db, "SELECT location_id, location_name FROM ces_locations ORDER BY location_name");
//output the query results in select list format
build_db_select_list($location_sql, 'location_id', '', 'location_name', 'id="location-select"');
?>
</div>
<div class="form-group" id="video-url">
<label for='url'>Video Link: </label>
<small class="help">(Please ensure that the videos are in WMV format)</small>
<input class="form-control" type='text' name='url' id="video-link">
</div>
<div id="meeting-key">
<div class="form-group">
<label for='webex_url'>Event address for attendees: </label>
<input class="form-control" type='url' name='webex_url' id='webex-url'>
</div>
<div class="form-group">
<label for='webex_meeting_key'>Event number: </label>
<input class="form-control width-20" type='text' name='webex_meeting_key' id='webex-meeting-key'>
</div>
<div class="form-group">
<label for='webex_password'>Event password: </label>
<input class="form-control width-20" type='text' name='webex_password' id='webex-password'>
</div>
</div>
<div class="checkbox form-group">
<label for='display' title="Display session on the current session list.">Show Session: <input name='display' type='checkbox' checked value='1'></label>
</div>
<input type='submit' name='submit' value="Add Session" class="btn btn-lg btn-block ces-green-btn">
<input type='reset' name='reset' value="Reset Form" class="btn btn-lg btn-block ces-grey-btn">
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type='submit' name='cancel' value="Cancel" class="btn btn-lg btn-block ces-orange-btn">
</form>
</article>
</section><!-- #middle-->
</div><!-- #wrapper -->
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/require/footer.php';?>
<script type="text/javascript">
document.getElementById('location').style.display = 'block';
document.getElementById('video-url').style.display = 'none';
document.getElementById('meeting-key').style.display = 'none';
document.getElementById('location-select').selectedIndex = '-1';
function showInput() {
if (document.getElementById('session-type').selectedIndex == "0") {
document.getElementById('video-link').value = '';
document.getElementById('webex-meeting-key').value = '';
document.getElementById('location').style.display = 'block';
document.getElementById('video-url').style.display = 'none';
document.getElementById('meeting-key').style.display = 'none';
}
else if (document.getElementById('session-type').selectedIndex == "1") {
document.getElementById('video-link').value = '';
document.getElementById('webex-meeting-key').value = '';
document.getElementById('location').style.display = 'block';
document.getElementById('video-url').style.display = 'none';
document.getElementById('meeting-key').style.display = 'none';
}
else if (document.getElementById('session-type').selectedIndex == "2") {
document.getElementById('location-select').selectedIndex = '-1';
document.getElementById('video-link').value = '';
document.getElementById('webex-meeting-key').value = '';
document.getElementById('location').style.display = 'none';
document.getElementById('video-url').style.display = 'block';
document.getElementById('meeting-key').style.display = 'none';
}
else if (document.getElementById('session-type').selectedIndex == "3") {
document.getElementById('location-select').selectedIndex = '-1';
document.getElementById('video-link').value = '';
document.getElementById('webex-meeting-key').value = '';
document.getElementById('location').style.display = 'none';
document.getElementById('video-url').style.display = 'none';
document.getElementById('meeting-key').style.display = 'block';
}
}
</script>