PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/gamification/rules/GamificationRules.php

https://bitbucket.org/sanbrar/zurmo_invoice
PHP | 262 lines | 140 code | 22 blank | 100 comment | 7 complexity | d51688538de14ca46bb14d58f4853389 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * Base class defining rules for gamification behavior.
  28. */
  29. class GamificationRules
  30. {
  31. /**
  32. * Score category used for when a model is created
  33. * @var string
  34. */
  35. const SCORE_CATEGORY_CREATE_MODEL = 'CreateModel';
  36. /**
  37. * Score category used for when a model is updated
  38. * @var string
  39. */
  40. const SCORE_CATEGORY_UPDATE_MODEL = 'UpdateModel';
  41. /**
  42. * Score category used for when a user logs into the system.
  43. * @var string
  44. */
  45. const SCORE_CATEGORY_LOGIN_USER = 'LoginUser';
  46. /**
  47. * Score category used for when a user performs a mass edit in a module
  48. * @var string
  49. */
  50. const SCORE_CATEGORY_MASS_EDIT = 'MassEdit';
  51. /**
  52. * Score category used for when a user searches in a module
  53. * @var string
  54. */
  55. const SCORE_CATEGORY_SEARCH = 'Search';
  56. /**
  57. * Score category used for when a user imports into a module
  58. * @var string
  59. */
  60. const SCORE_CATEGORY_IMPORT = 'Import';
  61. /**
  62. * Score category used for when a user performs a time sensitive action such as completing a task before the
  63. * due date.
  64. * @var string
  65. */
  66. const SCORE_CATEGORY_TIME_SENSITIVE_ACTION = 'TimeSensitiveAction';
  67. /**
  68. * Given a model class name attach scoring events to that class. Every model will then invoke the scoring event.
  69. * @param string $modelClassName
  70. */
  71. public function attachScoringEventsByModelClassName($modelClassName)
  72. {
  73. assert('is_string($modelClassName)');
  74. $modelClassName::model()->attachEventHandler('onAfterSave', array($this, 'scoreOnSaveModel'));
  75. }
  76. /**
  77. * Given a event, perform the onSave score logic for a model ($event->sender)
  78. * @param CEvent $event
  79. */
  80. public function scoreOnSaveModel(CEvent $event)
  81. {
  82. $model = $event->sender;
  83. assert('$model instanceof Item');
  84. if (Yii::app()->gameHelper->isScoringModelsOnSaveMuted())
  85. {
  86. return;
  87. }
  88. if ($model->getIsNewModel())
  89. {
  90. $scoreType = static::resolveCreateScoreTypeByModel($model);
  91. $category = static::SCORE_CATEGORY_CREATE_MODEL;
  92. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  93. }
  94. else
  95. {
  96. $scoreType = static::resolveUpdateScoreTypeByModel($model);
  97. $category = static::SCORE_CATEGORY_UPDATE_MODEL;
  98. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  99. }
  100. $gameScore->addValue();
  101. $saved = $gameScore->save();
  102. if (!$saved)
  103. {
  104. throw new FailedToSaveModelException();
  105. }
  106. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  107. static::getPointTypeAndValueDataByCategory($category));
  108. }
  109. protected static function resolveCreateScoreTypeByModel($model)
  110. {
  111. return 'Create' . get_class($model);
  112. }
  113. protected static function resolveUpdateScoreTypeByModel($model)
  114. {
  115. return 'Update' . get_class($model);
  116. }
  117. /**
  118. * Given a score type and score category @return the corresponding point type and value as an array indexed
  119. * by the point type.
  120. * @param string $type
  121. * @param string $category
  122. */
  123. public static function getPointTypeAndValueDataByCategory($category)
  124. {
  125. assert('is_string($category)');
  126. $methodName = 'getPointTypesAndValuesFor' . $category;
  127. if (method_exists(get_called_class(), $methodName))
  128. {
  129. return static::$methodName();
  130. }
  131. else
  132. {
  133. throw new NotImplementedException();
  134. }
  135. }
  136. /**
  137. * @return Point type/value data for generically creating a model.
  138. */
  139. public static function getPointTypesAndValuesForCreateModel()
  140. {
  141. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  142. }
  143. /**
  144. * @return Point type/value data for generically updating a model.
  145. */
  146. public static function getPointTypesAndValuesForUpdateModel()
  147. {
  148. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  149. }
  150. /**
  151. * @return Point type/value data for a user logging in.
  152. */
  153. public static function getPointTypesAndValuesForLoginUser()
  154. {
  155. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  156. }
  157. /**
  158. * @return Point type/value data for a user searching in a module.
  159. */
  160. public static function getPointTypesAndValuesForSearch()
  161. {
  162. return array(GamePoint::TYPE_USER_ADOPTION => 5);
  163. }
  164. /**
  165. * @return Point type/value data for a user performing a mass update in a module.
  166. */
  167. public static function getPointTypesAndValuesForMassEdit()
  168. {
  169. return array(GamePoint::TYPE_USER_ADOPTION => 15);
  170. }
  171. /**
  172. * @return Point type/value data for a user importing into a module
  173. */
  174. public static function getPointTypesAndValuesForImport()
  175. {
  176. return array(GamePoint::TYPE_USER_ADOPTION => 25);
  177. }
  178. /**
  179. * @return Point type/value data for a user performing a time-sensitive action
  180. */
  181. public static function getPointTypesAndValuesForTimeSensitiveAction()
  182. {
  183. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  184. }
  185. /**
  186. * @param string $modelClassName
  187. */
  188. public static function scoreOnSearchModels($modelClassName)
  189. {
  190. assert('is_string($modelClassName)');
  191. $scoreType = 'Search' . $modelClassName;
  192. $category = static::SCORE_CATEGORY_SEARCH;
  193. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  194. $gameScore->addValue();
  195. $saved = $gameScore->save();
  196. if (!$saved)
  197. {
  198. throw new FailedToSaveModelException();
  199. }
  200. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  201. static::getPointTypeAndValueDataByCategory($category));
  202. }
  203. /**
  204. * @param string $modelClassName
  205. */
  206. public static function scoreOnMassEditModels($modelClassName)
  207. {
  208. assert('is_string($modelClassName)');
  209. $scoreType = 'MassEdit' . $modelClassName;
  210. $category = static::SCORE_CATEGORY_MASS_EDIT;
  211. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  212. $gameScore->addValue();
  213. $saved = $gameScore->save();
  214. if (!$saved)
  215. {
  216. throw new FailedToSaveModelException();
  217. }
  218. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  219. static::getPointTypeAndValueDataByCategory($category));
  220. }
  221. /**
  222. * @param string $modelClassName
  223. */
  224. public static function scoreOnImportModels($modelClassName)
  225. {
  226. assert('is_string($modelClassName)');
  227. $scoreType = 'Import' . $modelClassName;
  228. $category = static::SCORE_CATEGORY_IMPORT;
  229. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  230. $gameScore->addValue();
  231. $saved = $gameScore->save();
  232. if (!$saved)
  233. {
  234. throw new FailedToSaveModelException();
  235. }
  236. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  237. static::getPointTypeAndValueDataByCategory($category));
  238. }
  239. }
  240. ?>