Location: PHPKode > projects > Community Learning Network > cln/lib/CLN/Cln_Metadata.php
<?php
/*
 * Metadata for the CLN Class
 *
 * Copyright (c) 2003-4 St. Christopher House
 *
 * Developed by The Working Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @version $Id: Cln_Metadata.php,v 1.15 2005/01/25 17:47:42 darcy Exp $
 *
 */

define('METADATA_ROOT',dirname($GLOBALS['classes']['metadata']['library'])."/");

// this is just defined here, but at the moment, i'm not tying it into a method
if(!defined('CLN_METADATA_KBSEARCH_INTERFACE')) define('CLN_METADATA_KBSEARCH_INTERFACE', METADATA_ROOT.'interfaces/kbSearch-metadata.php');


class Cln_Metadata
{

	/*
	 *
	 * Class Attributes:  Cln_Metadata
	 *
	 * 		The attributes for this class are:
	 *
	 *		TBD
	 *
	 */
	var $koId;
	var $objectId;
	var $contributors;
	var $unit;
	var $description;
	var $version;
	var $usersNaturalLanguage;
	var $learningResourceType;
	var $endUserRole;
	var $context;
	var $audienceAgeRange;
	var $rights;


	/***********************************************************************************
	 *
	 * Externally accessed methods
	 *
	 **********************************************************************************/

	/*
	 *
	 * Function:  Cln_Metadata()
	 *
	 * 		Class Constructor
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function Cln_Metadata($koId, $objectId)
	{
		d('Metadata for: ' . $koId . ': Cln_Metadata Constructor objectId: ' . $objectId, 3);
		$this->koId = $koId;
		$this->objectId = $objectId;
		$this->contributors = '';
		$this->unit = '';
		$this->description = '';
		$this->version = '';
		$this->usersNaturalLanguage = '';
		$this->learningResourceType = '';
		$this->endUserRole = '';
		$this->context = '';
		$this->audienceAgeRange = '';
		$this->rights = '';
		$this->_isNew = TRUE;
		$this->loadMetadata();
	}


	/*
	 *
	 * Function:  loadMetadata()
	 *
	 * 		Loads the metadata from the DB, if it exists
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function loadMetadata()
	{
		d('Metadata for: ' . $this->koId . ': Cln_Metadata loadMetadata', 3);
		// Get the metadata from the database
		$sql = sprintf("SELECT contributors, unit, description, version, usersNaturalLanguage,
								learningResourceType, endUserRole, context,
								audienceAgeRange, rights
						FROM `%s` WHERE koId = '%s' AND objectId = '%s'",
						KO_METADATA_TABLE, $this->koId, $this->objectId);

		$db = &Cln_Db::singleton(MAIN_CLN_DSN);

		$result = $db->query($sql);

		$numRows = $result->numRows();

		if ($numRows >= 1) {

			$row = $result->fetchRow(DB_FETCHMODE_ASSOC);

			$this->contributors = $row['contributors'];
			$this->unit = $row['unit'];
			$this->description = $row['description'];
			$this->version = $row['version'];
			$this->usersNaturalLanguage = $row['usersNaturalLanguage'];
			$this->learningResourceType = $row['learningResourceType'];
			$this->endUserRole = $row['endUserRole'];
			$this->context = $row['context'];
			$this->audienceAgeRange = $row['audienceAgeRange'];
			$this->rights = $row['rights'];

			$this->_isNew = FALSE;
			return TRUE;
		}
		else if ($numRows < 1) {
			$this->_isNew = TRUE;
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  captureMetadata()
	 *
	 * 		Captures the metadata if it's submitted
	 *
	 * @access public
	 * @return String 		$message
	 *
	 */
	function captureMetadata()
	{
		d('Metadata for: ' . $this->koId . ': Cln_Metadata captureMetadata', 3);
		$noErrors = TRUE;

		// The Contributors
		if (isset($_POST['koMetadataContributors'])) {
			$this->contributors = $_POST['koMetadataContributors'];
		}

		// The Unit
		if (isset($_POST['koMetadataUnit'])) {
			$this->unit = $_POST['koMetadataUnit'];
		}

		// The Description
		if (isset($_POST['koMetadataDescription'])) {
			$this->description = $_POST['koMetadataDescription'];
		}

		// The Version
		if (isset($_POST['koMetadataVersion'])) {
			$this->version = $_POST['koMetadataVersion'];
		}

		// The user's natural language
		if (isset($_POST['koMetadataUserLang'])) {
			$this->usersNaturalLanguage = $_POST['koMetadataUserLang'];
		}

		// The Learning Resource Type
		if (isset($_POST['koMetadataLearningResourceType'])) {
			$this->learningResourceType = $_POST['koMetadataLearningResourceType'];
		}

		// The Intended End-user role
		if (isset($_POST['koMetadataIntendedEndUserRole'])) {
			$this->endUserRole = $_POST['koMetadataIntendedEndUserRole'];
		}

		// The Context
		if (isset($_POST['koMetadataContext'])) {
			$this->context = $_POST['koMetadataContext'];
		}

		// The Audience Age Range
		if (isset($_POST['koMetadataTypicalAgeRange'])) {
			$this->audienceAgeRange = $_POST['koMetadataTypicalAgeRange'];
		}

		// The Rights
		if (isset($_POST['koMetadataRights'])) {
			$this->rights = $_POST['koMetadataRights'];
		}

		return $noErrors;
	}


	/*
	 *
	 * Function:  saveMetadata()
	 *
	 * 		saves the metadata
	 *
	 * @access public
	 * @return Boolean 	TRUE or FALSE
	 *
	 */
	function saveMetadata($koId = FALSE)
	{
		d('Metadata for: ' . $this->koId . ': Cln_Metadata saveMetadata', 3);
		if ($this->koId == 'NEW' && $koId) {
			$this->koId = $koId;
		}

		if ($this->koId == 'NEW' || $this->objectId == 'NEW') {
			PEAR::raiseError('Inside SaveMetadata and koId or object is still NEW', E_ERROR);
			return FALSE;
		}

		// If it's new
		if ($this->_isNew) {

			$sql = sprintf("INSERT INTO `%s` SET
								koId = '%s',
								objectId = '%s',
								contributors = '%s',
								unit = '%s',
								description = '%s',
								version = '%s',
								usersNaturalLanguage = '%s',
								learningResourceType = '%s',
								endUserRole = '%s',
								context = '%s',
								audienceAgeRange = '%s',
								rights = '%s',
								created = NOW()",
							KO_METADATA_TABLE,
							$this->koId,
							$this->objectId,
							addslashes($this->contributors),
							addslashes($this->unit),
							addslashes($this->description),
							addslashes($this->version),
							addslashes($this->usersNaturalLanguage),
							addslashes($this->learningResourceType),
							addslashes($this->endUserRole),
							addslashes($this->context),
							addslashes($this->audienceAgeRange),
							addslashes($this->rights));
		}
		else {
			$sql = sprintf("UPDATE `%s` SET
								contributors = '%s',
								unit = '%s',
								description = '%s',
								version = '%s',
								usersNaturalLanguage = '%s',
								learningResourceType = '%s',
								endUserRole = '%s',
								context = '%s',
								audienceAgeRange = '%s',
								rights = '%s'
							WHERE koId = '%s' AND objectId = '%s'",
							KO_METADATA_TABLE,
							addslashes($this->contributors),
							addslashes($this->unit),
							addslashes($this->description),
							addslashes($this->version),
							addslashes($this->usersNaturalLanguage),
							addslashes($this->learningResourceType),
							addslashes($this->endUserRole),
							addslashes($this->context),
							addslashes($this->audienceAgeRange),
							addslashes($this->rights),
							$this->koId,
							$this->objectId);
		}


		$db = &Cln_Db::singleton(MAIN_CLN_DSN);
		$result = $db->query($sql);
		if (PEAR::isError($result)) {
			PEAR::raiseError("Couldn't save the metadata with $sql", E_USER_ERROR);
			return FALSE;
		}
		else {
			$this->_isNew = FALSE;
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  deleteMetadata()
	 *
	 * 		deletes the metadata
	 *
	 * @access public
	 * @return Boolean 	TRUE or FALSE
	 *
	 */
	function deleteMetadata()
	{
		$sql = sprintf("DELETE FROM `%s` WHERE koId = '%d' AND objectId = '%d' ",
						KO_METADATA_TABLE, $this->koId, $this->objectId);

		$db = &Cln_Db::singleton(MAIN_CLN_DSN);
		$result = $db->query($sql);

		if (PEAR::isError($result)) {
			PEAR::raiseError("There was an error deleting the KO Metadata: $sql", E_ERROR);
			return FALSE;
		}
		else {
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  getUnits()
	 *
	 * 		get the metadata units from a file
	 *
	 * @access public
	 * @return Boolean 	TRUE or FALSE
	 *
	 */
	function getUnits()
	{
		$metadataUnitsFile = 'config/CLN/units.xml';
		$metadataUnitsExampleFile = 'config/CLN/units-example.xml';

		if(file_exists($metadataUnitsFile)) {
			$unitsXML =  file_get_contents($metadataUnitsFile);

			require_once('lib/CLN/XMLParser.php');

			$parser = new XMLParser($unitsXML, 'raw', 1);
			$unitTree = $parser->getTree();

			foreach($unitTree['UNITS']['UNIT'] as $unit) {
				$unitName = $unit['FULLNAME']['VALUE'];
				$units[($unitName)] = $unitName;
			}

			// Display the array
		} else {
			PEAR::raiseError('Metadata Units file does not exist.  Please copy '.$metadataUnitsExampleFile.' to '.$metadataUnitsFile, E_USER_WARNING);
		}

		return $units;
	}


	/*
	 *
	 * Function:  getAgeRanges()
	 *
	 * 		get the metadata age ranges from a file
	 *
	 * @access public
	 * @return Boolean 	TRUE or FALSE
	 *
	 */
	function getAgeRanges()
	{
		$metadataAgeRangesFile = 'config/CLN/ageRanges.xml';
		$metadataAgeRangesExampleFile = 'config/CLN/ageRanges-example.xml';

		if(file_exists($metadataAgeRangesFile)) {
			$ageRangesXML =  file_get_contents($metadataAgeRangesFile);

			require_once('lib/CLN/XMLParser.php');

			$parser = new XMLParser($ageRangesXML, 'raw', 1);
			$ageRangeTree = $parser->getTree();

			foreach($ageRangeTree['AGERANGES']['AGERANGE'] as $ageRange) {
				$ageRangeName = $ageRange['FULLNAME']['VALUE'];
				$ageRanges[($ageRangeName)] = $ageRangeName;
			}

			// Display the array
		} else {
			PEAR::raiseError('Metadata Age Range file does not exist.  Please copy '.$metadataAgeRangesExampleFile.' to '.$metadataAgeRangesFile, E_USER_WARNING);
		}

		return $ageRanges;
	}
	
	
	/*
	 *
	 * Function:  getPassedData()
	 *
	 * 		Captures any passed fields that are related to the kbSearch
	 *
	 * @access public
	 * @return Boolean 	TRUE or FALSE
	 *
	 */
	function getPassedData()
	{
		// prepare kbSearch object
		// :NOTE: this could be optimized, since we are creating this object, even if we aren't
		// doing a metadata search - dwc
		$metadataSearch =& new Cln_KB_Search();
		$metadataSearch->fetchColumns = '';
		$metadataSearch->tables = '';
		
		// handles contributors searches
		if(isset($_REQUEST['searchContributors']) && !empty($_REQUEST['searchContributors'])) {
			$metadataSearch->customWheres[] = "metadata.contributors LIKE '%".$_REQUEST['searchContributors']."%'";
			$_SESSION['searchTerms']['searchContributors'] = $_REQUEST['searchContributors'];
			
			$searchingMetadata = TRUE;
		}
		
		// handles units searches
		if(isset($_REQUEST['searchUnits']) && !empty($_REQUEST['searchUnits'])) {
			foreach($_REQUEST['searchUnits'] as $searchUnit) {
				$metadataSearch->customWheres[] = "metadata.unit LIKE '%".$searchUnit."%'";
			}
			$_SESSION['searchTerms']['searchUnits'] = $_REQUEST['searchUnits'];
		
			$searchingMetadata = TRUE;
		}
		
		// handles description searches
		if(isset($_REQUEST['searchDescription']) && !empty($_REQUEST['searchDescription'])) {
			$metadataSearch->customWheres[] = "metadata.description LIKE '%".$_REQUEST['searchDescription']."%'";
			$_SESSION['searchTerms']['searchDescription'] = $_REQUEST['searchDescription'];
		
			$searchingMetadata = TRUE;
		}
		
		// handles version searches
		if(isset($_REQUEST['searchVersion']) && !empty($_REQUEST['searchVersion'])) {
			$metadataSearch->customWheres[] = "metadata.version = '".$_REQUEST['searchVersion']."'";
			$_SESSION['searchTerms']['searchVersion'] = $_REQUEST['searchVersion'];
		
			$searchingMetadata = TRUE;
		}
		
		// handles user's language searches
		if(isset($_REQUEST['searchUserLang']) && !empty($_REQUEST['searchUserLang'])) {
			foreach($_REQUEST['searchUserLang'] as $searchUserLang) {
				$metadataSearch->customWheres[] = "metadata.usersNaturalLanguage LIKE '%".$searchUserLang."%'";
			}
			$_SESSION['searchTerms']['searchUserLang'] = $_REQUEST['searchUserLang'];
		
			$searchingMetadata = TRUE;
		}
		
		// handles context searches
		if(isset($_REQUEST['searchContext']) && !empty($_REQUEST['searchContext'])) {
			$metadataSearch->customWheres[] = "metadata.context LIKE '%".$_REQUEST['searchContext']."%'";
			$_SESSION['searchTerms']['searchContext'] = $_REQUEST['searchContext'];
		
			$searchingMetadata = TRUE;
		}
		
		// handles title searches
		if(isset($_REQUEST['searchAgeRanges']) && !empty($_REQUEST['searchAgeRanges'])) {
			foreach($_REQUEST['searchAgeRanges'] as $searchAgeRanges) {
				$metadataSearch->customWheres[] = "metadata.audienceAgeRange LIKE '%".$searchAgeRanges."%'";
			}
			$_SESSION['searchTerms']['searchAgeRanges'] = $_REQUEST['searchAgeRanges'];
		
			$searchingMetadata = TRUE;
		}
		
		
		// we are doing a metadata search as well
		if(isset($searchingMetadata)) {
			$metadataSearch->customWheres[] = 'ko.koId = metadata.koId';
			$metadataSearch->tables = sprintf('%s as metadata', KO_METADATA_TABLE);
		}
		
		return $metadataSearch;
	}
	
	
	/***********************************************************************************
	 *
	 * Internally accessed methods
	 *
	 **********************************************************************************/

}

//********************************************************************

?>
Return current item: Community Learning Network