1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitPerformanceRating extends UBItem {
19: 20: 21:
22: const SUBTYPE = "ClipitPerformanceRating";
23: public $performance_item = 0;
24: public $star_rating = 0;
25:
26: 27: 28: 29: 30:
31: protected function copy_from_elgg($elgg_entity) {
32: parent::copy_from_elgg($elgg_entity);
33: $this->performance_item = (int)$elgg_entity->get("performance_item");
34: $this->star_rating = (int)$elgg_entity->get("star_rating");
35: }
36:
37: 38: 39: 40: 41:
42: protected function copy_to_elgg($elgg_entity) {
43: parent::copy_to_elgg($elgg_entity);
44: $elgg_entity->set("performance_item", (int)$this->performance_item);
45: $elgg_entity->set("star_rating", (int)$this->star_rating);
46: }
47:
48: static function get_by_item($item_array) {
49: $performance_rating_array = array();
50: foreach($item_array as $item_id) {
51: $elgg_objects = elgg_get_entities_from_metadata(
52: array(
53: 'type' => static::TYPE, 'subtype' => static::SUBTYPE, 'metadata_names' => array("performance_item"),
54: 'metadata_values' => array($item_id), 'limit' => 0
55: )
56: );
57: if(!empty($elgg_objects)) {
58: $temp_array = array();
59: foreach($elgg_objects as $elgg_object) {
60: $temp_array[] = new static($elgg_object->guid, $elgg_object);
61: }
62: $performance_rating_array[$item_id] = $temp_array;
63: } else {
64: $performance_rating_array[$item_id] = null;
65: }
66: }
67: return $performance_rating_array;
68: }
69:
70: 71: 72: 73: 74: 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->performance_rating_array as $performance_rating_id) {
85: $prop_value_array = static::get_properties($performance_rating_id, array("star_rating"));
86: $average_rating += (int)$prop_value_array["star_rating"];
87: $count ++;
88: }
89: }
90: }
91: if(!empty($count)) {
92: return $average_rating = $average_rating / $count;
93: } else {
94: return null;
95: }
96: }
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->performance_rating_array as $performance_rating_id) {
113: $prop_value_array = static::get_properties($performance_rating_id, array("performance_item", "star_rating"));
114: $performance_item = $prop_value_array["performance_item"];
115: $star_rating = $prop_value_array["star_rating"];
116: if(!isset($average_rating[$performance_item])){
117: $average_rating[$performance_item] = (int)$star_rating;
118: $count[$performance_item] = 1;
119: } else{
120: $average_rating[$performance_item] += (int)$star_rating;
121: $count[$performance_item]++;
122: }
123: }
124: }
125: if(!empty($count)) {
126: foreach($count as $performance_item => $total) {
127: $average_rating[$performance_item] = $average_rating[$performance_item] / $total;
128: }
129: }
130: }
131: return $average_rating;
132: }
133:
134: static function get_average_user_rating_for_target($user_id, $target_id) {
135: $rating = ClipitRating::get_user_rating_for_target($user_id, $target_id);
136: $average_rating = 0;
137: $count = 0;
138: if(!empty($rating)) {
139: foreach ($rating->performance_rating_array as $performance_rating_id) {
140: $prop_value_array = static::get_properties($performance_rating_id, array("star_rating"));
141: $average_rating += (int)$prop_value_array["star_rating"];
142: $count++;
143: }
144: }
145: if(!empty($count)) {
146: return $average_rating = $average_rating / $count;
147: } else {
148: return null;
149: }
150: }
151: }