diff --git a/js/qsm-admin.js b/js/qsm-admin.js index 437eff05..f5ca5e80 100644 --- a/js/qsm-admin.js +++ b/js/qsm-admin.js @@ -4394,11 +4394,24 @@ var QSM_Quiz_Broadcast_Channel; }(jQuery)); - +function qsm_check_email_sent_option() { + if (jQuery('input[name="send_email"]').prop('checked') === true) { + jQuery('#check_already_sent_email input[name="check_already_sent_email"]').prop( 'disabled', false ); + jQuery('#check_already_sent_email').show(); + } else { + jQuery('#check_already_sent_email input[name="check_already_sent_email"]').prop( 'disabled', true ); + jQuery('#check_already_sent_email').hide(); + } +} /** * QSM - failed submission data table action */ (function ($) { + qsm_check_email_sent_option(); + $(document).on('change', 'input[name="send_email"]', function (event) { + event.preventDefault(); + qsm_check_email_sent_option(); + }); function submit_failed_submission_action_notice( res ) { if ( 'object' !== typeof res || null === res || undefined === res.message ) { return false; diff --git a/php/classes/class-qsm-emails.php b/php/classes/class-qsm-emails.php index c10da820..850781c6 100644 --- a/php/classes/class-qsm-emails.php +++ b/php/classes/class-qsm-emails.php @@ -173,7 +173,7 @@ public static function send_emails( $transient_id ) { if ( 0 == $email_send && ! empty( $default_email_content ) ) { self::send_results_email( $response_data, $default_email_to, $default_email_subject, $default_email_content, $default_email_reply_to, $default_index ); } - + delete_transient( 'qsm_already_sent_emails_' . $response_data['quiz_id'] ); remove_filter( 'wp_mail_content_type', 'mlw_qmn_set_html_content_type' ); } @@ -266,13 +266,25 @@ public static function send_results_email( $response_data, $to, $subject, $conte $attachments = apply_filters( 'qsm_admin_email_attachments', $attachments, $response_data ); } + $quiz_options = maybe_unserialize( $response_data['quiz_settings']['quiz_options'] ); + // Check if email has already been sent to the user for this quiz + if ( isset( $quiz_options['check_already_sent_email'] ) && 1 == $quiz_options['check_already_sent_email'] ) { + $already_sent_emails = get_transient( 'qsm_already_sent_emails_' . $response_data['quiz_id'] ); + } + $already_sent_emails = ! empty( $already_sent_emails ) ? $already_sent_emails : array(); + // Cycle through each to email address and send the email. foreach ( $to_array as $to_email ) { - if ( is_email( $to_email ) ) { + if ( is_email( $to_email ) && ! in_array( $to_email, $already_sent_emails ) ) { $mailResult = wp_mail( $to_email, $subject, $content, $headers, $attachments ); if ( $mailResult ) { do_action( 'qsm_after_sending_email', $response_data, $to_email, $subject ); - }else { + // Add the email to the list of already sent emails for this quiz + if ( isset( $quiz_options['check_already_sent_email'] ) && 1 == $quiz_options['check_already_sent_email'] ) { + $already_sent_emails[] = $to_email; + set_transient( 'qsm_already_sent_emails_' . $response_data['quiz_id'], $already_sent_emails, 3600 ); + } + } else { $mlwQuizMasterNext->log_manager->add( 'There has been an error in wp_mail. Please check SMTP details mail not sending. Error Code: 0001', 0, 'error' ); } } diff --git a/php/classes/class-qsm-install.php b/php/classes/class-qsm-install.php index 869207ef..42ef70f3 100644 --- a/php/classes/class-qsm-install.php +++ b/php/classes/class-qsm-install.php @@ -450,6 +450,16 @@ public function register_default_settings() { ), 'default' => 1, ), + 'check_already_sent_email' => array( + 'type' => 'checkbox', + 'options' => array( + array( + 'label' => __( 'Not send email if already sent', 'quiz-master-next' ), + 'value' => 1, + ), + ), + 'default' => 0, + ), ), 'help' => '', 'option_tab' => 'quiz_submission',