Kaynağa Gözat

tool announcement: body not sent in email #4617

Laurent Opprecht 13 yıl önce
ebeveyn
işleme
ac55044ae9

+ 305 - 0
main/announcements/announcement_email.class.php

@@ -0,0 +1,305 @@
+<?php
+
+/**
+ * Announcement Email
+ *
+ * @license see /license.txt 
+ * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
+ */
+class AnnouncementEmail
+{
+
+    /**
+     *
+     * @param int|array $course
+     * @param int|array $annoucement
+     * 
+     * @return AnnouncementEmail 
+     */
+    public static function create($course, $announcement)
+    {
+        return new self($course, $announcement);
+    }
+
+    protected $course = null;
+    protected $announcement = null;
+
+    function __construct($course, $announcement)
+    {
+        if (empty($course))
+        {
+            $course = api_get_course_int_id();
+            $course = CourseManager::get_course_information_by_id($course);
+        }
+        else if (is_numeric($course))
+        {
+            $course = CourseManager::get_course_information_by_id(intval($course));
+        }
+        $this->course = $course;
+
+        if (is_numeric($announcement))
+        {
+            $announcement = AnnouncementManager::get_by_id($course['id'], intval($announcement));
+        }
+        $this->announcement = $announcement;
+    }
+
+    /**
+     * Course info
+     * 
+     * @param string $key
+     * @return array 
+     */
+    public function course($key = '')
+    {
+        $result = $key ? $this->course[$key] : $this->course;
+        $result = $key == 'id' ? intval($result) : $result;
+        return $result;
+    }
+
+    /**
+     * Announcement info
+     * 
+     * @param string $key
+     * @return array 
+     */
+    public function announcement($key = '')
+    {
+        $result = $key ? $this->announcement[$key] : $this->announcement;
+        $result = $key == 'id' ? intval($result) : $result;
+        return $result;
+    }
+
+    /**
+     * Returns either all course users or all session users depending on whether
+     * session is turned on or not
+     * 
+     * @return array
+     */
+    public function all_users()
+    {
+        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
+        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
+        $course_code = $this->course('code');
+        $course_code = Database::escape_string($course_code);
+
+        if (empty($_SESSION['id_session']) || api_get_setting('use_session_mode') == 'false')
+        {
+            $rel_rh = COURSE_RELATION_TYPE_RRHH;
+            $sql = "SELECT user.user_id, user.email, user.lastname, user.firstname
+                    FROM $tbl_course_user, $tbl_user
+                    WHERE active = 1 AND 
+                          course_code='$course_code' AND
+                          course_rel_user.user_id = user.user_id AND 
+                          relation_type <> $rel_rh";
+        }
+        else
+        {
+            $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
+            $session_id = api_get_session_id();
+            $sql = "SELECT user.user_id, user.email, user.lastname, user.firstname
+                    FROM $tbl_user 
+                    INNER JOIN $tbl_session_course_user
+                          ON $tbl_user.user_id = $tbl_session_course_user.id_user AND
+                             $tbl_session_course_user.course_code = $course_code AND
+                             $tbl_session_course_user.id_session = $session_id
+                    WHERE
+                        active = 1";
+        }
+
+        $rs = Database::query($sql);
+
+        $result = array();
+        while ($data = Database::fetch_array($rs))
+        {
+            $result[] = $data;
+        }
+        return $result;
+    }
+
+    /**
+     * Returns users and groups an announcement item has been sent to.
+     * 
+     * @return array Array of users and groups to whom the element has been sent
+     */
+    public function sent_to_info()
+    {
+        $result = array();
+        $result['groups'] = array();
+        $result['users'] = array();
+
+        $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
+        $tool = TOOL_ANNOUNCEMENT;
+
+        $id = $this->announcement('id');
+        $course_id = $this->course('id');
+
+        $sql = "SELECT to_group_id, to_user_id FROM $tbl_item_property WHERE c_id = $course_id AND tool = '$tool' AND ref=$id";
+        $rs = Database::query($sql);
+
+        $sent_to_group = array();
+        $sent_to_user = array();
+        while ($row = Database::fetch_array($rs))
+        {
+            // if to_group_id is null then it is sent to a specific user
+            // if to_group_id = 0 then it is sent to everybody
+            $group_id = $row['to_group_id'];
+            if (!empty($group_id))
+            {
+                $result['groups'][] = (int)$group_id;
+            }
+
+            // if to_user_id <> 0 then it is sent to a specific user
+            $user_id = $row['to_user_id'];
+            if (!empty($user_id))
+            {
+                $result['users'][] = (int)$user_id;
+            }
+        }
+        return $result;
+    }
+
+    /**
+     * Returns the list of user info to which an announcement was sent.
+     * This function returns a list of actual users even when recipient 
+     * are groups
+     * 
+     * @return array
+     */
+    public function sent_to()
+    {
+        $sent_to = $this->sent_to_info();
+        $users = $sent_to['users'];
+        $groups = $sent_to['groups'];
+
+        if (!empty($groups))
+        {
+            $group_users = GroupManager::get_groups_users($groups);
+            $group_users = UserManager::get_user_list_by_ids($group_users, true);
+            $users = array_merge($users, $group_users);
+        }
+        if (empty($users))
+        {
+            $users = self::all_users();
+        }
+
+        return $users;
+    }
+
+    /**
+     * Sender info
+     * 
+     * @param string $key
+     * @return array 
+     */
+    public function sender($key = '')
+    {
+        global $_user;
+        return $key ? $_user[$key] : $_user;
+    }
+
+    /**
+     * Email subject
+     * 
+     * @return string
+     */
+    public function subject()
+    {
+        $result = $this->course('title');
+        $result = stripslashes($result);
+        return $result;
+    }
+
+    /**
+     * Email message
+     * 
+     * @return string
+     */
+    public function message()
+    {
+        $title = $this->announcement('title');
+        $title = stripslashes($title);
+
+        $content = $this->announcement('content');
+        $content = stripslashes($content);
+
+        $user_firstname = $this->sender('firstName');
+        $user_lastname = $this->sender('lastName');
+        $user_email = $this->sender('mail');
+
+        $www = api_get_path(WEB_CODE_PATH);
+        $course_param = api_get_cidreq();
+        $course_name = $this->course('name');
+
+        $result = '';
+        $result .= "<h1>$title</h1>";
+        $result .= "<div>$content</div>";
+        $result .= '--';
+        $result .= "<a href=\"mailto:$user_email\">$user_firstname $user_lastname</a><br/>";
+        $result .= "<a href=\"$www/announcements/announcements.php?$course_param\">$course_name</a><br/>";
+        return $result;
+    }
+
+    /**
+     * Returns the one file that can be attached to an announcement.
+     * 
+     * @return array
+     */
+    public function attachement()
+    {
+        $result = array();
+        $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
+        $id = $this->announcement('id');
+        $course_id = $this->course('id');
+        $sql = "SELECT * FROM $tbl_announcement_attachment WHERE c_id = $course_id AND announcement_id = $id ";
+        $rs = Database::query($sql);
+        $course_path = $this->course('directory');
+        while ($row = Database::fetch_array($rs))
+        {
+            $path = api_get_path(SYS_COURSE_PATH) . $course_path . '/upload/announcements/' . $row['path'];
+            $filename = $row['filename'];
+            $result[] = array('path' => $path, 'filename' => $filename);
+        }
+
+        $result = $result ? reset($result) : array();
+        return $result;
+    }
+
+    /**
+     * Send emails to users. 
+     */
+    public function send()
+    {
+        $sender = $this->sender();
+        $sender_name = api_get_person_name($sender['firstName'], $sender['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
+        $sender_email = $sender['mail'];
+
+        $subject = $this->subject();
+        $message = $this->message();
+        $attachement = $this->attachement();
+
+        // Send email one by one to avoid antispam         
+        $users = $this->sent_to();
+        foreach ($users as $user)
+        {
+            $recipient_name = api_get_person_name($user['firstname'], $user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
+            $recipient_email = $user['email'];
+            @api_mail_html($recipient_name, $recipient_email, $subject, $message, $sender_name, $sender_email, null, $attachement, true);
+        }
+        $this->log_mail_sent();
+    }
+
+    /**
+     * Store that emails where sent 
+     */
+    public function log_mail_sent()
+    {
+        $id = $this->announcement('id');
+        $course_id = $this->course('id');
+
+        $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
+        $sql = "UPDATE $tbl_announcement SET email_sent=1 WHERE c_id = $course_id AND id=$id";
+        Database::query($sql);
+    }
+
+}

+ 37 - 101
main/announcements/announcements.inc.php

@@ -702,6 +702,37 @@ class AnnouncementManager  {
 	/*
 			DATA FUNCTIONS
 	*/
+        
+        /**
+         * Returns announcement info from its id
+         * 
+         * @param type $course_id
+         * @param type $annoucement_id
+         * @return array
+         */
+        public static function get_by_id($course_id, $annoucement_id)
+        {
+            $annoucement_id = intval($annoucement_id);
+            $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
+            
+            $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
+            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
+
+            $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
+                           FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
+                           WHERE announcement.id = toolitemproperties.ref AND
+                                 toolitemproperties.tool='announcement' AND
+                                 announcement.c_id = $course_id AND
+                                 toolitemproperties.c_id = $course_id AND
+                                 announcement.id = $annoucement_id";                
+            $rs = Database::query($sql);
+            while ($data = Database::fetch_array($rs)) 
+            {
+                return $data;
+            }		
+            return array();
+            
+        }
 	
 	/**
 	* this function gets all the users of the course,
@@ -1094,7 +1125,7 @@ class AnnouncementManager  {
 		}
 		return $sent_to;
 	}
-	
+        
 	
 	/*		ATTACHMENT FUNCTIONS	*/
 	
@@ -1227,107 +1258,12 @@ class AnnouncementManager  {
 		// update item_property
 		//api_item_property_update($_course, 'announcement_attachment',  $id,'AnnouncementAttachmentDeleted', api_get_user_id());
 	}
-    
-    public static function send_email($id) {        
-        $sent_to	= self::sent_to("announcement", $id);
-        $userlist   = $sent_to['users'];
-        $userlist = array_map('intval', $userlist);
         
-        $grouplist  = $sent_to['groups'];
-        $grouplist = array_map('intval', $grouplist);
-        
-        $course_id = api_get_course_int_id();
-        $course_code = api_get_course_id();
-        $course_info = api_get_course_info($course_code);       
-        
-        $tbl_groupUser  		= Database::get_course_table(TABLE_GROUP_USER);   
-        $tbl_user          		= Database::get_main_table(TABLE_MAIN_USER);
-        $tbl_course_user   		= Database::get_main_table(TABLE_MAIN_COURSE_USER);
-        $tbl_session_course_user= Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);        
-        $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
-
-        // groepen omzetten in users
-        if (isset($grouplist) && !empty($grouplist)) {            
-            $grouplist = "'".implode("', '",$grouplist)."'";	//protect individual elements with surrounding quotes
-            $sql = "SELECT user_id
-                    FROM $tbl_groupUser gu
-                    WHERE c_id = $course_id AND gu.group_id IN (".$grouplist.")";
-            $groupMemberResult = Database::query($sql);
-            if ($groupMemberResult) {
-                while ($u = Database::fetch_array($groupMemberResult)) {
-                    $userlist[] = $u['user_id']; // complete the user id list ...
-                }
-            }
-        }
-
-        if (isset($userlist) && !empty($userlist) && is_array($userlist)) {            
-            $userlist = "'".implode("', '", array_unique($userlist) )."'";
-
-            // send to the created 'userlist'
-            $sqlmail = "SELECT user_id, lastname, firstname, email
-                        FROM $tbl_user
-                        WHERE active = 1 AND user_id IN (".$userlist.")";
-        } else if (empty($_POST['not_selected_form'])) {
-            if(empty($_SESSION['id_session']) || api_get_setting('use_session_mode')=='false') {
-                // send to everybody
-                $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
-                            FROM $tbl_course_user, $tbl_user
-                            WHERE  active = 1 AND 
-                                    course_code='".Database::escape_string($course_code)."' AND
-                                    course_rel_user.user_id = user.user_id AND 
-                                    relation_type <>".COURSE_RELATION_TYPE_RRHH." ";
-            } else {
-                $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
-                            FROM $tbl_user INNER JOIN $tbl_session_course_user
-                            ON $tbl_user.user_id = $tbl_session_course_user.id_user AND
-                            active = 1 AND
-                            $tbl_session_course_user.course_code = '".$course_code."' AND
-                            $tbl_session_course_user.id_session = ".api_get_session_id();
-            }
-        }                            
-        $user_info = api_get_user_info();
-        
-        if ($sqlmail != '') {
-            $rs_mail = Database::query($sqlmail);
-
-            /*	Send email one by one to avoid antispam */
-
-            //$db_name = Database::get_course_table(TABLE_MAIN_SURVEY);
-            while ($myrow = Database::fetch_array($rs_mail)) {
-
-                $emailSubject = "[" . $course_info['official_code'] . "] " . $emailTitle;
-
-                // intro of the email: receiver name and subject
-                $mail_body = api_get_person_name($myrow["lastname"], $myrow["firstname"], null, PERSON_NAME_EMAIL_ADDRESS)."<br />\n".stripslashes($emailTitle)."<br />";                
-
-                // Main part of the email
-                $mail_body .= trim(stripslashes(self::parse_content($newContent, api_get_course_id())));
-                // Signature of email: sender name and course URL after -- line
-                $mail_body .= "<br />-- <br />";
-                $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS)." \n";
-                $mail_body .= "<br /> \n<a href=\"".api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq()."\">";
-                $mail_body .= $course_info['official_code'].' '.$course_info['name'] . "</a>";
-
-                $recipient_name	= api_get_person_name($myrow["firstname"], $myrow["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
-                $mailid = $myrow["email"];
-                $sender_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
-                $sender_email = $user_info['mail'];
-
-                // send attachment file
-                $data_file = array();
-                $sql = 'SELECT path, filename FROM '.$tbl_announcement_attachment.' WHERE c_id = '.$course_id.' AND announcement_id = "'.$id.'"';
-                $rs_attach = Database::query($sql);
-                if (Database::num_rows($rs_attach) > 0) {
-                    $row_attach  = Database::fetch_array($rs_attach);
-                    $path_attach = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/upload/announcements/'.$row_attach['path'];
-                    $filename_attach = $row_attach['filename'];
-                    $data_file = array('path' => $path_attach,'filename' => $filename_attach);                    
-                }
-                @api_mail_html($recipient_name, $mailid, stripslashes($emailSubject), $mail_body, $sender_name, $sender_email, null, $data_file, true);          
-            }
-            self::update_mail_sent($id);            
+    
+        public static function send_email($annoucement_id) 
+        {        
+            $email = AnnouncementEmail::create(null, $annoucement_id);
+            $email->send();
         }
-        
-    }
     
 } //end class

+ 3 - 0
main/inc/lib/autoload.class.php

@@ -28,10 +28,13 @@ class Autoload
         }
 
         $dir = dirname(__FILE__);
+        $sys = api_get_path(SYS_CODE_PATH);
 
         $result = array();
         $result['Redirect'] = $dir . '/redirect.class.php';
         $result['Request'] = $dir . '/request.class.php';
+        $result['AnnouncementEmail'] = $sys. 'announcements/announcement_email.class.php';
+        
         return $result;
     }
 

+ 29 - 0
main/inc/lib/groupmanager.lib.php

@@ -743,6 +743,7 @@ class GroupManager {
 	/**
 	 * Get all users from a given group
 	 * @param int $group_id The group
+         * @return array list of user id
 	 */
 	public static function get_users ($group_id) {
 		$group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
@@ -756,6 +757,34 @@ class GroupManager {
 		}
 		return $users;
 	}
+        
+        
+        /**
+         * Returns users belonging to any of the group 
+         * 
+         * @param array $groups list of group ids
+         * @return array list of user ids
+         */
+        public static function get_groups_users($groups = array())
+        {
+            $result = array();
+            
+            $tbl_group_user = Database::get_course_table(TABLE_GROUP_USER); 
+            $course_id = api_get_course_int_id();
+        
+            $groups = array_map('intval', $groups);
+            $groups = implode(', ', $groups);	//protect individual elements with surrounding quotes
+            $sql = "SELECT DISTINCT user_id
+                    FROM $tbl_group_user gu
+                    WHERE c_id = $course_id AND gu.group_id IN ($groups)";
+            $rs = Database::query($sql);
+            while ($row = Database::fetch_array($rs)) 
+            {
+                $result[] = $row['user_id']; 
+            }
+
+            return $result;
+        }
 
 	/**
 	 * Fill the groups with students.

+ 27 - 0
main/inc/lib/usermanager.lib.php

@@ -600,6 +600,33 @@ class UserManager {
 	public static function is_username_too_long($username) {
 		return (strlen($username) > USERNAME_MAX_LENGTH);
 	}
+        
+	public static function get_user_list_by_ids($ids = array(), $active = null) 
+        {
+            if(empty($ids))
+            {
+                return array();
+            }
+            
+            $ids = is_array($ids) ? $ids : array($ids);            
+            $ids = array_map('intval', $ids);       
+            $ids = implode(',', $ids);
+            
+            $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
+            $sql = "SELECT * FROM $tbl_user WHERE user_id IN ($ids)";
+            if(! is_null($active))
+            {
+                $sql .= ' AND active=' . ($active ? '1' : '0'); 
+            }
+            
+            $rs = Database::query($sql);
+            $result = array();
+            while ($row = Database::fetch_array($rs)) 
+            {
+                $result[] = $row;
+            }
+            return $result;
+        }
 
 	/**
 	* Get a list of users of which the given conditions match with an = 'cond'