diff --git a/email.php b/email.php index ff70214c..dbe29ebf 100644 --- a/email.php +++ b/email.php @@ -160,240 +160,242 @@ echo html_writer::end_tag('div'); echo $OUTPUT->footer(); -} +} else { -// we are presenting the form with values populated from either the log or drafts table in the db -if (!empty($type)) { - - $email = $DB->get_record('block_quickmail_' . $type, array('id' => $typeid)); - //$emailmailto = array(); - if ($type == 'log') { - $attributes = array ('id' => "{$typeid}_id_message_editor"); - } + // we are presenting the form with values populated from either the log or drafts table in the db + if (!empty($type)) { + + $email = $DB->get_record('block_quickmail_' . $type, array('id' => $typeid)); + //$emailmailto = array(); + if ($type == 'log') { + $attributes = array ('id' => "{$typeid}_id_message_editor"); + } - if ($messageIDresend == 1) { - list($email->mailto, $email->additional_emails) = quickmail::clean($email->failuserids); + if ($messageIDresend == 1) { + list($email->mailto, $email->additional_emails) = quickmail::clean($email->failuserids); + } + } else { + $email = new stdClass; + $email->id = null; } -} else { - $email = new stdClass; - $email->id = null; -} -$email->messageformat = editors_get_preferred_format(); -$default_sigid = $DB->get_field('block_quickmail_signatures', 'id', array( - 'userid' => $USER->id, 'default_flag' => 1 -)); -$email->sigid = $default_sigid ? $default_sigid : -1; - -// Some setters for the form -$email->type = $type; -$email->typeid = $typeid; - -$editor_options = array( - 'trusttext' => false, - 'subdirs' => 1, - 'maxfiles' => EDITOR_UNLIMITED_FILES, - 'accepted_types' => '*', - 'context' => $context -); - -$email = file_prepare_standard_editor( - $email, 'message', $editor_options, $context, 'block_quickmail', $type, $email->id -); -$selected = array(); -if (!empty($email->mailto)) { - foreach (explode(',', $email->mailto) as $id) { - $selected[$id] = (object) array('id'=>$id,'firstname'=>null,'lastname'=>null,'email'=>$id,'mailformat'=>'1','suspended'=>'0','maildisplay'=>'2','status'=>'0'); - if(is_numeric($selected[$id]->id)) { - $selected[$id] = $users[$id]; - } - unset($users[$id]); + $email->messageformat = editors_get_preferred_format(); + $default_sigid = $DB->get_field('block_quickmail_signatures', 'id', array( + 'userid' => $USER->id, 'default_flag' => 1 + )); + $email->sigid = $default_sigid ? $default_sigid : -1; + + // Some setters for the form + $email->type = $type; + $email->typeid = $typeid; + + $editor_options = array( + 'trusttext' => false, + 'subdirs' => 1, + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'accepted_types' => '*', + 'context' => $context + ); + + $email = file_prepare_standard_editor( + $email, 'message', $editor_options, $context, 'block_quickmail', $type, $email->id + ); + + $selected = array(); + if (!empty($email->mailto)) { + foreach (explode(',', $email->mailto) as $id) { + $selected[$id] = (object) array('id'=>$id,'firstname'=>null,'lastname'=>null,'email'=>$id,'mailformat'=>'1','suspended'=>'0','maildisplay'=>'2','status'=>'0'); + if(is_numeric($selected[$id]->id)) { + $selected[$id] = $users[$id]; + } + unset($users[$id]); + } } -} -$form = new email_form(null, array( - 'editor_options' => $editor_options, - 'selected' => $selected, - 'users' => $users, - 'roles' => $roles, - 'groups' => $groups, - 'users_to_roles' => $users_to_roles, - 'users_to_groups' => $users_to_groups, - 'sigs' => array_map(function($sig) { return $sig->title; }, $sigs), - 'alternates' => $alternates, - 'attributes' => $attributes -)); - -$warnings = array(); -// -if ($form->is_cancelled()) { - redirect(new moodle_url('/course/view.php?id=' . $courseid)); - // DWE we should check if we have selected users or emails around here. -} else if ($data = $form->get_data()) { - if (empty($data->subject)) { - $warnings[] = get_string('no_subject', 'block_quickmail'); - } - if (empty($data->mailto) && empty($data->additional_emails)) { - $warnings[] = get_string('no_users', 'block_quickmail'); - } - if (empty($warnings)) { - // Submitted data ////////////////////////////////////////////////////// - $data->time = time(); - $data->format = $data->message_editor['format']; - $data->message = $data->message_editor['text']; - $data->attachment = quickmail::attachment_names($data->attachments); - $data->messageWithSigAndAttach = ""; - // Store data; id is needed for file storage /////////////////////////// - if (isset($data->send)) { - $data->id = $DB->insert_record('block_quickmail_log', $data); - $table = 'log'; - } else if (isset($data->draft)) { - $table = 'drafts'; - - if (!empty($typeid) and $type == 'drafts') { - $data->id = $typeid; - $DB->update_record('block_quickmail_drafts', $data); - } else { - $data->id = $DB->insert_record('block_quickmail_drafts', $data); - } + $form = new email_form(null, array( + 'editor_options' => $editor_options, + 'selected' => $selected, + 'users' => $users, + 'roles' => $roles, + 'groups' => $groups, + 'users_to_roles' => $users_to_roles, + 'users_to_groups' => $users_to_groups, + 'sigs' => array_map(function($sig) { return $sig->title; }, $sigs), + 'alternates' => $alternates, + 'attributes' => $attributes + )); + + $warnings = array(); + // + if ($form->is_cancelled()) { + redirect(new moodle_url('/course/view.php?id=' . $courseid)); + // DWE we should check if we have selected users or emails around here. + } else if ($data = $form->get_data()) { + if (empty($data->subject)) { + $warnings[] = get_string('no_subject', 'block_quickmail'); } - $data = file_postupdate_standard_editor( - $data, 'message', $editor_options, $context, 'block_quickmail', $table, $data->id - ); - $DB->update_record('block_quickmail_' . $table, $data); - - $prepender = $config['prepend_class']; - if (!empty($prepender) and !empty($course->$prepender)) { - $subject = "[{$course->$prepender}] $data->subject"; - } else { - $subject = $data->subject; + if (empty($data->mailto) && empty($data->additional_emails)) { + $warnings[] = get_string('no_users', 'block_quickmail'); } - - // An instance id is needed before storing the file repository ///////// - file_save_draft_area_files( - $data->attachments, $context->id, 'block_quickmail', 'attachment_' . $table, $data->id, $editor_options - ); - - // Send emails ///////////////////////////////////////////////////////// - if (isset($data->send)) { - if ($type == 'drafts') { - quickmail::draft_cleanup($context->id, $typeid); + if (empty($warnings)) { + // Submitted data ////////////////////////////////////////////////////// + $data->time = time(); + $data->format = $data->message_editor['format']; + $data->message = $data->message_editor['text']; + $data->attachment = quickmail::attachment_names($data->attachments); + $data->messageWithSigAndAttach = ""; + // Store data; id is needed for file storage /////////////////////////// + if (isset($data->send)) { + $data->id = $DB->insert_record('block_quickmail_log', $data); + $table = 'log'; + } else if (isset($data->draft)) { + $table = 'drafts'; + + if (!empty($typeid) and $type == 'drafts') { + $data->id = $typeid; + $DB->update_record('block_quickmail_drafts', $data); + } else { + $data->id = $DB->insert_record('block_quickmail_drafts', $data); + } } - // deal with possible signature, will be appended to message in a little bit. - if (!empty($sigs) and $data->sigid > -1) { - $sig = $sigs[$data->sigid]; - $signaturetext = file_rewrite_pluginfile_urls($sig->signature, 'pluginfile.php', $context->id, 'block_quickmail', 'signature', $sig->id, $editor_options); + $data = file_postupdate_standard_editor( + $data, 'message', $editor_options, $context, 'block_quickmail', $table, $data->id + ); + $DB->update_record('block_quickmail_' . $table, $data); + + $prepender = $config['prepend_class']; + if (!empty($prepender) and !empty($course->$prepender)) { + $subject = "[{$course->$prepender}] $data->subject"; + } else { + $subject = $data->subject; } - // Prepare html content of message ///////////////////////////////// - $data->message = file_rewrite_pluginfile_urls($data->message, 'pluginfile.php', $context->id, 'block_quickmail', $table, $data->id, $editor_options); + // An instance id is needed before storing the file repository ///////// + file_save_draft_area_files( + $data->attachments, $context->id, 'block_quickmail', 'attachment_' . $table, $data->id, $editor_options + ); - if(empty($signaturetext)){ - $data->messageWithSigAndAttach = $data->message; - } - else{ - if($data->format == 0 || $data->format == 2 ){ - $data->messageWithSigAndAttach = $data->message . "\n\n" .$signaturetext; - }else{ - $data->messageWithSigAndAttach = $data->message . "

" .$signaturetext; + // Send emails ///////////////////////////////////////////////////////// + if (isset($data->send)) { + if ($type == 'drafts') { + quickmail::draft_cleanup($context->id, $typeid); } - } - // Append links to attachments, if any ///////////////////////////// - $data->messageWithSigAndAttach .= quickmail::process_attachments( - $context, $data, $table, $data->id - ); - - // Same user, alternate email ////////////////////////////////////// - if (!empty($data->alternateid)) { - $user = clone($USER); - $user->email = $alternates[$data->alternateid]; - } else { - $user = $USER; - } - $data->failuserids = array(); - // DWE -> Begin hopefully new way of dealing with messagetext and messagehtml - - // TEXT - // This is where we'll need to factor in the preferences of the receiver. - $messagetext = format_text_email($data->messageWithSigAndAttach, $data->format); - - // HTML - $options = array('filter' => false); - $messagehtml = format_text($data->messageWithSigAndAttach, $data->format, $options); - - if(!empty($data->mailto)) { - - foreach (explode(',', $data->mailto) as $userid) { - // Email gets sent here - if ($everyone[$userid]->value) { $everyone[$userid]->email = $everyone[$userid]->value; } - $success = email_to_user($everyone[$userid], $user, $subject,$messagetext, $messagehtml); - if (!$success) { - $warnings[] = get_string("no_email", 'block_quickmail', $everyone[$userid]); - $data->failuserids[] = $userid; - } + // deal with possible signature, will be appended to message in a little bit. + if (!empty($sigs) and $data->sigid > -1) { + $sig = $sigs[$data->sigid]; + $signaturetext = file_rewrite_pluginfile_urls($sig->signature, 'pluginfile.php', $context->id, 'block_quickmail', 'signature', $sig->id, $editor_options); } - } - if(!empty($data->additional_emails)){ - $additional_email_array = preg_split('/[,;]/', $data->additional_emails); - $i = 0; - foreach ($additional_email_array as $additional_email) { - $additional_email = trim($additional_email); - $fakeuser = new stdClass(); - $fakeuser->id = 99999900 + $i; - $fakeuser->email = $additional_email; - // TODO make this into a menu option - $fakeuser->mailformat = 1; - $additional_email_success = email_to_user($fakeuser, $user, $subject, $messagetext, $messagehtml); - if (!$additional_email_success) { - $data->failuserids[] = $additional_email; - // will need to notify that an email is incorrect - $warnings[] = get_string("no_email_address", 'block_quickmail', $fakeuser->email); + // Prepare html content of message ///////////////////////////////// + $data->message = file_rewrite_pluginfile_urls($data->message, 'pluginfile.php', $context->id, 'block_quickmail', $table, $data->id, $editor_options); + + if(empty($signaturetext)){ + $data->messageWithSigAndAttach = $data->message; + } + else{ + if($data->format == 0 || $data->format == 2 ){ + $data->messageWithSigAndAttach = $data->message . "\n\n" .$signaturetext; + }else{ + $data->messageWithSigAndAttach = $data->message . "

" .$signaturetext; } - $i++; } - } + // Append links to attachments, if any ///////////////////////////// + $data->messageWithSigAndAttach .= quickmail::process_attachments( + $context, $data, $table, $data->id + ); + + // Same user, alternate email ////////////////////////////////////// + if (!empty($data->alternateid)) { + $user = clone($USER); + $user->email = $alternates[$data->alternateid]; + } else { + $user = $USER; + } + $data->failuserids = array(); + // DWE -> Begin hopefully new way of dealing with messagetext and messagehtml + + // TEXT + // This is where we'll need to factor in the preferences of the receiver. + $messagetext = format_text_email($data->messageWithSigAndAttach, $data->format); + + // HTML + $options = array('filter' => false); + $messagehtml = format_text($data->messageWithSigAndAttach, $data->format, $options); + + if(!empty($data->mailto)) { + + foreach (explode(',', $data->mailto) as $userid) { + // Email gets sent here + if ($everyone[$userid]->value) { $everyone[$userid]->email = $everyone[$userid]->value; } + $success = email_to_user($everyone[$userid], $user, $subject,$messagetext, $messagehtml); + if (!$success) { + $warnings[] = get_string("no_email", 'block_quickmail', $everyone[$userid]); + $data->failuserids[] = $userid; + } + } + } + + if(!empty($data->additional_emails)){ + $additional_email_array = preg_split('/[,;]/', $data->additional_emails); + $i = 0; + foreach ($additional_email_array as $additional_email) { + $additional_email = trim($additional_email); + $fakeuser = new stdClass(); + $fakeuser->id = 99999900 + $i; + $fakeuser->email = $additional_email; + // TODO make this into a menu option + $fakeuser->mailformat = 1; + $additional_email_success = email_to_user($fakeuser, $user, $subject, $messagetext, $messagehtml); + if (!$additional_email_success) { + $data->failuserids[] = $additional_email; + // will need to notify that an email is incorrect + $warnings[] = get_string("no_email_address", 'block_quickmail', $fakeuser->email); + } + $i++; + } + } - $data->failuserids = implode(',', $data->failuserids); - $DB->update_record('block_quickmail_log', $data); + $data->failuserids = implode(',', $data->failuserids); + $DB->update_record('block_quickmail_log', $data); - if ($data->receipt) { - email_to_user($USER, $user, $subject, $messagetext, $messagehtml); + if ($data->receipt) { + email_to_user($USER, $user, $subject, $messagetext, $messagehtml); + } } } + $email = $data; } - $email = $data; -} -if (empty($email->attachments)) { - if (!empty($type)) { - $attachid = file_get_submitted_draft_itemid('attachment'); - file_prepare_draft_area( - $attachid, $context->id, 'block_quickmail', 'attachment_' . $type, $typeid - ); - $email->attachments = $attachid; + if (empty($email->attachments)) { + if (!empty($type)) { + $attachid = file_get_submitted_draft_itemid('attachment'); + file_prepare_draft_area( + $attachid, $context->id, 'block_quickmail', 'attachment_' . $type, $typeid + ); + $email->attachments = $attachid; + } } -} -$form->set_data($email); -if (empty($warnings)) { - if (isset($email->send)) { - redirect(new moodle_url('/blocks/quickmail/emaillog.php', array('courseid' => $course->id))); - } else if (isset($email->draft)) { - $warnings['success'] = get_string("changessaved"); + $form->set_data($email); + if (empty($warnings)) { + if (isset($email->send)) { + redirect(new moodle_url('/blocks/quickmail/emaillog.php', array('courseid' => $course->id))); + } else if (isset($email->draft)) { + $warnings['success'] = get_string("changessaved"); + } } -} -echo $OUTPUT->header(); -echo $OUTPUT->heading($blockname); + echo $OUTPUT->header(); + echo $OUTPUT->heading($blockname); + + foreach ($warnings as $type => $warning) { + $class = ($type === 'success') ? 'notifysuccess' : 'notifyproblem'; + echo $OUTPUT->notification($warning, $class); + } -foreach ($warnings as $type => $warning) { - $class = ($type === 'success') ? 'notifysuccess' : 'notifyproblem'; - echo $OUTPUT->notification($warning, $class); + echo html_writer::start_tag('div', array('class' => 'no-overflow')); + $form->display(); + echo html_writer::end_tag('div'); + echo $OUTPUT->footer(); } - -echo html_writer::start_tag('div', array('class' => 'no-overflow')); -$form->display(); -echo html_writer::end_tag('div'); -echo $OUTPUT->footer();