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 complete User Rating linked to a published Resource, containing an Overall boolean rating, links to Tag Ratings and
 17:  * Performance item Ratings.
 18:  */
 19: class ClipitRating extends UBItem {
 20:     /**
 21:      * @const string Elgg entity SUBTYPE for this class
 22:      */
 23:     const SUBTYPE = "ClipitRating";
 24:     const REL_RATING_TAGRATING = "ClipitRating-ClipitTagRating";
 25:     const REL_RATING_PERFORMANCERATING = "ClipitRating-ClipitPerformanceRating";
 26:     /**
 27:      * @var int Rating target (ClipitVideo or ClipitStoryboard)
 28:      */
 29:     public $target = 0;
 30:     /**
 31:      * @var int Overall rating opinion (YES = true, NO = false=
 32:      */
 33:     public $overall = false;
 34:     /**
 35:      * @var array Ratings about Tags used
 36:      */
 37:     public $tag_rating_array = array();
 38:     /**
 39:      * @var array Ratings about Performance tips used
 40:      */
 41:     public $performance_rating_array = array();
 42: 
 43:     /**
 44:      * Loads object parameters stored in Elgg
 45:      *
 46:      * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
 47:      */
 48:     protected function copy_from_elgg($elgg_entity) {
 49:         parent::copy_from_elgg($elgg_entity);
 50:         $this->target = (int)$elgg_entity->get("target");
 51:         $this->overall = (bool)$elgg_entity->get("overall");
 52:         $this->tag_rating_array = (array)static::get_tag_ratings($this->id);
 53:         $this->performance_rating_array = (array)static::get_performance_ratings($this->id);
 54:     }
 55: 
 56:     /**
 57:      * Copy $this object parameters into an Elgg entity.
 58:      *
 59:      * @param ElggEntity $elgg_entity Elgg object instance to save $this to
 60:      */
 61:     protected function copy_to_elgg($elgg_entity) {
 62:         parent::copy_to_elgg($elgg_entity);
 63:         $elgg_entity->set("target", (int)$this->target);
 64:         $elgg_entity->set("overall", (bool)$this->overall);
 65:     }
 66: 
 67:     /**
 68:      * Saves this instance to the system.
 69:      * @param  bool $double_save if $double_save is true, this object is saved twice to ensure that all properties are updated properly. E.g. the time created property can only beset on ElggObjects during an update. Defaults to false!
 70:      * @return bool|int Returns the Id of the saved instance, or false if error
 71:      */
 72:     protected function save($double_save=false) {
 73:         parent::save($double_save);
 74:         static::set_tag_ratings($this->id, (array)$this->tag_rating_array);
 75:         static::set_performance_ratings($this->id, (array)$this->performance_rating_array);
 76:         return $this->id;
 77:     }
 78: 
 79:     static function get_target($id){
 80:         $prop_value_array = static::get_properties($id, array("target"));
 81:         if(empty($prop_value_array)){
 82:             return null;
 83:         }
 84:         return (int)$prop_value_array["target"];
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Get Ratings by Target
 90:      *
 91:      * @param array $target_array Array of Target IDs
 92:      *
 93:      * @return static[] Array of [target] => array(Ratings)
 94:      */
 95:     static function get_by_target($target_array) {
 96:         $rating_array = array();
 97:         foreach($target_array as $target_id) {
 98:             $elgg_objects = elgg_get_entities_from_metadata(
 99:                 array(
100:                     'type' => static::TYPE, 'subtype' => static::SUBTYPE, 'metadata_names' => array("target"),
101:                     'metadata_values' => array($target_id), 'limit' => 0
102:                 )
103:             );
104:             if(!empty($elgg_objects)) {
105:                 $temp_array = array();
106:                 foreach($elgg_objects as $elgg_object) {
107:                     $temp_array[] = new static($elgg_object->guid);
108:                 }
109:                 $rating_array[$target_id] = $temp_array;
110:             } else {
111:                 $rating_array[$target_id] = null;
112:             }
113:         }
114:         return $rating_array;
115:     }
116: 
117:     /**
118:      * Get Ratings made by a User for a Target
119:      *
120:      * @param int $user_id User ID
121:      * @param int $target_id Target ID
122:      *
123:      * @return ClipitRating|null Returns a Rating, or null if any.
124:      */
125:     static function get_user_rating_for_target($user_id, $target_id) {
126:         $rating = elgg_get_entities_from_metadata(array(
127:             'type' => static::TYPE,
128:             'subtype' => static::SUBTYPE,
129:             'metadata_names' => array("target"),
130:             'metadata_values' => array($target_id),
131:             'owner_guid' => $user_id
132:         ));
133:         if(empty($rating)){
134:             return null;
135:         }
136:         $rating_id = array_pop($rating)->guid;
137:         return new static($rating_id);
138:     }
139: 
140:     /**
141:      * Get the average overall rating
142:      *
143:      * @param int $target_id ID of target to return overall rating from
144:      *
145:      * @return float Average overall rating [0.0-1.0]
146:      */
147:     static function get_average_rating_for_target($target_id) {
148:         $rating_array = static::get_by_target(array($target_id));
149:         $rating_array = $rating_array[$target_id];
150:         $average_rating = 0;
151:         $count = 0;
152:         if(!empty($rating_array)) {
153:             foreach ($rating_array as $rating) {
154:                 if ($rating->overall === true) {
155:                     $average_rating++;
156:                 }
157:                 $count++;
158:             }
159:         }
160:         if(empty($count)) {
161:             return null;
162:         }
163:         return $average_rating = $average_rating / $count;
164:     }
165: 
166:     static function add_tag_ratings($id, $tag_rating_array) {
167:         return UBCollection::add_items($id, $tag_rating_array, static::REL_RATING_TAGRATING);
168:     }
169: 
170:     static function set_tag_ratings($id, $tag_rating_array) {
171:         return UBCollection::set_items($id, $tag_rating_array, static::REL_RATING_TAGRATING);
172:     }
173: 
174:     static function remove_tag_ratings($id, $tag_rating_array) {
175:         return UBCollection::remove_items($id, $tag_rating_array, static::REL_RATING_TAGRATING);
176:     }
177: 
178:     static function get_tag_ratings($id) {
179:         return UBCollection::get_items($id, static::REL_RATING_TAGRATING);
180:     }
181: 
182:     static function add_performance_ratings($id, $performance_rating_array) {
183:         return UBCollection::add_items($id, $performance_rating_array, static::REL_RATING_PERFORMANCERATING);
184:     }
185: 
186:     static function set_performance_ratings($id, $performance_rating_array) {
187:         return UBCollection::set_items($id, $performance_rating_array, static::REL_RATING_PERFORMANCERATING);
188:     }
189: 
190:     static function remove_performance_ratings($id, $performance_rating_array) {
191: 
192:         return UBCollection::remove_items($id, $performance_rating_array, static::REL_RATING_PERFORMANCERATING);
193:     }
194: 
195:     static function get_performance_ratings($id) {
196:         return UBCollection::get_items($id, static::REL_RATING_PERFORMANCERATING);
197:     }
198: }
199: 
200: 
API documentation generated by ApiGen 2.8.0