move code up one directory
[atutor.git] / mods / _standard / social / lib / Shindig / ATutorAppDataService.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 ATutorAppDataService extends ATutorService implements AppDataService {
16   public function deletePersonData($userId, GroupId $groupId, $appId, $fields, SecurityToken $token) {
17     $ids = $this->getIdSet($userId, $groupId, $token);
18     if (count($ids) < 1) {
19       throw new InvalidArgumentException("No userId specified");
20     } elseif (count($ids) > 1) {
21       throw new InvalidArgumentException("Multiple userIds not supported");
22     }
23     $userId = $ids[0];
24     $appId = $token->getAppId();
25     if ($fields == null) {
26       if (! ATutorDbFetcher::get()->deleteAppData($userId, '*', $appId)) {
27         throw new SocialSpiException("Internal server error", ResponseError::$INTERNAL_ERROR);
28       }
29     } else {
30       foreach ($fields as $key) {
31         if (! self::isValidKey($key) && $key != '*') {
32           throw new SocialSpiException("The person app data key had invalid characters", ResponseError::$BAD_REQUEST);
33         }
34       }
35       foreach ($fields as $key) {
36         if (! ATutorDbFetcher::get()->deleteAppData($userId, $key, $appId)) {
37           throw new SocialSpiException("Internal server error", ResponseError::$INTERNAL_ERROR);
38         }
39       }
40     }
41   }
42
43   public function getPersonData($userId, GroupId $groupId, $appId, $fields, SecurityToken $token) {
44     $ids = $this->getIdSet($userId, $groupId, $token);
45     $data = ATutorDbFetcher::get()->getAppData($ids, $fields, $appId);
46     // If the data array is empty, return empty DataCollection.
47     return new DataCollection($data);
48   }
49
50   public function updatePersonData(UserId $userId, GroupId $groupId, $appId, $fields, $values, SecurityToken $token) {
51     if ($userId->getUserId($token) == null) {
52       throw new SocialSpiException("Unknown person id.", ResponseError::$NOT_FOUND);
53     }
54     foreach ($fields as $key) {
55       if (! self::isValidKey($key)) {
56         throw new SocialSpiException("The person app data key had invalid characters", ResponseError::$BAD_REQUEST);
57       }
58     }
59     switch ($groupId->getType()) {
60       case 'self':
61         foreach ($fields as $key) {
62           $value = isset($values[$key]) ? $values[$key] : null;
63           if (! ATutorDbFetcher::get()->setAppData($userId->getUserId($token), $key, $value, $token->getAppId())) {
64             throw new SocialSpiException("Internal server error", ResponseError::$INTERNAL_ERROR);
65           }
66         }
67         break;
68       default:
69         throw new SocialSpiException("We don't support updating data in batches yet", ResponseError::$NOT_IMPLEMENTED);
70         break;
71     }
72   }
73 }
74 ?>