forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhrequentTrackController.php
71 lines (57 loc) · 1.77 KB
/
PhrequentTrackController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
final class PhrequentTrackController
extends PhrequentController {
private $verb;
private $phid;
public function willProcessRequest(array $data) {
$this->phid = $data['phid'];
$this->verb = $data['verb'];
}
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
if (!$this->isStartingTracking() &&
!$this->isStoppingTracking()) {
throw new Exception('Unrecognized verb: ' . $this->verb);
}
if ($this->isStartingTracking()) {
$this->startTracking($user, $this->phid);
} else if ($this->isStoppingTracking()) {
$this->stopTracking($user, $this->phid);
}
return id(new AphrontRedirectResponse());
}
private function isStartingTracking() {
return $this->verb === 'start';
}
private function isStoppingTracking() {
return $this->verb === 'stop';
}
private function startTracking($user, $phid) {
$usertime = new PhrequentUserTime();
$usertime->setDateStarted(time());
$usertime->setUserPHID($user->getPHID());
$usertime->setObjectPHID($phid);
$usertime->save();
}
private function stopTracking($user, $phid) {
if (!PhrequentUserTimeQuery::isUserTrackingObject($user, $phid)) {
// Don't do anything, it's not being tracked.
return;
}
$usertime_dao = new PhrequentUserTime();
$conn = $usertime_dao->establishConnection('r');
queryfx(
$conn,
'UPDATE %T usertime '.
'SET usertime.dateEnded = UNIX_TIMESTAMP() '.
'WHERE usertime.userPHID = %s '.
'AND usertime.objectPHID = %s '.
'AND usertime.dateEnded IS NULL '.
'ORDER BY usertime.dateStarted, usertime.id DESC '.
'LIMIT 1',
$usertime_dao->getTableName(),
$user->getPHID(),
$phid);
}
}