Exemple #1
0
function experimentmail__send_mails_from_queue($number = 0, $type = "", $experiment_id = "", $session_id = "")
{
    global $settings;
    $pars = array();
    if ($number > 0) {
        $limit = " LIMIT :number ";
        $pars[':number'] = $number;
    } else {
        $limit = "";
    }
    if ($type) {
        $tquery = " AND mail_type= :type ";
        $pars[':type'] = $type;
    } else {
        $tquery = "";
    }
    if ($experiment_id) {
        $equery = " AND experiment_id= :experiment_id ";
        $pars[':experiment_id'] = $experiment_id;
    } else {
        $equery = "";
    }
    if ($session_id) {
        $squery = " AND session_id= :session_id ";
        $pars[':session_id'] = $session_id;
    } else {
        $squery = "";
    }
    $smails = array();
    $smails_ids = array();
    $invitations = array();
    $reminders = array();
    $bulks = array();
    $warnings = array();
    $errors = array();
    $reminder_text = array();
    $warning_text = array();
    $inv_texts = array();
    $exps = array();
    $sesss = array();
    $parts = array();
    $labs = array();
    $pform_fields = array();
    $slists = array();
    // first get mails to send
    $query = "SELECT * FROM " . table('mail_queue') . "\n            WHERE error = '' " . $tquery . $equery . $squery . "\n            ORDER BY timestamp, mail_id " . $limit;
    $result = or_query($query, $pars);
    while ($line = pdo_fetch_assoc($result)) {
        $smails[] = $line;
        $smails_ids[] = $line['mail_id'];
    }
    // so we don't handle errors at all, and just delete here?!?
    //$pars=array();
    //foreach ($smails_ids as $id) $pars[]=array(':id'=>$id);
    //$query="DELETE FROM ".table('mail_queue')."
    //      WHERE mail_id = :id";
    //$done=or_query($query,$pars);
    foreach ($smails as $line) {
        $texp = $line['experiment_id'];
        $tsess = $line['session_id'];
        $tpart = $line['mail_recipient'];
        $ttype = $line['mail_type'];
        $tbulk = $line['bulk_id'];
        $continue = true;
        // well, if experiment_id, session_id, recipient, footer or inv_text, add to array
        if (!isset($exps[$texp]) && $texp) {
            $exps[$texp] = orsee_db_load_array("experiments", $texp, "experiment_id");
        }
        if (!isset($sesss[$tsess]) && $tsess) {
            $sesss[$tsess] = orsee_db_load_array("sessions", $tsess, "session_id");
        }
        if (!isset($parts[$tpart]) && $tpart) {
            $parts[$tpart] = orsee_db_load_array("participants", $tpart, "participant_id");
        }
        $tlang = $parts[$tpart]['language'];
        if (!isset($footers[$tlang])) {
            $footers[$tlang] = load_mail("public_mail_footer", $tlang);
        }
        if ($ttype == "session_reminder" && !isset($reminder_text[$texp][$tlang])) {
            $mailtext = false;
            if ($settings['enable_session_reminder_customization'] == 'y') {
                $mailtext = experimentmail__get_customized_mailtext('experiment_session_reminder_mail', $texp, $tlang);
            }
            if (!isset($mailtext) || !$mailtext || !is_array($mailtext)) {
                $mailtext['subject'] = load_language_symbol('email_session_reminder_subject', $tlang);
                $mailtext['body'] = load_mail("public_session_reminder", $tlang);
            }
            $reminder_text[$texp][$tlang] = $mailtext;
        }
        if ($ttype == "noshow_warning" && !isset($warning_text[$tlang])) {
            $warning_text[$tlang]['text'] = load_mail("public_noshow_warning", $tlang);
            $warning_text[$tlang]['subject'] = load_language_symbol('email_noshow_warning_subject', $tlang);
        }
        if (($ttype == "session_reminder" || $ttype == "noshow_warning") && !isset($labs[$tsess][$tlang])) {
            $labs[$tsess][$tlang] = laboratories__get_laboratory_text($sesss[$tsess]['laboratory_id'], $tlang);
        }
        if ($ttype == "invitation" && !isset($inv_texts[$texp][$tlang])) {
            $inv_texts[$texp][$tlang] = experimentmail__load_invitation_text($texp, $tlang);
        }
        if ($ttype == "invitation" && !isset($slists[$texp][$tlang])) {
            $slists[$texp][$tlang] = experimentmail__get_session_list($texp, $tlang);
        }
        if ($ttype == "bulk_mail" && !isset($bulk_mails[$tbulk][$tlang])) {
            $bulk_mails[$tbulk][$tlang] = experimentmail__load_bulk_mail($tbulk, $tlang);
        }
        // check for missing values ...
        if (!isset($parts[$tpart]['participant_id'])) {
            $continue = false;
            // email error: no recipient
            $line['error'] .= "no_recipient:";
        } else {
            if (!isset($pform_fields[$tlang])) {
                $pform_fields[$tlang] = participant__load_participant_email_fields($tlang);
            }
            $parts[$tpart] = experimentmail__fill_participant_details($parts[$tpart], $pform_fields[$tlang]);
        }
        if (!isset($exps[$texp]['experiment_id']) && ($ttype == "invitation" || $ttype == "session_reminder" || $ttype == "noshow_warning")) {
            $continue = false;
            // email error: no experiment id given
            $line['error'] .= "no_experiment:";
        }
        if (!isset($sesss[$tsess]['session_id']) && ($ttype == "session_reminder" || $ttype == "noshow_warning")) {
            $continue = false;
            // email error: no session id given
            $line['error'] .= "no_session:";
        }
        if (!isset($inv_texts[$texp][$tlang]) && $ttype == "invitation") {
            $continue = false;
            // email error: no inv_text given
            $line['error'] .= "no_inv_text:";
        }
        if (!isset($bulk_mails[$tbulk][$tlang]) && $ttype == "bulk_mail") {
            $continue = false;
            // email error: no bulk_mail given
            $line['error'] .= "no_bulk_mail_text:";
        }
        // fine, if no errors, add to arrays
        if ($continue) {
            switch ($line['mail_type']) {
                case "invitation":
                    $invitations[] = $line;
                    break;
                case "session_reminder":
                    $reminders[] = $line;
                    break;
                case "noshow_warning":
                    $warnings[] = $line;
                    break;
                case "bulk_mail":
                    $bulks[] = $line;
                    break;
            }
        } else {
            $errors[] = $line;
        }
    }
    // fine now we have everything we want, and we can proceed with sending the mails
    $mails_sent = 0;
    $mails_errors = 0;
    $invmails_not_sent = 0;
    // reminders
    foreach ($reminders as $mail) {
        $tlang = $parts[$mail['mail_recipient']]['language'];
        $done = experimentmail__send_session_reminder_mail($mail, $parts[$mail['mail_recipient']], $exps[$mail['experiment_id']], $sesss[$mail['session_id']], $reminder_text[$mail['experiment_id']][$tlang], $labs[$mail['session_id']][$tlang], $footers[$tlang]);
        if ($done) {
            $mails_sent++;
            $deleted = experimentmail__delete_from_queue($mail['mail_id']);
        } else {
            $mail['error'] = "sending";
            $errors[] = $mail;
        }
    }
    // noshow warnings
    foreach ($warnings as $mail) {
        $tlang = $parts[$mail['mail_recipient']]['language'];
        $done = experimentmail__send_noshow_warning_mail($mail, $parts[$mail['mail_recipient']], $exps[$mail['experiment_id']], $sesss[$mail['session_id']], $warning_text[$tlang], $labs[$mail['session_id']][$tlang], $footers[$tlang]);
        if ($done) {
            $mails_sent++;
            $deleted = experimentmail__delete_from_queue($mail['mail_id']);
        } else {
            $mail['error'] = "sending";
            $errors[] = $mail;
        }
    }
    // invitations
    foreach ($invitations as $mail) {
        $tlang = $parts[$mail['mail_recipient']]['language'];
        if ($exps[$mail['experiment_id']]['experiment_type'] == 'laboratory' && !trim($slists[$mail['experiment_id']][$tlang])) {
            $done = true;
            // do not send invitation when session_list is empty!
            $invmails_not_sent++;
        } else {
            $done = experimentmail__send_invitation_mail($mail, $parts[$mail['mail_recipient']], $exps[$mail['experiment_id']], $inv_texts[$mail['experiment_id']][$tlang], $slists[$mail['experiment_id']][$tlang], $footers[$tlang]);
            if ($done) {
                $mails_sent++;
            }
        }
        if ($done) {
            $deleted = experimentmail__delete_from_queue($mail['mail_id']);
        } else {
            $mail['error'] = "sending";
            $errors[] = $mail;
        }
    }
    // bulks
    foreach ($bulks as $mail) {
        $tlang = $parts[$mail['mail_recipient']]['language'];
        $done = experimentmail__send_bulk_mail($mail, $parts[$mail['mail_recipient']], $bulk_mails[$tbulk][$tlang], $footers[$tlang]);
        if ($done) {
            $mails_sent++;
            $deleted = experimentmail__delete_from_queue($mail['mail_id']);
        } else {
            $mail['error'] = "sending";
            $errors[] = $mail;
        }
    }
    $done = experimentmail__gc_bulk_mail_texts();
    // handle errors
    $pars = array();
    $mails_errors = count($errors);
    if ($mails_errors > 0) {
        foreach ($errors as $mail) {
            $pars[] = array(':error' => $mail['error'], ':mail_id' => $mail['mail_id']);
        }
        $query = "UPDATE " . table('mail_queue') . "\n                SET error= :error\n                WHERE mail_id= :mail_id";
        $done = or_query($query, $pars);
    }
    $mess['mails_sent'] = $mails_sent;
    $mess['mails_invmails_not_sent'] = $invmails_not_sent;
    $mess['mails_errors'] = $mails_errors;
    return $mess;
}
 echo '<center>';
 echo '<TABLE class="or_page_subtitle" style="background: ' . $color['page_subtitle_background'] . '; color: ' . $color['page_subtitle_textcolor'] . '; width: 80%;">
         <TR><TD align="center">
         ' . $experiment['experiment_name'] . '
         </TD>';
 echo '</TR></TABLE>';
 echo '<TABLE class="or_formtable" style="width: 80%;">';
 echo '<TR><TD colspan=2>
         ' . button_link('experiment_mail_participants.php?experiment_id=' . urlencode($experiment_id), lang('back_to_mail_page'), 'backward', 'font-size: 8pt;') . '
         </TD></TR>';
 foreach ($inv_langs as $inv_lang) {
     // split in subject and text
     $subject = str_replace(strstr($experiment_mail[$inv_lang], "\n"), "", $experiment_mail[$inv_lang]);
     $body = substr($experiment_mail[$inv_lang], strpos($experiment_mail[$inv_lang], "\n") + 1, strlen($experiment_mail[$inv_lang]));
     if ($experiment['experiment_type'] == "laboratory") {
         $sessionlist = experimentmail__get_session_list($experiment_id, $inv_lang);
     } else {
         $sessionlist = '';
     }
     $pform_fields = participant__load_participant_email_fields($inv_lang);
     $experimentmail = experimentmail__preview_fake_participant_details($pform_fields);
     $experimentmail = experimentmail__get_invitation_mail_details($experimentmail, $experiment, $sessionlist);
     if ($experiment['sender_mail']) {
         $sendermail = $experiment['sender_mail'];
     } else {
         $sendermail = $settings['support_mail'];
     }
     $email_text = process_mail_template(stripslashes($body), $experimentmail);
     if (count($inv_langs) > 1) {
         echo '<TR><TD colspan=2>
                     <TABLE width="100%" border=0 class="or_panel_title"><TR>