remove old readme
[atutor.git] / mods / _standard / social / lib / Shindig / ATutorPersonService.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 ATutorPersonService extends ATutorService implements PersonService {
16   public function getPerson($userId, $groupId, $fields, SecurityToken $token) {
17     if (! is_object($userId)) {
18       $userId = new UserId('userId', $userId);
19       $groupId = new GroupId('self', 'all');
20     }
21     $person = $this->getPeople($userId, $groupId, new CollectionOptions(), $fields, $token);
22     if (is_array($person->getEntry())) {
23       $person = $person->getEntry();
24       if (is_array($person) && count($person) == 1) {
25         return array_pop($person);
26       }
27     }
28     throw new SocialSpiException("Person not found", ResponseError::$BAD_REQUEST);
29   }
30
31   public function getPeople($userId, $groupId, CollectionOptions $options, $fields, SecurityToken $token) {
32     $ids = $this->getIdSet($userId, $groupId, $token);
33     $allPeople = ATutorDbFetcher::get()->getPeople($ids, $fields, $options, $token);
34     $totalSize = $allPeople['totalSize'];
35     $people = array();
36     foreach ($ids as $id) {
37       $person = null;
38       if (is_array($allPeople) && isset($allPeople[$id])) {
39         $person = $allPeople[$id];
40         if (! $token->isAnonymous() && $id == $token->getViewerId()) {
41           $person->setIsViewer(true);
42         }
43         if (! $token->isAnonymous() && $id == $token->getOwnerId()) {
44           $person->setIsOwner(true);
45         }
46         if (! in_array('@all', $fields)) {
47           $newPerson = array();
48           $newPerson['isOwner'] = $person->isOwner;
49           $newPerson['isViewer'] = $person->isViewer;
50           $newPerson['displayName'] = $person->displayName;
51           // Force these fields to always be present
52           $fields[] = 'id';
53           $fields[] = 'displayName';
54           $fields[] = 'thumbnailUrl';
55           $fields[] = 'profileUrl';
56           foreach ($fields as $field) {
57             if (isset($person->$field) && ! isset($newPerson[$field])) {
58               $newPerson[$field] = $person->$field;
59             }
60           }
61           $person = $newPerson;
62         }
63         array_push($people, $person);
64       }
65     }
66     $sorted = $this->sortPersonResults($people, $options);
67     $collection = new RestfulCollection($people, $options->getStartIndex(), $totalSize);
68     $collection->setItemsPerPage($options->getCount());
69     if (! $sorted) {
70       $collection->setSorted(false); // record that we couldn't sort as requested
71     }
72     if ($options->getUpdatedSince()) {
73       $collection->setUpdatedSince(false); // we can never process an updatedSince request
74     }
75     return $collection;
76   }
77 }
78 ?>