613a19f06b0ebad04aa672f4c55aaae3030f5f18
[atutor.git] / mods / social / lib / classes / Applications.class.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
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                 $id_list = implode(', ', $ids);
77                 $sql = 'DELETE FROM '.TABLE_PREFIX."social_applications WHERE id IN ($id_list)";
78                 mysql_query($sql, $db);
79         }
80
81         /**
82          * To determine which application to show on the home tab
83          * Save the settings in serialized format.
84          * @param       mixed           settings array. [note: upgrade this to  an object if needed later on]
85          */
86         function setHomeDisplaySettings($settings){
87                 global $db, $addslashes;
88                 $settings = $addslashes(serialize($settings));
89                 $sql = 'REPLACE INTO '.TABLE_PREFIX."social_user_settings SET app_settings='".$settings."', member_id=".$_SESSION['member_id'];
90                 $result = mysql_query($sql, $db);
91         }
92
93
94         /**
95          * Return the <a> link of an application by the given id.
96          * @param       string  the title/name of this application
97          * @param       int             application id
98          * @return      THe <a> tag link of the requested application.
99          */
100         function getAppLink($title, $id){
101                 return '<a href="'.url_rewrite(AT_SOCIAL_BASENAME.'applications.php?app_id='.$id) . '"><b>' . $title . '</b></a>';
102         }
103
104         
105         /** 
106          * Get the home display setting 
107          * @return      array of settings that define which gadget to be displayed on the social home page.
108          */
109         function getHomeDisplaySettings(){
110                 global $db;
111                 $sql = 'SELECT app_settings FROM '.TABLE_PREFIX.'social_user_settings WHERE member_id='.$_SESSION['member_id'];
112                 $rs = mysql_query($sql, $db);
113                 if ($rs){
114                         list($settings) = mysql_fetch_array($rs);
115                 }
116                 return unserialize($settings);
117         }
118
119 }
120 ?>