<?php
/**********************************************************************
	Copyright (C) Evolve Techniologies
	Author : Chathuri Gunawardana
	Email : chathuri@evolve-sl.com
	Company : Evolve Technologies(pvt)Ltd
***********************************************************************/

function add_courses($course_full_name, /*$course_short_name,*/ $total_duration /*, $total_course_fee*/)
{
	/*$sql = "INSERT INTO ".TB_PREF."courses (course_full_name, course_short_name, total_duration, total_course_fee)VALUES (
		".db_escape($course_full_name).",
		".db_escape($course_short_name).",
		".db_escape($total_duration).",
		".db_escape($total_course_fee)." )";
	db_query($sql, "could not add course");	*/	

	$sql = "INSERT INTO ".TB_PREF."courses (course_full_name, total_duration )VALUES (
		".db_escape($course_full_name).",
		".db_escape($total_duration)." )";
	
	db_query($sql, "could not add course");	
	
	$course_id = db_insert_id();
	
	$sql2_catagories = "INSERT INTO ".TB_PREF."main_category(main_cat_name,course_id,isAdminCat )VALUES (
		".db_escape($course_full_name).",
		".db_escape($course_id).",
		1 )";
		
	db_query($sql2_catagories, "could not add course to catagories table");	
	
}

function update_courses($id, $course_full_name, /*$course_short_name,*/ $total_duration/*,$total_course_fee*/)
{
	/*$sql = "UPDATE ".TB_PREF."courses SET 
	course_full_name = ".db_escape($course_full_name).",
	course_short_name =".db_escape($course_short_name).", 
	total_duration=".db_escape($total_duration).",
	total_course_fee=".db_escape($total_course_fee)."
	WHERE id = ".db_escape($id);*/
	
	$sql = "UPDATE ".TB_PREF."courses SET 
	course_full_name = ".db_escape($course_full_name).",
	total_duration=".db_escape($total_duration)."
	WHERE id = ".db_escape($id);
	//print_r($sql);
	db_query($sql, "could not update courses");
	
	$sql2 = "SELECT id FROM ".TB_PREF."courses 
	WHERE course_full_name = ".db_escape($course_full_name);
	
	$result = db_query($sql2, "could not update courses");
	$row = db_fetch_row($result);
	//print_r($row[0]);

	$updated_course_id = $row[0];
	
	$sql2_catagories = "UPDATE ".TB_PREF."main_category SET
	main_cat_name = ".db_escape($course_full_name)."
	WHERE course_id = ".db_escape($updated_course_id);		
	
	db_query($sql2_catagories, "could not update course in catagories table");		
}

function get_all_courses($all=false)
{
	$sql = "SELECT * FROM ".TB_PREF."courses";
	if (!$all)
		$sql .= " WHERE !inactive";
	
	return db_query($sql, "could not get all courses");
}

function get_course_name($id)
{
	$sql = "SELECT course_full_name FROM ".TB_PREF."courses WHERE id=".db_escape($id);
	
	$result = db_query($sql, "could not get course full name");
	
	$row = db_fetch_row($result);
	return $row[0];
}

function get_course_id($cname)
{
	$sql = "SELECT id FROM ".TB_PREF."courses WHERE course_full_name=".db_escape($cname);
	
	$result = db_query($sql, "could not get course id");
	
	$row = db_fetch_row($result);
	return $row[0];
}

function delete_courses($id)
{

	$sql="DELETE FROM ".TB_PREF."courses WHERE id=".db_escape($id);
	db_query($sql,"The Courses record could not be deleted");
	
	$sql2="DELETE FROM ".TB_PREF."main_category WHERE course_id=".db_escape($id);
	db_query($sql2,"The Courses record in Main Catagory Table could not be deleted");

}

function get_courses($id)
{
	$sql = "SELECT * FROM ".TB_PREF."courses WHERE id=".db_escape($id);
	
	$result = db_query($sql, "could not get courses");
	
	return db_fetch($result);
}

function get_course_sn_from_fn($fn)
{
	$sql = "SELECT course_short_name FROM ".TB_PREF."courses WHERE course_full_name=".db_escape($fn);
	
	$result = db_query($sql, "could not get courses");
	
	return db_fetch($result);
}

function get_mainCat_cat_id($selectdId)
{
	$sql = "SELECT main_category_id FROM ".TB_PREF."main_category WHERE course_id=".db_escape($selectdId);
	
	$result = db_query($sql,"main category id could not be retreived");
	
	$row =  db_fetch($result);
	return $row[0];
}
?>