Completed
Pull Request — master (#434)
by Joas
04:25
created

Api::getSinceFromOffset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.4286
cc 2
eloc 14
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * ownCloud - Activity App
5
 *
6
 * @author Joas Schilling
7
 * @copyright 2014 Joas Schilling [email protected]
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
 * License as published by the Free Software Foundation; either
12
 * version 3 of the License, or any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public
20
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Activity;
25
26
/**
27
 * Class Api
28
 *
29
 * @package OCA\Activity
30
 */
31
class Api
32
{
33
	const DEFAULT_LIMIT = 30;
34
35 5
	static public function get() {
36 5
		$app = new AppInfo\Application();
37
		/** @var Data $data */
38 5
		$data = $app->getContainer()->query('ActivityData');
39
40 5
		$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
41 5
		$count = isset($_GET['count']) ? (int) $_GET['count'] : self::DEFAULT_LIMIT;
42 5
		$user = $app->getContainer()->getServer()->getUserSession()->getUser()->getUID();
43
44 5
		if ($start !== 0) {
45 2
			$start = self::getSinceFromOffset($user, $start);
46 2
		}
47
48 5
		$activities = $data->get(
49 5
			$app->getContainer()->query('GroupHelper'),
50 5
			$app->getContainer()->query('UserSettings'),
51 5
			$user, $start, $count, 'desc', 'all'
52 5
		);
53
54 5
		$entries = array();
55 5
		foreach($activities['data'] as $entry) {
56 4
			$entries[] = array(
57 4
				'id' => $entry['activity_id'],
58 4
				'subject' => (string) $entry['subjectformatted']['full'],
59 4
				'message' => (string) $entry['messageformatted']['full'],
60 4
				'file' => $entry['object_name'],
61 4
				'link' => $entry['link'],
62 4
				'date' => date('c', $entry['timestamp']),
63
			);
64 5
		}
65
66 5
		return new \OC_OCS_Result($entries);
67
	}
68
69
	/**
70
	 * @param string $user
71
	 * @param int $offset
72
	 * @return int
73
	 */
74 2
	protected static function getSinceFromOffset($user, $offset) {
75 2
		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
76 2
		$query->select('activity_id')
77 2
			->from('activity')
78 2
			->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)))
79 2
			->orderBy('activity_id', 'desc')
80 2
			->setFirstResult($offset - 1)
81 2
			->setMaxResults(1);
82
83 2
		$result = $query->execute();
84 2
		$row = $result->fetch();
85 2
		$result->closeCursor();
86
87 2
		if ($row) {
88 1
			return (int) $row['activity_id'];
89
		}
90
91 1
		return 0;
92
	}
93
}
94