Example #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;
     }
 }
 if ($action == EMAIL_FORWARD) {
     $newmail = new stdClass();
     // Get mail
     if (!($oldmail = get_record('email_mail', 'id', $mailid))) {
         error('Can\'t found mail');
     }
     $newmail = PHP_VERSION < 5 ? $oldmail : clone $oldmail;
     // Remove id
     unset($newmail->id);
     $newmail->mailid = NULL;
     // Predefinity user send
     $user = email_get_user($mailid);
 }
 if ($action == EMAIL_EDITDRAFT) {
     // Predefinity user send
     $userwriter = email_get_user($mailid);
     // Get users sent mail, with option for reply all
     $selectedusersto = email_get_users_sent($mailid, true, false, 'to');
     $mail->nameto = '';
     foreach ($selectedusersto as $userid) {
         $mail->nameto .= email_fullname(get_record('user', 'id', $userid), $context) . ', ';
     }
     // Get users sent mail, with option for reply all
     $selecteduserscc = email_get_users_sent($mailid, true, false, 'cc');
     $mail->namecc = '';
     foreach ($selecteduserscc as $userid) {
         $mail->namecc .= email_fullname(get_record('user', 'id', $userid), $context) . ', ';
     }
     // Get users sent mail, with option for reply all
     $selectedusersbcc = email_get_users_sent($mailid, true, false, 'bcc');
     $mail->namebcc = '';