move code up one directory
[atutor.git] / mods / _standard / social / lib / classes / Applications.class.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
15 require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
16 require_once(AT_SOCIAL_INCLUDE.'classes/Application.class.php');
17 require_once(AT_SOCIAL_INCLUDE.'constants.inc.php');
18
19 /**
20  * Object for Applications, (aka Gadgets)
21  */
22
23 class Applications {
24         //constructor
25         function Applications(){}
26
27         /** 
28          * Retrieve a list of applications' titles
29          * @param       boolean         true if we only want to list the applications that we allow via the settings, false otherwise.
30          * @return hash of applications, id=>app obj
31          */
32         function listMyApplications($use_settings=false){
33                 global $db;
34                 $hash = array();
35
36                 $sql = 'SELECT id, title FROM '.TABLE_PREFIX.'social_applications a, (SELECT application_id FROM '.TABLE_PREFIX.'social_members_applications WHERE member_id='.$_SESSION['member_id'].') AS apps WHERE a.id=apps.application_id';
37                 $result = mysql_query($sql, $db);
38                 $home_settings = $this->getHomeDisplaySettings();
39                 if ($result){
40                         while($row = mysql_fetch_assoc($result)){
41                                 $app = new Application($row['id']);
42                                 if($use_settings){
43                                         if(!isset($home_settings[$row['id']])){
44                                                 continue;
45                                         }
46                                 }
47                                 $hash[$row['id']] = $app;
48                         }
49                 }
50                 return $hash;
51         }
52
53         /**
54          * Retrieve a list of all installed applications
55          */
56         function listApplications(){
57                 global $db;
58                 $hash = array();
59
60                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_applications';
61                 $result = mysql_query($sql, $db);
62
63                 while ($row = mysql_fetch_assoc($result)){
64                         $hash[$row['id']] = new Application($row['id']);
65                 }
66                 return $hash;
67         }
68
69         
70         /**
71          * Delete applications
72          * @param       array   array of application_id to be deleted.
73          */
74         function deleteApplications($ids){
75                 global $db;
76
77                 //foreach of these ids, delete all their associations
78                 foreach ($ids as $id){
79                         $app = new Application($id);
80                         $app->deleteApplication();
81                 }
82
83                 //now delete it from the application table
84                 $id_list = implode(', ', $ids);
85                 $sql = 'DELETE FROM '.TABLE_PREFIX."social_applications WHERE id IN ($id_list)";
86                 mysql_query($sql, $db);
87         }
88
89         /**
90          * To determine which application to show on the home tab
91          * Save the settings in serialized format.
92          * @param       mixed           settings array. [note: upgrade this to  an object if needed later on]
93          */
94         function setHomeDisplaySettings($settings){
95                 global $db, $addslashes;
96                 $settings = $addslashes(serialize($settings));
97                 $sql = 'REPLACE INTO '.TABLE_PREFIX."social_user_settings SET app_settings='".$settings."', member_id=".$_SESSION['member_id'];
98                 $result = mysql_query($sql, $db);
99         }
100
101
102         /**
103          * Return the <a> link of an application by the given id.
104          * @param       string  the title/name of this application
105          * @param       int             application id
106          * @return      THe <a> tag link of the requested application.
107          */
108         function getAppLink($title, $id){
109                 return '<a href="'.url_rewrite(AT_SOCIAL_BASENAME.'applications.php?app_id='.$id) . '"><b>' . $title . '</b></a>';
110         }
111
112         
113         /** 
114          * Get the home display setting 
115          * @return      array of settings that define which gadget to be displayed on the social home page.
116          */
117         function getHomeDisplaySettings(){
118                 global $db;
119                 $sql = 'SELECT app_settings FROM '.TABLE_PREFIX.'social_user_settings WHERE member_id='.$_SESSION['member_id'];
120                 $rs = mysql_query($sql, $db);
121                 if ($rs){
122                         list($settings) = mysql_fetch_array($rs);
123                 }
124                 return unserialize($settings);
125         }
126
127 }
128 ?>