PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/agreement.class.php

https://bitbucket.org/chamilo/chamilo-app-internship-organizer-dev/
PHP | 326 lines | 202 code | 48 blank | 76 comment | 11 complexity | bc64d3513ca22fc8012e246b73c57bfe MD5 | raw file
  1. <?php
  2. namespace application\internship_organizer;
  3. use rights\RightsUtilities;
  4. use common\libraries\DataClass;
  5. use common\libraries\Translation;
  6. use common\libraries\EqualityCondition;
  7. use common\libraries\AndCondition;
  8. use common\libraries\InCondition;
  9. class InternshipOrganizerAgreement extends DataClass
  10. {
  11. const CLASS_NAME = __CLASS__;
  12. /**
  13. * InternshipAgreement properties
  14. */
  15. const PROPERTY_ID = 'id';
  16. const PROPERTY_NAME = 'agreement_name';
  17. const PROPERTY_DESCRIPTION = 'agreement_description';
  18. const PROPERTY_BEGIN = 'agreement_begin';
  19. const PROPERTY_END = 'agreement_end';
  20. const PROPERTY_PERIOD_ID = 'agreement_period_id';
  21. const PROPERTY_STATUS = 'agreement_status';
  22. const PROPERTY_OWNER = 'agreement_owner_id';
  23. const STATUS_ADD_LOCATION = 1;
  24. const STATUS_TO_APPROVE = 2;
  25. const STATUS_APPROVED = 3;
  26. public function create()
  27. {
  28. $succes = parent :: create();
  29. if ($succes)
  30. {
  31. $parent_location = InternshipOrganizerRights :: get_internship_organizers_subtree_root_id();
  32. $location = InternshipOrganizerRights :: create_location_in_internship_organizers_subtree($this->get_name(), $this->get_id(), $parent_location, InternshipOrganizerRights :: TYPE_AGREEMENT, true);
  33. $rights = InternshipOrganizerRights :: get_available_rights_for_agreements();
  34. foreach ($rights as $right)
  35. {
  36. RightsUtilities :: set_user_right_location_value($right, $this->get_owner(), $location->get_id(), 1);
  37. }
  38. }
  39. return $succes;
  40. }
  41. public function delete()
  42. {
  43. $location = InternshipOrganizerRights :: get_location_by_identifier_from_internship_organizers_subtree($this->get_id(), InternshipOrganizerRights :: TYPE_AGREEMENT);
  44. if ($location)
  45. {
  46. if (! $location->remove())
  47. {
  48. return false;
  49. }
  50. }
  51. $succes = parent :: delete();
  52. return $succes;
  53. }
  54. /**
  55. * Get the default properties
  56. * @return array The property names.
  57. */
  58. static function get_default_property_names($extended_property_names = array())
  59. {
  60. return array(self :: PROPERTY_ID, self :: PROPERTY_NAME, self :: PROPERTY_DESCRIPTION, self :: PROPERTY_BEGIN,
  61. self :: PROPERTY_END, self :: PROPERTY_PERIOD_ID, self :: PROPERTY_STATUS);
  62. }
  63. function get_data_manager()
  64. {
  65. return InternshipOrganizerDataManager :: get_instance();
  66. }
  67. /**
  68. * Returns the id of this InternshipAgreement.
  69. * @return the id.
  70. */
  71. function get_id()
  72. {
  73. return $this->get_default_property(self :: PROPERTY_ID);
  74. }
  75. /**
  76. * Sets the id of this InternshipAgreement.
  77. * @param id
  78. */
  79. function set_id($id)
  80. {
  81. $this->set_default_property(self :: PROPERTY_ID, $id);
  82. }
  83. /**
  84. * Returns the name of this InternshipAgreement.
  85. * @return the name.
  86. */
  87. function get_name()
  88. {
  89. return $this->get_default_property(self :: PROPERTY_NAME);
  90. }
  91. /**
  92. * Sets the name of this InternshipAgreement.
  93. * @param name
  94. */
  95. function set_name($name)
  96. {
  97. $this->set_default_property(self :: PROPERTY_NAME, $name);
  98. }
  99. /**
  100. * Returns the description of this InternshipAgreement.
  101. * @return the description.
  102. */
  103. function get_description()
  104. {
  105. return $this->get_default_property(self :: PROPERTY_DESCRIPTION);
  106. }
  107. /**
  108. * Sets the description of this InternshipAgreement.
  109. * @param description
  110. */
  111. function set_description($description)
  112. {
  113. $this->set_default_property(self :: PROPERTY_DESCRIPTION, $description);
  114. }
  115. /**
  116. * Returns the begin of this InternshipAgreement.
  117. * @return begin.
  118. */
  119. function get_begin()
  120. {
  121. return $this->get_default_property(self :: PROPERTY_BEGIN);
  122. }
  123. /**
  124. * Sets the begin of this InternshipAgreement.
  125. * @param begin
  126. */
  127. function set_begin($begin)
  128. {
  129. $this->set_default_property(self :: PROPERTY_BEGIN, $begin);
  130. }
  131. /**
  132. * Returns the end of this InternshipAgreement.
  133. * @return end.
  134. */
  135. function get_end()
  136. {
  137. return $this->get_default_property(self :: PROPERTY_END);
  138. }
  139. /**
  140. * Sets the end of this InternshipAgreement.
  141. * @param end
  142. */
  143. function set_end($end)
  144. {
  145. $this->set_default_property(self :: PROPERTY_END, $end);
  146. }
  147. /**
  148. * Returns the period_id of this InternshipAgreement.
  149. * @return period_id.
  150. */
  151. function get_period_id()
  152. {
  153. return $this->get_default_property(self :: PROPERTY_PERIOD_ID);
  154. }
  155. /**
  156. * Sets the period_id of this InternshipAgreement.
  157. * @param period_id
  158. */
  159. function set_period_id($period_id)
  160. {
  161. $this->set_default_property(self :: PROPERTY_PERIOD_ID, $period_id);
  162. }
  163. /**
  164. * Returns the status of this InternshipAgreement.
  165. * @return status.
  166. */
  167. function get_status()
  168. {
  169. return $this->get_default_property(self :: PROPERTY_STATUS);
  170. }
  171. /**
  172. * Sets the status of this InternshipAgreement.
  173. * @param status
  174. */
  175. function set_status($status)
  176. {
  177. $this->set_default_property(self :: PROPERTY_STATUS, $status);
  178. }
  179. /**
  180. * Returns the owner of this InternshipAgreement.
  181. * @return owner.
  182. */
  183. function get_owner()
  184. {
  185. return $this->get_default_property(self :: PROPERTY_OWNER);
  186. }
  187. /**
  188. * Sets the owner of this InternshipAgreement.
  189. * @param owner
  190. */
  191. function set_owner($owner)
  192. {
  193. $this->set_default_property(self :: PROPERTY_OWNER, $owner);
  194. }
  195. static function get_status_name($index)
  196. {
  197. switch ($index)
  198. {
  199. case 1 :
  200. return Translation :: get('InternshipOrganizerAgreementAddLocation');
  201. // break;
  202. case 2 :
  203. return Translation :: get('InternshipOrganizerAgreementToApprove');
  204. // break;
  205. case 3 :
  206. return Translation :: get('InternshipOrganizerAgreementApproved');
  207. // break;
  208. default :
  209. //no default
  210. break;
  211. }
  212. }
  213. function update_status()
  214. {
  215. $dm = $this->get_data_manager();
  216. $condition = new EqualityCondition(InternshipOrganizerAgreementRelLocation :: PROPERTY_AGREEMENT_ID, $this->get_id());
  217. $count = $dm->count_agreement_rel_locations($condition);
  218. if ($count == 0)
  219. {
  220. $this->set_status(self :: STATUS_ADD_LOCATION);
  221. return $this->update();
  222. }
  223. $conditions = array();
  224. $conditions[] = new EqualityCondition(InternshipOrganizerAgreementRelLocation :: PROPERTY_AGREEMENT_ID, $this->get_id());
  225. $conditions[] = new EqualityCondition(InternshipOrganizerAgreementRelLocation :: PROPERTY_LOCATION_TYPE, InternshipOrganizerAgreementRelLocation :: APPROVED);
  226. $condition = new AndCondition($conditions);
  227. $count = $dm->count_agreement_rel_locations($condition);
  228. if ($count == 0)
  229. {
  230. $this->set_status(self :: STATUS_TO_APPROVE);
  231. return $this->update();
  232. }
  233. $this->set_status(self :: STATUS_APPROVED);
  234. return $this->update();
  235. }
  236. function get_user_ids($user_types)
  237. {
  238. if (! is_array($user_types))
  239. {
  240. $user_types = array($user_types);
  241. }
  242. $target_users = array();
  243. $type_index = $conditions = array();
  244. $conditions[] = new EqualityCondition(InternshipOrganizerAgreementRelUser :: PROPERTY_AGREEMENT_ID, $this->get_id());
  245. $conditions[] = new InCondition(InternshipOrganizerAgreementRelUser :: PROPERTY_USER_TYPE, $user_types);
  246. $condition = new AndCondition($conditions);
  247. $agreement_rel_users = $this->get_data_manager()->retrieve_agreement_rel_users($condition);
  248. while ($agreement_rel_user = $agreement_rel_users->next_result())
  249. {
  250. $target_users[] = $agreement_rel_user->get_user_id();
  251. }
  252. return array_unique($target_users);
  253. }
  254. function is_user_type($use_type, $user_id)
  255. {
  256. return in_array($user_id, $this->get_user_ids($use_type));
  257. }
  258. function get_location()
  259. {
  260. $conditions[] = new EqualityCondition(InternshipOrganizerAgreementRelLocation :: PROPERTY_AGREEMENT_ID, $this->get_id());
  261. $conditions[] = new EqualityCondition(InternshipOrganizerAgreementRelLocation :: PROPERTY_LOCATION_TYPE, InternshipOrganizerAgreementRelLocation :: APPROVED);
  262. $condition = new AndCondition($conditions);
  263. $agreement_rel_location = $this->get_data_manager()->retrieve_agreement_rel_locations($condition)->next_result();
  264. if (isset($agreement_rel_location))
  265. {
  266. return $agreement_rel_location->get_location();
  267. }
  268. else
  269. {
  270. return null;
  271. }
  272. }
  273. static function get_table_name()
  274. {
  275. return 'agreement';
  276. // return Utilities::camelcase_to_underscores ( self::CLASS_NAME );
  277. }
  278. }