/php/survey_template_user.class.php
PHP | 326 lines | 306 code | 20 blank | 0 comment | 4 complexity | 09ed5c79185b7588f53fd07e8b91b0e9 MD5 | raw file
- <?php
- namespace repository\content_object\survey;
-
- use common\libraries\DataClass;
- use common\libraries\Utilities;
-
- abstract class SurveyTemplateUser extends DataClass
- {
-
- const CLASS_NAME = __CLASS__;
-
- const PROPERTY_TYPE = 'type';
- const PROPERTY_USER_ID = 'user_id';
- const PROPERTY_TEMPLATE_ID = 'template_id';
-
- const CONTEXT_USERS = 'context_users';
- const LEVELS = 'levels';
- const CONTEXT_LEVEL = 'context_level';
- const HIERARCHY = 'hierarchy';
- const CONTEXT_PATHS = 'context_paths';
- const PARENT_CHILD_IDS = 'parent_child';
- const PARENT_CHILD_CONTEXT_PATHS = 'parent_child_context_paths';
- const PARENT_ID = 'parent_id';
-
- private $additionalProperties;
-
- public function __construct($defaultProperties = array (), $additionalProperties = null)
- {
- parent :: __construct($defaultProperties);
- if (isset($additionalProperties))
- {
- $this->additionalProperties = $additionalProperties;
- }
-
- }
-
- abstract static function get_additional_property_names($with_context_type = false);
-
- public function create()
- {
- $dm = SurveyContextDataManager :: get_instance();
-
- if (! $dm->create_survey_template_user($this))
- {
- return false;
- }
- else
- {
- return true;
- }
-
- }
-
- public function delete()
- {
-
- $dm = SurveyContextDataManager :: get_instance();
-
- if (! $dm->delete_survey_template_user($this))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- public function update()
- {
-
- $dm = SurveyContextDataManager :: get_instance();
-
- if (! $dm->update_survey_template_user($this))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- static function factory($type, $defaultProperties = array(), $additionalProperties = null)
- {
- $class = self :: type_to_class($type);
- require_once dirname(__FILE__) . '/template/' . $type . '/' . $type . '.class.php';
- return new $class($defaultProperties, $additionalProperties);
- }
-
- static function type_to_class($type)
- {
- return __NAMESPACE__ . '\\' . Utilities :: underscores_to_camelcase($type);
- }
-
- static function class_to_type($class)
- {
- return Utilities :: get_classname_from_namespace($class, true);
- }
-
- 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_type()
- {
- return self :: class_to_type(get_class($this));
-
- }
-
- function get_template_id()
- {
- return $this->get_default_property(self :: PROPERTY_TEMPLATE_ID);
- }
-
- function set_template_id($template_id)
- {
- $this->set_default_property(self :: PROPERTY_TEMPLATE_ID, $template_id);
- }
-
- static function get_default_property_names()
- {
- return parent :: get_default_property_names(array(self :: PROPERTY_TYPE, self :: PROPERTY_USER_ID,
- self :: PROPERTY_TEMPLATE_ID));
- }
-
- /**
- * inherited
- */
- function get_data_manager()
- {
- return SurveyContextDataManager :: get_instance();
- }
-
- static function get_table_name()
- {
- return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
- }
-
- function get_additional_property($name)
- {
- $this->check_for_additional_properties();
- return $this->additionalProperties[$name];
- }
-
- function set_additional_property($name, $value)
- {
- //$this->check_for_additional_properties();
- $this->additionalProperties[$name] = $value;
- }
-
- function get_additional_properties()
- {
- $this->check_for_additional_properties();
- return $this->additionalProperties;
- }
-
- private function check_for_additional_properties()
- {
- if (isset($this->additionalProperties))
- {
- return;
- }
- $dm = SurveyContextDataManager :: get_instance();
- $this->additionalProperties = $dm->retrieve_additional_survey_template_user_properties($this);
- }
-
- public static function get_by_id($survey_template_user_id, $type)
- {
- $dm = SurveyContextDataManager :: get_instance();
- return $dm->retrieve_survey_template_user_by_id($survey_template_user_id, $type);
- }
-
- public static function get_context_hierarchy($type, $condition = null)
- {
-
- $result = array();
-
- $template_users = SurveyContextDataManager :: get_instance()->retrieve_survey_template_users($type, $condition);
-
- $hierarchy = array();
- $context_users = array();
- $levels = array();
- $context_levels = array();
- $strict_parent_child_context_ids = array();
- $parent_ids = array();
-
- $parent_child_context_paths = array();
-
- $index = 0;
-
- while ($template_user = $template_users->next_result())
- {
-
- // $index ++;
- // if ($index == 50)
- // {
- // break;
- // }
- $parent_context = 0;
- $users_id = $template_user->get_user_id();
-
- $property_names = $template_user->get_additional_property_names();
-
- $context_ids = array();
- $path = null;
- $level = 1;
- foreach ($property_names as $name)
- {
- $context_id = $template_user->get_additional_property($name);
-
- $context_levels[$context_id] = $level;
- $level ++;
-
- $parent_ids[$context_id] = $parent_context;
-
- if (! in_array($context_id, $strict_parent_child_context_ids[$parent_context]))
- {
- $strict_parent_child_context_ids[$parent_context][] = $context_id;
- }
-
- if ($path)
- {
-
- $path = $path . '_' . $context_id;
- }
- else
- {
- $path = (string) $context_id;
- }
-
- $path = (string) $path;
-
- // if (! in_array($context_id, $levels[$name][$path]))
- // {
- $levels[$name][$path] = $context_id;
- // }
-
- // if (! in_array($users_id, $context_users[$context_id]))
- // {
- // $context_users[$context_id][] = $users_id;
- // }
-
-
- if (! in_array($path, $parent_child_context_paths[$parent_context]))
- {
- $parent_child_context_paths[$parent_context][] = $path;
- }
-
- if (! in_array($users_id, $context_users[$path]))
- {
- $context_users[$path][] = $users_id;
- }
-
- // if (! in_array($path, $hierarchy))
- // {
- $hierarchy[] = $path;
-
- // }
- $parent_context = $context_id;
-
- }
-
- }
-
- // dump($levels);
- // exit;
-
- $result[self :: CONTEXT_USERS] = $context_users;
- $result[self :: LEVELS] = $levels;
- $result[self :: PARENT_CHILD_IDS] = $strict_parent_child_context_ids;
- $result[self :: CONTEXT_LEVEL] = $context_levels;
- $result[self :: PARENT_ID] = $parent_ids;
- $result[self :: PARENT_CHILD_CONTEXT_PATHS] = $parent_child_context_paths;
-
- $hierarchy = array_unique($hierarchy);
-
- $parent_child_context_ids = array();
-
- foreach ($hierarchy as $path)
- {
-
- $context_ids = explode('_', $path);
- $context_count = count($context_ids);
- while ($context_count > 0)
- {
- $context_count --;
- $context_id = array_shift($context_ids);
- $attached_context_ids = $parent_child_context_ids[$context_id];
-
- if (count($attached_context_ids) > 0)
- {
- $merged_array = array_merge($context_ids, $attached_context_ids);
- $parent_child_context_ids[$context_id] = array_unique($merged_array);
-
- }
- else
- {
- $parent_child_context_ids[$context_id] = array_unique($context_ids);
- }
- }
-
- }
-
- // foreach ($hierarchy as $path)
- // {
- // $context_ids = explode('_', $path);
- // $context_id = array_pop($context_ids);
- // $path_child_context_ids[$path] = $parent_child_context_ids[$context_id];
- // }
-
-
- $result[self :: HIERARCHY] = $parent_child_context_ids;
-
- $result[self :: CONTEXT_PATHS] = $hierarchy;
-
- return $result;
- }
- }
- ?>