Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • UBCollection
  • UBEvent
  • UBFile
  • UBItem
  • UBMessage
  • UBSite
  • UBUser
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * ClipIt - JuxtaLearn Web Space
  4:  * PHP version:     >= 5.2
  5:  * Creation date:   2013-10-10
  6:  * Last update:     $Date$
  7:  * @author          Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
  8:  * @version         $Version$
  9:  * @link            http://www.juxtalearn.eu
 10:  * @license         GNU Affero General Public License v3
 11:  * @package         ClipIt
 12:  * @subpackage      urjc_backend
 13:  */
 14: 
 15: /**
 16:  * <Class Description>
 17:  */
 18: class UBMessage extends UBItem {
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 21:      */
 22:     const SUBTYPE = "UBMessage";
 23:     const REL_MESSAGE_DESTINATION = "UBMessage-destination";
 24:     const REL_MESSAGE_FILE = "UBMessage-UBFile";
 25:     const REL_MESSAGE_USER = "UBMessage-UBUser";
 26: 
 27:     public $destination = 0;
 28:     public $file_array = array();
 29:     public $read_array = array();
 30: 
 31:     /**
 32:      * Loads object parameters stored in Elgg
 33:      *
 34:      * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
 35:      */
 36:     protected function copy_from_elgg($elgg_entity) {
 37:         parent::copy_from_elgg($elgg_entity);
 38:         //$this->read_array = (array)$elgg_entity->get("read_array");
 39:         $this->read_array = (array)static::get_read_array($this->id);
 40:         $this->destination = (int)static::get_destination($this->id);
 41:         $this->file_array = (array)static::get_files($this->id);
 42:     }
 43: 
 44:     /**
 45:      * Copy $this object parameters into an Elgg entity.
 46:      *
 47:      * @param ElggEntity $elgg_entity Elgg object instance to save $this to
 48:      */
 49:     protected function copy_to_elgg($elgg_entity) {
 50:         parent::copy_to_elgg($elgg_entity);
 51:         //$elgg_entity->set("read_array", (array)$this->read_array);
 52:     }
 53: 
 54:     /**
 55:      * Saves this instance to the system.
 56:      * @param  bool $double_save if $double_save is true, this object is saved twice to ensure that all properties are
 57:      *     updated properly. E.g. the time created property can only beset on ElggObjects during an update. Defaults to
 58:      *     false!
 59:      * @return bool|int Returns the Id of the saved instance, or false if error
 60:      */
 61:     protected function save($double_save = false) {
 62:         parent::save($double_save);
 63:         static::set_destination($this->id, $this->destination);
 64:         static::add_files($this->id, $this->file_array);
 65:         static::set_read_array($this->id, $this->read_array);
 66:         return $this->id;
 67:     }
 68: 
 69:     /* STATIC FUNCTIONS */
 70:     /**
 71:      * Get Messages by Destination
 72:      *
 73:      * @param array $destination_array Array of Destination IDs
 74:      * @param int $offset (default = 0 : begining)
 75:      * @param int $limit (default = 0 : none)
 76:      * @param bool $count_only (default = false : no)
 77:      * @param string $order_by property to order results by
 78:      * @param bool $ascending Whether to order ascending (true) or descending (false)
 79:      *
 80:      * @return static[] Array of Messages
 81:      */
 82:     static function get_by_destination($destination_array, $limit = 0, $offset = 0, $count_only = false, $order_by = "", $ascending = true) {
 83:         $message_array = array();
 84:         foreach ($destination_array as $destination_id) {
 85:             $item_array = UBCollection::get_items($destination_id, static::REL_MESSAGE_DESTINATION, true);
 86:             if($count_only) {
 87:                 $message_array[$destination_id] = count($item_array);
 88:                 continue;
 89:             }
 90:             $temp_array = array();
 91:             foreach ($item_array as $item_id) {
 92:                 $temp_array[$item_id] = new static((int)$item_id);
 93:             }
 94:             if (empty($temp_array)) {
 95:                 $message_array[$destination_id] = null;
 96:             } else {
 97:                 if(!empty($order_by)) {
 98:                     $args = array("order_by" => $order_by, "ascending" => $ascending);
 99:                     uasort($temp_array, function ($i1, $i2) use ($args) {
100:                         if (!$i1 && !$i2) {
101:                             return 0;
102:                         }
103:                         if ($i1->$args["order_by"] == $i2->$args["order_by"]) {
104:                             return 0;
105:                         }
106:                         if ((bool)$args["ascending"]) {
107:                             if (!$i1) {
108:                                 return 1;
109:                             }
110:                             if (!$i2) {
111:                                 return -1;
112:                             }
113:                             return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
114:                         } else {
115:                             if (!$i1) {
116:                                 return -1;
117:                             }
118:                             if (!$i2) {
119:                                 return 1;
120:                             }
121:                             return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
122:                         }
123:                     });
124:                 }
125:                 $message_array[$destination_id] = $temp_array;
126:                 if (!empty($limit)) {
127:                     $message_array[$destination_id] = array_slice($message_array[$destination_id], $offset, $limit, true);
128:                 } else {
129:                     $message_array[$destination_id] = array_slice($message_array[$destination_id], $offset, null, true);
130:                 }
131:             }
132:         }
133:         return $message_array;
134:     }
135: 
136:     /**
137:      * Get Messages by Sender
138:      *
139:      * @param array $sender_array Array of Sender IDs
140:      *
141:      * @return static[] Array of Messages
142:      */
143:     static function get_by_sender($sender_array) {
144:         return static::get_by_owner($sender_array);
145:     }
146: 
147:     /**
148:      * Get the Destination of a Message
149:      *
150:      * @param int $id ID of Message
151:      *
152:      * @return int|null ID of Destination or null if error
153:      */
154:     static function get_destination($id) {
155:         $item_array = UBCollection::get_items($id, static::REL_MESSAGE_DESTINATION);
156:         if (empty($item_array)) {
157:             return 0;
158:         }
159:         return (int)array_pop($item_array);
160:     }
161: 
162:     /**
163:      * Set the Destination of a Message
164:      *
165:      * @param int $id Id of the Message
166:      * @param int $destination_id ID of the Destination
167:      *
168:      * @return bool True if OK, false if error
169:      */
170:     static function set_destination($id, $destination_id) {
171:         if ($destination_id > 0) {
172:             UBCollection::remove_all_items($id, static::REL_MESSAGE_DESTINATION);
173:             return UBCollection::add_items($id, array($destination_id), static::REL_MESSAGE_DESTINATION);
174:         }
175:         return false;
176:     }
177: 
178:     /**
179:      * Get Sender of a Message
180:      *
181:      * @param int $id ID of the Message
182:      *
183:      * @return int ID of the Sender
184:      */
185:     static function get_sender($id) {
186:         $message = new static($id);
187:         return $message->owner_id;
188:     }
189: 
190:     /**
191:      * Add Files attached to a Message
192:      *
193:      * @param int $id ID of the Message
194:      * @param array $file_array Array of File IDs
195:      *
196:      * @return bool True if OK, false if error
197:      */
198:     static function add_files($id, $file_array) {
199:         return UBCollection::add_items($id, $file_array, static::REL_MESSAGE_FILE, true);
200:     }
201: 
202:     /**
203:      * Set Files attached to a Message
204:      *
205:      * @param int $id ID of the Message
206:      * @param array $file_array Array of File IDs
207:      *
208:      * @return bool True if OK, false if error
209:      */
210:     static function set_files($id, $file_array) {
211:         return UBCollection::set_items($id, $file_array, static::REL_MESSAGE_FILE, true);
212:     }
213: 
214:     /**
215:      * Remove Files attached to a Message
216:      *
217:      * @param int $id ID of the Message
218:      * @param array $file_array Array of File IDs
219:      *
220:      * @return bool True if OK, false if error
221:      */
222:     static function remove_files($id, $file_array) {
223:         return UBCollection::remove_items($id, $file_array, static::REL_MESSAGE_FILE);
224:     }
225: 
226:     /**
227:      * Get Files attached to a Message
228:      *
229:      * @param int $id ID of the Message
230:      *
231:      * @return static[] Array of File IDs
232:      */
233:     static function get_files($id) {
234:         return UBCollection::get_items($id, static::REL_MESSAGE_FILE);
235:     }
236: 
237:     /**
238:      * Add Read Array for a Message
239:      *
240:      * @param int $id ID of the Message
241:      * @param array $read_array Array of User IDs who have read the Message
242:      *
243:      * @return bool True if OK, false if error
244:      */
245:     static function add_read_array($id, $read_array) {
246:         return UBCollection::add_items($id, $read_array, static::REL_MESSAGE_USER);
247:     }
248: 
249:     /**
250:      * Set Read Array for a Message
251:      *
252:      * @param int $id ID of the Message
253:      * @param array $read_array Array of User IDs who have read the Message
254:      *
255:      * @return bool True if OK, false if error
256:      */
257:     static function set_read_array($id, $read_array) {
258:         return UBCollection::set_items($id, $read_array, static::REL_MESSAGE_USER);
259:     }
260: 
261:     /**
262:      * Remove Read Array for a Message
263:      *
264:      * @param int $id ID of the Message
265:      * @param array $read_array Array of User IDs who have read the Message
266:      *
267:      * @return bool True if OK, false if error
268:      */
269:     static function remove_read_array($id, $read_array) {
270:         return UBCollection::remove_items($id, $read_array, static::REL_MESSAGE_USER);
271:     }
272: 
273:     /**
274:      * Get Read Array for a Message
275:      *
276:      * @param int $id ID of the Message
277:      *
278:      * @return static[] Array of File IDs
279:      */
280:     static function get_read_array($id) {
281:         return UBCollection::get_items($id, static::REL_MESSAGE_USER);
282:     }
283: 
284:     /**
285:      * Get a list of Users who have Read a Message, or optionally whether certain Users have read it
286:      *
287:      * @param int $id ID of the Message
288:      * @param null|array $user_array List of User IDs - optional
289:      *
290:      * @return static[] Array with key => value: user_id => read_status, where read_status is bool
291:      */
292:     static function get_read_status($id, $user_array = null) {
293:         $props = static::get_properties($id, array("read_array", "owner_id"));
294:         $read_array = $props["read_array"];
295:         $owner_id = $props["owner_id"];
296:         if (!$user_array) {
297:             return $read_array;
298:         } else {
299:             $return_array = array();
300:             foreach ($user_array as $user_id) {
301:                 if ((int)$user_id == (int)$owner_id || in_array($user_id, $read_array)) {
302:                     $return_array[$user_id] = true;
303:                 } else {
304:                     $return_array[$user_id] = false;
305:                 }
306:             }
307:             return $return_array;
308:         }
309:     }
310: 
311:     /**
312:      * Set the Read status for a Message
313:      *
314:      * @param int $id ID of Message
315:      * @param bool $read_value Read status value: true = read, false = unread
316:      * @param array $user_array Array of User IDs
317:      *
318:      * @return bool|int ID of Message if Ok, false if error
319:      * @throws InvalidParameterException
320:      */
321:     static function set_read_status($id, $read_value, $user_array) {
322:         $read_array = static::get_read_array($id);
323:         $update_flag = false;
324:         foreach ($user_array as $user_id) {
325:             $index = array_search((int)$user_id, $read_array);
326:             if ($read_value === true && $index === false) {
327:                 array_push($read_array, $user_id);
328:                 $update_flag = true;
329:             } elseif ($read_value === false && $index !== false) {
330:                 array_splice($read_array, $index, 1);
331:                 $update_flag = true;
332:             }
333:         }
334:         if ($update_flag) {
335:             return static::set_read_array($id, $read_array);
336:         } else {
337:             return $id;
338:         }
339:     }
340: 
341:     /**
342:      * Count the number os Messages for each Destination specified
343:      *
344:      * @param array $destination_array List of Destination IDs
345:      * @param bool $recursive Whether to recurse
346:      *
347:      * @return array Returns array of destination => message_count elements.
348:      */
349:     static function count_by_destination($destination_array, $recursive = false) {
350:         $count_array = array();
351:         foreach ($destination_array as $dest_id) {
352:             $count_array[$dest_id] = UBCollection::count_items($dest_id,
353:                 static::REL_MESSAGE_DESTINATION,
354:                 true,
355:                 $recursive);
356:         }
357:         return $count_array;
358:     }
359: 
360:     /**
361:      * Count number of Messages sent by Sender
362:      *
363:      * @param array $sender_array Array of User IDs
364:      *
365:      * @return array Array of sender => message_count elements
366:      */
367:     static function count_by_sender($sender_array) {
368:         $message_array = static::get_by_sender($sender_array);
369:         $count_array = array();
370:         foreach ($sender_array as $sender_id) {
371:             $count_array[$sender_id] = count((array)$message_array[$sender_id]);
372:         }
373:         return $count_array;
374:     }
375: 
376:     static function unread_by_destination($destination_array, $user_id, $recursive = false) {
377:         $destination_messages = static::get_by_destination($destination_array);
378:         $count_array = array();
379:         foreach ($destination_array as $destination_id) {
380:             $message_array = $destination_messages[$destination_id];
381:             if (!$message_array) {
382:                 $count_array[$destination_id] = 0;
383:                 continue;
384:             }
385:             $count = 0;
386:             foreach ($message_array as $message) {
387:                 if (array_search((int)$user_id, $message->read_array) === false) {
388:                     $count++;
389:                 }
390:                 if ($recursive) {
391:                     $temp_count = static::unread_by_destination(array((int)$message->id), $user_id, $recursive);
392:                     $count += $temp_count[$message->id];
393:                 }
394:             }
395:             $count_array[$destination_id] = $count;
396:         }
397:         return $count_array;
398:     }
399: 
400:     static function unread_by_sender($sender_array, $user_id) {
401:         $sender_messages = static::get_by_sender($sender_array);
402:         $count_array = array();
403:         foreach ($sender_array as $sender_id) {
404:             $count = 0;
405:             if (!empty($sender_messages[$sender_id])) {
406:                 $message_array = $sender_messages[$sender_id];
407:                 foreach ($message_array as $message) {
408:                     if (array_search((int)$user_id, $message->read_array) === false) {
409:                         $count++;
410:                     }
411:                 }
412:             }
413:             $count_array[$sender_id] = $count;
414:         }
415:         return $count_array;
416:     }
417: }
API documentation generated by ApiGen 2.8.0