/php/lib/complex_content_object_item.class.php
PHP | 336 lines | 225 code | 51 blank | 60 comment | 14 complexity | 4ad07753ad01338e05711288a97be9d9 MD5 | raw file
- <?php
- namespace repository;
-
- use common\libraries\Translation;
- use common\libraries\Utilities;
- use common\libraries\DataClass;
- use common\libraries\StringUtilities;
-
- /**
- * $Id: complex_content_object_item.class.php 204 2009-11-13 12:51:30Z kariboe $
- * @package repository.lib
- */
- /**
- * Instances of this class group generic information about a complex learning object item
- *
- * @author Sven Vanpoucke
- */
-
- class ComplexContentObjectItem extends DataClass
- {
- const CLASS_NAME = __CLASS__;
-
- const PROPERTY_REF = 'ref_id';
- const PROPERTY_PARENT = 'parent_id';
- const PROPERTY_USER_ID = 'user_id';
- const PROPERTY_DISPLAY_ORDER = 'display_order';
- const PROPERTY_ADD_DATE = 'add_date';
-
- const PROPERTIES_ADDITIONAL = 'additional_properties';
-
- /**
- * @var ContentObject
- */
- private $reference_object;
-
- function __construct($default_properties = array (), $additional_properties = array())
- {
- parent :: __construct($default_properties);
- $this->set_additional_properties($additional_properties);
- }
-
- function get_additional_property($name)
- {
- return $this->get_specific_property(self :: PROPERTIES_ADDITIONAL, $name);
- }
-
- /**
- * Gives a value to a given additional property
- * @param String $name the name of the additional property
- * @param Object $value the new value
- */
- function set_additional_property($name, $value)
- {
- $this->set_specific_property(self :: PROPERTIES_ADDITIONAL, $name, $value);
- }
-
- /**
- * Retrieves the default properties of this class
- * @return Array of Objects
- */
- function get_additional_properties()
- {
- return $this->get_specific_properties(self :: PROPERTIES_ADDITIONAL);
- }
-
- /**
- * Sets the default properties of this class
- * @param Array Of Objects $defaultProperties
- */
- function set_additional_properties($additional_properties)
- {
- $this->set_specific_properties(self :: PROPERTIES_ADDITIONAL, $additional_properties);
- }
-
- /**
- * Get the default property names
- * @return array The property names.
- */
- static function get_default_property_names($extended_property_names = array())
- {
- return parent :: get_default_property_names(array(self :: PROPERTY_REF, self :: PROPERTY_PARENT,
- self :: PROPERTY_USER_ID, self :: PROPERTY_DISPLAY_ORDER, self :: PROPERTY_ADD_DATE));
- }
-
- /**
- * inherited
- */
- function get_data_manager()
- {
- return RepositoryDataManager :: get_instance();
- }
-
- /**
- * Gets the additional property names
- * Should be overridden when needed
- * @return array the aditional property names
- */
- static function get_additional_property_names()
- {
- return array();
- }
-
- function get_add_date()
- {
- return $this->get_default_property(self :: PROPERTY_ADD_DATE);
- }
-
- function set_add_date($add_date)
- {
- $this->set_default_property(self :: PROPERTY_ADD_DATE, $add_date);
- }
-
- function get_ref()
- {
- return $this->get_default_property(self :: PROPERTY_REF);
- }
-
- function get_ref_object()
- {
- if (! isset($this->reference_object))
- {
- $this->reference_object = RepositoryDataManager :: get_instance()->retrieve_content_object($this->get_ref());
- }
- return $this->reference_object;
- }
-
- function set_ref($ref)
- {
- $this->set_default_property(self :: PROPERTY_REF, $ref);
- }
-
- /**
- * @param ContentObject $reference_object
- */
- function set_ref_object(ContentObject $reference_object)
- {
- $this->reference_object = $reference_object;
- }
-
- function get_parent()
- {
- return $this->get_default_property(self :: PROPERTY_PARENT);
- }
-
- function get_parent_object()
- {
- return RepositoryDataManager :: get_instance()->retrieve_content_object($this->get_parent());
- }
-
- function set_parent($parent)
- {
- $this->set_default_property(self :: PROPERTY_PARENT, $parent);
- }
-
- function get_user_id()
- {
- return $this->get_default_property(self :: PROPERTY_USER_ID);
- }
-
- function set_user_id($user_id)
- {
- $this->set_default_property(self :: PROPERTY_USER_ID, $user_id);
- }
-
- function get_display_order()
- {
- return $this->get_default_property(self :: PROPERTY_DISPLAY_ORDER);
- }
-
- function set_display_order($display_order)
- {
- $this->set_default_property(self :: PROPERTY_DISPLAY_ORDER, $display_order);
- }
-
- function create()
- {
- $rdm = RepositoryDataManager :: get_instance();
-
- if (! $this->check_before_save())
- {
- return false;
- }
-
- if (! $rdm->create_complex_content_object_item($this))
- {
- $this->add_error(Translation :: get('CouldNotCreateObjectInDatabase', array(
- 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
-
- return false;
- }
-
- return true;
- }
-
- /**
- * Checks this object before saving + adds some default values
- * @return boolean
- */
- function check_before_save()
- {
- $rdm = RepositoryDataManager :: get_instance();
-
- if (! $this->get_add_date())
- {
- $this->set_add_date(time());
- }
-
- if (! $this->get_display_order())
- {
- $this->set_display_order($rdm->select_next_display_order($this->get_parent()));
- }
-
- if (StringUtilities :: is_null_or_empty($this->get_ref()))
- {
- $this->add_error(Translation :: get('ReferenceObjectShouldNotBeEmpty'));
- }
- else
- {
- $ref_content_object = $rdm->retrieve_content_object($this->get_ref());
- if (! $ref_content_object)
- {
- $this->add_error(Translation :: get('ReferenceObjectDoesNotExist'));
- }
- }
-
- if (StringUtilities :: is_null_or_empty($this->get_parent()))
- {
- $this->add_error(Translation :: get('ReferenceObjectShouldNotBeEmpty'));
- }
- else
- {
- $parent_content_object = $rdm->retrieve_content_object($this->get_parent());
- if (! $parent_content_object)
- {
- $this->add_error(Translation :: get('ParentObjectDoesNotExist'));
- }
- }
-
- return ! $this->has_errors();
- }
-
- function update()
- {
- if (! $this->check_before_save())
- {
- return false;
- }
-
- if (! RepositoryDataManager :: get_instance()->update_complex_content_object_item($this))
- {
- $this->add_error(Translation :: get('CouldNotUpdateObjectInDatabase', array(
- 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
-
- return false;
- }
-
- return true;
- }
-
- function delete()
- {
- if (! RepositoryDataManager :: get_instance()->delete_complex_content_object_item($this))
- {
- $this->add_error(Translation :: get('CouldNotDeleteObjectInDatabase', array(
- 'OBJECT' => Translation :: get('ComplexContentObjectItem'), Utilities :: COMMON_LIBRARIES)));
-
- return false;
- }
-
- return true;
- }
-
- /**
- * Determines whether this complex learning object is an extended complex learning object or not
- */
- function is_extended()
- {
- return count($this->get_additional_property_names()) > 0;
-
- //return count($this->get_additional_properties()) > 0;
- }
-
- /**
- * Retrieves the allowed types to add to this complex learning object item
- * @return Array of learning object types
- */
- function get_allowed_types()
- {
- return array();
- }
-
- function is_complex()
- {
- return count($this->get_allowed_types()) > 0;
- }
-
- function has_right($right, $user_id = null)
- {
- $ref_object = $this->get_ref_object();
- if(!$ref_object)
- {
- // if we cannot fetch the reference object, surely we have no rights to do anything with it...
- return false;
- }
- return $ref_object->has_right($right, $user_id);
- }
-
- /**
- * Creates an instance of an extended class with the given type
- * @param String type
- * @param Array defaultProperties
- * @param Array additionalProperties
- * @return ComplexContentObjectItem
- */
- static function factory($type, $defaultProperties = array(), $additionalProperties = array())
- {
- if ($type)
- {
- $type = Utilities :: get_classname_from_namespace($type);
- $class = __NAMESPACE__ . '\content_object\\' . $type . '\Complex' . Utilities :: underscores_to_camelcase($type);
- require_once dirname(__FILE__) . '/../../content_object/' . $type . '/php/complex_' . $type . '.class.php';
- return new $class($defaultProperties, $additionalProperties);
- }
- else
- {
- return new self($defaultProperties, $additionalProperties);
- }
- }
-
- static function get_table_name()
- {
- return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
- }
-
- }
- ?>