1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18:
19: class ClipitRating extends UBItem {
20: 21: 22:
23: const SUBTYPE = "ClipitRating";
24: const REL_RATING_TAGRATING = "ClipitRating-ClipitTagRating";
25: const REL_RATING_PERFORMANCERATING = "ClipitRating-ClipitPerformanceRating";
26: 27: 28:
29: public $target = 0;
30: 31: 32:
33: public $overall = false;
34: 35: 36:
37: public $tag_rating_array = array();
38: 39: 40:
41: public $performance_rating_array = array();
42:
43: 44: 45: 46: 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: 58: 59: 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: 69: 70: 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: 90: 91: 92: 93: 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: 119: 120: 121: 122: 123: 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: 142: 143: 144: 145: 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: