PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/wildanm/zurmo
PHP | 297 lines | 161 code | 25 blank | 111 comment | 8 complexity | 98f28406e3d66de79051479e8de0bb2a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-3.0, BSD-3-Clause
  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 performs a mass delete in a module
  53. * @var string
  54. */
  55. const SCORE_CATEGORY_MASS_DELETE = 'MassDelete';
  56. /**
  57. * Score category used for when a user searches in a module
  58. * @var string
  59. */
  60. const SCORE_CATEGORY_SEARCH = 'Search';
  61. /**
  62. * Score category used for when a user imports into a module
  63. * @var string
  64. */
  65. const SCORE_CATEGORY_IMPORT = 'Import';
  66. /**
  67. * Score category used for when a user performs a time sensitive action such as completing a task before the
  68. * due date.
  69. * @var string
  70. */
  71. const SCORE_CATEGORY_TIME_SENSITIVE_ACTION = 'TimeSensitiveAction';
  72. /**
  73. * Whether to score when a model is updated or not.
  74. * @var boolean
  75. */
  76. protected $scoreOnUpdate = true;
  77. /**
  78. * Given a model class name attach scoring events to that class. Every model will then invoke the scoring event.
  79. * @param string $modelClassName
  80. */
  81. public function attachScoringEventsByModelClassName($modelClassName)
  82. {
  83. assert('is_string($modelClassName)');
  84. $modelClassName::model()->attachEventHandler('onAfterSave', array($this, 'scoreOnSaveModel'));
  85. }
  86. /**
  87. * Given a event, perform the onSave score logic for a model ($event->sender)
  88. * @param CEvent $event
  89. */
  90. public function scoreOnSaveModel(CEvent $event)
  91. {
  92. $model = $event->sender;
  93. assert('$model instanceof Item');
  94. if (Yii::app()->gameHelper->isScoringModelsOnSaveMuted())
  95. {
  96. return;
  97. }
  98. if ($model->getIsNewModel())
  99. {
  100. $scoreType = static::resolveCreateScoreTypeByModel($model);
  101. $category = static::SCORE_CATEGORY_CREATE_MODEL;
  102. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  103. }
  104. elseif (!$this->scoreOnUpdate)
  105. {
  106. return;
  107. }
  108. else
  109. {
  110. $scoreType = static::resolveUpdateScoreTypeByModel($model);
  111. $category = static::SCORE_CATEGORY_UPDATE_MODEL;
  112. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  113. }
  114. $gameScore->addValue();
  115. $saved = $gameScore->save();
  116. if (!$saved)
  117. {
  118. throw new FailedToSaveModelException();
  119. }
  120. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  121. static::getPointTypeAndValueDataByCategory($category));
  122. }
  123. protected static function resolveCreateScoreTypeByModel($model)
  124. {
  125. return 'Create' . get_class($model);
  126. }
  127. protected static function resolveUpdateScoreTypeByModel($model)
  128. {
  129. return 'Update' . get_class($model);
  130. }
  131. /**
  132. * Given a score type and score category @return the corresponding point type and value as an array indexed
  133. * by the point type.
  134. * @param string $type
  135. * @param string $category
  136. */
  137. public static function getPointTypeAndValueDataByCategory($category)
  138. {
  139. assert('is_string($category)');
  140. $methodName = 'getPointTypesAndValuesFor' . $category;
  141. if (method_exists(get_called_class(), $methodName))
  142. {
  143. return static::$methodName();
  144. }
  145. else
  146. {
  147. throw new NotImplementedException();
  148. }
  149. }
  150. /**
  151. * @return Point type/value data for generically creating a model.
  152. */
  153. public static function getPointTypesAndValuesForCreateModel()
  154. {
  155. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  156. }
  157. /**
  158. * @return Point type/value data for generically updating a model.
  159. */
  160. public static function getPointTypesAndValuesForUpdateModel()
  161. {
  162. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  163. }
  164. /**
  165. * @return Point type/value data for a user logging in.
  166. */
  167. public static function getPointTypesAndValuesForLoginUser()
  168. {
  169. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  170. }
  171. /**
  172. * @return Point type/value data for a user searching in a module.
  173. */
  174. public static function getPointTypesAndValuesForSearch()
  175. {
  176. return array(GamePoint::TYPE_USER_ADOPTION => 5);
  177. }
  178. /**
  179. * @return Point type/value data for a user performing a mass update in a module.
  180. */
  181. public static function getPointTypesAndValuesForMassEdit()
  182. {
  183. return array(GamePoint::TYPE_USER_ADOPTION => 15);
  184. }
  185. /**
  186. * @return Point type/value data for a user importing into a module
  187. */
  188. public static function getPointTypesAndValuesForImport()
  189. {
  190. return array(GamePoint::TYPE_USER_ADOPTION => 25);
  191. }
  192. /**
  193. * @return Point type/value data for a user performing a time-sensitive action
  194. */
  195. public static function getPointTypesAndValuesForTimeSensitiveAction()
  196. {
  197. return array(GamePoint::TYPE_USER_ADOPTION => 10);
  198. }
  199. /**
  200. * @param string $modelClassName
  201. */
  202. public static function scoreOnSearchModels($modelClassName)
  203. {
  204. assert('is_string($modelClassName)');
  205. $scoreType = 'Search' . $modelClassName;
  206. $category = static::SCORE_CATEGORY_SEARCH;
  207. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  208. $gameScore->addValue();
  209. $saved = $gameScore->save();
  210. if (!$saved)
  211. {
  212. throw new FailedToSaveModelException();
  213. }
  214. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  215. static::getPointTypeAndValueDataByCategory($category));
  216. }
  217. /**
  218. * @param string $modelClassName
  219. */
  220. public static function scoreOnMassEditModels($modelClassName)
  221. {
  222. assert('is_string($modelClassName)');
  223. $scoreType = 'MassEdit' . $modelClassName;
  224. $category = static::SCORE_CATEGORY_MASS_EDIT;
  225. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  226. $gameScore->addValue();
  227. $saved = $gameScore->save();
  228. if (!$saved)
  229. {
  230. throw new FailedToSaveModelException();
  231. }
  232. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  233. static::getPointTypeAndValueDataByCategory($category));
  234. }
  235. /**
  236. * @param string $modelClassName(mass delete)
  237. */
  238. public static function scoreOnMassDeleteModels($modelClassName)
  239. {
  240. assert('is_string($modelClassName)');
  241. $scoreType = 'MassDelete' . $modelClassName;
  242. $category = static::SCORE_CATEGORY_MASS_DELETE;
  243. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  244. $gameScore->addValue();
  245. $saved = $gameScore->save();
  246. if (!$saved)
  247. {
  248. throw new FailedToSaveModelException();
  249. }
  250. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  251. static::getPointTypeAndValueDataByCategory($category));
  252. }
  253. /**
  254. * @param string $modelClassName
  255. */
  256. public static function scoreOnImportModels($modelClassName)
  257. {
  258. assert('is_string($modelClassName)');
  259. $scoreType = 'Import' . $modelClassName;
  260. $category = static::SCORE_CATEGORY_IMPORT;
  261. $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel);
  262. $gameScore->addValue();
  263. $saved = $gameScore->save();
  264. if (!$saved)
  265. {
  266. throw new FailedToSaveModelException();
  267. }
  268. GamePointUtil::addPointsByPointData(Yii::app()->user->userModel,
  269. static::getPointTypeAndValueDataByCategory($category));
  270. }
  271. }
  272. ?>