1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18:
19: class ClipitTagRating extends UBItem {
20: 21: 22:
23: const SUBTYPE = "ClipitTagRating";
24: 25: 26:
27: public $tag_id = 0;
28: 29: 30:
31: public $is_used = false;
32:
33: 34: 35: 36: 37:
38: protected function copy_from_elgg($elgg_entity) {
39: parent::copy_from_elgg($elgg_entity);
40: $this->tag_id = (int)$elgg_entity->get("tag_id");
41: $this->is_used = (bool)$elgg_entity->get("is_used");
42: }
43:
44: 45: 46: 47: 48:
49: protected function copy_to_elgg($elgg_entity) {
50: parent::copy_to_elgg($elgg_entity);
51: $elgg_entity->set("tag_id", (int)$this->tag_id);
52: $elgg_entity->set("is_used", (bool)$this->is_used);
53: }
54:
55: static function get_by_tag($tag_array) {
56: $tag_rating_array = array();
57: foreach($tag_array as $tag_id) {
58: $elgg_objects = elgg_get_entities_from_metadata(
59: array(
60: 'type' => static::TYPE, 'subtype' => static::SUBTYPE, 'metadata_names' => array("tag_id"),
61: 'metadata_values' => array($tag_id), 'limit' => 0
62: )
63: );
64: if(!empty($elgg_objects)) {
65: $temp_array = array();
66: foreach($elgg_objects as $elgg_object) {
67: $temp_array[] = new static($elgg_object->guid, $elgg_object);
68: }
69: $tag_rating_array[$tag_id] = $temp_array;
70: } else {
71: $tag_rating_array[$tag_id] = null;
72: }
73: }
74: return $tag_rating_array;
75: }
76:
77: static function get_average_rating_for_target($target_id) {
78: $rating_array = ClipitRating::get_by_target(array($target_id));
79: $rating_array = $rating_array[$target_id];
80: $average_rating = 0;
81: $count = 0;
82: if(!empty($rating_array)) {
83: foreach ($rating_array as $rating) {
84: foreach ($rating->tag_rating_array as $tag_rating_id) {
85: $tag_rating = new static($tag_rating_id);
86: if ($tag_rating->is_used) {
87: $average_rating++;
88: }
89: $count++;
90: }
91: }
92: }
93: if(!empty($count)) {
94: return $average_rating = $average_rating / $count;
95: } else {
96: return null;
97: }
98: }
99:
100: 101: 102: 103: 104:
105: static function get_item_average_rating_for_target($target_id) {
106: $rating_array = ClipitRating::get_by_target(array($target_id));
107: $rating_array = $rating_array[$target_id];
108: $average_rating = array();
109: $count = array();
110: if(!empty($rating_array)) {
111: foreach($rating_array as $rating) {
112: foreach($rating->tag_rating_array as $tag_rating_id) {
113: $tag_rating = new static($tag_rating_id);
114: $tag_id = (int)$tag_rating->tag_id;
115: $is_used = (bool)$tag_rating->is_used;
116: if(!isset($average_rating[$tag_id])){
117: if($is_used) {
118: $average_rating[$tag_id] = 1;
119: }
120: $count[$tag_id] = 1;
121: } else{
122: if($is_used) {
123: $average_rating[$tag_id]++;
124: }
125: $count[$tag_id]++;
126: }
127: }
128: }
129: if(!empty($count)) {
130: foreach($count as $performance_item => $total) {
131: $average_rating[$performance_item] = $average_rating[$performance_item] / $total;
132: }
133: }
134: }
135: return $average_rating;
136: }
137:
138: static function get_average_user_rating_for_target($user_id, $target_id) {
139: $rating = ClipitRating::get_user_rating_for_target($user_id, $target_id);
140: $average_rating = 0;
141: $count = 0;
142: if(!empty($rating)) {
143: foreach ($rating->tag_rating_array as $tag_rating_id) {
144: $tag_rating = new static($tag_rating_id);
145: if ($tag_rating->is_used) {
146: $average_rating++;
147: }
148: $count++;
149: }
150: }
151: if(!empty($count)) {
152: return $average_rating = $average_rating / $count;
153: } else {
154: return null;
155: }
156: }
157: }