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
9 * This class was written with pure intentions.
\r
11 * @author gorzan <gorzan@gmail.com>
\r
15 class subscription {
\r
19 public $entity_type;
\r
20 private $ent_param = array();
\r
22 // Constructor. Does nothing at the moment.
\r
23 public function subscription() {
\r
28 // Checks if user is subscribed to feed.
\r
29 public function is_subscribed($entity_type, $member_id, $entity_id) {
\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
35 // Run SQL and check if table is populated for given member id and entity id
\r
37 $result = mysql_fetch_array(mysql_query($sql));
\r
38 return (empty($result[0]))?false:true;
\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
52 $gid = (is_array($oid))?$oid[0]:$oid;
\r
54 $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id='$gid'";
\r
55 $result = mysql_fetch_row(mysql_query($sql));
\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
75 // Subscribes user to feed
\r
76 public function set_subscription($entity_type, $member_id, $entity_id){
\r
78 //Checks subscribability (only for blogs)
\r
79 if ($entity_type == 'blog' && !$this->check_blog_subscribability($entity_id,$member_id)){
\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
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
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
100 //Also, we need to know what ballpark we're in
\r
101 $ent_param = $this->entity_switch($entity_type);
\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
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
113 $sysinfo = $this->get_system_email();
\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
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
131 if(!$mail->Send()) {
\r
132 $msg->addError('SENDING_ERROR');
\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
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
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
175 default: //If unknown entity type, return false
\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