<?php
$page_title = "Edit Course | 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:index.php');
exit;
}//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 - Edit Course</h2>
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/php/nav-admin.php");
//opens connection to database
$db = mysqli_connect($mysql_server, $user, $pass, $database);
if(isset($_POST['submit'])){ //form has been submitted, update database with new values
$course_name = htmlentities($_POST['course_name'], ENT_QUOTES);
$course_description = htmlentities($_POST['course_description'], ENT_QUOTES);
$sql = "UPDATE ces_courses
SET course_name=\"$course_name\",
course_description=\"$course_description\",
course_active=$_POST[course_active],
level_id=$_POST[level_id]
WHERE course_id='$_POST[course_id]'";
if (mysqli_query($db, $sql)){ //updates the database
echo "<p class='alert alert-block alert-success'>Record updated successfully</p>";
echo "<a class='btn btn-lg btn-block ces-green-btn' href='index.php#".$_POST['course_id']."'>Back to Course List</a>";
}
else
print_sql_error('Error - could not update course when trying to do:', $sql);
} //if
if(isset($_GET['course_id'])){ //we can identify the record, course_id is the primary key in the database, print out the record and allow updates
$sql = mysqli_query($db, "SELECT course_id, course_name, course_description, course_active, level_id FROM ces_courses WHERE course_id=$_GET[course_id]");
while ($query = mysqli_fetch_array($sql)) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type='hidden' name='course_id' value="<?php echo $_GET['course_id']?>">
<div class="form-group">
<label for="course_name">Course Name:</label>
<input class="form-control" type='text' id='course_name' name='course_name' value="<?php echo $query['course_name']?>" maxlength='200' placeholder="Course Name" autofocus required>
</div>
<div class="form-group">
<label for="course_description">Course Description:</label>
<textarea class="form-control" rows='8' style="height:auto; resize:none;" name='course_description' wrap='soft' required placeholder="Course Description"><?php echo $query['course_description']?></textarea>
</div>
<div class="form-group">
<label for="level_description">Course Level:</label>
<?php
//generate select list of course level IDs
$level_sql = mysqli_query($db, "SELECT level_id, level_description FROM ces_levels ORDER BY level_id");
build_db_select_list($level_sql, 'level_id', $query['level_id'], 'level_description', 'class="form-control" style="max-width:100%"');
?>
</div>
<div class="form-group">
<label for='course_active'>Course Status:</label>
<div class="radio">
<label><input name='course_active' type='radio' value='1' <?php echo ($query["course_active"] == 1)?" checked":"" ?>>Active</label>
</div>
<div class="radio">
<label><input name='course_active' type='radio' value='0' <?php echo ($query["course_active"] == 0)?" checked":"" ?>>Inactive</label>
</div>
</div>
<?php } //while?>
<input type='submit' name='submit' value="Update Course" class="btn btn-lg btn-block ces-green-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>
<?php
} //if
?>
</article>
</section><!-- #middle-->
</div><!-- #wrapper -->
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/require/footer.php';?>