changed git call from https to git readonly
[atutor.git] / mods / social / lib / Shindig / ATutorService.php
1 <?php
2 // $Id$
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 /**
23  * Implementation of supported services backed using ATutor's DB Fetcher
24  * Check shindig/php/src/social/spi/ for the list of services within the interfaces.
25  */
26 class ATutorService {
27   function debug($var, $title='') {
28         echo '<pre style="border: 1px black solid; padding: 0px; margin: 10px;" title="debugging box">';
29         if ($title) {
30                 echo '<h4>'.$title.'</h4>';
31         }
32         
33         ob_start();
34         print_r($var);
35         $str = ob_get_contents();
36         ob_end_clean();
37
38         $str = str_replace('<', '&lt;', $str);
39
40         $str = str_replace('[', '<span style="color: red; font-weight: bold;">[', $str);
41         $str = str_replace(']', ']</span>', $str);
42         $str = str_replace('=>', '<span style="color: blue; font-weight: bold;">=></span>', $str);
43         $str = str_replace('Array', '<span style="color: purple; font-weight: bold;">Array</span>', $str);
44         echo $str;
45         echo '</pre>';
46 }
47
48   /**
49    * Get the set of user id's from a user or collection of users, and group
50    */
51   protected function getIdSet($user, GroupId $group, SecurityToken $token) {
52     $ids = array();
53     if ($user instanceof UserId) {
54       $userId = $user->getUserId($token);
55       if ($group == null) {
56         return array($userId);
57       }
58       switch ($group->getType()) {
59         case 'all':
60         case 'friends':
61         case 'groupId':
62           $friendIds = ATutorDbFetcher::get()->getFriendIds($userId);
63           if (is_array($friendIds) && count($friendIds)) {
64             $ids = $friendIds;
65           }
66           break;
67         case 'self':
68           $ids[] = $userId;
69           break;
70       }
71     } elseif (is_array($user)) {
72       $ids = array();
73       foreach ($user as $id) {
74         $ids = array_merge($ids, $this->getIdSet($id, $group, $token));
75       }
76     }
77     return $ids;
78   }
79
80   /**
81    * Determines whether the input is a valid key. Valid keys match the regular
82    * expression [\w\-\.]+.
83    * 
84    * @param key the key to validate.
85    * @return true if the key is a valid appdata key, false otherwise.
86    */
87   public static function isValidKey($key) {
88     if (empty($key)) {
89       return false;
90     }
91     for ($i = 0; $i < strlen($key); ++ $i) {
92       $c = substr($key, $i, 1);
93       if (($c >= 'a' && $c <= 'z') || ($c >= 'A' && $c <= 'Z') || ($c >= '0' && $c <= '9') || ($c == '-') || ($c == '_') || ($c == '.')) {
94         continue;
95       }
96       return false;
97     }
98     return true;
99   }
100
101   protected function sortPersonResults(&$people, $options) {
102     if (! $options->getSortBy()) {
103       return true; // trivially sorted
104     }
105     // for now, ATutor can only sort by displayName, which also demonstrates returning sorted: false
106     if ($options->getSortBy() != 'displayName') {
107       return false;
108     }
109     usort($people, array($this, 'comparator'));
110     if ($options->getSortOrder() != CollectionOptions::SORT_ORDER_ASCENDING) {
111       $people = array_reverse($people);
112     }
113     return true;
114   }
115
116   protected function comparator($person, $person1) {
117     $name = ($person instanceof Person ? $person->getDisplayName() : $person['displayName']);
118     $name1 = ($person1 instanceof Person ? $person1->getDisplayName() : $person1['displayName']);
119     return strnatcasecmp($name, $name1);
120   }
121 }