Skip to content
Browse files

adding whether the logged in user is attending an event, and also the…

… ability to filter events by a particular user attending
  • Loading branch information...
1 parent 83b312f commit 20c29e50a1112b09cb37795d56fc6225d9ad8e2b @lornajane lornajane committed
View
4 src/api-v2/controllers/UsersController.php
@@ -25,6 +25,10 @@ public function getAction($request, $db) {
$talk_mapper = new TalkMapper($db, $request);
$list = $talk_mapper->getTalksBySpeaker($user_id, $resultsperpage, $start, $request, $verbose);
break;
+ case 'attended':
+ $event_mapper = new EventMapper($db, $request);
+ $list = $event_mapper->getEventsAttendedByUser($user_id, $resultsperpage, $start, $request, $verbose);
+ break;
default:
throw new InvalidArgumentException('Unknown Subrequest', 404);
break;
View
50 src/api-v2/models/EventMapper.php
@@ -23,6 +23,8 @@ public function getDefaultFields()
'description' => 'event_desc',
'href' => 'event_href',
'attendee_count' => 'attendee_count',
+ 'attending' => 'attending',
+ 'event_comments_count' => 'event_comments_count',
'icon' => 'event_icon'
);
return $fields;
@@ -51,6 +53,7 @@ public function getVerboseFields()
'location' => 'event_loc',
'hashtag' => 'event_hashtag',
'attendee_count' => 'attendee_count',
+ 'attending' => 'attending',
'comments_enabled' => 'comments_enabled',
'event_comments_count' => 'event_comments_count',
'cfp_start_date' => 'event_cfp_start',
@@ -71,7 +74,7 @@ public function getVerboseFields()
public function getEventById($event_id, $verbose = false)
{
$order = 'events.event_start desc';
- $results = $this->getEvents(1, 0, 'ID=' . (int)$event_id, null);
+ $results = $this->getEvents(1, 0, 'events.ID=' . (int)$event_id, null);
if ($results) {
$retval = $this->transformResults($results, $verbose);
return $retval;
@@ -92,6 +95,7 @@ public function getEventById($event_id, $verbose = false)
*/
protected function getEvents($resultsperpage, $start, $where = null, $order = null)
{
+ $data = array();
$sql = 'select events.*, '
. '(select count(*) from user_attend where user_attend.eid = events.ID)
as attendee_count, '
@@ -103,9 +107,18 @@ protected function getEvents($resultsperpage, $start, $where = null, $order = nu
. 'CASE
WHEN (((events.event_start - 3600*24) < '.mktime(0,0,0).') and (events.event_start + (3*30*3600*24)) > '.mktime(0,0,0).') THEN 1
ELSE 0
- END as comments_enabled '
+ END as comments_enabled, '
+ . 'current_ua.uid as attending '
. 'from events '
- . 'where active = 1 and '
+ . 'left join user_attend ua on ua.eid = events.ID '
+ . 'left join user_attend current_ua on (current_ua.eid = events.ID and current_ua.uid ';
+ if(isset($this->_request->user_id)) {
+ $sql .= ' = ' . $this->_request->user_id;
+ } else {
+ $sql .= ' is null';
+ }
+ $sql .= ') ';
+ $sql .= 'where active = 1 and '
. '(pending = 0 or pending is NULL) and '
. 'private <> "y" ';
@@ -123,7 +136,7 @@ protected function getEvents($resultsperpage, $start, $where = null, $order = nu
$sql .= $this->buildLimit($resultsperpage, $start);
$stmt = $this->_db->prepare($sql);
- $response = $stmt->execute();
+ $response = $stmt->execute($data);
if ($response) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results;
@@ -254,6 +267,13 @@ public function transformResults($results, $verbose)
// add per-item links
if (is_array($list) && count($list)) {
foreach ($results as $key => $row) {
+ // flip the attending to be true/false rather than user id or null
+ if($row['attending']) {
+ $list[$key]['attending'] = true;
+ } else {
+ $list[$key]['attending'] = false;
+ }
+
$list[$key]['tags'] = $this->getTags($row['ID']);;
$list[$key]['uri'] = $base . '/' . $version . '/events/'
. $row['ID'];
@@ -336,4 +356,26 @@ protected function getTags($event_id)
}
return $retval;
}
+
+ /**
+ * Events that the currently logged-in user is marked as attending
+ *
+ * @param int $resultsperpage how many records to return
+ * @param int $start offset to start returning records from
+ * @param boolean $verbose used to determine how many fields are needed
+ *
+ * @return array the data, or false if something went wrong
+ */
+ public function getEventsAttendedByUser($user_id, $resultsperpage, $start, $verbose = false)
+ {
+ $where = ' ua.uid = ' . (int)$user_id;
+ $order = ' events.event_start desc ';
+ $results = $this->getEvents($resultsperpage, $start, $where, $order);
+ if (is_array($results)) {
+ $retval = $this->transformResults($results, $verbose);
+ return $retval;
+ }
+ return false;
+ }
+
}
View
2 src/api-v2/models/UserMapper.php
@@ -109,6 +109,8 @@ public function transformResults($results, $verbose)
$list[$key]['website_uri'] = 'http://joind.in/user/view/' . $row['ID'];
$list[$key]['talks_uri'] = $base . '/' . $version . '/users/'
. $row['ID'] . '/talks/';
+ $list[$key]['attended_events_uri'] = $base . '/' . $version . '/users/'
+ . $row['ID'] . '/attended/';
}
}
$retval = array();

0 comments on commit 20c29e5

Please sign in to comment.
Something went wrong with that request. Please try again.