forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_students.js
More file actions
202 lines (181 loc) · 7.61 KB
/
Copy pathmessage_students.js
File metadata and controls
202 lines (181 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Copyright (C) 2011 Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
'i18n!message_students',
'jquery' /* $ */,
'jsx/shared/helpers/numberHelper',
'jquery.instructure_forms' /* formSubmit */,
'jqueryui/dialog',
'jquery.instructure_misc_plugins', /* showIf */
], function (I18n, $, numberHelper) {
var $message_students_dialog = $("#message_students_dialog");
var $sendButton = $message_students_dialog.find(".send_button");
var currentSettings = {};
var checkSendable = function() {
disableSend(
$message_students_dialog.find("#body").val().length == 0 ||
$message_students_dialog.find(".student:not(.blank):visible").length == 0
);
}
/*global messageStudents*/
window.messageStudents = function(settings) {
currentSettings = settings;
$message_students_dialog.find(".message_types").empty();
for(var idx=0, l=settings.options.length; idx < l; idx++) {
var $option = $("<option/>");
var option = settings.options[idx];
$option.val(idx).text(option.text);
$message_students_dialog.find(".message_types").append($option);
}
var title = settings.title,
$li = $message_students_dialog.find("ul li.blank:first"),
$ul = $message_students_dialog.find("ul"),
students_hash = {};
$message_students_dialog.find("ul li:not(.blank)").remove();
for (var i = 0; i < settings.students.length; i++) {
var $student = $li.clone(true).removeClass('blank');
$student.find('.name').text(settings.students[i].name);
$student.find('.score').text(settings.students[i].score);
var remove_text = I18n.t("Remove %{student} from recipients", {student: settings.students[i].name});
var $remove_button = $student.find('.remove-button');
$remove_button.attr('title', remove_text).append($("<span class='screenreader-only'></span>").text(remove_text));
$remove_button.click(function(event) {
event.preventDefault();
// hide the selected student
var $s = $(this).closest('li');
$s.hide('fast', checkSendable);
// focus the next visible student, or the subject field if that was the last one in the list
var $next = $s.nextAll(':visible:first');
if ($next.length) {
$('button', $next).focus();
} else {
$('#message_assignment_recipients #subject').focus();
}
});
$student.data('id', settings.students[i].id);
$student.user_data = settings.students[i];
$ul.append($student.show());
students_hash[settings.students[i].id] = $student;
}
$ul.show();
$message_students_dialog.data('students_hash', students_hash),
$message_students_dialog.find(".asset_title").text(title);
$message_students_dialog.find(".out_of").showIf(settings.points_possible != null);
$message_students_dialog.find(".send_button").text(I18n.t("send_message", "Send Message"));
$message_students_dialog.find(".points_possible").text(I18n.n(settings.points_possible));
$message_students_dialog.find("[name=context_code]").val(settings.context_code);
$message_students_dialog.find("textarea").val("");
$message_students_dialog.find("select")[0].selectedIndex = 0;
$message_students_dialog.find("select").change();
$message_students_dialog.dialog({
width: 600,
modal: true
}).dialog('open').dialog('option', 'title', I18n.t("message_student", "Message Students for %{course_name}", {course_name: title}));
};
$(document).ready(function() {
$message_students_dialog.find("button").click(function(e) {
var btn = $(e.target);
if (btn.hasClass("disabled")) {
e.preventDefault();
e.stopPropagation();
}
});
$("#message_assignment_recipients").formSubmit({
processData: function(data) {
var ids = [];
$(this).find(".student:visible").each(function() {
ids.push($(this).data('id'));
});
if(ids.length == 0) { return false; }
data['recipients'] = ids.join(",");
return data;
},
beforeSubmit: function(data) {
disableButtons(true)
$(this).find(".send_button").text(I18n.t("Sending Message..."));
},
success: function(data) {
$.flashMessage(I18n.t("Message sent!"));
disableButtons(false);
$(this).find(".send_button").text(I18n.t("Send Message"));
$("#message_students_dialog").dialog('close');
},
error: function(data) {
disableButtons(false);
$(this).find(".send_button").text(I18n.t("Sending Message Failed, please try again"));
}
});
var showStudentsMessageSentTo = function() {
var idx = parseInt($message_students_dialog.find("select").val(), 10) || 0;
var option = currentSettings.options[idx];
var students_hash = $message_students_dialog.data('students_hash');
var cutoff = numberHelper.parse($message_students_dialog.find('.cutoff_score').val());
if (isNaN(cutoff)) {
cutoff = null;
}
var student_ids = null;
var students_list = [];
for(var idx in students_hash) {
students_list.push(students_hash[idx]);
}
if(students_hash) {
if(option && option.callback) {
student_ids = option.callback.call(window.messageStudents, cutoff, students_list);
} else if(currentSettings.callback) {
student_ids = currentSettings.callback.call(window.messageStudents, option.text, cutoff, students_list);
}
}
student_ids = student_ids || [];
if (currentSettings.subjectCallback) {
$message_students_dialog.find("[name=subject]").val(currentSettings.subjectCallback(option.text, cutoff));
}
$message_students_dialog.find(".cutoff_holder").showIf(option.cutoff);
$message_students_dialog.find(".student_list").toggleClass('show_score', !!(option.cutoff || option.score));
disableButtons(student_ids.length === 0);
var student_ids_hash = {};
for(var idx in student_ids) {
if (student_ids.hasOwnProperty(idx)) {
student_ids_hash[parseInt(student_ids[idx], 10) || 0] = true;
}
}
for(var idx in students_hash) {
students_hash[idx].showIf(student_ids_hash[idx]);
}
};
var closeDialog = function() {
$message_students_dialog.dialog('close');
};
$message_students_dialog.find(".cancel_button").click(closeDialog);
$message_students_dialog.find("select").change(showStudentsMessageSentTo).change(checkSendable);
$message_students_dialog.find(".cutoff_score").bind('change blur keyup', showStudentsMessageSentTo)
.bind('change blur keyup', checkSendable);
$message_students_dialog.find("#body").bind('change blur keyup', checkSendable);
});
function disableButtons(disabled, buttons) {
if (buttons == null) {
buttons = $message_students_dialog.find("button");
}
buttons
.toggleClass("disabled", disabled)
.attr("aria-disabled", disabled);
}
function disableSend(disabled) {
disableButtons(disabled, $sendButton);
}
return messageStudents;
});