예제 #1
0
 /**
  * Function to be run periodically according to the moodle cron
  * This function searches for things that need to be done, such
  * as sending out mail, toggling flags etc ...
  *
  * @uses $CFG
  * @return boolean
  * @todo Finish documenting this function
  **/
 function cron()
 {
     global $CFG;
     // If no isset trackbymail, return cron.
     if (!isset($CFG->email_trackbymail)) {
         return true;
     }
     // If NOT enabled
     if ($CFG->email_trackbymail == 0) {
         return true;
     }
     // Get actualtime
     $now = time();
     // Get record for mail list
     if ($block = get_record('block', 'name', 'email_list')) {
         if ($now > $block->lastcron) {
             $unreadmails = new stdClass();
             // Get users who have unread mails
             $from = "{$CFG->prefix}user u,\n\t\t\t\t\t\t {$CFG->prefix}block_email_list_send s,\n\t\t\t\t\t\t {$CFG->prefix}block_email_list_mail m";
             $where = " WHERE u.id = s.userid\n\t\t\t\t\t\t\t\tAND s.mailid = m.id\n\t\t\t\t\t\t\t\tAND m.timecreated > {$block->lastcron}\n\t\t\t\t\t\t\t\tAND s.readed = 0\n\t\t\t\t\t\t\t\tAND s.sended = 1";
             // If exist any users
             if ($users = get_records_sql('SELECT u.* FROM ' . $from . $where)) {
                 // For each user ... get this unread mails, and send alert mail.
                 foreach ($users as $user) {
                     $mails = new stdClass();
                     // Preferences! Can send mail?
                     // Case:
                     // 		1.- Site allow send trackbymail
                     //			1.1.- User doesn't define this settings -> Send mail
                     //			1.2.- User allow trackbymail -> Send mail
                     //			1.3.- User denied trackbymail -> Don't send mail
                     // User can definied this preferences?
                     if ($preferences = get_record('block_email_list_preference', 'userid', $user->id)) {
                         if ($preferences->trackbymail == 0) {
                             continue;
                         }
                     }
                     // Get this unread mails
                     if ($mails = email_get_unread_mails($user->id)) {
                         $bodyhtml = '<head>';
                         foreach ($CFG->stylesheets as $stylesheet) {
                             $bodyhtml .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . "\n";
                         }
                         $bodyhtml .= '</head>';
                         $bodyhtml .= "\n<body id=\"email\">\n\n";
                         $bodyhtml .= '<div class="content">' . get_string('listmails', 'block_email_list') . ": </div>\n\n";
                         $body = get_string('listmails', 'block_email_list') . ": \n\n";
                         $bodyhtml .= '<table border="0" cellpadding="3" cellspacing="0">';
                         $bodyhtml .= '<th class="header">' . get_string('course') . '</th>';
                         $bodyhtml .= '<th class="header">' . get_string('subject', 'block_email_list') . '</th>';
                         $bodyhtml .= '<th class="header">' . get_string('from', 'block_email_list') . '</th>';
                         $bodyhtml .= '<th class="header">' . get_string('date', 'block_email_list') . '</th>';
                         // Prepare messagetext
                         foreach ($mails as $mail) {
                             // Get folder
                             $folder = email_get_root_folder($mail->userid, EMAIL_SENDBOX);
                             if (!email_isfolder_type($folder, EMAIL_SENDBOX)) {
                                 continue;
                             }
                             if (isset($mail->mailid)) {
                                 $message = get_record('block_email_list_mail', 'id', $mail->mailid);
                                 $mailcourse = get_record('course', 'id', $mail->course);
                                 $body .= "---------------------------------------------------------------------\n";
                                 $body .= get_string('course') . ": {$mailcourse->fullname} \n";
                                 $body .= get_string('subject', 'block_email_list') . ": {$message->subject} \n";
                                 $body .= get_string('from', 'block_email_list') . ": " . fullname(email_get_user($message->id));
                                 $body .= " - " . userdate($message->timecreated) . "\n";
                                 $body .= "---------------------------------------------------------------------\n\n";
                                 $bodyhtml .= '<tr  class="r0">';
                                 $bodyhtml .= '<td class="cell c0">' . $mailcourse->fullname . '</td>';
                                 $bodyhtml .= '<td class="cell c0">' . $message->subject . '</td>';
                                 $bodyhtml .= '<td class="cell c0">' . fullname(email_get_user($message->id)) . '</td>';
                                 $bodyhtml .= '<td class="cell c0">' . userdate($message->timecreated) . '</td>';
                                 $bodyhtml .= '</tr>';
                             }
                         }
                         $bodyhtml .= '</table>';
                         $bodyhtml .= '</body>';
                         $body .= "\n\n\n\n";
                         email_to_user($user, get_string('emailalert', 'block_email_list'), get_string('emailalert', 'block_email_list') . ': ' . get_string('newmails', 'block_email_list'), $body, $bodyhtml);
                     }
                 }
             }
         }
         return true;
     } else {
         mtrace('FATAL ERROR: I couldn\'t read eMail list block');
         return false;
     }
 }
예제 #2
0
파일: lib.php 프로젝트: henriquecrang/e-UNI
/**
 * This function prints select folders combobox, for move any mails
 *
 * @uses $USER
 * @param object $options
 */
function email_print_movefolder_button($options)
{
    global $CFG, $USER;
    $courseid = NULL;
    if ($options->id == SITEID and $options->course != SITEID) {
        $courseid = $options->course;
    } else {
        $courseid = $options->id;
    }
    /// TODO: Changed this function, now cases are had:
    //						1.- Inbox folder: Only can move to subfolders inbox and trash folder.
    //						2.- Sendbox and draft folder: Only can move on this subfolders.
    //						3.- Trash folder: Can move any folder
    if (isset($options->folderid)) {
        // Get folder
        $folderbe = email_get_folder($options->folderid);
    } else {
        if (isset($options->folderoldid)) {
            // Get folder
            $folderbe = email_get_folder($options->folderoldid);
        } else {
            // Inbox folder
            $folderbe = email_get_root_folder($USER->id, EMAIL_INBOX);
        }
    }
    if (email_isfolder_type($folderbe, EMAIL_SENDBOX)) {
        // Get my sendbox folders
        $folders = email_get_my_folders($USER->id, $courseid, false, true, false, true);
    } else {
        if (email_isfolder_type($folderbe, EMAIL_DRAFT)) {
            // Get my sendbox folders
            $folders = email_get_my_folders($USER->id, $courseid, false, true, true, true);
        } else {
            if (email_isfolder_type($folderbe, EMAIL_TRASH)) {
                // Get my folders
                $folders = email_get_my_folders($USER->id, $courseid, false, false, false, false);
            } else {
                // Get my folders
                $folders = email_get_my_folders($USER->id, $courseid, false, true, true, false);
            }
        }
    }
    if ($folders) {
        $choose = '';
        // Get my courses
        foreach ($folders as $key => $foldername) {
            $choose .= '<option value="' . $key . '">' . $foldername . '</option>';
        }
    }
    echo '<select name="folderid" onchange="addAction(this)">
					<option value="" selected="selected">' . get_string('movetofolder', 'block_email_list') . ':</option>' . $choose . '
    		</select>';
    // Add 2 space
    echo '&#160;&#160;';
    // Change, now folderoldid is actual folderid
    if (!$options->folderid) {
        if ($inbox = email_get_root_folder($USER->id, EMAIL_INBOX)) {
            echo '<input type="hidden" name="folderoldid" value="' . $inbox->id . '" />';
        }
    } else {
        echo '<input type="hidden" name="folderoldid" value="' . $options->folderid . '" />';
    }
    // Define action
    //echo '<input type="hidden" name="action" value="move2folder" />';
    // Add javascript for insert person/s who I've send mail
    $javascript = '<script type="text/javascript" language="JavaScript">
                <!--
                		function addAction(form) {

                			var d = document.createElement("div");
                        d.setAttribute("id", "action");
                        var act = document.createElement("input");
                        act.setAttribute("type", "hidden");
                        act.setAttribute("name", "action");
                        act.setAttribute("id", "action");
                        act.setAttribute("value", "move2folder");
                        d.appendChild(act);
                        document.getElementById("move2folder").appendChild(d);

                			document.sendmail.submit();
                		}
                	-->
                 </script>';
    echo $javascript;
    // Print sent button
    //echo '<input type="submit" value="' .get_string('move'). '" onclick="javascript:addAction(this);" />';
    //echo '</div>';
}
/**
 * This functions return number of unreaded mails
 *
 * @uses $CFG
 * @param int $userid User ID
 * @param int $courseid Course ID
 * @param int $folderid Folder ID (Optional) When fault this param, return total number of unreaded mails
 * @return int Number of unread mails.
 * @todo Finish documenting this function
 **/
function email_count_unreaded_mails($userid, $courseid, $folderid = NULL)
{
    global $CFG;
    if (!$folderid or $folderid <= 0) {
        // Get draft folder
        if ($folder = email_get_root_folder($userid, EMAIL_INBOX)) {
            $foldersid = $folder->id;
            // Get all subfolders
            if ($subfolders = email_get_all_subfolders($folder->id)) {
                foreach ($subfolders as $subfolder) {
                    $foldersid .= ', ' . $subfolder->id;
                }
            }
            $sql = "SELECT count(*)\n\t\t                            FROM {$CFG->prefix}email_mail m\n\t\t                   LEFT JOIN {$CFG->prefix}email_send s ON m.id = s.mailid\n\t\t                   LEFT JOIN {$CFG->prefix}email_foldermail fm ON m.id = fm.mailid ";
            // WHERE principal clause for filter by user and course
            $wheresql = " WHERE s.userid = {$userid}\n\t\t\t\t\t\t  AND s.course = {$courseid}\n\t\t\t\t\t\t  AND fm.folderid IN ( {$foldersid} )\n\t\t\t\t\t\t  AND s.readed = 0\n\t\t\t\t\t\t  AND s.sended = 1";
            return count_records_sql($sql . $wheresql);
        } else {
            return 0;
        }
        // return mails unreaded
        // wreturn count_records('email_send', 'userid', $userid, 'course', $courseid, 'readed', 0);
    } else {
        // Get folder
        if (!($folder = email_get_folder($folderid))) {
            return 0;
        }
        if (email_isfolder_type($folder, EMAIL_INBOX)) {
            // For apply order, I've writting an sql clause
            $sql = "SELECT count(*)\n\t\t                            FROM {$CFG->prefix}email_mail m\n\t\t                   LEFT JOIN {$CFG->prefix}email_send s ON m.id = s.mailid\n\t\t                   LEFT JOIN {$CFG->prefix}email_foldermail fm ON m.id = fm.mailid ";
            // WHERE principal clause for filter by user and course
            $wheresql = " WHERE s.userid = {$userid}\n\t\t\t\t\t\t  AND s.course = {$courseid}\n\t\t\t\t\t\t  AND fm.folderid = {$folder->id}\n\t\t\t\t\t\t  AND s.readed = 0\n\t\t\t\t\t\t  AND s.sended = 1";
            return count_records_sql($sql . $wheresql);
        } else {
            if (email_isfolder_type($folder, EMAIL_DRAFT)) {
                // For apply order, I've writting an sql clause
                $sql = "SELECT count(*)\n\t\t                   \tFROM {$CFG->prefix}email_mail m\n\t\t                   \tLEFT JOIN {$CFG->prefix}email_foldermail fm ON m.id = fm.mailid ";
                // WHERE principal clause for filter user and course
                $wheresql = " WHERE m.userid = {$userid}\n\t\t\t\t\t\t  AND m.course = {$courseid}\n\t\t\t\t\t\t  AND fm.folderid = {$folder->id}";
                return count_records_sql($sql . $wheresql);
            } else {
                return 0;
            }
        }
    }
}
예제 #4
0
 /**
  * This function remove eMail, if this does in TRASH folder remove of BBDD else move to TRASH folder.
  *
  * @access public
  * @version 1.0
  * @param int $userid User Id
  * @param int $courseid Course Id
  * @param int $folderid Folder Id
  * @param boolean $silent Show or not show messages
  * @return boolean Success/Fail
  * @todo Finish documenting this function
  */
 function remove($userid, $courseid, $folderid, $silent = false)
 {
     // First, show if folder remove or not
     $deletemails = false;
     $success = true;
     if (email_isfolder_type(get_record('block_email_list_folder', 'id', $folderid), EMAIL_TRASH)) {
         $deletemails = true;
     }
     // FIXME: Esborrar els attachments quan no hagi cap referència al mail
     // If delete definity mails ...
     if ($deletemails) {
         // Delete reference mail
         if (!delete_records('block_email_list_send', 'mailid', $this->id, 'userid', $userid, 'course', $courseid)) {
             return false;
         }
     } else {
         // Get remove folder user
         $removefolder = email_get_root_folder($userid, EMAIL_TRASH);
         // Get actual folder
         $actualfolder = email_get_reference2foldermail($this->id, $folderid);
         if ($actualfolder) {
             // Move mails to trash
             if (!email_move2folder($this->id, $actualfolder->id, $removefolder->id)) {
                 $success = false;
             } else {
                 // Mark the message as read
                 set_field('block_email_list_send', 'readed', 1, 'mailid', $this->id, 'userid', $userid, 'course', $courseid);
                 //Thanks Ann
             }
         } else {
             $success = false;
         }
     }
     // Notify
     if ($success) {
         add_to_log($this->course, 'email_list', 'remove mail', '', 'Remove ' . $this->subject, 0, $this->userid);
         if (!$silent) {
             notify(get_string('removeok', 'block_email_list'), 'notifysuccess');
         }
         return true;
     } else {
         if (!$silent) {
             notify(get_string('removefail', 'block_email_list'));
         }
         return false;
     }
 }