Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/jabber/XMPP/Roster.php
Go to the documentation of this file.
00001 <?php
00041 class Roster {
00047         protected $roster_array = array();
00052         public function __construct($roster_array = array()) {
00053                 if ($this->verifyRoster($roster_array)) {
00054                         $this->roster_array = $roster_array; //Allow for prepopulation with existing roster
00055                 } else {
00056                         $this->roster_array = array();
00057                 }
00058         }
00059 
00066         protected function verifyRoster($roster_array) {
00067                 #TODO once we know *what* a valid roster array looks like
00068                 return True;
00069         }
00070 
00080         public function addContact($jid, $subscription, $name='', $groups=array()) {
00081                 $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
00082                 if ($this->isContact($jid)) {
00083                         $this->roster_array[$jid]['contact'] = $contact;
00084                 } else {
00085                         $this->roster_array[$jid] = array('contact' => $contact);
00086                 }
00087         }
00088 
00095         public function getContact($jid) {
00096                 if ($this->isContact($jid)) {
00097                         return $this->roster_array[$jid]['contact'];
00098                 }
00099         }
00100 
00107         public function isContact($jid) {
00108                 return (array_key_exists($jid, $this->roster_array));
00109         }
00110 
00120         public function setPresence($presence, $priority, $show, $status) {
00121                 list($jid, $resource) = explode("/", $presence);
00122                 if ($show != 'unavailable') {
00123                         if (!$this->isContact($jid)) {
00124                                 $this->addContact($jid, 'not-in-roster');
00125                         }
00126                         $resource = $resource ? $resource : '';
00127                         $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
00128                 } else { //Nuke unavailable resources to save memory
00129                         unset($this->roster_array[$jid]['resource'][$resource]);
00130                 }
00131         }
00132 
00133         /*
00134          *
00135          * Return best presence for jid
00136          *
00137          * @param string $jid
00138          */
00139         public function getPresence($jid) {
00140                 $split = explode("/", $jid);
00141                 $jid = $split[0];
00142                 if($this->isContact($jid)) {
00143                         $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
00144                         foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
00145                                 //Highest available priority or just highest priority
00146                                 if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
00147                                         $current = $presence;
00148                                         $current['resource'] = $resource;
00149                                 }
00150                         }
00151                         return $current;
00152                 }
00153         }
00159         public function getRoster() {
00160                 return $this->roster_array;
00161         }
00162 }
00163 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations