b0014b5948bccf4a3ed4d8d4a498d161a5d2bd61
[atutor.git] / docs / mods / _standard / social / applications.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 $_user_location = 'public';
15
16 define('AT_INCLUDE_PATH', '../../../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18 include(AT_SOCIAL_INCLUDE.'friends.inc.php');
19 include(AT_SOCIAL_INCLUDE.'classes/Application.class.php');
20 $_custom_css = $_base_path . AT_SOCIAL_BASENAME . 'module.css'; // use a custom stylesheet
21
22 if (!$_SESSION['valid_user']) {
23         require(AT_INCLUDE_PATH.'header.inc.php');
24         $info = array('INVALID_USER', $_SESSION['course_id']);
25         $msg->printInfos($info);
26         require(AT_INCLUDE_PATH.'footer.inc.php');
27         exit;
28 }
29
30 //initialization
31 $app = new Application();
32
33 // Install gadgets
34 if (isset($_POST['add_application']) && isset($_POST['app_url'])){
35         //check if curl is installed
36         if (!extension_loaded('curl')){
37                 $msg->addError('CURL_NOT_INSTALLED');
38                 header('Location: '. url_rewrite(AT_SOCIAL_BASENAME.'applications.php', AT_PRETTY_URL_IS_HEADER));
39                 exit; 
40         }
41         $app_url = urldecode(trim($_POST['app_url']));
42         //grep the XML file out from any given URL
43         preg_match('/url\=((http[s]?\:\/\/)?(.*)\.xml)/', $app_url, $matches);
44         if ($matches[1]!=''){
45                 $app_url = $matches[1];
46         }       
47         //validate app_url to make sure it always has http:// on it 
48         if (preg_match('/^http[s]?\:\/\//', $app_url)==0){
49                 $app_url = 'http://'.$app_url;
50         }
51
52         $gadget = $app->parseModulePrefs($app_url);
53         $gadget = $gadget[0];
54
55         if (empty($gadget->errors)){
56                 //add applicatoin to database
57                 $app->addApplication($gadget);
58                 $msg->addFeedback('GADGET_ADDED_SUCCESSFULLY');
59                 header('Location: '. url_rewrite(AT_SOCIAL_BASENAME.'applications.php', AT_PRETTY_URL_IS_HEADER));
60                 exit;
61         } else {
62                 $msg->addError(array('GADGET_ADDED_FAILURE', implode(', ', $gadget->errors)));
63                 header('Location: '. url_rewrite(AT_SOCIAL_BASENAME.'applications.php', AT_PRETTY_URL_IS_HEADER));
64                 exit; 
65         }
66 }
67
68 // Show all gadgets
69 if (isset($_POST['show_applications'])){
70         $list_of_all_apps = $app->listApplications();
71 }
72
73 //Display individual application
74 if (isset($_REQUEST['app_id'])){
75         $_REQUEST['app_id'] = intval($_REQUEST['app_id']);
76         $app = new Application($_REQUEST['app_id']);    //testing application 1, 2
77         
78         //Add application
79         if (isset($_GET['add']) && intval($_GET['add'])==1){
80                 $app->addMemberApplication($_SESSION['member_id'], $_GET['app_id']);
81                 $msg->addFeedback('GADGET_ADDED_SUCCESSFULLY');
82                 header('Location: '. url_rewrite(AT_SOCIAL_BASENAME.'applications.php', AT_PRETTY_URL_IS_HEADER));
83                 exit;
84         }
85
86         //Delete application
87         if (isset($_GET['delete']) && intval($_GET['delete']) > 0) {
88                 $app->deleteApplication();
89                 $msg->addFeedback('GADGET_REMOVED_SUCCESSFULLY');
90                 header('Location: '. $_SERVER['HTTP_REFERER']);
91                 exit;
92         }
93
94         //Display application settings
95         if (isset($_GET['settings'])){
96                 include(AT_INCLUDE_PATH.'header.inc.php');
97                 $savant->assign('settings', $app->getSettings());       //userPrefs
98                 $savant->assign('user_settings', $app->getApplicationSettings($_SESSION['member_id']));
99                 $savant->assign('app_id', $app->getId());       //id
100                 $savant->display('social/application_settings.tmpl.php');
101                 include(AT_INCLUDE_PATH.'footer.inc.php');              
102                 exit;
103         }
104
105         //Save settings
106         if (isset($_POST['app_settings'])){
107                 foreach ($app->getSettings() as $key=>$value){
108                         if(isset($_POST[$key])){
109                                 //save values iff it is in the userPrefs serialized string.
110                                 //don't save values blindly from the $_POST.
111                                 $value = $addslashes($_POST[$key]);
112                                 $app->setApplicationSettings($_SESSION['member_id'], $key, $value);
113                         }
114                 }
115                 $msg->addFeedback('GADGET_SETTINGS_SAVED');
116                 header('Location: '. url_rewrite(AT_SOCIAL_BASENAME.'applications.php', AT_PRETTY_URL_IS_HEADER));
117                 exit;
118         }
119
120         //loop through all app and print out the thumbnail
121         $iframe_url = $app->getIframeUrl($_REQUEST['id'], 'canvas', $_GET['appParams']);
122
123         //display
124         include(AT_INCLUDE_PATH.'header.inc.php');
125         $savant->assign('iframe_url', $iframe_url);
126         $savant->assign('app', $app);
127         $savant->display('social/individual_application.tmpl.php');
128         include(AT_INCLUDE_PATH.'footer.inc.php');
129         exit;
130 }
131
132 //list all my applications
133 $list_of_my_apps = $app->listMyApplications();
134
135 include(AT_INCLUDE_PATH.'header.inc.php');
136 $savant->display('social/pubmenu.tmpl.php');
137 $savant->assign('list_of_my_apps', $list_of_my_apps);
138 $savant->assign('list_of_all_apps', $list_of_all_apps);
139 $savant->display('social/applications.tmpl.php');
140 include(AT_INCLUDE_PATH.'footer.inc.php');
141 ?>