Form Wizard Pro - Option to Use Question Aliases in E-mail

This is a forum to suggest new features for mojoPortal. 

This thread is closed to new posts. You must sign in to post in the forums.
11/21/2013 1:18:36 PM
Gravatar
Total Posts 40

Form Wizard Pro - Option to Use Question Aliases in E-mail

Joe/others:

It would be handy to be able to check a box under the settings of an instance of FWP, in order to use the question alias instead of the full question in the generated e-mail that goes out.  Eg:

Question for website: "What is your affiliation with CSU? (student, employee, instructor, professor, neighbor, etc.)"
In the generated e-mail: "Affiliation"

This obviously is pretty minor, but it would be helpful in certain situations where the admins who are receiving the e-mail don't need/want to see the full text of the question.

Thanks, as always, Joe!

11/22/2013 9:38:50 AM
Gravatar
Total Posts 40

Re: Form Wizard Pro - Option to Use Question Aliases in E-mail

This morning, I decided to just create a form submission handler to do the job.  This might be the better approach in any case, so that you don't have to create too many checkboxes for all the various ways people might like to tweak the default behavior of FWP. smiley

Here's the code (to try and use the question alias text in the generated e-mail) in case it saves anyone a few minutes:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using log4net;
using sts.Business;
using mojoPortal.Business;
using sts.FormWizard.Web.UI;
using mojoPortal.Net;
using mojoPortal.Web;
using System.Configuration;

namespace MojoMailerByQuestionAlias
{
    public class MojoFWPMailer : FormSubmissionHandlerProvider
    {
        private string _FromEmailAddress = ConfigurationManager.AppSettings["MojoMailerFromAddress"];
        private List<string> _ToEmailAddresses;
        private static readonly ILog log = LogManager.GetLogger(typeof(MojoFWPMailer));

        public MojoFWPMailer()
        { }

        public override void FormSubmittedEventHandler(object sender, FormSubmissionEventArgs e)
        {
            if (e == null) return;
            if (e.ResponseSet == null) return;

            log.Info("MojoFWPMailerFormSubmissionHandlerProvider called");

            StringBuilder results = new StringBuilder();

            // grab to e-mail addresses
            // I'm grabbing the "to" e-mails from the "Display Aliases for Email Addresses" box, bc I'm not using this field, and it allows content admins to specify the addresses themselves
            // If addresses are stored in the usual place, "Email Address(es) to Receive Form Submissions", mojo will send out its own e-mail in addition to this one
            _ToEmailAddresses = e.Config.EmailAliases;

            // notify admins to whom the email is sent
            results.Append(string.Format("This message has been sent to {0}", string.Join(",", _ToEmailAddresses.ToArray())));

            // assemble questions and answers
            List<WebFormQuestion> questionList = WebFormQuestion.GetByForm(e.ResponseSet.FormGuid);
            List<WebFormResponse> responses = WebFormResponse.GetByResponseSet(e.ResponseSet.Guid);

            results.Append("<p>");

            foreach (WebFormQuestion question in questionList)
            {
                if (question.QuestionTypeId == 8) { continue; } //skip instruction block

                //// this is an e-mail question type 
                //if (question.QuestionTypeId == 10) {
                //    _FromEmailAddress = GetResponse(e.ResponseSet.Guid, question.Guid, responses);
                //} 

                string response = GetResponse(e.ResponseSet.Guid, question.Guid, responses);

                if (!string.IsNullOrEmpty(question.QuestionAlias))
                    results.Append(string.Format("<strong>{0}: </strong>{1}<br />", question.QuestionAlias, response)); // first try question alias
                else
                    results.Append(string.Format("<strong>{0}: </strong>{1}<br />", question.QuestionText, response)); // if it's blank, then just use the question text
            }

            results.Append("</p><hr>");

            // how to get the site user if the user was authenticated
            if (e.User != null)
            {
                results.Append(string.Format("<p>submitted by user {0}</p>", e.User.Name));
            }

            // add detail link
            results.Append(string.Format("<p>{0}</p>", e.DetailUrl));

            //how to send an email with the results and file attachments
            //you could get settings from config settings if you don't want to hard code it
            //you would need references to mojoPortal.Web.dll, mojoPortal.Business.dll, and mojoPortal.Net.dll
            //to use this commented code
            string subject = e.Config.PickListLabel; //can't see how to get subject off-hand, so am using a different field that I wouldn't otherwise use

            foreach (string email in _ToEmailAddresses)
            {
                Email.SendEmail(
                    SiteUtils.GetSmtpSettings(),    // smtp settings
                    _FromEmailAddress,              // from
                    email,                          // to
                    string.Empty,                   // cc
                    string.Empty,                   // bc
                    subject,                        // subject
                    results.ToString(),             // body
                    true,                           // is html email?
                    Email.PriorityNormal);          // priority

                log.Info(results.ToString());
            }

        }

        private string GetResponse(Guid responseSetGuid, Guid questionGuid, List<WebFormResponse> responses)
        {
            foreach (WebFormResponse response in responses)
            {
                if (
                    (response.ResponseSetGuid == responseSetGuid)
                    && (response.QuestionGuid == questionGuid)
                    )
                {
                    return response.Response;
                }
            }

            return string.Empty;
        }
    }
}

You must sign in to post in the forums. This thread is closed to new posts.