Source for file HarmoniEditableAgent.class.php

Documentation is available at HarmoniEditableAgent.class.php

  1. <?php
  2.  
  3. require_once("HarmoniAgent.class.php");
  4.  
  5. /**
  6. * EditableAgent is an extension of the OSID Agent to allow for
  7. * modifications.
  8. *
  9. * <p>
  10. * WARNING: NOT IN OSID! Use at your own risk
  11. * </p>
  12. *
  13. * @package harmoni.osid_v2.agent
  14. *
  15. * @copyright Copyright &copy; 2005, Middlebury College
  16. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  17. *
  18. * @version $Id: HarmoniEditableAgent.class.php,v 1.5 2007/09/04 20:25:36 adamfranco Exp $
  19. */
  20.  
  21. class HarmoniEditableAgent
  22. extends HarmoniAgent
  23. {
  24. /*
  25. * changes the agent's display name to $newDisplayName
  26. * and store it in the DB
  27. *
  28. * @param string $newDisplayName
  29. *
  30. * @access public
  31. * @return boolean
  32. */
  33. function updateDisplayName($newDisplayName){
  34. //make sure its a string
  35. ArgumentValidator::validate($newDisplayName, new StringValidatorRule(), true);
  36. //set the display name in the object
  37. $this->_node->updateDisplayName($newDisplayName);
  38. }
  39. /*
  40. * adds a property to the agent and stores it
  41. *
  42. * @param object Type $type
  43. * @param string $key
  44. * @param mixed $value
  45. *
  46. * @access public
  47. *
  48. * @return boolean
  49. */
  50. function addProperty($type, $key, $value){
  51. //properties manager is used to store properties
  52. $propertiesManager = Services::getService("Property");
  53. //get the database id of the type
  54. $typeIdString = $propertiesManager->getTypeId($type);
  55. //an agent has one properties object for each Type, this gets the one for the type we're adding.
  56. $property =$this->getPropertiesByType($type);
  57. //if there aren't any properties of Type yet, create a new object
  58. if($property===null){//if agent has no properties of type
  59. $property = new HarmoniProperties($type);//create a new property of type $type
  60. }
  61. //add the property
  62. $property->addProperty($key, $value);
  63. //store the property to the database
  64. return $propertiesManager->storeProperties($this->_idString, $property);
  65. }
  66. /*
  67. * Updates a property of the agent
  68. *
  69. * @param object Type $type
  70. * @param string $key
  71. * @param mixed $value
  72. *
  73. * @access public
  74. *
  75. * @return boolean
  76. */
  77. function updateProperty( $type, $key, $value ){
  78. //get the properties object for Type
  79. $property =$this->getPropertiesByType($type);
  80. //if it doesn't exist, the property doesn't exit, quit
  81. if($property===null){
  82. return false;
  83. }
  84. //get the value
  85. $propertyValue = $property->getProperty($key);
  86. //if the value is null, the key doesn't exist (cleared values are set to FALSE not NULL)
  87. if($propertyValue===null) {
  88. return false;
  89. }
  90. //get rid of the old property
  91. $property->deleteProperty($key);
  92. //add the new value
  93. $property->addProperty($key, $value);
  94. //the property manager is for setting properties in the DB
  95. $propertyManager = Services::getService("Property");
  96. //store the new values and return true or false
  97. return $propertyManager->storeProperties($this->_idString, $property);
  98. }
  99. /*
  100. * Removes a property key/value pair from an agent and agent DB entries
  101. *
  102. * @param object Type $type
  103. * @param string $key
  104. *
  105. * @return boolean
  106. * @access public
  107. */
  108. function deleteProperty( $type, $key ) {
  109. //the properties object hold properties of this type
  110. $property =$this->getPropertiesByType($type);
  111. //our default assumption is that the key sent was bad
  112. $keyExists = false;
  113. //if there is no properties object of that type, there's no property to delete
  114. if($property===null){
  115. return false;
  116. }
  117. //get all the keys for the object and compare them to the target if any one matches, it will be set to true
  118. $keys =$property->getKeys();
  119. while($keys->hasNextObject()){
  120. $tempKey = $keys->nextObject();
  121. if($tempKey==$key){
  122. //remove the property from the object
  123. $property->deleteProperty($key);
  124.  
  125. //property manager is for storing properties info in the DB
  126. $propertyManager = Services::getService("Property");
  127.  
  128. //store property
  129. return $propertyManager->storeProperties($this->_idString, $property);
  130. }
  131. }
  132.  
  133. //if we didn't have a key return false
  134. return false;
  135. }
  136. /*
  137. * Clears the values (replaces them with false) but leaves the properties in
  138. * in existence. The function above destroys them completely.
  139. *
  140. * @return void
  141. * @access public
  142. */
  143. function clearAllProperties() {
  144. $value = false;//indicating set but empty. NULL would confuse things if the property key didn't exist (which would be a true null)
  145. $properties =$this->getProperties();
  146. //cycle through the properties
  147. while($properties->hasNext()){
  148. $property =$properties->next();
  149. //keys for properties of this type
  150. $keys = $property->getKeys();
  151. //cycle through each key value pair
  152. while($keys->hasNextObject()){
  153. $key = $keys->nextObject();
  154. //set value to false
  155. $this->updateProperty($property->getType(), $key, $value);
  156. }
  157. }
  158. return;
  159. }
  160. /*
  161. * Deletes the values for all the properties.
  162. *
  163. * @return void
  164. * @access public
  165. */
  166. function deleteAllProperties() {
  167. $properties =$this->getProperties();
  168. //cycle through the properties
  169. while($properties->hasNext()){
  170. $property =$properties->next();
  171. //keys for properties of this type
  172. $keys = $property->getKeys();
  173. //cycle through each key value pair
  174. while($keys->hasNextObject()){
  175. $key = $keys->nextObject();
  176. //set value to false
  177. $this->deleteProperty($property->getType(), $key);
  178. }
  179. }
  180. return;
  181. }
  182.  
  183. }
  184.  
  185.  
  186. ?>

Documentation generated on Wed, 19 Sep 2007 10:23:52 -0400 by phpDocumentor 1.3.0RC3