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

  • ClipitActivity
  • ClipitChat
  • ClipitComment
  • ClipitEvent
  • ClipitExample
  • ClipitFile
  • ClipitGroup
  • ClipitLA
  • ClipitLabel
  • ClipitPerformanceItem
  • ClipitPerformanceRating
  • ClipitPost
  • ClipitQuiz
  • ClipitQuizQuestion
  • ClipitQuizResult
  • ClipitRating
  • ClipitRemoteTrickyTopic
  • ClipitRemoteVideo
  • ClipitResource
  • ClipitSite
  • ClipitStoryboard
  • ClipitTag
  • ClipitTagRating
  • ClipitTask
  • ClipitTrickyTopic
  • ClipitUser
  • ClipitVideo
  • 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      clipit_api
 13:  */
 14: 
 15: /**
 16:  * A binary file with thumbnails (in case of images), and can be tagged and labeled.
 17:  */
 18: class ClipitFile extends UBFile {
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 21:      */
 22:     const SUBTYPE = "ClipitFile";
 23:     const REL_FILE_TAG = "ClipitFile-ClipitTag";
 24:     const REL_FILE_LABEL = "ClipitFile-ClipitLabel";
 25:     const REL_FILE_USER = "ClipitFile-ClipitUser";
 26:     const SCOPE_GROUP = "group";
 27:     const SCOPE_ACTIVITY = "activity";
 28:     const SCOPE_SITE = "site";
 29:     // Scopes
 30:     const SCOPE_EXAMPLE = "example";
 31:     const SCOPE_TRICKYTOPIC = "tricky_topic";
 32:     const SCOPE_TASK = "task";
 33:     public $tag_array = array();
 34:     public $label_array = array();
 35:     public $read_array = array();
 36: 
 37:     /**
 38:      * Loads object parameters stored in Elgg
 39:      *
 40:      * @param ElggFile $elgg_file Elgg Object to load parameters from.
 41:      */
 42:     protected function copy_from_elgg($elgg_file)
 43:     {
 44:         parent::copy_from_elgg($elgg_file);
 45:         $this->tag_array = (array)static::get_tags($this->id);
 46:         $this->label_array = (array)static::get_labels($this->id);
 47:         $this->read_array = (array)static::get_read_array($this->id);
 48:     }
 49: 
 50:     /**
 51:      * Copy $this object parameters into an Elgg file.
 52:      *
 53:      * @param ElggFile $elgg_file Elgg file instance to save $this to
 54:      */
 55:     protected function copy_to_elgg($elgg_file)
 56:     {
 57:         parent::copy_to_elgg($elgg_file);
 58:         //$elgg_file->set("read_array", (array)$this->read_array);
 59:     }
 60: 
 61:     /**
 62:      * Saves this instance to the system.
 63:      *
 64:      * @param bool $double_save Specifies whether to save file twice (necessary for file_creation_date edit)
 65:      *
 66:      * @return bool|int Returns id of saved instance, or false if error.
 67:      */
 68:     protected function save($double_save = false)
 69:     {
 70:         parent::save($double_save);
 71:         static::set_tags($this->id, $this->tag_array);
 72:         static::set_labels($this->id, $this->label_array);
 73:         static::set_read_array($this->id, $this->read_array);
 74:         return $this->id;
 75:     }
 76: 
 77: 
 78:     static function get_by_tags($tag_array)
 79:     {
 80:         $return_array = array();
 81:         $all_items = static::get_all(0, 0, "", true, true); // Get all item ids, not objects
 82:         foreach ($all_items as $item_id) {
 83:             $item_tags = static::get_tags((int)$item_id);
 84:             foreach ($tag_array as $search_tag) {
 85:                 if (array_search($search_tag, $item_tags) !== false) {
 86:                     $return_array[(int)$item_id] = new static((int)$item_id);
 87:                     break;
 88:                 }
 89:             }
 90:         }
 91:         return $return_array;
 92:     }
 93: 
 94:     static function get_by_labels($label_array)
 95:     {
 96:         $return_array = array();
 97:         $all_items = static::get_all(0, 0, "", true, true); // Get all item ids, not objects
 98:         foreach ($all_items as $item_id) {
 99:             $item_labels = (array)static::get_labels((int)$item_id);
100:             foreach ($label_array as $search_tag) {
101:                 if (array_search($search_tag, $item_labels) !== false) {
102:                     $return_array[(int)$item_id] = new static((int)$item_id);
103:                     break;
104:                 }
105:             }
106:         }
107:         return $return_array;
108:     }
109: 
110:     static function get_scope($id)
111:     {
112:         $site = static::get_site($id, false);
113:         if (!empty($site)) {
114:             return static::SCOPE_SITE;
115:         }
116:         $task = static::get_task($id);
117:         if (!empty($task)) {
118:             return static::SCOPE_TASK;
119:         }
120:         $group = static::get_group($id);
121:         if (!empty($group)) {
122:             return static::SCOPE_GROUP;
123:         }
124:         $activity = static::get_activity($id);
125:         if (!empty($activity)) {
126:             return static::SCOPE_ACTIVITY;
127:         }
128:         $example = static::get_example($id);
129:         if (!empty($example)) {
130:             return static::SCOPE_EXAMPLE;
131:         }
132:         $tricky_topic = static::get_tricky_topic($id);
133:         if (!empty($tricky_topic)) {
134:             return static::SCOPE_TRICKYTOPIC;
135:         }
136:         return null;
137:     }
138: 
139:     static function get_site($id, $recursive = false)
140:     {
141:         $site_array = UBCollection::get_items($id, ClipitSite::REL_SITE_VIDEO, true);
142:         if (!empty($site_array)) {
143:             return array_pop($site_array);
144:         }
145:         if ($recursive) {
146:             $clone_array = static::get_clones($id, true);
147:             foreach ($clone_array as $clone_id) {
148:                 $site_array = UBCollection::get_items($clone_id, ClipitSite::REL_SITE_VIDEO, true);
149:                 if (!empty($site_array)) {
150:                     return array_pop($site_array);
151:                 }
152:             }
153:         }
154:         return null;
155:     }
156: 
157:     static function get_task($id)
158:     {
159:         $task = UBCollection::get_items($id, ClipitTask::REL_TASK_FILE, true);
160:         if (empty($task)) {
161:             return null;
162:         }
163:         return array_pop($task);
164:     }
165: 
166:     /**
167:      * Get the Group where a Resource is located
168:      *
169:      * @param int $id Resource ID
170:      *
171:      * @return int|null Returns the Group ID, or null if none.
172:      */
173:     static function get_group($id) {
174:         $file = new static($id);
175:         if(!empty($file->cloned_from)) {
176:             return static::get_group($file->cloned_from);
177:         }
178:         $group = UBCollection::get_items($id, ClipitGroup::REL_GROUP_FILE, true);
179:         if (empty($group)) {
180:             return null;
181:         }
182:         return (int)array_pop($group);
183:     }
184: 
185:     static function get_activity($id)
186:     {
187:         $group_id = static::get_group($id);
188:         if (!empty($group_id)) {
189:             return ClipitGroup::get_activity($group_id);
190:         } else {
191:             $activity = UBCollection::get_items($id, ClipitActivity::REL_ACTIVITY_FILE, true);
192:             if (empty($activity)) {
193:                 return null;
194:             }
195:             return array_pop($activity);
196:         }
197:     }
198: 
199:     static function get_example($id)
200:     {
201:         $example = UBCollection::get_items($id, ClipitExample::REL_EXAMPLE_FILE, true);
202:         if (empty($example)) {
203:             return null;
204:         }
205:         return array_pop($example);
206:     }
207: 
208:     static function get_tricky_topic($id)
209:     {
210:         $activity_array = UBCollection::get_items($id, ClipitTrickyTopic::REL_TRICKYTOPIC_FILE, true);
211:         if (empty($activity_array)) {
212:             return null;
213:         }
214:         return array_pop($activity_array);
215:     }
216: 
217:     /**
218:      * Add Tags to a File.
219:      *
220:      * @param int $id Id of the File to add Tags to.
221:      * @param array $tag_array Array of Tag Ids to add to the File.
222:      *
223:      * @return bool Returns true if added correctly, or false if error.
224:      */
225:     static function add_tags($id, $tag_array)
226:     {
227:         return UBCollection::add_items($id, $tag_array, static::REL_FILE_TAG);
228:     }
229: 
230:     /**
231:      * Remove Tags from a File.
232:      *
233:      * @param int $id Id of the File to remove Tags from.
234:      * @param array $tag_array Array of Tags Ids to remove from the File.
235:      *
236:      * @return bool Returns true if removed correctly, or false if error.
237:      */
238:     static function remove_tags($id, $tag_array)
239:     {
240:         return UBCollection::remove_items($id, $tag_array, static::REL_FILE_TAG);
241:     }
242: 
243:     /**
244:      * Add Labels to a File.
245:      *
246:      * @param int $id Id of the File to add Labels to.
247:      * @param array $label_array Array of Label Ids to add to the File.
248:      *
249:      * @return bool Returns true if added correctly, or false if error.
250:      */
251:     static function add_labels($id, $label_array)
252:     {
253:         return UBCollection::add_items($id, $label_array, static::REL_FILE_LABEL);
254:     }
255: 
256:     /**
257:      * Remove Labels from a File.
258:      *
259:      * @param int $id Id of the File to remove Labels from.
260:      * @param array $label_array Array of Label Ids to remove from the File.
261:      *
262:      * @return bool Returns true if removed correctly, or false if error.
263:      */
264:     static function remove_labels($id, $label_array)
265:     {
266:         return UBCollection::remove_items($id, $label_array, static::REL_FILE_LABEL);
267:     }
268: 
269:     /**
270:      * Add Read Array for a File
271:      *
272:      * @param int $id ID of the File
273:      * @param array $read_array Array of User IDs who have read the File
274:      *
275:      * @return bool True if OK, false if error
276:      */
277:     static function add_read_array($id, $read_array) {
278:         return UBCollection::add_items($id, $read_array, static::REL_FILE_USER);
279:     }
280: 
281:     /**
282:      * Set Read Array for a File
283:      *
284:      * @param int $id ID of the File
285:      * @param array $read_array Array of User IDs who have read the File
286:      *
287:      * @return bool True if OK, false if error
288:      */
289:     static function set_read_array($id, $read_array) {
290:         return UBCollection::set_items($id, $read_array, static::REL_FILE_USER);
291:     }
292: 
293:     /**
294:      * Remove Read Array for a File
295:      *
296:      * @param int $id ID of the File
297:      * @param array $read_array Array of User IDs who have read the File
298:      *
299:      * @return bool True if OK, false if error
300:      */
301:     static function remove_read_array($id, $read_array) {
302:         return UBCollection::remove_items($id, $read_array, static::REL_FILE_USER);
303:     }
304: 
305:     /**
306:      * Get Read Array for a File
307:      *
308:      * @param int $id ID of the File
309:      *
310:      * @return static[] Array of File IDs
311:      */
312:     static function get_read_array($id) {
313:         return UBCollection::get_items($id, static::REL_FILE_USER);
314:     }
315: 
316: 
317:     /**
318:      * Get a list of Users who have read a File, or optionally whether certain Users have read it
319:      *
320:      * @param int $id ID of the File
321:      * @param null|array $user_array List of User IDs - optional
322:      *
323:      * @return static[] Array with key => value: user_id => read_status, where read_status is bool
324:      */
325:     static function get_read_status($id, $user_array = null) {
326:         $props = static::get_properties($id, array("read_array", "owner_id"));
327:         $read_array = $props["read_array"];
328:         $owner_id = $props["owner_id"];
329:         if (!$user_array) {
330:             return $read_array;
331:         } else {
332:             $return_array = array();
333:             foreach ($user_array as $user_id) {
334:                 if ((int)$user_id == (int)$owner_id || in_array($user_id, $read_array)) {
335:                     $return_array[$user_id] = true;
336:                 } else {
337:                     $return_array[$user_id] = false;
338:                 }
339:             }
340:             return $return_array;
341:         }
342:     }
343: 
344:     /**
345:      * Set the Read status for a File
346:      *
347:      * @param int $id ID of File
348:      * @param bool $read_value Read status value: true = read, false = unread
349:      * @param array $user_array Array of User IDs
350:      *
351:      * @return bool|int ID of File if Ok, false if error
352:      * @throws InvalidParameterException
353:      */
354:     static function set_read_status($id, $read_value, $user_array) {
355:         $read_array = static::get_read_array($id);
356:         $update_flag = false;
357:         foreach ($user_array as $user_id) {
358:             $index = array_search((int)$user_id, $read_array);
359:             if ($read_value === true && $index === false) {
360:                 array_push($read_array, $user_id);
361:                 $update_flag = true;
362:             } elseif ($read_value === false && $index !== false) {
363:                 array_splice($read_array, $index, 1);
364:                 $update_flag = true;
365:             }
366:         }
367:         if ($update_flag) {
368:             return static::set_read_array($id, $read_array);
369:         } else {
370:             return $id;
371:         }
372:     }
373: 
374: 
375: 
376:     /**
377:      * Get Tag Ids from a File.
378:      *
379:      * @param int $id Id of the File to get Tags from.
380:      *
381:      * @return int[] Returns array of Tag Ids, or false if error.
382:      */
383:     static function get_tags($id)
384:     {
385:         return UBCollection::get_items($id, static::REL_FILE_TAG);
386:     }
387: 
388:     /**
389:      * Get Label Ids from a File.
390:      *
391:      * @param int $id Id of the File to get Labels from.
392:      *
393:      * @return int[] Returns array of Label Ids, or false if error.
394:      */
395:     static function get_labels($id)
396:     {
397:         return UBCollection::get_items($id, static::REL_FILE_LABEL);
398:     }
399: 
400:     /**
401:      * Set Tags to a File.
402:      *
403:      * @param int $id Id of the File to set Tags to.
404:      * @param array $tag_array Array of Tag Ids to set to the File.
405:      *
406:      * @return bool Returns true if added correctly, or false if error.
407:      */
408:     static function set_tags($id, $tag_array)
409:     {
410:         return UBCollection::set_items($id, $tag_array, static::REL_FILE_TAG);
411:     }
412: 
413:     /**
414:      * Set Labels to a File.
415:      *
416:      * @param int $id Id of the File to set Labels to.
417:      * @param array $label_array Array of Label Ids to set to the File.
418:      *
419:      * @return bool Returns true if added correctly, or false if error.
420:      */
421:     static function set_labels($id, $label_array)
422:     {
423:         return UBCollection::set_items($id, $label_array, static::REL_FILE_LABEL);
424:     }
425: }
426: 
API documentation generated by ApiGen 2.8.0