PageRenderTime 45ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/event.class.php

https://bitbucket.org/chamilo/chamilo-tracking-dev/
PHP | 224 lines | 130 code | 35 blank | 59 comment | 7 complexity | fd6c7333f1746204ba8229fc6bc92a87 MD5 | raw file
  1. <?php
  2. namespace tracking;
  3. use common\libraries\Utilities;
  4. use common\libraries\DataClass;
  5. use admin\AdminDataManager;
  6. use common\libraries\PlatformSetting;
  7. /**
  8. * $Id: event.class.php 213 2009-11-13 13:38:50Z vanpouckesven $
  9. * @package tracking.lib
  10. */
  11. /**
  12. * This class presents a event
  13. *
  14. * @author Sven Vanpoucke
  15. */
  16. class Event extends DataClass
  17. {
  18. private $trackers;
  19. const CLASS_NAME = __CLASS__;
  20. /**
  21. * Event properties
  22. */
  23. const PROPERTY_NAME = 'name';
  24. const PROPERTY_ACTIVE = 'active';
  25. const PROPERTY_BLOCK = 'block';
  26. /**
  27. * Get the default properties
  28. * @return array The property names.
  29. */
  30. static function get_default_property_names()
  31. {
  32. return parent :: get_default_property_names(array(self :: PROPERTY_NAME, self :: PROPERTY_ACTIVE,
  33. self :: PROPERTY_BLOCK));
  34. }
  35. /**
  36. * inherited
  37. */
  38. function get_data_manager()
  39. {
  40. return TrackingDataManager :: get_instance();
  41. }
  42. /**
  43. * Returns the name of this Event.
  44. * @return the name.
  45. */
  46. function get_name()
  47. {
  48. return $this->get_default_property(self :: PROPERTY_NAME);
  49. }
  50. /**
  51. * Sets the name of this Event.
  52. * @param name
  53. */
  54. function set_name($name)
  55. {
  56. $this->set_default_property(self :: PROPERTY_NAME, $name);
  57. }
  58. /**
  59. * Returns the active of this Event.
  60. * @return the active.
  61. */
  62. function get_active()
  63. {
  64. return $this->get_default_property(self :: PROPERTY_ACTIVE);
  65. }
  66. /**
  67. * Sets the active of this Event.
  68. * @param active
  69. */
  70. function set_active($active)
  71. {
  72. $this->set_default_property(self :: PROPERTY_ACTIVE, $active);
  73. }
  74. /**
  75. * @return boolean
  76. */
  77. function is_active()
  78. {
  79. return $this->get_active() == true;
  80. }
  81. /**
  82. * Returns the block of this Event.
  83. * @return the block.
  84. */
  85. function get_block()
  86. {
  87. return $this->get_default_property(self :: PROPERTY_BLOCK);
  88. }
  89. /**
  90. * Sets the block of this Event.
  91. * @param block
  92. */
  93. function set_block($block)
  94. {
  95. $this->set_default_property(self :: PROPERTY_BLOCK, $block);
  96. }
  97. /**
  98. * Creates this event in the database
  99. */
  100. function create()
  101. {
  102. $trkdmg = TrackingDataManager :: get_instance();
  103. $succes = $trkdmg->create_event($this);
  104. if (! $succes)
  105. {
  106. return false;
  107. }
  108. $parent = TrackingRights :: get_tracking_subtree_root_id();
  109. if (! $parent)
  110. {
  111. TrackingRights :: create_tracking_subtree_root_location();
  112. $parent = TrackingRights :: get_tracking_subtree_root_id();
  113. }
  114. return TrackingRights :: create_location_in_tracking_subtree($this->get_name(), $this->get_id(), $parent);
  115. }
  116. static function get_table_name()
  117. {
  118. return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
  119. }
  120. /**
  121. * @param string $name The name of the event
  122. * @param string $application The name of the application
  123. *
  124. * @return Event The event
  125. */
  126. static function factory($name, $application)
  127. {
  128. return self :: get_data_manager()->retrieve_event_by_name($name, $application);
  129. }
  130. function get_trackers()
  131. {
  132. if (! $this->trackers)
  133. {
  134. $tracker_registrations = $this->get_data_manager()->retrieve_trackers_from_event($this->get_id());
  135. $trackers = array();
  136. foreach ($tracker_registrations as $tracker_registration)
  137. {
  138. $trackers[] = Tracker :: factory($tracker_registration->get_tracker(), $tracker_registration->get_application());
  139. }
  140. $this->trackers = $trackers;
  141. }
  142. return $this->trackers;
  143. }
  144. function set_trackers($trackers)
  145. {
  146. $this->trackers = $trackers;
  147. }
  148. /**
  149. * @deprecated
  150. */
  151. function get_tracker_registrations()
  152. {
  153. return $this->get_data_manager()->retrieve_trackers_from_event($this->get_id());
  154. }
  155. public static function trigger($name, $application, $parameters)
  156. {
  157. return self :: factory($name, $application)->run($parameters);
  158. }
  159. function run($parameters)
  160. {
  161. $setting = PlatformSetting :: get('enable_tracking', TrackingManager :: APPLICATION_NAME);
  162. if (! $setting)
  163. {
  164. return false;
  165. }
  166. if ($this->is_active())
  167. {
  168. $parameters['event'] = $this->get_name();
  169. $data = array();
  170. $trackers = $this->get_trackers();
  171. foreach ($trackers as $tracker)
  172. {
  173. // FIXME: Temporary solution untill all trackers have been converted
  174. if (method_exists($tracker, 'set_event'))
  175. {
  176. $tracker->set_event($this);
  177. }
  178. $tracker->run($parameters);
  179. $data[] = $tracker;
  180. }
  181. return $data;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }
  188. }
  189. ?>