Implements keyword sub feature for bulk emails
This commit pulls in changes from #4487 that implements keyword substitution for bulk emails. With these changes, an instructor can include keywords in their bulk emails which will be automatically substituted with the corresponding value for the recepient of the email. Keywords are of the form %%keyword%%, and the keywords implemented in this commit include: %%USER_ID%% => anonymous_user_id %%USER_FULLNAME%% => user profile name %%COURSE_DISPLAY_NAME%% => display name of the course %%COURSE_END_DATE%% => end date of the course Client-side validations have also been implemented to ensure that only emails with well-formed keywords can be sent. The architecture is designed such that adding in new keywords in the future would be relatively straight-forward.
This commit is contained in:
@@ -13,6 +13,7 @@ PendingInstructorTasks = -> window.InstructorDashboard.util.PendingInstructorTas
|
||||
create_task_list_table = -> window.InstructorDashboard.util.create_task_list_table.apply this, arguments
|
||||
create_email_content_table = -> window.InstructorDashboard.util.create_email_content_table.apply this, arguments
|
||||
create_email_message_views = -> window.InstructorDashboard.util.create_email_message_views.apply this, arguments
|
||||
KeywordValidator = -> window.InstructorDashboard.util.KeywordValidator
|
||||
|
||||
class SendEmail
|
||||
constructor: (@$container) ->
|
||||
@@ -42,6 +43,14 @@ class SendEmail
|
||||
alert gettext("Your message cannot be blank.")
|
||||
|
||||
else
|
||||
# Validation for keyword substitution
|
||||
validation = KeywordValidator().validate_string @$emailEditor.save()['data']
|
||||
if not validation.is_valid
|
||||
message = gettext("There are invalid keywords in your email. Please check the following keywords and try again:")
|
||||
message += "\n" + validation.invalid_keywords.join('\n')
|
||||
alert message
|
||||
return
|
||||
|
||||
success_message = gettext("Your email was successfully queued for sending.")
|
||||
send_to = @$send_to.val().toLowerCase()
|
||||
if send_to == "myself"
|
||||
|
||||
@@ -300,6 +300,31 @@ class PendingInstructorTasks
|
||||
error: std_ajax_err => console.error "Error finding pending instructor tasks to display"
|
||||
### /Pending Instructor Tasks Section ####
|
||||
|
||||
class KeywordValidator
|
||||
|
||||
@keyword_regex = /%%+[^%]+%%/g
|
||||
@keywords = ['%%USER_ID%%', '%%USER_FULLNAME%%', '%%COURSE_DISPLAY_NAME%%', '%%COURSE_END_DATE%%']
|
||||
|
||||
@validate_string: (string) =>
|
||||
regex_match = string.match(@keyword_regex)
|
||||
found_keywords = if regex_match == null then [] else regex_match
|
||||
invalid_keywords = []
|
||||
is_valid = true
|
||||
keywords = @keywords
|
||||
|
||||
for found_keyword in found_keywords
|
||||
do (found_keyword) ->
|
||||
if found_keyword not in keywords
|
||||
invalid_keywords.push found_keyword
|
||||
|
||||
if invalid_keywords.length != 0
|
||||
is_valid = false
|
||||
|
||||
return {
|
||||
is_valid: is_valid,
|
||||
invalid_keywords: invalid_keywords
|
||||
}
|
||||
|
||||
# export for use
|
||||
# create parent namespaces if they do not already exist.
|
||||
# abort if underscore can not be found.
|
||||
@@ -314,3 +339,4 @@ if _?
|
||||
create_email_content_table: create_email_content_table
|
||||
create_email_message_views: create_email_message_views
|
||||
PendingInstructorTasks: PendingInstructorTasks
|
||||
KeywordValidator: KeywordValidator
|
||||
|
||||
Reference in New Issue
Block a user