PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/complex_content_object_item.class.php

https://bitbucket.org/chamilo/chamilo-repository/
PHP | 336 lines | 225 code | 51 blank | 60 comment | 14 complexity | 4ad07753ad01338e05711288a97be9d9 MD5 | raw file
  1. <?php
  2. namespace repository;
  3. use common\libraries\Translation;
  4. use common\libraries\Utilities;
  5. use common\libraries\DataClass;
  6. use common\libraries\StringUtilities;
  7. /**
  8. * $Id: complex_content_object_item.class.php 204 2009-11-13 12:51:30Z kariboe $
  9. * @package repository.lib
  10. */
  11. /**
  12. * Instances of this class group generic information about a complex learning object item
  13. *
  14. * @author Sven Vanpoucke
  15. */
  16. class ComplexContentObjectItem extends DataClass
  17. {
  18. const CLASS_NAME = __CLASS__;
  19. const PROPERTY_REF = 'ref_id';
  20. const PROPERTY_PARENT = 'parent_id';
  21. const PROPERTY_USER_ID = 'user_id';
  22. const PROPERTY_DISPLAY_ORDER = 'display_order';
  23. const PROPERTY_ADD_DATE = 'add_date';
  24. const PROPERTIES_ADDITIONAL = 'additional_properties';
  25. /**
  26. * @var ContentObject
  27. */
  28. private $reference_object;
  29. function __construct($default_properties = array (), $additional_properties = array())
  30. {
  31. parent :: __construct($default_properties);
  32. $this->set_additional_properties($additional_properties);
  33. }
  34. function get_additional_property($name)
  35. {
  36. return $this->get_specific_property(self :: PROPERTIES_ADDITIONAL, $name);
  37. }
  38. /**
  39. * Gives a value to a given additional property
  40. * @param String $name the name of the additional property
  41. * @param Object $value the new value
  42. */
  43. function set_additional_property($name, $value)
  44. {
  45. $this->set_specific_property(self :: PROPERTIES_ADDITIONAL, $name, $value);
  46. }
  47. /**
  48. * Retrieves the default properties of this class
  49. * @return Array of Objects
  50. */
  51. function get_additional_properties()
  52. {
  53. return $this->get_specific_properties(self :: PROPERTIES_ADDITIONAL);
  54. }
  55. /**
  56. * Sets the default properties of this class
  57. * @param Array Of Objects $defaultProperties
  58. */
  59. function set_additional_properties($additional_properties)
  60. {
  61. $this->set_specific_properties(self :: PROPERTIES_ADDITIONAL, $additional_properties);
  62. }
  63. /**
  64. * Get the default property names
  65. * @return array The property names.
  66. */
  67. static function get_default_property_names($extended_property_names = array())
  68. {
  69. return parent :: get_default_property_names(array(self :: PROPERTY_REF, self :: PROPERTY_PARENT,
  70. self :: PROPERTY_USER_ID, self :: PROPERTY_DISPLAY_ORDER, self :: PROPERTY_ADD_DATE));
  71. }
  72. /**
  73. * inherited
  74. */
  75. function get_data_manager()
  76. {
  77. return RepositoryDataManager :: get_instance();
  78. }
  79. /**
  80. * Gets the additional property names
  81. * Should be overridden when needed
  82. * @return array the aditional property names
  83. */
  84. static function get_additional_property_names()
  85. {
  86. return array();
  87. }
  88. function get_add_date()
  89. {
  90. return $this->get_default_property(self :: PROPERTY_ADD_DATE);
  91. }
  92. function set_add_date($add_date)
  93. {
  94. $this->set_default_property(self :: PROPERTY_ADD_DATE, $add_date);
  95. }
  96. function get_ref()
  97. {
  98. return $this->get_default_property(self :: PROPERTY_REF);
  99. }
  100. function get_ref_object()
  101. {
  102. if (! isset($this->reference_object))
  103. {
  104. $this->reference_object = RepositoryDataManager :: get_instance()->retrieve_content_object($this->get_ref());
  105. }
  106. return $this->reference_object;
  107. }
  108. function set_ref($ref)
  109. {
  110. $this->set_default_property(self :: PROPERTY_REF, $ref);
  111. }
  112. /**
  113. * @param ContentObject $reference_object
  114. */
  115. function set_ref_object(ContentObject $reference_object)
  116. {
  117. $this->reference_object = $reference_object;
  118. }
  119. function get_parent()
  120. {
  121. return $this->get_default_property(self :: PROPERTY_PARENT);
  122. }
  123. function get_parent_object()
  124. {
  125. return RepositoryDataManager :: get_instance()->retrieve_content_object($this->get_parent());
  126. }
  127. function set_parent($parent)
  128. {
  129. $this->set_default_property(self :: PROPERTY_PARENT, $parent);
  130. }
  131. function get_user_id()
  132. {
  133. return $this->get_default_property(self :: PROPERTY_USER_ID);
  134. }
  135. function set_user_id($user_id)
  136. {
  137. $this->set_default_property(self :: PROPERTY_USER_ID, $user_id);
  138. }
  139. function get_display_order()
  140. {
  141. return $this->get_default_property(self :: PROPERTY_DISPLAY_ORDER);
  142. }
  143. function set_display_order($display_order)
  144. {
  145. $this->set_default_property(self :: PROPERTY_DISPLAY_ORDER, $display_order);
  146. }
  147. function create()
  148. {
  149. $rdm = RepositoryDataManager :: get_instance();
  150. if (! $this->check_before_save())
  151. {
  152. return false;
  153. }
  154. if (! $rdm->create_complex_content_object_item($this))
  155. {
  156. $this->add_error(Translation :: get('CouldNotCreateObjectInDatabase', array(
  157. 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
  158. return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * Checks this object before saving + adds some default values
  164. * @return boolean
  165. */
  166. function check_before_save()
  167. {
  168. $rdm = RepositoryDataManager :: get_instance();
  169. if (! $this->get_add_date())
  170. {
  171. $this->set_add_date(time());
  172. }
  173. if (! $this->get_display_order())
  174. {
  175. $this->set_display_order($rdm->select_next_display_order($this->get_parent()));
  176. }
  177. if (StringUtilities :: is_null_or_empty($this->get_ref()))
  178. {
  179. $this->add_error(Translation :: get('ReferenceObjectShouldNotBeEmpty'));
  180. }
  181. else
  182. {
  183. $ref_content_object = $rdm->retrieve_content_object($this->get_ref());
  184. if (! $ref_content_object)
  185. {
  186. $this->add_error(Translation :: get('ReferenceObjectDoesNotExist'));
  187. }
  188. }
  189. if (StringUtilities :: is_null_or_empty($this->get_parent()))
  190. {
  191. $this->add_error(Translation :: get('ReferenceObjectShouldNotBeEmpty'));
  192. }
  193. else
  194. {
  195. $parent_content_object = $rdm->retrieve_content_object($this->get_parent());
  196. if (! $parent_content_object)
  197. {
  198. $this->add_error(Translation :: get('ParentObjectDoesNotExist'));
  199. }
  200. }
  201. return ! $this->has_errors();
  202. }
  203. function update()
  204. {
  205. if (! $this->check_before_save())
  206. {
  207. return false;
  208. }
  209. if (! RepositoryDataManager :: get_instance()->update_complex_content_object_item($this))
  210. {
  211. $this->add_error(Translation :: get('CouldNotUpdateObjectInDatabase', array(
  212. 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
  213. return false;
  214. }
  215. return true;
  216. }
  217. function delete()
  218. {
  219. if (! RepositoryDataManager :: get_instance()->delete_complex_content_object_item($this))
  220. {
  221. $this->add_error(Translation :: get('CouldNotDeleteObjectInDatabase', array(
  222. 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
  223. return false;
  224. }
  225. return true;
  226. }
  227. /**
  228. * Determines whether this complex learning object is an extended complex learning object or not
  229. */
  230. function is_extended()
  231. {
  232. return count($this->get_additional_property_names()) > 0;
  233. //return count($this->get_additional_properties()) > 0;
  234. }
  235. /**
  236. * Retrieves the allowed types to add to this complex learning object item
  237. * @return Array of learning object types
  238. */
  239. function get_allowed_types()
  240. {
  241. return array();
  242. }
  243. function is_complex()
  244. {
  245. return count($this->get_allowed_types()) > 0;
  246. }
  247. function has_right($right, $user_id = null)
  248. {
  249. $ref_object = $this->get_ref_object();
  250. if(!$ref_object)
  251. {
  252. // if we cannot fetch the reference object, surely we have no rights to do anything with it...
  253. return false;
  254. }
  255. return $ref_object->has_right($right, $user_id);
  256. }
  257. /**
  258. * Creates an instance of an extended class with the given type
  259. * @param String type
  260. * @param Array defaultProperties
  261. * @param Array additionalProperties
  262. * @return ComplexContentObjectItem
  263. */
  264. static function factory($type, $defaultProperties = array(), $additionalProperties = array())
  265. {
  266. if ($type)
  267. {
  268. $type = Utilities :: get_classname_from_namespace($type);
  269. $class = __NAMESPACE__ . '\content_object\\' . $type . '\Complex' . Utilities :: underscores_to_camelcase($type);
  270. require_once dirname(__FILE__) . '/../../content_object/' . $type . '/php/complex_' . $type . '.class.php';
  271. return new $class($defaultProperties, $additionalProperties);
  272. }
  273. else
  274. {
  275. return new self($defaultProperties, $additionalProperties);
  276. }
  277. }
  278. static function get_table_name()
  279. {
  280. return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
  281. }
  282. }
  283. ?>