1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitTask extends UBItem {
19: 20: 21:
22: const SUBTYPE = "ClipitTask";
23:
24: const TYPE_QUIZ_TAKE = "quiz_take";
25: const TYPE_RESOURCE_DOWNLOAD = "resource_download";
26: const TYPE_STORYBOARD_UPLOAD = "storyboard_upload";
27: const TYPE_STORYBOARD_FEEDBACK = "storyboard_feedback";
28: const TYPE_VIDEO_UPLOAD = "video_upload";
29: const TYPE_VIDEO_FEEDBACK = "video_feedback";
30: const TYPE_OTHER = "other";
31:
32: const REL_TASK_STORYBOARD = "ClipitTask-ClipitStoryboard";
33: const REL_TASK_VIDEO = "ClipitTask-ClipitVideo";
34: const REL_TASK_FILE = "ClipitTask-ClipitFile";
35: const REL_TASK_QUIZ = "ClipitTask-ClipitQuiz";
36: const REL_TASK_PERFORMANCE = "ClipitTask-ClipitPerformanceItem";
37:
38: const STATUS_LOCKED = "locked";
39: const STATUS_ACTIVE = "active";
40: const STATUS_FINISHED = "finished";
41:
42: public $task_type = "";
43: public $start = 0;
44: public $end = 0;
45: public $status = "";
46: public $parent_task = 0;
47: public $task_count = 0;
48: public $activity = 0;
49: public $quiz = 0;
50: public $storyboard_array = array();
51: public $video_array = array();
52: public $file_array = array();
53: public $performance_item_array = array();
54:
55: 56: 57: 58: 59:
60: protected function copy_from_elgg($elgg_entity) {
61: parent::copy_from_elgg($elgg_entity);
62: $this->task_type = (string)$elgg_entity->get("task_type");
63: $this->start = (int)$elgg_entity->get("start");
64: $this->end = (int)$elgg_entity->get("end");
65: $this->status = (string)static::calc_status($this->start, $this->end);
66: $this->parent_task = (int)$elgg_entity->get("parent_task");
67: $this->task_count = (int)$elgg_entity->get("task_count");
68: if($this->end == 0) {
69: $activity_id = static::get_activity($this->id);
70: if(!empty($activity_id)) {
71: $prop_value_array = (int)ClipitActivity::get_properties($activity_id, array("end"));
72: $this->end = $prop_value_array["end"];
73: }
74: }
75: $this->activity = (int)static::get_activity((int)$this->id);
76: $this->quiz = (int)static::get_quiz($this->id);
77: $this->storyboard_array = static::get_storyboards((int)$this->id);
78: $this->video_array = static::get_videos($this->id);
79: $this->file_array = static::get_files($this->id);
80: $this->performance_item_array = (array)static::get_performance_items($this->id);
81: }
82:
83: 84: 85: 86: 87:
88: protected function copy_to_elgg($elgg_entity) {
89: parent::copy_to_elgg($elgg_entity);
90: $elgg_entity->set("task_type", (string)$this->task_type);
91: $elgg_entity->set("start", (int)$this->start);
92: $elgg_entity->set("end", (int)$this->end);
93: $elgg_entity->set("parent_task", (int)$this->parent_task);
94: $elgg_entity->set("task_count", (int)$this->task_count);
95: }
96:
97: 98: 99: 100: 101:
102: protected function save($double_save=false) {
103: parent::save($double_save);
104: static::set_quiz($this->id, $this->quiz);
105: static::set_activity($this->id, $this->activity);
106: static::set_storyboards($this->id, $this->storyboard_array);
107: static::set_videos($this->id, $this->video_array);
108: static::set_files($this->id, $this->file_array);
109: static::set_performance_items($this->id, (array)$this->performance_item_array);
110: return $this->id;
111: }
112:
113: 114: 115: 116: 117: 118: 119: 120:
121: private function calc_status($start, $end) {
122: $date = new DateTime();
123: $now = (int)$date->getTimestamp();
124: if($now < $start) {
125: return static::STATUS_LOCKED;
126: } elseif($now >= $start && $now <= $end) {
127: return static::STATUS_ACTIVE;
128: } else {
129: return static::STATUS_FINISHED;
130: }
131: }
132:
133:
134: 135: 136: 137: 138: 139: 140:
141: static function get_activity($id) {
142: $activity = UBCollection::get_items($id, ClipitActivity::REL_ACTIVITY_TASK, true);
143: return array_pop($activity);
144: }
145:
146: 147: 148: 149: 150: 151: 152: 153:
154: static function set_activity($id, $activity_id) {
155: return UBCollection::add_items($activity_id, array($id), ClipitActivity::REL_ACTIVITY_TASK, true);
156: }
157:
158:
159: static function set_quiz($id, $quiz_id) {
160: return UBCollection::set_items((int)$id, array($quiz_id), static::REL_TASK_QUIZ, true);
161: }
162:
163: static function get_quiz($id) {
164: $quiz_array = UBCollection::get_items((int)$id, static::REL_TASK_QUIZ);
165: if(empty($quiz_array)){
166: return 0;
167: }
168: return (int)array_pop($quiz_array);
169: }
170:
171:
172: static function add_storyboards($id, $storyboard_array) {
173: return UBCollection::add_items($id, $storyboard_array, static::REL_TASK_STORYBOARD);
174: }
175:
176: static function set_storyboards($id, $storyboard_array) {
177: return UBCollection::set_items($id, $storyboard_array, static::REL_TASK_STORYBOARD);
178: }
179:
180: static function remove_storyboards($id, $storyboard_array) {
181: return UBCollection::remove_items($id, $storyboard_array, static::REL_TASK_STORYBOARD);
182: }
183:
184: static function get_storyboards($id) {
185: return UBCollection::get_items($id, static::REL_TASK_STORYBOARD);
186: }
187:
188:
189: static function add_videos($id, $video_array) {
190: return UBCollection::add_items($id, $video_array, static::REL_TASK_VIDEO);
191: }
192:
193: static function set_videos($id, $video_array) {
194: return UBCollection::set_items($id, $video_array, static::REL_TASK_VIDEO);
195: }
196:
197: static function remove_videos($id, $video_array) {
198: return UBCollection::remove_items($id, $video_array, static::REL_TASK_VIDEO);
199: }
200:
201: static function get_videos($id) {
202: return UBCollection::get_items($id, static::REL_TASK_VIDEO);
203: }
204:
205:
206: static function add_files($id, $file_array) {
207: return UBCollection::add_items($id, $file_array, static::REL_TASK_FILE);
208: }
209:
210: static function set_files($id, $file_array) {
211: return UBCollection::set_items($id, $file_array, static::REL_TASK_FILE);
212: }
213:
214: static function remove_files($id, $file_array) {
215: return UBCollection::remove_items($id, $file_array, static::REL_TASK_FILE);
216: }
217:
218: static function get_files($id) {
219: return UBCollection::get_items($id, static::REL_TASK_FILE);
220: }
221:
222:
223: static function add_performance_items($id, $performance_item_array) {
224: return UBCollection::add_items($id, $performance_item_array, static::REL_TASK_PERFORMANCE);
225: }
226:
227: static function set_performance_items($id, $performance_item_array) {
228: return UBCollection::set_items($id, $performance_item_array, static::REL_TASK_PERFORMANCE);
229: }
230:
231: static function remove_performance_items($id, $performance_item_array) {
232: return UBCollection::remove_items($id, $performance_item_array, static::REL_TASK_PERFORMANCE);
233: }
234:
235: static function get_performance_items($id) {
236: return UBCollection::get_items($id, static::REL_TASK_PERFORMANCE);
237: }
238:
239:
240:
241: 242: 243: 244: 245:
246: static function get_child($id){
247: $task_array = static::get_all();
248: foreach($task_array as $task){
249: if((int)$task->parent_task == (int)$id){
250: return $task->id;
251: }
252: }
253: return 0;
254: }
255:
256: 257: 258: 259: 260: 261: 262:
263: static function get_status($id){
264: $prop_value_array = static::get_properties($id, array("status"));
265: return (string)$prop_value_array["status"];
266: }
267:
268:
269: static function get_completed_status($id, $entity_id) {
270: $task = new static((int)$id);
271: switch($task->task_type) {
272: case static::TYPE_QUIZ_TAKE:
273: return ClipitQuiz::has_finished_quiz($task->quiz, $entity_id);
274: case static::TYPE_RESOURCE_DOWNLOAD:
275: $latest_timestamp = 0;
276: $task_files = $task->file_array;
277: foreach($task_files as $file_id){
278: $read_status = ClipitFile::get_read_status($file_id, array($entity_id));
279: if((bool)$read_status[$entity_id] !== true){
280: return false;
281: }
282: $timestamp = UBCollection::get_timestamp($file_id, $entity_id, ClipitFile::REL_FILE_USER);
283: $latest_timestamp = ($timestamp > $latest_timestamp ? $timestamp : $latest_timestamp);
284: }
285: $task_videos = $task->video_array;
286: foreach($task_videos as $video_id){
287: $read_status = ClipitVideo::get_read_status($video_id, array($entity_id));
288: if((bool)$read_status[$entity_id] !== true){
289: return false;
290: }
291: $timestamp = UBCollection::get_timestamp($video_id, $entity_id, ClipitVideo::REL_RESOURCE_USER);
292: $latest_timestamp = ($timestamp > $latest_timestamp ? $timestamp : $latest_timestamp);
293: }
294: $task_storyboards = $task->storyboard_array;
295: foreach($task_storyboards as $storyboard_id){
296: $read_status = ClipitStoryboard::get_read_status($storyboard_id, array($entity_id));
297: if((bool)$read_status[$entity_id] !== true){
298: return false;
299: }
300: $timestamp = UBCollection::get_timestamp($storyboard_id, $entity_id, ClipitStoryboard::REL_RESOURCE_USER);
301: $latest_timestamp = ($timestamp > $latest_timestamp ? $timestamp : $latest_timestamp);
302: }
303: return $latest_timestamp;
304: case static::TYPE_STORYBOARD_UPLOAD:
305: foreach($task->storyboard_array as $storyboard_id) {
306: if((int)ClipitStoryboard::get_group($storyboard_id) === (int)$entity_id) {
307: return true;
308: }
309: }
310: return false;
311: case static::TYPE_STORYBOARD_FEEDBACK:
312: $parent_task = new static($task->parent_task);
313:
314: if(empty($parent_task->storyboard_array)){
315: return false;
316: }
317: $user_ratings = ClipitRating::get_by_owner(array($entity_id));
318: $rating_targets = array();
319: if(!empty($user_ratings[$entity_id])) {
320: foreach ($user_ratings[$entity_id] as $user_rating) {
321: $rating_targets[] = (int)$user_rating->target;
322: }
323: }
324:
325: if(count($parent_task->storyboard_array) == 1){
326: $storyboard_id = array_pop($parent_task->storyboard_array);
327: $storyboard_group = (int)ClipitStoryboard::get_group($storyboard_id);
328: $user_group = (int)ClipitGroup::get_from_user_activity($entity_id, $task->activity);
329: if($storyboard_group === $user_group){
330: return false;
331: }
332: if(array_search((int)$storyboard_id, $rating_targets) === false){
333: return false;
334: }
335: return true;
336: }
337: foreach($parent_task->storyboard_array as $storyboard_id) {
338: if(array_search((int)$storyboard_id, $rating_targets) === false) {
339: $storyboard_group = (int)ClipitStoryboard::get_group((int)$storyboard_id);
340: $user_group = (int)ClipitGroup::get_from_user_activity($entity_id, $task->activity);
341: if($storyboard_group !== $user_group) {
342:
343: return false;
344: }
345: }
346: }
347: return true;
348: case static::TYPE_VIDEO_UPLOAD:
349: foreach($task->video_array as $video_id) {
350: if((int)ClipitVideo::get_group($video_id) === (int)$entity_id) {
351: return true;
352: }
353: }
354: return false;
355: case static::TYPE_VIDEO_FEEDBACK:
356: $parent_task = new static($task->parent_task);
357:
358: if(empty($parent_task->video_array)){
359: return false;
360: }
361: $user_ratings = ClipitRating::get_by_owner(array($entity_id));
362: $rating_targets = array();
363: if(!empty($user_ratings[$entity_id])) {
364: foreach ($user_ratings[$entity_id] as $user_rating) {
365: $rating_targets[] = (int)$user_rating->target;
366: }
367: }
368:
369: if(count($parent_task->video_array) == 1){
370: $video_id = array_pop($parent_task->video_array);
371: $video_group = (int)ClipitVideo::get_group($video_id);
372: $user_group = (int)ClipitGroup::get_from_user_activity($entity_id, $task->activity);
373: if($video_group === $user_group){
374: return false;
375: }
376: if(array_search((int)$video_id, $rating_targets) === false){
377: return false;
378: }
379: return true;
380: }
381: foreach($parent_task->video_array as $video_id) {
382: if(array_search((int)$video_id, $rating_targets) === false) {
383: $video_group = (int)ClipitVideo::get_group((int)$video_id);
384: $user_group = (int)ClipitGroup::get_from_user_activity($entity_id, $task->activity);
385: if($video_group !== $user_group) {
386:
387: return false;
388: }
389:
390: }
391: }
392: return true;
393: case static::TYPE_OTHER:
394: if(static::get_status($id) !== static::STATUS_FINISHED){
395: return false;
396: } else{
397: return true;
398: }
399: default:
400: return false;
401: }
402: }
403: }