PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/php/survey_template_user.class.php

https://bitbucket.org/chamilo/chamilo-survey/
PHP | 326 lines | 306 code | 20 blank | 0 comment | 4 complexity | 09ed5c79185b7588f53fd07e8b91b0e9 MD5 | raw file
  1. <?php
  2. namespace repository\content_object\survey;
  3. use common\libraries\DataClass;
  4. use common\libraries\Utilities;
  5. abstract class SurveyTemplateUser extends DataClass
  6. {
  7. const CLASS_NAME = __CLASS__;
  8. const PROPERTY_TYPE = 'type';
  9. const PROPERTY_USER_ID = 'user_id';
  10. const PROPERTY_TEMPLATE_ID = 'template_id';
  11. const CONTEXT_USERS = 'context_users';
  12. const LEVELS = 'levels';
  13. const CONTEXT_LEVEL = 'context_level';
  14. const HIERARCHY = 'hierarchy';
  15. const CONTEXT_PATHS = 'context_paths';
  16. const PARENT_CHILD_IDS = 'parent_child';
  17. const PARENT_CHILD_CONTEXT_PATHS = 'parent_child_context_paths';
  18. const PARENT_ID = 'parent_id';
  19. private $additionalProperties;
  20. public function __construct($defaultProperties = array (), $additionalProperties = null)
  21. {
  22. parent :: __construct($defaultProperties);
  23. if (isset($additionalProperties))
  24. {
  25. $this->additionalProperties = $additionalProperties;
  26. }
  27. }
  28. abstract static function get_additional_property_names($with_context_type = false);
  29. public function create()
  30. {
  31. $dm = SurveyContextDataManager :: get_instance();
  32. if (! $dm->create_survey_template_user($this))
  33. {
  34. return false;
  35. }
  36. else
  37. {
  38. return true;
  39. }
  40. }
  41. public function delete()
  42. {
  43. $dm = SurveyContextDataManager :: get_instance();
  44. if (! $dm->delete_survey_template_user($this))
  45. {
  46. return false;
  47. }
  48. else
  49. {
  50. return true;
  51. }
  52. }
  53. public function update()
  54. {
  55. $dm = SurveyContextDataManager :: get_instance();
  56. if (! $dm->update_survey_template_user($this))
  57. {
  58. return false;
  59. }
  60. else
  61. {
  62. return true;
  63. }
  64. }
  65. static function factory($type, $defaultProperties = array(), $additionalProperties = null)
  66. {
  67. $class = self :: type_to_class($type);
  68. require_once dirname(__FILE__) . '/template/' . $type . '/' . $type . '.class.php';
  69. return new $class($defaultProperties, $additionalProperties);
  70. }
  71. static function type_to_class($type)
  72. {
  73. return __NAMESPACE__ . '\\' . Utilities :: underscores_to_camelcase($type);
  74. }
  75. static function class_to_type($class)
  76. {
  77. return Utilities :: get_classname_from_namespace($class, true);
  78. }
  79. function get_user_id()
  80. {
  81. return $this->get_default_property(self :: PROPERTY_USER_ID);
  82. }
  83. function set_user_id($user_id)
  84. {
  85. $this->set_default_property(self :: PROPERTY_USER_ID, $user_id);
  86. }
  87. function get_type()
  88. {
  89. return self :: class_to_type(get_class($this));
  90. }
  91. function get_template_id()
  92. {
  93. return $this->get_default_property(self :: PROPERTY_TEMPLATE_ID);
  94. }
  95. function set_template_id($template_id)
  96. {
  97. $this->set_default_property(self :: PROPERTY_TEMPLATE_ID, $template_id);
  98. }
  99. static function get_default_property_names()
  100. {
  101. return parent :: get_default_property_names(array(self :: PROPERTY_TYPE, self :: PROPERTY_USER_ID,
  102. self :: PROPERTY_TEMPLATE_ID));
  103. }
  104. /**
  105. * inherited
  106. */
  107. function get_data_manager()
  108. {
  109. return SurveyContextDataManager :: get_instance();
  110. }
  111. static function get_table_name()
  112. {
  113. return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
  114. }
  115. function get_additional_property($name)
  116. {
  117. $this->check_for_additional_properties();
  118. return $this->additionalProperties[$name];
  119. }
  120. function set_additional_property($name, $value)
  121. {
  122. //$this->check_for_additional_properties();
  123. $this->additionalProperties[$name] = $value;
  124. }
  125. function get_additional_properties()
  126. {
  127. $this->check_for_additional_properties();
  128. return $this->additionalProperties;
  129. }
  130. private function check_for_additional_properties()
  131. {
  132. if (isset($this->additionalProperties))
  133. {
  134. return;
  135. }
  136. $dm = SurveyContextDataManager :: get_instance();
  137. $this->additionalProperties = $dm->retrieve_additional_survey_template_user_properties($this);
  138. }
  139. public static function get_by_id($survey_template_user_id, $type)
  140. {
  141. $dm = SurveyContextDataManager :: get_instance();
  142. return $dm->retrieve_survey_template_user_by_id($survey_template_user_id, $type);
  143. }
  144. public static function get_context_hierarchy($type, $condition = null)
  145. {
  146. $result = array();
  147. $template_users = SurveyContextDataManager :: get_instance()->retrieve_survey_template_users($type, $condition);
  148. $hierarchy = array();
  149. $context_users = array();
  150. $levels = array();
  151. $context_levels = array();
  152. $strict_parent_child_context_ids = array();
  153. $parent_ids = array();
  154. $parent_child_context_paths = array();
  155. $index = 0;
  156. while ($template_user = $template_users->next_result())
  157. {
  158. // $index ++;
  159. // if ($index == 50)
  160. // {
  161. // break;
  162. // }
  163. $parent_context = 0;
  164. $users_id = $template_user->get_user_id();
  165. $property_names = $template_user->get_additional_property_names();
  166. $context_ids = array();
  167. $path = null;
  168. $level = 1;
  169. foreach ($property_names as $name)
  170. {
  171. $context_id = $template_user->get_additional_property($name);
  172. $context_levels[$context_id] = $level;
  173. $level ++;
  174. $parent_ids[$context_id] = $parent_context;
  175. if (! in_array($context_id, $strict_parent_child_context_ids[$parent_context]))
  176. {
  177. $strict_parent_child_context_ids[$parent_context][] = $context_id;
  178. }
  179. if ($path)
  180. {
  181. $path = $path . '_' . $context_id;
  182. }
  183. else
  184. {
  185. $path = (string) $context_id;
  186. }
  187. $path = (string) $path;
  188. // if (! in_array($context_id, $levels[$name][$path]))
  189. // {
  190. $levels[$name][$path] = $context_id;
  191. // }
  192. // if (! in_array($users_id, $context_users[$context_id]))
  193. // {
  194. // $context_users[$context_id][] = $users_id;
  195. // }
  196. if (! in_array($path, $parent_child_context_paths[$parent_context]))
  197. {
  198. $parent_child_context_paths[$parent_context][] = $path;
  199. }
  200. if (! in_array($users_id, $context_users[$path]))
  201. {
  202. $context_users[$path][] = $users_id;
  203. }
  204. // if (! in_array($path, $hierarchy))
  205. // {
  206. $hierarchy[] = $path;
  207. // }
  208. $parent_context = $context_id;
  209. }
  210. }
  211. // dump($levels);
  212. // exit;
  213. $result[self :: CONTEXT_USERS] = $context_users;
  214. $result[self :: LEVELS] = $levels;
  215. $result[self :: PARENT_CHILD_IDS] = $strict_parent_child_context_ids;
  216. $result[self :: CONTEXT_LEVEL] = $context_levels;
  217. $result[self :: PARENT_ID] = $parent_ids;
  218. $result[self :: PARENT_CHILD_CONTEXT_PATHS] = $parent_child_context_paths;
  219. $hierarchy = array_unique($hierarchy);
  220. $parent_child_context_ids = array();
  221. foreach ($hierarchy as $path)
  222. {
  223. $context_ids = explode('_', $path);
  224. $context_count = count($context_ids);
  225. while ($context_count > 0)
  226. {
  227. $context_count --;
  228. $context_id = array_shift($context_ids);
  229. $attached_context_ids = $parent_child_context_ids[$context_id];
  230. if (count($attached_context_ids) > 0)
  231. {
  232. $merged_array = array_merge($context_ids, $attached_context_ids);
  233. $parent_child_context_ids[$context_id] = array_unique($merged_array);
  234. }
  235. else
  236. {
  237. $parent_child_context_ids[$context_id] = array_unique($context_ids);
  238. }
  239. }
  240. }
  241. // foreach ($hierarchy as $path)
  242. // {
  243. // $context_ids = explode('_', $path);
  244. // $context_id = array_pop($context_ids);
  245. // $path_child_context_ids[$path] = $parent_child_context_ids[$context_id];
  246. // }
  247. $result[self :: HIERARCHY] = $parent_child_context_ids;
  248. $result[self :: CONTEXT_PATHS] = $hierarchy;
  249. return $result;
  250. }
  251. }
  252. ?>