app\locker\data\dashboards\BaseDashboard::getStatementNumbersByDate PHP Method

getStatementNumbersByDate() public method

Get a count of statements on each day the lrs has been active.
public getStatementNumbersByDate ( DateTime $startDate = null, DateTime $endDate = null )
$startDate DateTime
$endDate DateTime
    public function getStatementNumbersByDate(\DateTime $startDate = null, \DateTime $endDate = null)
    {
        // If neither of the dates are set, default to the last week
        $startDate = $startDate ? Carbon::instance($startDate) : Carbon::now()->subWeek();
        $endDate = $endDate ? Carbon::instance($endDate) : Carbon::now();
        // Create the timestamp filter.
        $timestamp = [];
        if ($startDate !== null) {
            $timestamp['$gte'] = new MongoDate($startDate->timestamp, $startDate->micro);
        }
        if ($endDate !== null) {
            $timestamp['$lte'] = new MongoDate($endDate->timestamp, $endDate->micro);
        }
        $match = ['timestamp' => $timestamp];
        if ($this->has_lrs) {
            $match['lrs_id'] = new \MongoId($this->lrs);
        }
        $statements = $this->db->statements->aggregate(['$match' => $match], ['$project' => ['year' => ['$year' => '$timestamp'], 'month' => ['$month' => '$timestamp'], 'day' => ['$dayOfMonth' => '$timestamp'], 'actor' => '$statement.actor']], ['$group' => ['_id' => ['year' => '$year', 'month' => '$month', 'day' => '$day'], 'count' => ['$sum' => 1], 'actors' => ['$addToSet' => '$actor']]], ['$sort' => ['_id' => 1]], ['$project' => ['a' => '$count', 'b' => ['$size' => '$actors'], 'y' => ['$concat' => [['$substr' => ['$_id.year', 0, 4]], '-', ['$cond' => [['$lte' => ['$_id.month', 9]], ['$concat' => ['0', ['$substr' => ['$_id.month', 0, 2]]]], ['$substr' => ['$_id.month', 0, 2]]]], '-', ['$cond' => [['$lte' => ['$_id.day', 9]], ['$concat' => ['0', ['$substr' => ['$_id.day', 0, 2]]]], ['$substr' => ['$_id.day', 0, 2]]]]]]]]);
        //set statements for graphing
        $data = array();
        if (isset($statements['result'])) {
            foreach ($statements['result'] as $s) {
                $data[$s['y']] = json_encode($s);
            }
        }
        // Add empty point in data (fixes issue #265).
        $dates = array_keys($data);
        if (count($dates) > 0) {
            sort($dates);
            $start = strtotime(reset($dates));
            $end = strtotime(end($dates));
            for ($i = $start; $i <= $end; $i += 24 * 60 * 60) {
                $date = date("Y-m-d", $i);
                if (!isset($data[$date])) {
                    $data[$date] = json_encode(array("y" => $date, "a" => 0, 'b' => 0));
                }
            }
        }
        return trim(implode(" ", $data));
    }