1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18:
19: abstract class ClipitResource extends UBItem {
20: 21: 22:
23: const SUBTYPE = "ClipitResource";
24: const REL_RESOURCE_TAG = "ClipitResource-ClipitTag";
25: const REL_RESOURCE_LABEL = "ClipitResource-ClipitLabel";
26: const REL_RESOURCE_PERFORMANCE = "ClipitResource-ClipitPerformanceItem";
27: const REL_RESOURCE_USER = "ClipitResource-ClipitUser";
28:
29: const REL_SITE_RESOURCE = "";
30: const REL_EXAMPLE_RESOURCE = "";
31: const REL_ACTIVITY_RESOURCE = "";
32: const REL_TRICKYTOPIC_RESOURCE = "";
33: const REL_TASK_RESOURCE = "";
34: const REL_GROUP_RESOURCE = "";
35:
36: const SCOPE_GROUP = "group";
37: const SCOPE_ACTIVITY = "activity";
38: const SCOPE_SITE = "site";
39: const SCOPE_EXAMPLE = "example";
40: const SCOPE_TRICKYTOPIC = "tricky_topic";
41: const SCOPE_TASK = "task";
42:
43: public $tag_array = array();
44: public $performance_item_array = array();
45: public $label_array = array();
46: public $read_array = array();
47:
48: public $overall_rating_average = 0.0;
49: public $tag_rating_average = 0.0;
50: public $performance_rating_average = 0.0;
51:
52: 53: 54: 55: 56:
57: protected function copy_from_elgg($elgg_entity)
58: {
59: parent::copy_from_elgg($elgg_entity);
60: $this->tag_array = (array)static::get_tags($this->id);
61: $this->performance_item_array = (array)static::get_performance_items($this->id);
62: $this->label_array = (array)static::get_labels($this->id);
63: $this->read_array = (array)static::get_read_array($this->id);
64: $this->overall_rating_average = (float)$elgg_entity->get("overall_rating_average");
65: $this->tag_rating_average = (float)$elgg_entity->get("tag_rating_average");
66: $this->performance_rating_average = (float)$elgg_entity->get("performance_rating_average");
67: }
68:
69: 70: 71: 72: 73:
74: protected function copy_to_elgg($elgg_entity)
75: {
76: parent::copy_to_elgg($elgg_entity);
77: $elgg_entity->set("overall_rating_average", (float)$this->overall_rating_average);
78: $elgg_entity->set("tag_rating_average", (float)$this->tag_rating_average);
79: $elgg_entity->set("performance_rating_average", (float)$this->performance_rating_average);
80: }
81:
82: 83: 84: 85: 86:
87: protected function save($double_save = false)
88: {
89: parent::save($double_save);
90: static::set_tags($this->id, (array)$this->tag_array);
91: static::set_performance_items($this->id, (array)$this->performance_item_array);
92: static::set_labels($this->id, (array)$this->label_array);
93: static::set_read_array($this->id, $this->read_array);
94: return $this->id;
95: }
96:
97: 98: 99: 100: 101: 102: 103: 104:
105: static function add_read_array($id, $read_array) {
106: return UBCollection::add_items($id, $read_array, static::REL_RESOURCE_USER);
107: }
108:
109: 110: 111: 112: 113: 114: 115: 116:
117: static function set_read_array($id, $read_array) {
118: return UBCollection::set_items($id, $read_array, static::REL_RESOURCE_USER);
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128:
129: static function remove_read_array($id, $read_array) {
130: return UBCollection::remove_items($id, $read_array, static::REL_RESOURCE_USER);
131: }
132:
133: 134: 135: 136: 137: 138: 139:
140: static function get_read_array($id) {
141: return UBCollection::get_items($id, static::REL_RESOURCE_USER);
142: }
143:
144: static function update_average_ratings($id)
145: {
146: $prop_value_array["overall_rating_average"] = (float)ClipitRating::get_average_rating_for_target($id);
147: $prop_value_array["tag_rating_average"] = (float)ClipitTagRating::get_average_rating_for_target($id);
148: $prop_value_array["performance_rating_average"] = (float)ClipitPerformanceRating::get_average_rating_for_target($id);
149: return static::set_properties($id, $prop_value_array);
150: }
151:
152: static function get_by_tags($tag_array)
153: {
154: $return_array = array();
155: $all_items = static::get_all(0, 0, "", true, true);
156: foreach ($all_items as $item_id) {
157: $item_tags = (array)static::get_tags((int)$item_id);
158: foreach ($tag_array as $search_tag) {
159: if (array_search($search_tag, $item_tags) !== false) {
160: $return_array[(int)$item_id] = new static((int)$item_id);
161: break;
162: }
163: }
164: }
165: return $return_array;
166: }
167:
168: 169: 170: 171: 172: 173: 174:
175: static function get_tags($id)
176: {
177: return UBCollection::get_items($id, static::REL_RESOURCE_TAG);
178: }
179:
180: static function add_performance_items($id, $performance_item_array) {
181: return UBCollection::add_items($id, $performance_item_array, static::REL_RESOURCE_PERFORMANCE);
182: }
183: static function set_performance_items($id, $performance_item_array) {
184: return UBCollection::set_items($id, $performance_item_array, static::REL_RESOURCE_PERFORMANCE);
185: }
186: static function remove_performance_items($id, $performance_item_array) {
187: return UBCollection::remove_items($id, $performance_item_array, static::REL_RESOURCE_PERFORMANCE);
188: }
189: static function get_performance_items($id) {
190: return UBCollection::get_items($id, static::REL_RESOURCE_PERFORMANCE);
191: }
192:
193: static function get_by_labels($label_array)
194: {
195: $return_array = array();
196: $all_items = static::get_all(0, 0, "", true, true);
197: foreach ($all_items as $item_id) {
198: $item_labels = (array)static::get_labels((int)$item_id);
199: foreach ($label_array as $search_tag) {
200: if (array_search($search_tag, $item_labels) !== false) {
201: $return_array[(int)$item_id] = new static((int)$item_id);
202: break;
203: }
204: }
205: }
206: return $return_array;
207: }
208:
209: 210: 211: 212: 213: 214: 215:
216: static function get_labels($id)
217: {
218: return UBCollection::get_items($id, static::REL_RESOURCE_LABEL);
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: static function add_labels($id, $label_array)
230: {
231: return UBCollection::add_items($id, $label_array, static::REL_RESOURCE_LABEL);
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241:
242: static function remove_labels($id, $label_array)
243: {
244: return UBCollection::remove_items($id, $label_array, static::REL_RESOURCE_LABEL);
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254:
255: static function set_labels($id, $label_array)
256: {
257: return UBCollection::set_items($id, $label_array, static::REL_RESOURCE_LABEL);
258: }
259:
260: static function get_scope($id)
261: {
262: $site = static::get_site($id, false);
263: if (!empty($site)) {
264: return static::SCOPE_SITE;
265: }
266: $example = static::get_example($id);
267: if (!empty($example)) {
268: return static::SCOPE_EXAMPLE;
269: }
270: $task = static::get_task($id);
271: if (!empty($task)) {
272: return static::SCOPE_TASK;
273: }
274: $group = static::get_group($id);
275: if (!empty($group)) {
276: return static::SCOPE_GROUP;
277: }
278: $activity = static::get_activity($id);
279: if (!empty($activity)) {
280: return static::SCOPE_ACTIVITY;
281: }
282: $tricky_topic = static::get_tricky_topic($id);
283: if (!empty($tricky_topic)) {
284: return static::SCOPE_TRICKYTOPIC;
285: }
286: return null;
287: }
288:
289: static function get_site($id, $recursive = false)
290: {
291: $site_array = UBCollection::get_items($id, static::REL_SITE_RESOURCE, true);
292: if (!empty($site_array)) {
293: return array_pop($site_array);
294: }
295: if ($recursive) {
296: $clone_array = static::get_clones($id, true);
297: foreach ($clone_array as $clone_id) {
298: $site_array = UBCollection::get_items($clone_id, static::REL_SITE_RESOURCE, true);
299: if (!empty($site_array)) {
300: return array_pop($site_array);
301: }
302: }
303: }
304: return null;
305: }
306:
307: static function get_example($id)
308: {
309: $example = UBCollection::get_items($id, static::REL_EXAMPLE_RESOURCE, true);
310: if (empty($example)) {
311: return null;
312: }
313: return array_pop($example);
314: }
315:
316: static function get_task($id)
317: {
318: $task = UBCollection::get_items($id, static::REL_TASK_RESOURCE, true);
319: if (empty($task)) {
320: return null;
321: }
322: return array_pop($task);
323: }
324:
325: 326: 327: 328: 329: 330: 331:
332: static function get_group($id) {
333: $resource = new static($id);
334: if(!empty($resource->cloned_from)) {
335: return static::get_group($resource->cloned_from);
336: }
337: $group = UBCollection::get_items($id, static::REL_GROUP_RESOURCE, true);
338: if(empty($group)) {
339: return null;
340: }
341: return (int)array_pop($group);
342: }
343:
344: static function get_activity($id) {
345: $group_id = static::get_group($id);
346: if(!empty($group_id)) {
347: return ClipitGroup::get_activity($group_id);
348: } else {
349: $activity = UBCollection::get_items($id, static::REL_ACTIVITY_RESOURCE, true);
350: if(empty($activity)) {
351: return null;
352: }
353: return array_pop($activity);
354: }
355: }
356:
357: static function get_tricky_topic($id)
358: {
359: $activity_array = UBCollection::get_items($id, static::REL_TRICKYTOPIC_RESOURCE, true);
360: if (empty($activity_array)) {
361: return null;
362: }
363: return array_pop($activity_array);
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373:
374: static function add_tags($id, $tag_array)
375: {
376: return UBCollection::add_items($id, $tag_array, static::REL_RESOURCE_TAG);
377: }
378:
379: 380: 381: 382: 383: 384: 385: 386:
387: static function remove_tags($id, $tag_array)
388: {
389: return UBCollection::remove_items($id, $tag_array, static::REL_RESOURCE_TAG);
390: }
391:
392:
393: 394: 395: 396: 397: 398: 399: 400:
401: static function get_read_status($id, $user_array = null)
402: {
403: $props = static::get_properties($id, array("read_array", "owner_id"));
404: $read_array = $props["read_array"];
405: $owner_id = $props["owner_id"];
406: if (!$user_array) {
407: return $read_array;
408: } else {
409: $return_array = array();
410: foreach ($user_array as $user_id) {
411: if ((int)$user_id == (int)$owner_id || in_array($user_id, $read_array)) {
412: $return_array[$user_id] = true;
413: } else {
414: $return_array[$user_id] = false;
415: }
416: }
417: return $return_array;
418: }
419: }
420:
421: 422: 423: 424: 425: 426: 427: 428: 429: 430:
431: static function set_read_status($id, $read_value, $user_array)
432: {
433: $read_array = static::get_read_array($id);
434: $update_flag = false;
435: foreach ($user_array as $user_id) {
436: $index = array_search((int)$user_id, $read_array);
437: if ($read_value === true && $index === false) {
438: array_push($read_array, $user_id);
439: $update_flag = true;
440: } elseif ($read_value === false && $index !== false) {
441: array_splice($read_array, $index, 1);
442: $update_flag = true;
443: }
444: }
445: if ($update_flag) {
446: return static::set_read_array($id, $read_array);
447: } else {
448: return $id;
449: }
450: }
451:
452:
453:
454: 455: 456: 457: 458: 459: 460: 461:
462: static function set_tags($id, $tag_array)
463: {
464: return UBCollection::set_items($id, $tag_array, static::REL_RESOURCE_TAG);
465: }
466:
467: }