remove old readme
[atutor.git] / docs / include / classes / subscribe.class.php
1 <?php\r
2 \r
3 /**\r
4 *\r
5 * Subscription\r
6 *\r
7 * Used to check and set/unset email subscription status for a user for various feeds. Also used to send mails when feeds are fed.\r
8 *\r
9 * This class was written with pure intentions.\r
10 *\r
11 * @author   gorzan <gorzan@gmail.com>\r
12 * @access   public\r
13 */\r
14 \r
15 class subscription {\r
16         \r
17         public $member_id;\r
18         public $entity_id;\r
19         public $entity_type;\r
20         private $ent_param = array(); \r
21 \r
22         // Constructor. Does nothing at the moment.\r
23         public function subscription() {\r
24                 return true;\r
25         }\r
26         \r
27         \r
28         // Checks if user is subscribed to feed.\r
29         public function is_subscribed($entity_type, $member_id, $entity_id) {\r
30                 \r
31                 // Get appropriate sql parameters and write sql query\r
32                 $ent_param = $this->entity_switch($entity_type);\r
33                 $sql = ($ent_param) ? "SELECT COUNT(*) FROM $ent_param[sub_table] WHERE member_id = '$member_id' AND $ent_param[sub_id] = '$entity_id'" : false;\r
34                                 \r
35                 // Run SQL and check if table is populated for given member id and entity id\r
36                 if ($sql){\r
37                         $result = mysql_fetch_array(mysql_query($sql));\r
38                         return (empty($result[0]))?false:true;\r
39                 }       else {\r
40                         return false;\r
41                 }\r
42         }\r
43         \r
44         // Gets group name for blog posts\r
45         private function get_group_title(){\r
46                 if (isset($_GET['oid'])){\r
47                         $oid = $_GET['oid'];\r
48                 } elseif (isset($_POST['oid'])){\r
49                         $oid = $_POST['oid'];\r
50                 }\r
51                 \r
52                 $gid = (is_array($oid))?$oid[0]:$oid;\r
53                 if (!empty($gid)){\r
54                         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id='$gid'";\r
55                         $result = mysql_fetch_row(mysql_query($sql));\r
56                         return $result[0];\r
57                 } else {\r
58                         return false;\r
59                 }\r
60         }\r
61         \r
62         // Gets email and site name\r
63         private function get_system_email (){\r
64                 $sql = "SELECT * FROM ".TABLE_PREFIX."config WHERE name = 'site_name' OR name = 'contact_email'";\r
65                 $result = mysql_query($sql);\r
66                 while($row = mysql_fetch_row($result)){\r
67                         if ($row[0] == 'site_name'){\r
68                                 $sysinfo['site_name'] = $row[1];\r
69                         } elseif ($row[0] == 'contact_email'){\r
70                                 $sysinfo['contact_email'] = $row[1];\r
71                         }\r
72                 }\r
73                 return $sysinfo;\r
74         }\r
75         // Subscribes user to feed\r
76         public function set_subscription($entity_type, $member_id, $entity_id){\r
77                 \r
78                 //Checks subscribability (only for blogs)\r
79                 if ($entity_type == 'blog' && !$this->check_blog_subscribability($entity_id,$member_id)){\r
80                         return false;\r
81                 }\r
82                 \r
83                 $ent_param = $this->entity_switch($entity_type);\r
84                 $sql = ($ent_param) ? "INSERT INTO $ent_param[sub_table] (member_id, $ent_param[sub_id]) VALUES('$member_id','$entity_id')" : false;\r
85                 return (mysql_query($sql))?true:false;\r
86         }\r
87         \r
88         // Unsubscribes user to feed\r
89         public function unset_subscription($entity_type, $member_id, $entity_id){\r
90                 $ent_param = $this->entity_switch($entity_type);\r
91                 $sql = ($ent_param) ? "DELETE FROM $ent_param[sub_table] WHERE member_id = '$member_id' AND $ent_param[sub_id] = '$entity_id'" : false;\r
92                 return (mysql_query($sql))?true:false;\r
93         }\r
94         \r
95         // Sends mail to all subscribed users\r
96         public function send_mail($entity_type,$entity_id,$post_id){\r
97                 // We need the automailer\r
98                 require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');\r
99                 \r
100                 //Also, we need to know what ballpark we're in\r
101                 $ent_param = $this->entity_switch($entity_type);\r
102                 \r
103                 // Now, what are we going to send?\r
104                 $fetch = (!empty($ent_param[content_head]))?$ent_param[content_head].",".$ent_param[content_body]:$ent_param[content_body];\r
105                 $sql = "SELECT $fetch FROM $ent_param[content_table] WHERE $ent_param[content_id] = '$post_id'";\r
106                 $post = mysql_fetch_array(mysql_query($sql));\r
107                 \r
108                 //Get all subscribers\r
109                 $sql = "SELECT t1.email, t1.member_id FROM ".TABLE_PREFIX."members t1, $ent_param[sub_table] t2 WHERE t2.$ent_param[sub_id] = '$entity_id' AND t1.member_id=t2.member_id";\r
110                 $result = mysql_query($sql);\r
111                 \r
112                 //get system email\r
113                 $sysinfo = $this->get_system_email();\r
114                 \r
115                 //Send lots of mails\r
116                 while ($subscriber = mysql_fetch_array($result)){\r
117                         $mail = new ATutorMailer;\r
118                         $mail->AddAddress($subscriber['email'], get_display_name($subscriber['member_id']));\r
119                         $body = $ent_param[mail_header];\r
120                         $body .= "<hr />";\r
121                         $body .= _AT('posted_by').": ".get_display_name($_SESSION['member_id'])."<br />";\r
122                         $body .= (!empty($ent_param[content_head]))?"<h2>".$post[$ent_param[content_head]]."</h2><br />":'';\r
123                         $body .= format_content($post[$ent_param[content_body]],$_POST['formatting'],$glossary)."<br />";\r
124                         $mail->CharSet = 'utf-8';\r
125                         $mail->ContentType = 'text/html';\r
126                         $mail->FromName = $sysinfo['site_name'];\r
127                         $mail->From     = $sysinfo['contact_email'];\r
128                         $mail->Subject = $ent_param[mail_subject];\r
129                         $mail->Body    = $body;\r
130 \r
131                         if(!$mail->Send()) {\r
132                                 $msg->addError('SENDING_ERROR');\r
133                         }\r
134 \r
135                         unset($mail);\r
136                 }\r
137                 \r
138         }\r
139         \r
140         // Internal function used to set appropriate SQL parameters for a given entity type\r
141         private function entity_switch($entity_type){\r
142                 switch($entity_type){\r
143                         case "blog":\r
144                                 $param[sub_table] = TABLE_PREFIX.'blog_subscription';\r
145                                 $param[sub_id] = 'group_id';\r
146                                 $param[content_table] = TABLE_PREFIX.'blog_posts';\r
147                                 $param[content_id] = 'post_id';\r
148                                 $param[content_head] = 'title';\r
149                                 $param[content_body] = 'body';\r
150                                 $param[group_title] = $this->get_group_title();\r
151                                 $param[mail_subject] = _AT('blog_notify_subject');\r
152                                 $param[mail_header] = _AT('blog_notify_body', $param[group_title], AT_BASE_HREF.'bounce.php?course='.$_SESSION['course_id']);\r
153                         break;\r
154                         case "blogcomment":\r
155                                 $param[sub_table] = TABLE_PREFIX.'blog_subscription';\r
156                                 $param[sub_id] = 'group_id';\r
157                                 $param[content_table] = TABLE_PREFIX.'blog_posts_comments';\r
158                                 $param[content_id] = 'comment_id';\r
159                                 //$param[content_head] = 'date';\r
160                                 $param[content_body] = 'comment';\r
161                                 $param[group_title] = $this->get_group_title();\r
162                                 $param[mail_subject] = _AT('blogcomment_notify_subject');\r
163                                 $param[mail_header] = _AT('blogcomment_notify_body', $param[group_title], AT_BASE_HREF.'bounce.php?course='.$_SESSION['course_id']);\r
164                                 \r
165                         break;  \r
166                         case "course":\r
167                         break;\r
168         \r
169                         case "forum":\r
170                         break;\r
171         \r
172                         case "thread":\r
173                         break;\r
174         \r
175                         default: //If unknown entity type, return false\r
176                                 return false;\r
177                 }\r
178                 return $param;\r
179         }\r
180 \r
181         private function check_blog_subscribability($group_id,$member_id){\r
182                 $sql="SELECT COUNT(*) FROM ".TABLE_PREFIX."groups t1 LEFT JOIN ".TABLE_PREFIX."groups_types t2 ON t1.type_id=t2.type_id LEFT JOIN ".TABLE_PREFIX."course_enrollment t3 ON t2.course_id=t3.course_id WHERE group_id='".$group_id."' AND member_id='".$member_id."'";\r
183                 $result = mysql_fetch_row(mysql_query($sql));\r
184                 return (empty($result[0]))?false:true;\r
185         }\r
186 }\r
187 ?>