Skip to content

Commit

Permalink
user experience first version added
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius committed Jan 25, 2020
1 parent 52876c0 commit f4a8cf9
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 3 deletions.
9 changes: 8 additions & 1 deletion data/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ INSERT INTO `user_xp_level` (`Level_ID`, `xp_total`) VALUES
(57, 223640),
(58, 230990),
(59, 238460),
(60, 245790);
(60, 245790);

--
-- Default User XP Activities
INSERT INTO `user_xp_activity` (`Activity_ID`, `xp_key`, `label`, `xp_base`) VALUES
(1, 'login', 'Login', 10),
(2, 'user-add', 'Add New User', 50),
(3, 'user-edit', 'Edit User', 5);
17 changes: 17 additions & 0 deletions data/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ ALTER TABLE `user_xp_level`
ALTER TABLE `user_xp_level`
MODIFY `Level_ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

--
-- User XP Activity
--
CREATE TABLE `user_xp_activity` (
`Activity_ID` int(11) NOT NULL,
`xp_key` varchar(100) NOT NULL,
`label` varchar(255) NOT NULL,
`xp_base` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `user_xp_activity`
ADD PRIMARY KEY (`Activity_ID`),
ADD UNIQUE KEY `xp_key` (`xp_key`);

ALTER TABLE `user_xp_activity`
MODIFY `Activity_ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

--
-- Save
--
Expand Down
45 changes: 45 additions & 0 deletions docs/book/experience.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# user experience

We love games ! And we think that people should have fun when they
are at work - so we added some gameification perks to onePlace ;D

And because we think this is important - it is part of Core, to encourage
developers building apps on top of oneplace, to also have some sort
of game design within their app.

Users have Levels, and they can gain experience by activity to raise
level. There are also Achievements, another form of rewarding user activity, coming soon.

You can use the users level to block or unblock certain actions, like
"only level 3 users can add new contacts" and so on, so its also an extension to the
permissions system.

### Default Actions

There are some default xp triggers shipped with oneplace

> +10 for login
> +50 for adding a new user
> +5 for editing a user
### Add your own triggers and activities

You can add your own triggers and activities for the XP system.

If you want to add new activities that users can gain xp from, add them
to `user_xp_activity` table

````sql
INSERT INTO `user_xp_activity` (`Activity_ID`, `xp_key`, `label`, `xp_base`) VALUES (NULL, 'custom-action', 'My Custom Action', '10');
````

Then add the following line of code into all actions you want it to be triggered.
````php
CoreController::$oSession->oUser->addXP('custom-action');
````

This way you can also trigger already existing activities.

Have fun :D
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ nav:
- 'Form Tabs': formtabs.md
- 'Form Fields': formfields.md
- 'Index Columns': indexcols.md
- Gameification:
- 'Experience': experience.md
site_name: oneplace-user
site_description: "onePlace User Module"
repo_url: 'https://github.com/OnePlc/PLC_X_User'
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function loginAction() {

# Login Successful - redirect to Dashboard
CoreController::$oSession->oUser = $oUser;

# Add XP for successful login
$oUser->addXP('login');

return $this->redirect()->toRoute('home');
} else {
# Show Login Form
Expand Down
6 changes: 6 additions & 0 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ public function addAction() {
$aDataFields = (is_array($_REQUEST[$this->sSingleForm.'-formfields'])) ? $_REQUEST[$this->sSingleForm.'-formfields'] : [];
$oUser->updateFormFields($aDataFields);

# Add XP for creating a new user
CoreController::$oSession->oUser->addXP('user-add');

# Log Performance in DB
$aMeasureEnd = getrusage();
$this->logPerfomance('user-save',$this->rutime($aMeasureEnd,CoreController::$aPerfomanceLogStart,"utime"),$this->rutime($aMeasureEnd,CoreController::$aPerfomanceLogStart,"stime"));
Expand Down Expand Up @@ -363,6 +366,9 @@ public function editAction() {
$aDataFields = (is_array($_REQUEST[$this->sSingleForm.'-formfields'])) ? $_REQUEST[$this->sSingleForm.'-formfields'] : [];
$oUser->updateFormFields($aDataFields);

# Add XP for managing a user
CoreController::$oSession->oUser->addXP('user-edit');

# Log Performance in DB
$aMeasureEnd = getrusage();
$this->logPerfomance('user-save',$this->rutime($aMeasureEnd,CoreController::$aPerfomanceLogStart,"utime"),$this->rutime($aMeasureEnd,CoreController::$aPerfomanceLogStart,"stime"));
Expand Down
95 changes: 95 additions & 0 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ class User extends CoreEntityModel {
*/
public $password_reset_date;

/**
* User Gameification - XP Level
*
* @var int $xp_level current level
* @since 1.0.4
*/
public $xp_level;

/**
* User Gameification - Total XP
*
* @var int $xp_total total experience
* @since 1.0.4
*/
public $xp_total;

/**
* User Gameification - Current XP
*
* @var int $xp_current current level experience
* @since 1.0.4
*/
public $xp_current;

/**
* User Selected Theme
*
Expand Down Expand Up @@ -157,6 +181,11 @@ public function exchangeArray(array $data) {
$this->password_reset_token = !empty($data['password_reset_token']) ? $data['password_reset_token'] : '';
$this->password_reset_date = !empty($data['password_reset_date']) ? $data['password_reset_date'] : '0000-00-00 00:00:00';
$this->theme = !empty($data['theme']) ? $data['theme'] : 'default';

# User XP Plugin
$this->xp_level = !empty($data['xp_level']) ? $data['xp_level'] : 1;
$this->xp_current = !empty($data['xp_current']) ? $data['xp_current'] : 0;
$this->xp_total = !empty($data['xp_total']) ? $data['xp_total'] : 0;
}

/**
Expand Down Expand Up @@ -492,4 +521,70 @@ public function setPasswordResetToken($sTokenHash) {
'password_reset_date'=>date('Y-m-d H:i:s',time()),
],['User_ID'=>$this->getID()]);
}

/**
* Get User Experience info
*
* @return array Experience info
* @since 1.0.4
*/
public function getExperience() {
$oNextLvl = CoreController::$aCoreTables['user-xp-level']->select(['Level_ID'=>($this->xp_level+1)])->current();
$dPercent = 0;
if($this->xp_current != 0) {
$dPercent = round((100/($oNextLvl->xp_total/$this->xp_current)),2);
}

$aExp = [
'level'=>$this->xp_level,
'total'=>$this->xp_total,
'current'=>$this->xp_current,
'percent'=>$dPercent,
];

return $aExp;
}

/**
* Grant user experience based on activity
*
* @param string $sXPKey name of activity
* @return bool true if successfull otherwise false
* @since 1.0.4
*/
public function addXP($sXPKey) {
# Load XP Activity
$oActivity = CoreController::$aCoreTables['user-xp-activity']->select(['xp_key'=>$sXPKey]);
if(count($oActivity) > 0) {
# get activity
$oActivity = $oActivity->current();
# get base xp
$iXP = $oActivity->xp_base;
# get next level
$oNextLvl = CoreController::$aCoreTables['user-xp-level']->select(['Level_ID'=>($this->xp_level+1)])->current();

# calculate new level and experience
$iNewLvl = $this->xp_level;
$iCurrentXP = $this->xp_current;
if($oNextLvl->xp_total <= ($iCurrentXP+$iXP)) {
$iNewLvl++;
$iCurrentXP = ($this->xp_current+$iXP)-$oNextLvl->xp_total;
} else {
$iCurrentXP = $iCurrentXP+$iXP;
}
# Set new information to entity
$this->xp_level = $iNewLvl;
$this->xp_current = $iCurrentXP;
$this->xp_total = $this->xp_total+$iXP;

# save to database
CoreController::$aCoreTables['user']->update([
'xp_level'=>$this->xp_level,
'xp_total'=>$this->xp_total,
'xp_current'=>$this->xp_current,
],['User_ID'=>$this->getID()]);
}

return false;
}
}
6 changes: 4 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Module
/**
* Module Version
*
* @since 1.0.3
* @since 1.0.4
*/
const VERSION = '1.0.3';
const VERSION = '1.0.4';

/**
* Load module config file
Expand Down Expand Up @@ -114,6 +114,8 @@ function($e) {
$sRouteName = $routeMatch->getMatchedRouteName();
$aRouteInfo = $routeMatch->getParams();

$app->getMvcEvent()->getViewModel()->setVariables(['sRouteName'=>$sRouteName]);

# get session
$container = new Container('plcauth');
$bLoggedIn = false;
Expand Down

0 comments on commit f4a8cf9

Please sign in to comment.