<?
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	material
* Version :	1.0
* Date    :	19 December 2005
* Author  :	Darwish A. Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class material {

	// Constructor of the class
  function material() {

	}

	// This function is used to retrieve the information of a certain material
	function getMaterialInfo($MaterialID)
	{
		$query  = "Select * From material Where MaterialID = $MaterialID";
		// exit($query);
    $result = mysql_query($query);
    return $result;
	}

	// This function is used to check if a certain material exists or not
  function isExistMaterial($MaterialID, $language)
  {
    $query  = "Select MaterialID From material ";
    $query .= "Where MaterialID = $MaterialID And Language = '$language'";
    $result = mysql_query($query);
    // exit( $query);
    if (mysql_num_rows($result)>0) {
			return true;
		}
		return false;
	}

	// This function is used to retrieve the first location of a certain material
	function getSiteMapSectionID($MaterialID)
	{
		$query  = "Select SiteMapSectionID From material_sitemap_section ";
		$query .= "Where MaterialID = $MaterialID And Enabled = 1 ";
		$query .= "Order By ID ASC Limit 1";
    $result = mysql_query($query);
    // echo $query;
    if (mysql_num_rows($result)>0) {
    	$row = mysql_fetch_object($result);
			return $row->SiteMapSectionID;
		}
    return -1;
	}

	// This function is used to retrieve the locations of a certain material
	function getMaterialLocations($MaterialID)
	{
		$query  = "Select SiteMapSectionID, Priority, Enabled From material_sitemap_section ";
		$query .= "Where MaterialID = $MaterialID";
		// exit($query);
    $result = mysql_query($query);
    return $result;
	}

	// This function is used to get the last n recently added materials.
	function getRecentlyAddedMaterials($n, $language)
	{
		$query  = "Select Distinct M.MaterialID, Title, DATE_FORMAT(DataEntryDate,'%d/%m/%Y') DataEntryDate ";
		$query .= "From material M, material_sitemap_section MS ";
		$query .= "Where M.MaterialID = MS.MaterialID And Language = '$language' And Enabled = 1 ";
		$query .= "Order By M.MaterialID DESC Limit $n";
    // $query .= "Order By DataEntryDate DESC, TimeStamp DESC Limit $n";
    // echo( $query );
    $result = mysql_query($query);
    return $result;
	}

	// This function is used to retrieve all the materials of the whole site
	function getAllMaterials($language, $start=-1, $num=-1)
	{
		$query  = "Select Distinct M.MaterialID, Title, Author, HtmlText, DATE_FORMAT(DataEntryDate,'%Y-%m-%d') DataEntryDate ";
		$query .= "From material M, material_sitemap_section MS ";
		$query .= "Where M.MaterialID = MS.MaterialID And Language = '$language' And Enabled = 1 ";
		$query .= "Order By DataEntryDate DESC, TimeStamp DESC ";

		if (($start!=-1) && ($num!=-1)) {
      $query .= "Limit $start,$num";
		}

		// echo($query);
    $result = mysql_query($query);
    return $result;
	}

	// This function is used to get the top n related materials.
	function getRelatedMaterials($MaterialID, $SiteMapSectionID, $n)
	{
		$query  = "Select M.MaterialID, M.Title ";
		$query .= "From material M, material_sitemap_section MS ";
		$query .= "Where M.MaterialID = MS.MaterialID AND MS.SiteMapSectionID = $SiteMapSectionID ";
		$query .= "And M.MaterialID <> $MaterialID ";
    $query .= "Order By MS.Priority ASC, M.TimeStamp DESC Limit $n";
    // echo( $query );
    $result = mysql_query($query);
    return $result;
	}

}

?>