remove old readme
[atutor.git] / mods / _standard / social / lib / Shindig / ATutorService.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 /**
16  * Implementation of supported services backed using ATutor's DB Fetcher
17  * Check shindig/php/src/social/spi/ for the list of services within the interfaces.
18  */
19 class ATutorService {
20   function debug($var, $title='') {
21         echo '<pre style="border: 1px black solid; padding: 0px; margin: 10px;" title="debugging box">';
22         if ($title) {
23                 echo '<h4>'.$title.'</h4>';
24         }
25         
26         ob_start();
27         print_r($var);
28         $str = ob_get_contents();
29         ob_end_clean();
30
31         $str = str_replace('<', '&lt;', $str);
32
33         $str = str_replace('[', '<span style="color: red; font-weight: bold;">[', $str);
34         $str = str_replace(']', ']</span>', $str);
35         $str = str_replace('=>', '<span style="color: blue; font-weight: bold;">=></span>', $str);
36         $str = str_replace('Array', '<span style="color: purple; font-weight: bold;">Array</span>', $str);
37         echo $str;
38         echo '</pre>';
39 }
40
41   /**
42    * Get the set of user id's from a user or collection of users, and group
43    */
44   protected function getIdSet($user, GroupId $group, SecurityToken $token) {
45     $ids = array();
46     if ($user instanceof UserId) {
47       $userId = $user->getUserId($token);
48       if ($group == null) {
49         return array($userId);
50       }
51       switch ($group->getType()) {
52         case 'all':
53         case 'friends':
54         case 'groupId':
55           $friendIds = ATutorDbFetcher::get()->getFriendIds($userId);
56           if (is_array($friendIds) && count($friendIds)) {
57             $ids = $friendIds;
58           }
59           break;
60         case 'self':
61           $ids[] = $userId;
62           break;
63       }
64     } elseif (is_array($user)) {
65       $ids = array();
66       foreach ($user as $id) {
67         $ids = array_merge($ids, $this->getIdSet($id, $group, $token));
68       }
69     }
70     return $ids;
71   }
72
73   /**
74    * Determines whether the input is a valid key. Valid keys match the regular
75    * expression [\w\-\.]+.
76    * 
77    * @param key the key to validate.
78    * @return true if the key is a valid appdata key, false otherwise.
79    */
80   public static function isValidKey($key) {
81     if (empty($key)) {
82       return false;
83     }
84     for ($i = 0; $i < strlen($key); ++ $i) {
85       $c = substr($key, $i, 1);
86       if (($c >= 'a' && $c <= 'z') || ($c >= 'A' && $c <= 'Z') || ($c >= '0' && $c <= '9') || ($c == '-') || ($c == '_') || ($c == '.')) {
87         continue;
88       }
89       return false;
90     }
91     return true;
92   }
93
94   protected function sortPersonResults(&$people, $options) {
95     if (! $options->getSortBy()) {
96       return true; // trivially sorted
97     }
98     // for now, ATutor can only sort by displayName, which also demonstrates returning sorted: false
99     if ($options->getSortBy() != 'displayName') {
100       return false;
101     }
102     usort($people, array($this, 'comparator'));
103     if ($options->getSortOrder() != CollectionOptions::SORT_ORDER_ASCENDING) {
104       $people = array_reverse($people);
105     }
106     return true;
107   }
108
109   protected function comparator($person, $person1) {
110     $name = ($person instanceof Person ? $person->getDisplayName() : $person['displayName']);
111     $name1 = ($person1 instanceof Person ? $person1->getDisplayName() : $person1['displayName']);
112     return strnatcasecmp($name, $name1);
113   }
114 }