move code up one directory
[atutor.git] / mods / _standard / social / lib / Shindig / ATutorActivityService.php
1 <?php
2 /***********************************************************************/
3 /* ATutor                                                                                                                          */
4 /***********************************************************************/
5 /* Copyright (c) 2002-2010                                             */
6 /* Inclusive Design Institute                                          */
7 /* http://atutor.ca                                                                                                        */
8 /*                                                                                                                                         */
9 /* This program is free software. You can redistribute it and/or           */
10 /* modify it under the terms of the GNU General Public License             */
11 /* as published by the Free Software Foundation.                                           */
12 /***********************************************************************/
13 // $Id$
14
15 class ATutorActivityService extends ATutorService implements ActivityService {
16   public function getActivity($userId, $groupId, $appdId, $fields, $activityId, SecurityToken $token) {
17     $activities = $this->getActivities($userId, $groupId, $appdId, null, null, null, null, 0, 20, $fields, array($activityId), $token);
18     if ($activities instanceof RestFulCollection) {
19       $activities = $activities->getEntry();
20       foreach ($activities as $activity) {
21         if ($activity->getId() == $activityId) {
22           return $activity;
23         }
24       }
25     }
26     throw new SocialSpiException("Activity not found", ResponseError::$NOT_FOUND);
27   }
28
29   public function getActivities($userIds, $groupId, $appId, $sortBy, $filterBy, $filterOp, $filterValue, $startIndex, $count, $fields, $activityIds, $token) {
30 //  public function getActivities($userIds, $groupId, $appId, $sortBy, $filterBy, $filterOp, $filterValue, $startIndex, $count, $fields, $token) {
31     $ids = $this->getIdSet($userIds, $groupId, $token);
32     $activities = ATutorDbFetcher::get()->getActivities($ids, $appId, $sortBy, $filterBy, $filterOp, $filterValue, $startIndex, $count, $fields, $activityIds);
33 //    $activities = ATutorDbFetcher::get()->getActivities($ids, $appId, $sortBy, $filterBy, $filterOp, $filterValue, $startIndex, $count, $fields);
34     if ($activities) {
35       $totalResults = $activities['totalResults'];
36       $startIndex = $activities['startIndex'];
37       $count = $activities['count'];
38       unset($activities['totalResults']);
39       unset($activities['startIndex']);
40       unset($activities['count']);
41       $ret = new RestfulCollection($activities, $startIndex, $totalResults);
42       $ret->setItemsPerPage($count);
43       return $ret;
44     } else {
45       throw new SocialSpiException("Invalid activity specified", ResponseError::$NOT_FOUND);
46     }
47   }
48
49   public function createActivity($userId, $groupId, $appId, $fields, $activity, SecurityToken $token) {
50     try {
51       if ($token->getOwnerId() != $token->getViewerId()) {
52         throw new SocialSpiException("unauthorized: Create activity permission denied.", ResponseError::$UNAUTHORIZED);
53       }
54       ATutorDbFetcher::get()->createActivity($userId->getUserId($token), $activity, $token->getAppId());
55     } catch (SocialSpiException $e) {
56       throw $e;
57     } catch (Exception $e) {
58       throw new SocialSpiException("Invalid create activity request: " . $e->getMessage(), ResponseError::$INTERNAL_ERROR);
59     }
60   }
61
62
63   /**
64    * Delete activity
65    * @param             String userId = "@me"
66    * @param             String groupId = "@self"
67    * @param             String appId = auth.AppId
68    * @param             String activityId       
69    * @param             AuthToken auth = HttpRequest.Authorization
70    * @return    Void
71    *
72    * Check http://www.atutor.ca/atutor/mantis/view.php?id=4230 for details regarding to $activityId
73    */
74   public function deleteActivities($userId, $groupId, $appId, $activityIds, SecurityToken $token) {
75     $ids = $this->getIdSet($userId, $groupId, $token);
76     if (count($ids) < 1 || count($ids) > 1) {
77       throw new SocialSpiException("Invalid user id or count", ResponseError::$BAD_REQUEST);
78     }
79     if (! ATutorDbFetcher::get()->deleteActivities($ids[0], $appId, $activityIds)) {
80       throw new SocialSpiException("Invalid activity id(s)", ResponseError::$NOT_FOUND);
81     }
82   }
83
84   /** 
85    * Update Activities, based on Opensocial v8.1
86    * http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/rpc-protocol.html
87    * @param             String userId = "@me"
88    * @param             String groupId = "@self"
89    * @param             String appId = auth.AppId
90    * @param             Object Activity activity        
91    * @param             AuthToken auth = HttpRequest.Authorization
92    * @return    opensocial.Activity
93    * @date              Apr 23, 2010
94    * @author    Harris Wong
95    *
96    * Note: Shindig 1.0 shindig/php/src/social/spi/ActivityService.php does not support this.
97    */
98   public function updateActivity($userId, $groupId, $appId, $activity, SecurityToken $token) {
99     throw new SocialSpiException("Not implemented", ResponseError::$NOT_IMPLEMENTED);
100   }
101 }
102 ?>