Commits

Jason Green committed 946ac20

Adding afterLogin scoring.

Comments (0)

Files changed (6)

app/protected/config/common.php

                 'allowAutoLogin' => true,
                 'class'          => 'WebUser',
                 'loginUrl'       => array('zurmo/default/login'),
+        'behaviors' => array(
+            'onAfterLogin' => array(
+                'class' => 'application.modules.gamification.behaviors.WebUserAfterLoginGamificationBehavior'
+            ),
+        ),
             ),
             'widgetFactory' => array(
                 'widgets' => array(

app/protected/modules/gamification/behaviors/WebUserAfterLoginGamificationBehavior.php

+<?php
+    /*********************************************************************************
+     * Zurmo is a customer relationship management program developed by
+     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
+     *
+     * Zurmo is free software; you can redistribute it and/or modify it under
+     * the terms of the GNU General Public License version 3 as published by the
+     * Free Software Foundation with the addition of the following permission added
+     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
+     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
+     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
+     *
+     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
+     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+     * details.
+     *
+     * You should have received a copy of the GNU General Public License along with
+     * this program; if not, see http://www.gnu.org/licenses or write to the Free
+     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+     * 02110-1301 USA.
+     *
+     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
+     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
+     ********************************************************************************/
+
+    /**
+     * Attaches events to the WebUser as needed for gamification.
+     */
+    class WebUserAfterLoginGamificationBehavior extends CBehavior
+    {
+        public function attach($owner)
+        {
+            $owner->attachEventHandler('onAfterLogin', array($this, 'handleScoreLogin'));
+        }
+
+        /**
+         * The login of a user is a scored game event.  This method processes this.
+         * @param CEvent $event
+         */
+        public function handleScoreLogin($event)
+        {
+            $scoreType           = 'LoginUser';
+            $category            = GamificationRules::SCORE_CATEGORY_LOGIN_USER;
+            $gameScore           = GameScore::resolveToGetByTypeAndUser($scoreType, Yii::app()->user->userModel);
+            $gameScore->addValue();
+            $saved = $gameScore->save();
+            if(!$saved)
+            {
+                throw new NotSupportedException();
+            }
+            GamePointUtil::addPointsByGameScore($gameScore->type, Yii::app()->user->userModel,
+                                                'GamificationRules', $category);
+        }
+     }
+?>

app/protected/modules/gamification/observers/GamificationScoringObserver.php

             {
                 throw new NotSupportedException();
             }
-            $this->addPointsByGameScore($gameScore->type, Yii::app()->user->userModel,
-                                        $this->gamificationRulesClassName, $category);
-        }
-
-        protected function addPointsByGameScore($type, User $user, $gamificationRulesClassName, $category)
-        {
-            assert('is_string($type)');
-            assert('$user->id > 0');
-            assert('is_string($gamificationRulesClassName)');
-            assert('is_string($category)');
-            $pointTypeAndValueData = $gamificationRulesClassName::
-                                        getPointTypeAndValueDataByScoreTypeAndCategory($type, $category);
-            foreach($pointTypeAndValueData as $type => $value)
-            {
-                GamePointManager::addPointsByUserDeferred($user, $type, $value);
-            }
+            GamePointUtil::addPointsByGameScore($gameScore->type, Yii::app()->user->userModel,
+                                                $gamificationRulesClassName, $category);
         }
     }
 ?>

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

 
         const SCORE_CATEGORY_UPDATE_MODEL = 'UpdateModel';
 
+        const SCORE_CATEGORY_LOGIN_USER   = 'LoginUser';
+
         /**
          * @returns The observer name used for scoring.
          */
         {
             return array(GamePoint::TYPE_USER_ADOPTION => 10);
         }
+
+        public static function getPointTypesAndValuesForLoginUser()
+        {
+            return array(GamePoint::TYPE_USER_ADOPTION => 5);
+        }
     }
 ?>

app/protected/modules/gamification/utils/GamePointUtil.php

+<?php
+    /*********************************************************************************
+     * Zurmo is a customer relationship management program developed by
+     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
+     *
+     * Zurmo is free software; you can redistribute it and/or modify it under
+     * the terms of the GNU General Public License version 3 as published by the
+     * Free Software Foundation with the addition of the following permission added
+     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
+     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
+     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
+     *
+     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
+     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+     * details.
+     *
+     * You should have received a copy of the GNU General Public License along with
+     * this program; if not, see http://www.gnu.org/licenses or write to the Free
+     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+     * 02110-1301 USA.
+     *
+     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
+     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
+     ********************************************************************************/
+
+    /**
+     * Helper class for working with game points.
+     */
+    class GamePointUtil
+    {
+        public static function addPointsByGameScore($type, User $user, $gamificationRulesClassName, $category)
+        {
+            assert('is_string($type)');
+            assert('$user->id > 0');
+            assert('is_string($gamificationRulesClassName)');
+            assert('is_string($category)');
+            $pointTypeAndValueData = $gamificationRulesClassName::
+                                        getPointTypeAndValueDataByScoreTypeAndCategory($type, $category);
+            foreach($pointTypeAndValueData as $type => $value)
+            {
+                GamePointManager::addPointsByUserDeferred($user, $type, $value);
+            }
+        }
+    }
+?>

app/protected/modules/zurmo/components/WebUser.php

         protected function afterLogin($fromCookie)
         {
             AuditEvent::logAuditEvent('UsersModule', UsersModule::AUDIT_EVENT_USER_LOGGED_IN);
+            if($this->hasEventHandler('onAfterLogin'))
+            {
+                $this->onAfterLogin(new CEvent($this));
+            }
+        }
+
+        /**
+         * Raised right AFTER the login is completed for a WebUser
+         * @param CEvent $event the event parameter
+         */
+        public function onAfterLogin($event)
+        {
+            $this->raiseEvent('onAfterLogin',$event);
         }
 
         protected function beforeLogout()