2017-01-11 08:25:26 +01:00
|
|
|
<?php
|
2017-01-12 06:50:45 +01:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-18 02:09:16 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /system/get-stats Get stats
|
2017-06-28 15:02:54 +02:00
|
|
|
* @apiVersion 4.1.0
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
|
|
|
* @apiName Get stats
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiGroup System
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path retrieves the last stats.
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff1
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
|
|
|
* @apiParam {String} period Period of search.
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiParam {Number} staffId Id of the current staff.
|
|
|
|
|
|
|
|
* @apiUse NO_PERMISSION
|
|
|
|
* @apiUse INVALID_PERIOD
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-04-22 03:33:17 +02:00
|
|
|
* @apiSuccess {[StatList](#api-Data_Structures-ObjectStatlist)[]} data Array of the stats
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-11 08:25:26 +01:00
|
|
|
class GetStatsController extends Controller {
|
|
|
|
const PATH = '/get-stats';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2017-01-11 08:25:26 +01:00
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'staff_1',
|
2017-01-12 06:50:45 +01:00
|
|
|
'requestData' => [
|
|
|
|
'period' => [
|
2017-02-18 23:47:19 +01:00
|
|
|
'validation' => DataValidator::in(['WEEK', 'MONTH', 'QUARTER', 'YEAR']),
|
2017-01-12 06:50:45 +01:00
|
|
|
'error' => ERRORS::INVALID_PERIOD
|
|
|
|
]
|
|
|
|
]
|
2017-01-11 08:25:26 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2017-01-12 06:50:45 +01:00
|
|
|
$this->generationNewStats();
|
|
|
|
|
|
|
|
$staffId = Controller::request('staffId');
|
|
|
|
|
|
|
|
if($staffId) {
|
|
|
|
if($staffId !== Controller::getLoggedUser()->id && !Controller::isStaffLogged(3)) {
|
|
|
|
Response::respondError(ERRORS::NO_PERMISSION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getStaffStat();
|
|
|
|
} else {
|
|
|
|
$this->getGeneralStat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generationNewStats() {
|
|
|
|
$lastStatDay = Setting::getSetting('last-stat-day');
|
2017-01-12 20:30:58 +01:00
|
|
|
$previousCurrentDate = floor(Date::getPreviousDate() / 10000);
|
|
|
|
$currentDate = floor(Date::getCurrentDate() / 10000);
|
2017-01-12 06:50:45 +01:00
|
|
|
|
|
|
|
if($lastStatDay->value !== $previousCurrentDate) {
|
|
|
|
|
|
|
|
$begin = new DateTime($lastStatDay->value);
|
2017-01-12 20:30:58 +01:00
|
|
|
$end = new DateTime($currentDate);
|
2017-01-12 06:50:45 +01:00
|
|
|
|
|
|
|
$interval = new DateInterval('P1D');
|
|
|
|
$dateRange = new DatePeriod($begin, $interval ,$end);
|
|
|
|
|
|
|
|
$staffList = Staff::getAll();
|
|
|
|
|
|
|
|
foreach($dateRange as $date) {
|
|
|
|
$this->generateGeneralStat('CREATE_TICKET', $date);
|
|
|
|
$this->generateGeneralStat('CLOSE', $date);
|
|
|
|
$this->generateGeneralStat('SIGNUP', $date);
|
|
|
|
$this->generateGeneralStat('COMMENT', $date);
|
|
|
|
|
|
|
|
foreach($staffList as $staff) {
|
|
|
|
$assignments = Ticketevent::count('type=? AND author_staff_id=? AND date LIKE ?',['ASSIGN',$staff->id, $date->format('Ymd') . '%']);
|
|
|
|
$closed = Ticketevent::count('type=? AND author_staff_id=? AND date LIKE ?',['CLOSE',$staff->id, $date->format('Ymd') . '%']);
|
|
|
|
|
|
|
|
$statAssign = new Stat();
|
|
|
|
$statAssign->setProperties([
|
|
|
|
'date' => $date->format('Ymd'),
|
|
|
|
'type' => 'ASSIGN',
|
|
|
|
'general' => 0,
|
|
|
|
'value' => $assignments,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$statClose = new Stat();
|
|
|
|
$statClose->setProperties([
|
|
|
|
'date' => $date->format('Ymd'),
|
|
|
|
'type' => 'CLOSE',
|
|
|
|
'general' => 0,
|
|
|
|
'value' => $closed,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$staff->ownStatList->add($statAssign);
|
|
|
|
$staff->ownStatList->add($statClose);
|
|
|
|
|
|
|
|
$staff->store();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 20:30:58 +01:00
|
|
|
$lastStatDay->value = $currentDate;
|
2017-01-12 06:50:45 +01:00
|
|
|
$lastStatDay->store();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateGeneralStat($type, $date) {
|
|
|
|
$value = Log::count('type=? AND date LIKE ?',[$type, $date->format('Ymd') . '%']);
|
|
|
|
$stat = new Stat();
|
|
|
|
|
|
|
|
$stat->setProperties([
|
|
|
|
'date' => $date->format('Ymd'),
|
|
|
|
'type' => $type,
|
|
|
|
'general' => 1,
|
|
|
|
'value' => $value,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$stat->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGeneralStat() {
|
|
|
|
$daysToRetrieve = $this->getDaysToRetrieve();
|
|
|
|
|
|
|
|
$statList = Stat::find('general=\'1\' ORDER BY id desc LIMIT ? ', [4 * $daysToRetrieve]);
|
|
|
|
|
|
|
|
Response::respondSuccess($statList->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStaffStat() {
|
|
|
|
$staffId = Controller::request('staffId');
|
|
|
|
$daysToRetrieve = $this->getDaysToRetrieve();
|
|
|
|
|
|
|
|
$statList = Stat::find('general=\'0\' AND staff_id=? ORDER BY id desc LIMIT ? ', [$staffId, 4 * $daysToRetrieve]);
|
|
|
|
|
|
|
|
Response::respondSuccess($statList->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDaysToRetrieve() {
|
|
|
|
$period = Controller::request('period');
|
|
|
|
$daysToRetrieve = 0;
|
|
|
|
|
|
|
|
switch ($period) {
|
2017-02-19 00:58:31 +01:00
|
|
|
case 'WEEK':
|
2017-01-12 06:50:45 +01:00
|
|
|
$daysToRetrieve = 7;
|
|
|
|
break;
|
2017-02-19 00:58:31 +01:00
|
|
|
case 'MONTH':
|
2017-01-12 06:50:45 +01:00
|
|
|
$daysToRetrieve = 30;
|
|
|
|
break;
|
2017-02-19 00:58:31 +01:00
|
|
|
case 'QUARTER':
|
2017-01-12 06:50:45 +01:00
|
|
|
$daysToRetrieve = 90;
|
|
|
|
break;
|
2017-02-19 00:58:31 +01:00
|
|
|
case 'YEAR':
|
2017-01-12 06:50:45 +01:00
|
|
|
$daysToRetrieve = 365;
|
|
|
|
break;
|
2017-01-11 08:25:26 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 06:50:45 +01:00
|
|
|
return $daysToRetrieve;
|
2017-01-11 08:25:26 +01:00
|
|
|
}
|
|
|
|
}
|