initial commit of Gorzan's announcement subscription module
authorgreg gay <ggay@ocad.ca>
Mon, 25 Oct 2010 16:57:56 +0000 (16:57 -0000)
committergreg gay <ggay@ocad.ca>
Mon, 25 Oct 2010 16:57:56 +0000 (16:57 -0000)
mods/announcement_subscription/module.php [new file with mode: 0755]
mods/announcement_subscription/module.sql [new file with mode: 0755]
mods/announcement_subscription/module.xml [new file with mode: 0755]
mods/announcement_subscription/module_install.php [new file with mode: 0755]
mods/announcement_subscription/module_uninstall.php [new file with mode: 0644]
mods/announcement_subscription/readme.txt [new file with mode: 0755]
mods/announcement_subscription/sendmail.php [new file with mode: 0755]
mods/announcement_subscription/side_menu.inc.php [new file with mode: 0755]
mods/announcement_subscription/subscribe.php [new file with mode: 0755]
mods/announcement_subscription/subscribe_button.php [new file with mode: 0755]

diff --git a/mods/announcement_subscription/module.php b/mods/announcement_subscription/module.php
new file mode 100755 (executable)
index 0000000..c013223
--- /dev/null
@@ -0,0 +1,7 @@
+<?php\r
+if (!defined('AT_INCLUDE_PATH')) { exit; }\r
+if (!isset($this) || (isset($this) && (strtolower(get_class($this)) != 'module'))) { exit(__FILE__ . ' is not a Module'); }\r
+\r
+\r
+$this->_stacks['announcement_subscription'] = array('title_var'=>'announcement_subscription', 'file'=>'mods/announcement_subscription/side_menu.inc.php');\r
+?>\r
diff --git a/mods/announcement_subscription/module.sql b/mods/announcement_subscription/module.sql
new file mode 100755 (executable)
index 0000000..47f11f9
--- /dev/null
@@ -0,0 +1,116 @@
+#\r
+# sql file for announcement_subscription module\r
+#\r
+\r
+\r
+# Set up table for subscribers\r
+CREATE TABLE `courses_members_subscription` (\r
+  `member_id` MEDIUMINT NOT NULL ,\r
+  `course_id` MEDIUMINT NOT NULL ,\r
+  `subscribe` MEDIUMINT NULL DEFAULT '0',\r
+  PRIMARY KEY (member_id)\r
+) TYPE=MyISAM;\r
+\r
+\r
+\r
+# Insert module specific language:\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_subscription',\r
+                                    'Announcement Subscription',\r
+                                     NOW(),\r
+                                    'Title');\r
+                                    \r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_subscribe_subject',\r
+                                    'Course Announcement Subscription',\r
+                                     NOW(),\r
+                                    'Announcement subscription mail subject');                           \r
+                                    \r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_subscribe_body',\r
+                                    '*DO NOT REPLY TO THIS MESSAGE* \n\r
+You are now subscribed to the newsfeed in the ATutor course "%s". Login at: %s to view the course.',\r
+                                     NOW(),\r
+                                    'New announcement mail body');\r
+                                    \r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_unsubscribe_body',\r
+                                    '*DO NOT REPLY TO THIS MESSAGE* \n\r
+Your subscription to the newsfeed in the ATutor course "%s" has been cancelled. Login at: %s to view the course.',\r
+                                     NOW(),\r
+                                    'New announcement mail body');   \r
+                                    \r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_notify_subject',\r
+                                    'New Course Announcement',\r
+                                     NOW(),\r
+                                    'New announcement mail subject');\r
+                                    \r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_notify_body1',\r
+                                    '*DO NOT REPLY TO THIS MESSAGE* \n\r
+A new announcement has been published in the ATutor course "%s". Login at: %s to view the course.',\r
+                                     NOW(),\r
+                                    'New announcement mail body');\r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_subscription_subscribe',\r
+                                    'Subscribe',\r
+                                     NOW(),\r
+                                    'Subscribe');\r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_module',\r
+                                    'announcement_subscription_unsubscribe',\r
+                                    'Unsubscribe',\r
+                                     NOW(),\r
+                                    'Unsubscribe'); \r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_INFOS_ANNOUNCEMENTSUB_ALREADYINSTALLED_ADDNEWS',\r
+                                    'Module already installed in file /editor/add_news.php ',\r
+                                     NOW(),\r
+                                    'Module installed warning');                                  \r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_FEEDBACK_ANNOUNCEMENTSUB_INSTALL_ADDNEWS',\r
+                                    'Changes made to file /editor/add_news.php ',\r
+                                     NOW(),\r
+                                    'Module installed feedback');\r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_ERROR_ANNOUNCEMENTSUB_INSTALL_ADDNEWS',\r
+                                    'Could not write to file /editor/add_news.php ',\r
+                                     NOW(),\r
+                                    'Module install error');\r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_FEEDBACK_ANNOUNCEMENTSUB_SUBSCRIBE',\r
+                                    'You have successfully subscribed to the announcement newsfeed for this course.',\r
+                                     NOW(),\r
+                                    'Subscribed feedback message');   \r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_ERROR_ANOUNCEMENTSUB_INSTALL_UNWRITE',\r
+                                    'Cannot write to file /editor/add_news.php. Please make sure appropriate permissions are set for writing to this file.',\r
+                                     NOW(),\r
+                                    'file unwritable error');                                          \r
+\r
+INSERT INTO `language_text` VALUES ('en',\r
+                                    '_msgs',\r
+                                    'AT_FEEDBACK_ANNOUNCEMENTSUB_UNSUBSCRIBE',\r
+                                    'You have successfully unsubscribed from the announcement newsfeed for this course.',\r
+                                     NOW(),\r
+                                    'Unsubscribed feedback message');          \r
diff --git a/mods/announcement_subscription/module.xml b/mods/announcement_subscription/module.xml
new file mode 100755 (executable)
index 0000000..154cdf7
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?> \r
+<module version="0.1"> \r
+    <name lang="en">Course Announcement Subscription</name> \r
+    <description lang="en">This module, when enabled, allows users to subscribe to the newsfeed from course announcements, much like they subscribe to forum threads.</description> \r
+    <maintainers>\r
+        <maintainer> \r
+            <name>Gøran Berntsen</name> \r
+            <email>gorzan@gmail.com</email> \r
+        </maintainer>\r
+    </maintainers> \r
+    <url></url> \r
+    <license></license> \r
+       <release> \r
+        <version>1.2</version> \r
+        <date>2010-10-25</date> \r
+        <state>Stable</state> \r
+        <notes>Please note that this module makes changes to some of the ATutor core files on installation. \r
+        If you want a notification email to be sent to the user upon subscription/unsubscription, uncomment the appropriate lines in subscribe.php</notes> \r
+    </release> \r
+</module>\r
diff --git a/mods/announcement_subscription/module_install.php b/mods/announcement_subscription/module_install.php
new file mode 100755 (executable)
index 0000000..d11b88a
--- /dev/null
@@ -0,0 +1,82 @@
+<?php\r
+/*******\r
+ * the line below safe-guards this file from being accessed directly from\r
+ * a web browser. It will only execute if required from within an ATutor script,\r
+ * in our case the Module::install() method.\r
+ */\r
+if (!defined('AT_INCLUDE_PATH')) { exit; }\r
+require(AT_INCLUDE_PATH.'lib/constants.inc.php');\r
+\r
+$_course_privilege = TRUE; // possible values: FALSE | AT_PRIV_ADMIN | TRUE\r
+$_admin_privilege  = TRUE; // possible values: FALSE | TRUE\r
+$_cron_interval    = 0; // disable\r
+\r
+\r
+/******\r
+ * the following code checks if there are any errors (generated previously)\r
+ * then uses the SqlUtility to run any database queries it needs, ie. to create\r
+ * its own tables.\r
+ */\r
\r
+if (!$msg->containsErrors() && file_exists(dirname(__FILE__) . '/module.sql')) {\r
+       // deal with the SQL file:\r
+       require(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');\r
+       $sqlUtility =& new SqlUtility();\r
+\r
+       /*\r
+        * the SQL file could be stored anywhere, and named anything, "module.sql" is simply\r
+        * a convention we're using.\r
+        */\r
+       $sqlUtility->queryFromFile(dirname(__FILE__) . '/module.sql', TABLE_PREFIX);\r
+}\r
+\r
+/******\r
+ * The following core files are changed: \r
+ * \r
+ *  editor/add_news.php:   \r
+ *\r
+ */  \r
+\r
+/******\r
+ * Open editor/add_news.php, check if changes need to be made, make them if neccessary\r
+ */ \r
+\r
+$installed=FALSE;\r
+$needle = "if (!\$msg->containsErrors() && (!isset(\$_POST['setvisual']) || isset(\$_POST['submit']))) {";\r
+$changes = "\n\t/***** \r
+\t* Added by announcement_subscription: Send mail to announcement subscribers \r
+\t*/\n\r
+\t \$subscriberMod =& \$moduleFactory->getModule('announcement_subscription'); \r
+\t if (\$subscriberMod->isEnabled() && !\$subscriberMod->isMissing()) { \r
+\t\t include_once(AT_MODULE_PATH . 'announcement_subscription/sendmail.php'); \r
+\t } \n\r
+\t/***** \r
+\t* End announcement_subscription \r
+\t*/ \n ";\r
+\r
+\r
+$filename=('../../editor/add_news.php');\r
+if(!is_writable($filename)){\r
+$msg->addError('ANOUNCEMENTSUB_INSTALL_UNWRITE');\r
+}else{\r
+  $data = file($filename);\r
+  foreach($data as $line){\r
+    $newfile .= (strpos($line,$needle))? $line . "\n" . $changes . "\n": $line;\r
+    if(strpos($line,'announcement_subscription/sendmail.php')){\r
+      $msg->addInfo('ANNOUNCEMENTSUB_ALREADYINSTALLED_ADDNEWS');\r
+      $installed=TRUE;\r
+    }\r
+  }\r
+  \r
+  if (!$installed) {\r
+    $file = fopen('$filename','w');\r
+    if(fwrite($file,$newfile)){\r
+      $msg->addFeedback('ANNOUNCEMENTSUB_INSTALL_ADDNEWS');\r
+    }else{\r
+      $msg->addError('ANNOUNCEMENTSUB_INSTALL_ADDNEWS');\r
+    }\r
+    fclose($file);\r
+  }\r
+}\r
+\r
+?>\r
diff --git a/mods/announcement_subscription/module_uninstall.php b/mods/announcement_subscription/module_uninstall.php
new file mode 100644 (file)
index 0000000..52d8f81
--- /dev/null
@@ -0,0 +1,31 @@
+<?php\r
+/*******\r
+ * module_uninstall.php performs reversion of module_install.php\r
+ */\r
+\r
+/*******\r
+ * the line below safe-guards this file from being accessed directly from\r
+ * a web browser. It will only execute if required from within an ATutor script,\r
+ * in our case the Module::uninstall() method.\r
+ */\r
+if (!defined('AT_INCLUDE_PATH')) { exit; }\r
+\r
+\r
+/******\r
+ * the following code checks if there are any errors (generated previously)\r
+ * then uses the SqlUtility to run reverted database queries of module.sql, \r
+ * ie. "create table" statement in module.sql is run as drop according table.\r
+ */\r
+if (!$msg->containsErrors() && file_exists(dirname(__FILE__) . '/module.sql')) {\r
+       // deal with the SQL file:\r
+       require(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');\r
+       $sqlUtility = new SqlUtility();\r
+\r
+       /*\r
+        * the SQL file could be stored anywhere, and named anything, "module.sql" is simply\r
+        * a convention we're using.\r
+        */\r
+       $sqlUtility->revertQueryFromFile(dirname(__FILE__) . '/module.sql', TABLE_PREFIX);\r
+}\r
+\r
+?>
\ No newline at end of file
diff --git a/mods/announcement_subscription/readme.txt b/mods/announcement_subscription/readme.txt
new file mode 100755 (executable)
index 0000000..1bd3cf5
--- /dev/null
@@ -0,0 +1,25 @@
+This module installes the following into the file /editor/add_news.php:\r
+\r
+       /***** \r
+       * Added by announcement_subscription: Send mail to announcement subscribers \r
+       */\r
+\r
+        $subscriberMod =& $moduleFactory->getModule('announcement_subscription'); \r
+        if ($subscriberMod->isEnabled() && !$subscriberMod->isMissing()) { \r
+                include_once(AT_MODULE_PATH . 'announcement_subscription/sendmail.php'); \r
+        } \r
+\r
+       /***** \r
+       * End announcement_subscription \r
+       */ \r
+       \r
+These lines are added immediately after the line \r
+\r
+  if (!$msg->containsErrors() && (!isset($_POST['setvisual']) || isset($_POST['submit']))) {\r
+\r
+which should be around line 70 of the file. add_news.php needs to be writable for this module\r
+to install properly - if the file is unwritable you will need to change the file permissions\r
+before installing the mod. There are different ways to do this depending on the web server software,\r
+please see the web server documentation for details. \r
+\r
+To learn more about file permissions, see http://en.wikipedia.org/wiki/Permissions\r
diff --git a/mods/announcement_subscription/sendmail.php b/mods/announcement_subscription/sendmail.php
new file mode 100755 (executable)
index 0000000..b20a3dc
--- /dev/null
@@ -0,0 +1,51 @@
+<?php          \r
+\r
+  // Extract memberids of members who subscribe to newsfeed in this course\r
+  $subscriber_list = '';\r
+       $sql = "SELECT member_id from ".TABLE_PREFIX."courses_members_subscription WHERE subscribe = '1' AND course_id=".$_SESSION['course_id'];\r
+       $result = mysql_query($sql, $db) or die(mysql_error());\r
+       while($row = mysql_fetch_assoc($result)){\r
+               $subscriber_list .= $row['member_id'] . ',';\r
+       }\r
+       $subscriber_list = $substr($subscriber_list, 0, -1); //strip last comma from list\r
+       \r
+       \r
+       // Get name and email adress for members in $subscriber_list\r
+       $subscriber_email_list = array();\r
+  if ($subscriber_list != '') {\r
+       $sql = "SELECT first_name, second_name, last_name, email, member_id FROM ".TABLE_PREFIX."members WHERE member_id IN ($subscriber_list)";\r
+               $result = mysql_query($sql, $db);\r
+               while ($row = mysql_fetch_assoc($result)) {\r
+                       $subscriber_email_list[] = array('email'=> $row['email'], 'full_name' => $row['first_name'] . ' '. $row['second_name'] . ' ' . $row['last_name'], 'member_id'=>$row['member_id']);\r
+               }\r
+       }\r
+\r
+\r
+\r
+  // SEND MAIL\r
+       if ($subscriber_email_list) {\r
+               require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');\r
+               foreach ($subscriber_email_list as $subscriber){\r
+                       $mail = new ATutorMailer;\r
+                       $mail->AddAddress($subscriber['email'], get_display_name($subscriber['member_id']));\r
+                       $body = _AT('announcement_notify_body1', $_SESSION['course_title'], AT_BASE_HREF.'bounce.php?course='.$_SESSION['course_id']);\r
+                       $body .= "\n----------------------------------------------\n";\r
+                       $body .= _AT('posted_by').": ".get_display_name($_SESSION['member_id'])."\n";\r
+                       $body .= $_POST['title']."\n";\r
+      $body .= $_POST['body_text']."\n";\r
+                       $mail->FromName = $_config['site_name'];\r
+                       $mail->From     = $_config['contact_email'];\r
+                       $mail->Subject = _AT('announcement_notify_subject');\r
+                       $mail->Body    = $body;\r
+\r
+                       if(!$mail->Send()) {\r
+                               $msg->addError('SENDING_ERROR');\r
+                       }\r
+\r
+                       unset($mail);\r
+               }\r
+       }\r
+       \r
+\r
+\r
+?>\r
diff --git a/mods/announcement_subscription/side_menu.inc.php b/mods/announcement_subscription/side_menu.inc.php
new file mode 100755 (executable)
index 0000000..c33c36f
--- /dev/null
@@ -0,0 +1,12 @@
+<?php \r
+  global $savant;\r
+  \r
+  require('subscribe_button.php');\r
+  $box_content = '<a href="'.$sub_href.'">'.$sub_button.'</a>';\r
+  \r
+  \r
+  $savant->assign('dropdown_contents', $box_content);\r
+  \r
+  $savant->assign('title', _AT('announcement_subscription'));\r
+  $savant->display('include/box.tmpl.php');\r
+?>\r
diff --git a/mods/announcement_subscription/subscribe.php b/mods/announcement_subscription/subscribe.php
new file mode 100755 (executable)
index 0000000..57e5074
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+define('AT_INCLUDE_PATH', '../../include/');
+require(AT_INCLUDE_PATH.'vitals.inc.php');
+
+// PREPARE MAIL
+  $sql = "SELECT email FROM ".TABLE_PREFIX."members WHERE member_id=".$_SESSION['member_id'];
+  $user_email = mysql_fetch_assoc(mysql_query($sql));
+  require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
+       
+  $mail = new ATutorMailer;
+       $mail->AddAddress($user_email['email'], get_display_name($_SESSION['member_id']));
+       $mail->FromName = $_config['site_name'];
+       $mail->From     = $_config['contact_email'];
+
+// SUBSCRIBE OR UNSUBSCRIBE? TAKE APPROPRIATE ACTION
+  if ($_GET['a'] == "subscribe"){
+    // CHECK FOR EXISTING TABLE ENTRY
+    $check = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM ".TABLE_PREFIX."courses_members_subscription WHERE course_id=".$_SESSION['course_id']." AND member_id=".$_SESSION['member_id']));
+      if(empty($check[0])){
+        $sql = "INSERT INTO ".TABLE_PREFIX."courses_members_subscription (member_id,course_id,subscribe) VALUES(".$_SESSION['member_id'].",".$_SESSION['course_id'].",'1')";      
+      } else {
+        $sql = "UPDATE ".TABLE_PREFIX."courses_members_subscription SET subscribe='1' WHERE course_id=".$_SESSION['course_id']." AND member_id=".$_SESSION['member_id'];      
+      }    
+    $mail->Subject = _AT('announcement_subscribe_subject');    
+    $body = _AT('announcement_subscribe_body', $_SESSION['course_title'], AT_BASE_HREF.'bounce.php?course='.$_SESSION['course_id']);
+    $msg->addFeedback('ANNOUNCEMENTSUB_SUBSCRIBE');
+  }elseif ($_GET['a'] == "unsubscribe"){
+    $sql = "UPDATE ".TABLE_PREFIX."courses_members_subscription SET subscribe='0' WHERE course_id=".$_SESSION['course_id']." AND member_id=".$_SESSION['member_id'];
+    $mail->Subject = _AT('announcement_subscribe_subject');    
+    $body = _AT('announcement_unsubscribe_body', $_SESSION['course_title'], AT_BASE_HREF.'bounce.php?course='.$_SESSION['course_id']);
+    $msg->addFeedback('ANNOUNCEMENTSUB_UNSUBSCRIBE');
+  }else {
+    die("Error: action not defined");
+  }
+
+// FIX SUBSCRIPTION IN DB
+  mysql_query($sql) or die(mysql_error());
+  
+// FINISH UP MAIL AND SHIP IT OFF
+
+  $mail->Body    = $body;
+
+  // UNCOMMENT THE FOLLOWING 3 LINES IF YOU WANT ATUTOR TO SEND AN EMAIL TO THE USER WHEN THE USER SUBSCRIBES/UNSUBSCRIBES TO A NEWSFEED
+       //if(!$mail->Send()) {
+       //      $msg->addError('SENDING_ERROR');
+       //}
+
+       unset($mail);
+  
+// GO HOME
+
+header('Location: ../../index.php');
+
+?>
diff --git a/mods/announcement_subscription/subscribe_button.php b/mods/announcement_subscription/subscribe_button.php
new file mode 100755 (executable)
index 0000000..85fb236
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+$sql = "SELECT subscribe FROM ".TABLE_PREFIX."courses_members_subscription WHERE member_id=".$_SESSION['member_id']." AND course_id=".$_SESSION['course_id']." LIMIT 1";
+$subscribed = mysql_fetch_assoc(mysql_query($sql));
+
+if ($subscribed['subscribe']=="1"){
+  $sub_href = "../mods/announcement_subscription/subscribe.php?a=unsubscribe";
+  $sub_button = _AT('announcement_subscription_unsubscribe');
+} else {
+  $sub_href = "../mods/announcement_subscription/subscribe.php?a=subscribe";
+  $sub_button = _AT('announcement_subscription_subscribe');
+}
+
+
+?>