View file upload/src/addons/XenCentral/Feedback/Repository/Stats.php

File size: 4.74Kb
<?php
/**
 * @package XenCentral Feedback System
 * @author DNF Technology
 * @copyright Drnoyan & Nalyan LDA, Portugal, EU
 * @license http://dnf.technology/terms/
 * @link http://customers.dnf.technology
 * @version 2.0.0 Beta 10
 * @revision 12
 */

namespace XenCentral\Feedback\Repository;

use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;

class Stats extends Repository
{
    use FeedbackAbstractRepository;

    public function getFirstFeedbackDate($user_id = false)
	{
		if ($user_id)
		{
			$where = "WHERE foruserid=$user_id";
		}
		else
		{
			$where = '';
		}

		$date = $this->db()->fetchOne("
			SELECT MIN(dateline) FROM xf_xc_feedback_feedback
			$where
		");

		if (!$date)
		{
			return \XF::$time;
		}

		return $date;
	}

	public function getNewFeedbackData($user_id, $filter, $from, $to)
	{
		$feedbackType = array();

		if (count($filter) < 3)
		{
			// filter is enabled
			foreach ($filter AS $filterType)
			{
				switch ($filter)
				{
				case 'positive':
					$feedbackType[] = 1;
					break;
				case 'neutral':
					$feedbackType[] = 0;
					break;
				case 'negative':
					$feedbackType[] = 0;
					break;
				}
			}
		}

		$feedbackList = $this->_getFeedbackModel()->getFeedbackListForStats(array('foruserid'=>$user_id, 'from' => $from, 'to' => $to, 'feedback' => $feedbackType));

		if (empty($feedbackList))
		{
			return false;
		}

		$data = array(
			'columns' => array(
				array('type' => $this->_getDateColumnType($from, $to), 'label' => $this->_renderPhrase('xcfs_date'))
			),
			'rows' => array(
			)
		);

		$totalCount = count($feedbackList);

		$indexCounts = array();

		foreach ($feedbackList AS $feedback)
		{
			$colIndex = array_search($feedback['feedback_string'], $filter);

			if ($colIndex === false)
			{
				continue;
			}

			$colIndex++;

			if (!isset($data['columns'][$colIndex]))
			{
				$data['columns'][$colIndex] = array(
					'type' => 'number',
					'label' => $this->_renderPhrase('xcfs_' . $feedback['feedback_string'])
				);

				$indexCounts[$colIndex] = 0;
			}

			$dateline = $this->_getOptimizedDate($feedback['dateline'], $from, $to);

			if (!isset($data['rows'][$dateline][$colIndex]))
			{
				$data['rows'][$dateline][$colIndex] = 1;
				$indexCounts[$colIndex]++;
			}
			else
			{
				$data['rows'][$dateline][$colIndex]++;
			}
		}

        if(empty($data['rows'])) {
            // no data found for this
            return false;
        }

        foreach($data['rows'] AS &$row) {
            foreach($data['columns'] AS $index=>$column) {
                if($index==0) {
                    continue;
                }
                if(!isset($row[$index])) {
                    $row[$index]=0;
                }
            }
        }

		$data['dataCount'] = count($data['rows']);

		return $data;
	}

	public function getFeedbackPercentageData($user_id, $from, $to)
	{
		$data = array(
			'columns' => array(
				array('type' => 'string', 'label' => \XF::phrase('xcfs_feedback_type')),
				array('type' => 'number', 'label' => \XF::phrase('xcfs_feedback_count'))
			),
			'rows' => array(

			)
		);

		if($user_id)
		{
			$where=" AND foruserid=$user_id";
		}
		else
		{
			$where="";
		}

		$feedbackData = $this->db()->fetchAllKeyed("
			SELECT amount, count(*) as count from xf_xc_feedback_feedback
			WHERE dateline>=$from
			AND dateline<=$to 
			".$where."
			GROUP BY amount
		", 'amount');

		if(empty($feedbackData))
		{
			return false;
		}

		if (!isset($feedbackData['1']['count']))
		{
			$feedbackData['1']['count'] = 0;
		}

		if (!isset($feedbackData['0']['count']))
		{
			$feedbackData['0']['count'] = 0;
		}

		if (!isset($feedbackData['-1']['count']))
		{
			$feedbackData['-1']['count'] = 0;
		}


        $total = $feedbackData['-1']['count']+ $feedbackData['0']['count']+ $feedbackData['1']['count'];

		$data['rows'] = array(
			array($this->_renderPhrase('xcfs_positive'), $feedbackData['1']['count']),
			array($this->_renderPhrase('xcfs_negative'), $feedbackData['-1']['count']),
			array($this->_renderPhrase('xcfs_neutral'), $feedbackData['0']['count'])
		);

        $data['total']=$total;

		return $data;
	}

	protected function _getDateColumnType($from, $to)
	{
		if ($to - $from > 5 * 86400)
		{
			return 'date';
		}

		return 'datetime';
	}

	protected function _getOptimizedDate($date, $min, $max)
	{
		$dateDelta = $max - $min;

		if ($dateDelta < 1000)
		{
			return $date;
		}

		if ($date < $min || $date > $max)
		{
			return $date;
		}

		$dateStep = floor($dateDelta / 1000);

		$currentDate = $min;

		while (true)
		{
			if ($currentDate > $date)
			{
				return $currentDate;
			}

			$currentDate += $dateStep;

			if ($currentDate > $max)
			{
				break;
			}
		}

		return $max;
	}

	protected function _renderPhrase($phrase)
	{
		$phrase = \XF::phrase($phrase);

		return $phrase->render();
	}
}