1: <?php
2: /**
3: * ClipIt - JuxtaLearn Web Space
4: * PHP version: >= 5.2
5: * Creation date: 2013-10-10
6: * Last update: $Date$
7: * @author Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
8: * @version $Version$
9: * @link http://www.juxtalearn.eu
10: * @license GNU Affero General Public License v3
11: * @package ClipIt
12: * @subpackage clipit_api
13: */
14:
15: /**
16: * The Site class, which is unique (only one instance) and holds general Site information and Site-layer Resources.
17: */
18: class ClipitSite extends UBSite {
19:
20: const SUBTYPE = "ClipitSite";
21:
22: // ClipIt Site Types
23: const TYPE_SITE = "site";
24: const TYPE_GLOBAL = "global";
25: const TYPE_DEMO = "demo";
26:
27: // The SITE SCOPE and PUBLIC SCOPE below represent materials published publicly on the LOCAL SITE and/or the
28: // PUBLIC GLOBAL SITE respectively. Only Tricky Topics and Videos are capable of being published.
29:
30: // SITE SCOPE (Public for users of the site)
31: const REL_SITE_TRICKY_TOPIC = "ClipitSite-ClipitTrickyTopic";
32: const REL_SITE_VIDEO = "ClipitSite-ClipitVideo";
33: const REL_SITE_FILE = "ClipitSite-ClipitFile";
34: const REL_SITE_STORYBOARD = "ClipitSite-ClipitStoryboard";
35: public $tricky_topic_array = array();
36: public $video_array = array();
37: // public $file_array = array();
38: // public $storyboard_array = array();
39:
40: // PUBLIC SCOPE (Public for everyone on the Global ClipIt Site)
41: const REL_SITE_PUB_TRICKYTOPIC = "ClipitSite-PUB-ClipitTrickyTopic";
42: const REL_SITE_PUB_VIDEO = "ClipitSite-PUB-ClipitVideo";
43: const REL_SITE_PUB_FILE = "ClipitSite-PUB-ClipitFile";
44: const REL_SITE_PUB_STORYBOARD = "ClipitSite-PUB-ClipitStoryboard";
45: public $pub_tricky_topic_array = array();
46: public $pub_video_array = array();
47: // public $pub_file_array = array();
48: // public $pub_storyboard_array = array();
49:
50: protected function copy_from_elgg($elgg_entity) {
51: parent::copy_from_elgg($elgg_entity);
52: $this->tricky_topic_array = (array)static::get_tricky_topics();
53: $this->video_array = (array)static::get_videos();
54: // $this->file_array = (array)static::get_files();
55: // $this->storyboard_array = (array)static::get_storyboards();
56: $this->pub_tricky_topic_array = (array)static::get_pub_tricky_topics();
57: $this->pub_video_array = (array)static::get_pub_videos();
58: // $this->pub_file_array = (array)static::get_pub_files();
59: // $this->pub_storyboard_array = (array)static::get_pub_storyboards();
60: }
61:
62: /**
63: * Saves Site parameters into Elgg
64: * @return int Site ID
65: */
66: protected function save() {
67: $site_id = parent::save();
68: static::set_tricky_topics($this->tricky_topic_array);
69: static::set_videos($this->video_array);
70: // static::set_files($this->file_array);
71: // static::set_storyboards($this->storyboard_array);
72: static::set_pub_tricky_topics($this->pub_tricky_topic_array);
73: static::set_pub_videos($this->pub_video_array);
74: // static::set_pub_files($this->pub_file_array);
75: // static::set_pub_storyboards($this->pub_storyboard_array);
76: return $site_id;
77: }
78:
79: // SITE SCOPE //
80:
81: // SITE TRICKY TOPICS
82: static function add_tricky_topics($tricky_topic_array) {
83: $id = static::get_site_id();
84: return UBCollection::add_items($id, $tricky_topic_array, static::REL_SITE_TRICKY_TOPIC);
85: }
86:
87: static function set_tricky_topics($tricky_topic_array) {
88: $id = static::get_site_id();
89: return UBCollection::set_items($id, $tricky_topic_array, static::REL_SITE_TRICKY_TOPIC);
90: }
91:
92: static function remove_tricky_topics($tricky_topic_array) {
93: $id = static::get_site_id();
94: return UBCollection::remove_items($id, $tricky_topic_array, static::REL_SITE_TRICKY_TOPIC);
95: }
96:
97: static function get_tricky_topics() {
98: $id = static::get_site_id();
99: return UBCollection::get_items($id, static::REL_SITE_TRICKY_TOPIC);
100: }
101:
102: // SITE VIDEOS
103: static function add_videos($video_array) {
104: $id = static::get_site_id();
105: return UBCollection::add_items($id, $video_array, static::REL_SITE_VIDEO);
106: }
107:
108: static function set_videos($video_array) {
109: $id = static::get_site_id();
110: return UBCollection::set_items($id, $video_array, static::REL_SITE_VIDEO);
111: }
112:
113: static function remove_videos($video_array) {
114: $id = static::get_site_id();
115: return UBCollection::remove_items($id, $video_array, static::REL_SITE_VIDEO);
116: }
117:
118: static function get_videos() {
119: $id = static::get_site_id();
120: return UBCollection::get_items($id, static::REL_SITE_VIDEO);
121: }
122:
123: // // SITE FILES
124: // static function add_files($file_array) {
125: // $id = static::get_site_id();
126: // return UBCollection::add_items($id, $file_array, static::REL_SITE_FILE);
127: // }
128: //
129: // static function set_files($file_array) {
130: // $id = static::get_site_id();
131: // return UBCollection::set_items($id, $file_array, static::REL_SITE_FILE);
132: // }
133: //
134: // static function remove_files($file_array) {
135: // $id = static::get_site_id();
136: // return UBCollection::remove_items($id, $file_array, static::REL_SITE_FILE);
137: // }
138: //
139: // static function get_files() {
140: // $id = static::get_site_id();
141: // return UBCollection::get_items($id, static::REL_SITE_FILE);
142: // }
143: //
144: // // SITE STORYBOARDS
145: // static function add_storyboards($storyboard_array) {
146: // $id = static::get_site_id();
147: // return UBCollection::add_items($id, $storyboard_array, static::REL_SITE_STORYBOARD);
148: // }
149: //
150: // static function set_storyboards($storyboard_array) {
151: // $id = static::get_site_id();
152: // return UBCollection::set_items($id, $storyboard_array, static::REL_SITE_STORYBOARD);
153: // }
154: //
155: // static function remove_storyboards($storyboard_array) {
156: // $id = static::get_site_id();
157: // return UBCollection::remove_items($id, $storyboard_array, static::REL_SITE_STORYBOARD);
158: // }
159: //
160: // static function get_storyboards() {
161: // $id = static::get_site_id();
162: // return UBCollection::get_items($id, static::REL_SITE_STORYBOARD);
163: // }
164:
165: // PUBLIC SCOPE //
166:
167: // PUBLIC TRICKY TOPICS
168: static function add_pub_tricky_topics($tricky_topic_array) {
169: $id = static::get_site_id();
170: UBCollection::add_items($id, $tricky_topic_array, static::REL_SITE_PUB_TRICKYTOPIC);
171: return static::update_global_resources();
172: }
173:
174: static function set_pub_tricky_topics($tricky_topic_array) {
175: $id = static::get_site_id();
176: UBCollection::set_items($id, $tricky_topic_array, static::REL_SITE_PUB_TRICKYTOPIC);
177: return static::update_global_resources();
178: }
179:
180: static function remove_pub_tricky_topics($tricky_topic_array) {
181: $id = static::get_site_id();
182: UBCollection::remove_items($id, $tricky_topic_array, static::REL_SITE_PUB_TRICKYTOPIC);
183: return static::update_global_resources();
184: }
185:
186: static function get_pub_tricky_topics() {
187: $id = static::get_site_id();
188: return UBCollection::get_items($id, static::REL_SITE_PUB_TRICKYTOPIC);
189: }
190:
191: // PUBLIC VIDEOS
192: static function add_pub_videos($video_array) {
193: $id = static::get_site_id();
194: UBCollection::add_items($id, $video_array, static::REL_SITE_PUB_VIDEO);
195: return static::update_global_resources();
196: }
197:
198: static function set_pub_videos($video_array) {
199: $id = static::get_site_id();
200: UBCollection::set_items($id, $video_array, static::REL_SITE_PUB_VIDEO);
201: return static::update_global_resources();
202: }
203:
204: static function remove_pub_videos($video_array) {
205: $id = static::get_site_id();
206: UBCollection::remove_items($id, $video_array, static::REL_SITE_PUB_VIDEO);
207: return static::update_global_resources();
208: }
209:
210: static function get_pub_videos() {
211: $id = static::get_site_id();
212: return UBCollection::get_items($id, static::REL_SITE_PUB_VIDEO);
213: }
214:
215: // // PUBLIC FILES
216: // static function add_pub_files($file_array) {
217: // $id = static::get_site_id();
218: // UBCollection::add_items($id, $file_array, static::REL_SITE_PUB_FILE);
219: // return static::update_global_resources();
220: // }
221: //
222: // static function set_pub_files($file_array) {
223: // $id = static::get_site_id();
224: // UBCollection::set_items($id, $file_array, static::REL_SITE_PUB_FILE);
225: // return static::update_global_resources();
226: // }
227: //
228: // static function remove_pub_files($file_array) {
229: // $id = static::get_site_id();
230: // UBCollection::remove_items($id, $file_array, static::REL_SITE_PUB_FILE);
231: // return static::update_global_resources();
232: // }
233: //
234: // static function get_pub_files() {
235: // $id = static::get_site_id();
236: // return UBCollection::get_items($id, static::REL_SITE_PUB_FILE);
237: // }
238: //
239: // // PUBLIC STORYBOARDS
240: // static function add_pub_storyboards($storyboard_array) {
241: // $id = static::get_site_id();
242: // UBCollection::add_items($id, $storyboard_array, static::REL_SITE_PUB_STORYBOARD);
243: // return static::update_global_resources();
244: // }
245: //
246: // static function set_pub_storyboards($storyboard_array) {
247: // $id = static::get_site_id();
248: // UBCollection::set_items($id, $storyboard_array, static::REL_SITE_PUB_STORYBOARD);
249: // return static::update_global_resources();
250: // }
251: //
252: // static function remove_pub_storyboards($storyboard_array) {
253: // $id = static::get_site_id();
254: // UBCollection::remove_items($id, $storyboard_array, static::REL_SITE_PUB_STORYBOARD);
255: // return static::update_global_resources();
256: // }
257: //
258: // static function get_pub_storyboards() {
259: // $id = static::get_site_id();
260: // return UBCollection::get_items($id, static::REL_SITE_PUB_STORYBOARD);
261: // }
262:
263: // ClipIt Global (to be called from SITE)
264:
265: static function global_site_call($data, $type = "GET"){
266: $clipit_global_url = get_config("clipit_global_url");
267: $clipit_global_user = get_config("clipit_global_login");
268: $clipit_global_password = get_config("clipit_global_password");
269: // Get authentication token
270: $params = array(
271: "method" => "clipit.site.get_token",
272: "login" => $clipit_global_user,
273: "password" => $clipit_global_password,
274: );
275: $response = file_get_contents($clipit_global_url.'?'.http_build_query($params));
276: $decoded_response = json_decode($response);
277: if($decoded_response->status != 0){
278: return null;
279: }
280: $auth_token = $decoded_response->result;
281: // Make call
282: $data += array("auth_token" => $auth_token);
283: $params = array(
284: 'http'=> array(
285: "method" => $type));
286: $context = stream_context_create($params);
287: $json_response = file_get_contents($clipit_global_url.'?'.http_build_query($data), false, $context);
288: $response = json_decode($json_response);
289: if($response->status != 0){
290: return null;
291: } else{
292: return $response->result;
293: }
294: }
295:
296: static function publish_to_global(){
297: $site = new static();
298: $data = array("method" => "clipit.remote_site.create");
299: $data += array("prop_value_array[name]" => base64_encode($site->name));
300: $data += array("prop_value_array[description]" => base64_encode($site->description));
301: $data += array("prop_value_array[url]" => base64_encode($site->url));
302: $data += array("prop_value_array[timezone]" => (string)get_config("timezone"));
303: if(static::global_site_call($data, "POST") == null){
304: return null;
305: }
306: return static::update_global_resources();
307: }
308:
309: static function update_global_resources(){
310: // GET current remote resources for this site on global site
311: $data = array("method" => "");
312: $data += array("remote_site" => base64_encode(elgg_get_site_url()));
313: $data += array("remote_ids_only" => true);
314: // REMOTE TRICKY TOPICS
315: $data["method"] = "clipit.remote_tricky_topic.get_from_site";
316: $remote_tricky_topics = static::global_site_call($data, "GET");
317: // // REMOTE ACTIVITIES
318: // $data["method"] = "clipit.remote_activity.get_from_site";
319: // $remote_activities = static::global_site_call($data, "GET");
320: // REMOTE VIDEOS
321: $data["method"] = "clipit.remote_video.get_from_site";
322: $remote_videos = static::global_site_call($data, "GET");
323: // // REMOTE FILES
324: // $data["method"] = "clipit.remote_file.get_from_site";
325: // $remote_files = static::global_site_call($data, "GET");
326: // LOCAL public resources
327: $pub_tricky_topics = static::get_pub_tricky_topics();
328: // $pub_activities = static::get_pub_activities();
329: $pub_videos = static::get_pub_videos();
330: // $pub_files = static::get_pub_files();
331:
332: // ADD new content to Global
333: // NEW TRICKY TOPICS
334: foreach($pub_tricky_topics as $pub_tricky_topic_id){
335: if(array_search($pub_tricky_topic_id, $remote_tricky_topics) === false){
336: $tricky_topic = array_pop(ClipitTrickyTopic::get_by_id(array($pub_tricky_topic_id)));
337: $tag_name_array = array();
338: $tag_array = ClipitTag::get_by_id($tricky_topic->tag_array);
339: foreach($tag_array as $tag){
340: $tag_name_array[] = $tag->name;
341: }
342: $data = array("method" => "clipit.remote_tricky_topic.create");
343: $data += array("prop_value_array[remote_site]" => base64_encode(elgg_get_site_url()));
344: $data += array("prop_value_array[remote_id]" => $tricky_topic->id);
345: $data += array("prop_value_array[name]" => base64_encode($tricky_topic->name));
346: $data += array("prop_value_array[description]" => base64_encode($tricky_topic->description));
347: $data += array("prop_value_array[tag_array]" => base64_encode(json_encode($tag_name_array)));
348: static::global_site_call($data, "POST");
349: }
350: }
351: // // NEW ACTIVITIES
352: // foreach($pub_activities as $pub_activity_id){
353: // if(array_search($pub_activity_id, $remote_activities) === false){
354: // $activity = array_pop(ClipitActivity::get_by_id(array($pub_activity_id)));
355: // $tricky_topic = array_pop(ClipitTrickyTopic::get_by_id(array($activity->tricky_topic)));
356: // $data = array("method" => "clipit.remote_activity.create");
357: // $data += array("prop_value_array[remote_site]" => base64_encode(elgg_get_site_url()));
358: // $data += array("prop_value_array[remote_id]" => $activity->id);
359: // $data += array("prop_value_array[name]" => base64_encode($activity->name));
360: // $data += array("prop_value_array[description]" => base64_encode($activity->description));
361: // $data += array("prop_value_array[tricky_topic]" => base64_encode($tricky_topic->name));
362: // static::global_site_call($data, "POST");
363: // }
364: // }
365: // NEW VIDEOS
366: foreach($pub_videos as $pub_video_id){
367: if(array_search($pub_video_id, $remote_videos) === false){
368: $video = array_pop(ClipitVideo::get_by_id(array($pub_video_id)));
369: $tag_name_array = array();
370: $tag_array = ClipitTag::get_by_id($video->tag_array);
371: foreach($tag_array as $tag){
372: $tag_name_array[] = $tag->name;
373: }
374: $data = array("method" => "clipit.remote_video.create");
375: $data += array("prop_value_array[remote_site]" => base64_encode(elgg_get_site_url()));
376: $data += array("prop_value_array[remote_id]" => $video->id);
377: $data += array("prop_value_array[name]" => base64_encode($video->name));
378: $data += array("prop_value_array[description]" => base64_encode($video->description));
379: $data += array("prop_value_array[url]" => base64_encode($video->url));
380: $data += array("prop_value_array[tag_array]" => base64_encode(json_encode($tag_name_array)));
381: static::global_site_call($data, "POST");
382: }
383: }
384: // // NEW FILES
385: // foreach($pub_files as $pub_file_id){
386: // if(array_search($pub_file_id, $remote_files) === false){
387: // $file = array_pop(ClipitFile::get_by_id(array($pub_file_id)));
388: // $tag_name_array = array();
389: // $tag_array = ClipitTag::get_by_id($file->tag_array);
390: // foreach($tag_array as $tag){
391: // $tag_name_array[] = $tag->name;
392: // }
393: // $data = array("method" => "clipit.remote_file.create");
394: // $data += array("prop_value_array[remote_site]" => base64_encode(elgg_get_site_url()));
395: // $data += array("prop_value_array[remote_id]" => $file->id);
396: // $data += array("prop_value_array[name]" => base64_encode($file->name));
397: // $data += array("prop_value_array[description]" => base64_encode($file->description));
398: // $data += array("prop_value_array[url]" => base64_encode($file->url));
399: // $data += array("prop_value_array[tag_array]" => base64_encode(json_encode($tag_name_array)));
400: // $data += array("prop_value_array[gdrive_id]" => base64_encode($file->gdrive_id));
401: // static::global_site_call($data, "POST");
402: // }
403: // }
404: // REMOVE content no longer public on local site
405: // OLD TRICKY TOPICS
406: $remove_array = array();
407: foreach($remote_tricky_topics as $remote_tricky_topic_id){
408: if(array_search($remote_tricky_topic_id, $pub_tricky_topics) === false){
409: $remove_array[] = $remote_tricky_topic_id;
410: }
411: }
412: if(!empty($remove_array)) {
413: $data = array("method" => "clipit.remote_tricky_topic.delete_by_remote_id");
414: $data += array("remote_site" => base64_encode(elgg_get_site_url()));
415: foreach ($remove_array as $remove_id) {
416: $data += array("remote_id_array[]" => $remove_id);
417: }
418: static::global_site_call($data, "POST");
419: }
420: // //OLD ACTIVITIES
421: // $remove_array = array();
422: // foreach($remote_activities as $remote_activity_id){
423: // if(array_search($remote_activity_id, $pub_activities) === false){
424: // $remove_array[] = $remote_activity_id;
425: // }
426: // }
427: // if(!empty($remove_array)) {
428: // $data = array("method" => "clipit.remote_activity.delete_by_remote_id");
429: // $data += array("remote_site" => base64_encode(elgg_get_site_url()));
430: // foreach ($remove_array as $remove_id) {
431: // $data += array("remote_id_array[]" => $remove_id);
432: // }
433: // static::global_site_call($data, "POST");
434: // }
435: // OLD VIDEOS
436: $remove_array = array();
437: foreach($remote_videos as $remote_video_id){
438: if(array_search($remote_video_id, $pub_videos) === false){
439: $remove_array[] = $remote_video_id;
440: }
441: }
442: if(!empty($remove_array)) {
443: $data = array("method" => "clipit.remote_video.delete_by_remote_id");
444: $data += array("remote_site" => base64_encode(elgg_get_site_url()));
445: foreach ($remove_array as $remove_id) {
446: $data += array("remote_id_array[]" => $remove_id);
447: }
448: static::global_site_call($data, "POST");
449: }
450: // // OLD FILES
451: // $remove_array = array();
452: // foreach($remote_files as $remote_file_id){
453: // if(array_search($remote_file_id, $pub_files) === false){
454: // $remove_array[] = $remote_file_id;
455: // }
456: // }
457: // if(!empty($remove_array)) {
458: // $data = array("method" => "clipit.remote_video.delete_by_remote_id");
459: // $data += array("remote_site" => base64_encode(elgg_get_site_url()));
460: // foreach ($remove_array as $remove_id) {
461: // $data += array("remote_id_array[]" => $remove_id);
462: // }
463: // static::global_site_call($data, "POST");
464: // }
465: return true;
466: }
467:
468: // static function update_global_resources(){
469: // // Get current remote resources for this site and current local public resources
470: // $data = array("method" => "clipit.remote_resource.get_from_site");
471: // $data += array("remote_site" => elgg_get_site_url());
472: // $data += array("remote_ids_only" => true);
473: // $remote_resources = static::global_site_call($data, "GET");
474: // $pub_tricky_topics = static::get_pub_tricky_topics();
475: // $pub_videos = static::get_pub_videos();
476: //// $pub_storyboards = static::get_pub_storyboards();
477: //// $pub_files = static::get_pub_files();
478: // // Figure out what to add
479: // $add_array = array();
480: // foreach($pub_tricky_topics as $pub_tricky_topic_id){
481: // if(array_search($pub_tricky_topic_id, $remote_resources) === false){
482: // $add_array = array_merge($add_array, ClipitTrickyTopic::get_by_id(array($pub_tricky_topic_id)));
483: // }
484: // }
485: // foreach($pub_videos as $pub_video_id){
486: // if(array_search($pub_video_id, $remote_resources) === false){
487: // $add_array = array_merge($add_array, ClipitVideo::get_by_id(array($pub_video_id)));
488: // }
489: // }
490: //// foreach($pub_storyboards as $pub_storyboard_id){
491: //// if(array_search($pub_storyboard_id, $remote_resources) === false){
492: //// $add_array = array_merge($add_array, ClipitStoryboard::get_by_id(array($pub_storyboard_id)));
493: //// }
494: //// }
495: //// foreach($pub_files as $pub_file_id){
496: //// if(array_search($pub_file_id, $remote_resources) === false){
497: //// $add_array = array_merge($add_array, ClipitFile::get_by_id(array($pub_file_id)));
498: //// }
499: //// }
500: // foreach($add_array as $object) {
501: // $tag_name_array = array();
502: // $tag_array = ClipitTag::get_by_id($object->tag_array);
503: // foreach($tag_array as $tag){
504: // $tag_name_array[] = $tag->name;
505: // }
506: // $data = array("method" => "clipit.remote_resource.create");
507: // $data += array("prop_value_array[remote_site]" => base64_encode(elgg_get_site_url()));
508: // $data += array("prop_value_array[remote_id]" => $object->id);
509: // $data += array("prop_value_array[remote_type]" => $object::SUBTYPE);
510: // $data += array("prop_value_array[name]" => base64_encode($object->name));
511: // $data += array("prop_value_array[description]" => base64_encode($object->description));
512: // $data += array("prop_value_array[url]" => base64_encode($object->url));
513: // $data += array("prop_value_array[tag_array]" => base64_encode(json_encode($tag_name_array)));
514: // static::global_site_call($data, "POST");
515: // }
516: // // Figure out what to remove
517: // $remove_array = array();
518: // $all_pub_resources = array_merge($pub_tricky_topics, $pub_videos); //$pub_storyboards, $pub_files);
519: // foreach($remote_resources as $remote_resource_id){
520: // if(array_search($remote_resource_id, $all_pub_resources) === false){
521: // $remove_array[] = $remote_resource_id;
522: // }
523: // }
524: // $data = array("method" => "clipit.remote_resource.delete_by_remote_id");
525: // $data += array("remote_site" => elgg_get_site_url());
526: // foreach($remove_array as $remove_id){
527: // $data += array("remote_id_array[$remove_id]" => $remove_id);
528: // }
529: // static::global_site_call($data, "POST");
530: // return true;
531: // }
532: }