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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.
27 function debug($var, $title='') {
28 echo '<pre style="border: 1px black solid; padding: 0px; margin: 10px;" title="debugging box">';
30 echo '<h4>'.$title.'</h4>';
35 $str = ob_get_contents();
38 $str = str_replace('<', '<', $str);
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);
49 * Get the set of user id's from a user or collection of users, and group
51 protected function getIdSet($user, GroupId $group, SecurityToken $token) {
53 if ($user instanceof UserId) {
54 $userId = $user->getUserId($token);
56 return array($userId);
58 switch ($group->getType()) {
62 $friendIds = ATutorDbFetcher::get()->getFriendIds($userId);
63 if (is_array($friendIds) && count($friendIds)) {
71 } elseif (is_array($user)) {
73 foreach ($user as $id) {
74 $ids = array_merge($ids, $this->getIdSet($id, $group, $token));
81 * Determines whether the input is a valid key. Valid keys match the regular
82 * expression [\w\-\.]+.
84 * @param key the key to validate.
85 * @return true if the key is a valid appdata key, false otherwise.
87 public static function isValidKey($key) {
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 == '.')) {
101 protected function sortPersonResults(&$people, $options) {
102 if (! $options->getSortBy()) {
103 return true; // trivially sorted
105 // for now, ATutor can only sort by displayName, which also demonstrates returning sorted: false
106 if ($options->getSortBy() != 'displayName') {
109 usort($people, array($this, 'comparator'));
110 if ($options->getSortOrder() != CollectionOptions::SORT_ORDER_ASCENDING) {
111 $people = array_reverse($people);
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);