Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-50853 Gradebook Webcomponents more messaging fixes #13253

Merged
merged 8 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -359,40 +359,37 @@ public ActionReturn listMessageRecipients(final EntityView view, final Map<Strin

@EntityCustomAction(action = "messageStudents", viewKey = EntityView.VIEW_NEW)
public ActionReturn messageStudents(final EntityView view, final Map<String, Object> params) {
int success = 0;

Set<String> recipients = getRecipients(params);

if (!recipients.isEmpty()) {
recipients.add(getCurrentUserId());
List<User> users = userDirectoryService.getUsers(recipients);

List<User> users = recipients.stream().map(s -> {
try {
return userDirectoryService.getUser(s);
} catch (UserNotDefinedException unde) {
return null;
}
}).collect(Collectors.toList());

if (users.contains(null)) {
String errorMsg = "At least one of the students to message is null. No messsages sent.";
log.warn(errorMsg);
throw new EntityException(errorMsg, "", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} else {
if (!users.isEmpty()) {
String from = serverConfigurationService.getSmtpFrom();
List<String> headers = new ArrayList<>();
String subject = (String) params.get("subject");
headers.add("Subject: " + subject);
headers.add("From: " + "\"" + serverConfigurationService.getString("ui.service", "Sakai") + "\" <" + from + ">");
users.forEach(u -> emailService.send(from, u.getEmail(), subject, (String) params.get("body"), null, null, headers));
Map<String, Object> data = new HashMap<>();
data.put("result", "SUCCESS");
return new ActionReturn(data);

for (User u : users) {
if (u != null && u.getEmail() != null && !u.getEmail().isEmpty()) {
try {
emailService.send(from, u.getEmail(), subject, (String) params.get("body"), null, null, headers);
// Don't scare the instructor into thinking that the email went to too many people
if (!u.getId().equals(getCurrentUserId())) {
success++;
}
} catch (Exception e) {
log.error("Error sending email to {}", u.getEmail(), e);
}
}
}
}
} else {
Map<String, Object> data = new HashMap<>();
data.put("result", "SUCCESS");
return new ActionReturn(data);
}

return new ActionReturn(Map.of("result", "SUCCESS", "num_sent", success));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ more_options_tooltip=Show or hide extra options
recipients=Recipients
no_recipients=No recipients
validation_error=You need to supply a subject and body!
send_another=Send Another Message
messages_sent_detail=Successfully sent messages to {numSent} recipients.
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { SakaiElement } from "@sakai-ui/sakai-element";
import { html, nothing } from "lit";
import { ifDefined } from "lit/directives/if-defined.js";
import "@sakai-ui/sakai-group-picker";
import "@sakai-ui/sakai-group-picker/sakai-group-picker.js";
import "@spectrum-web-components/progress-bar/sp-progress-bar.js";

export class SakaiSubmissionMessager extends SakaiElement {

static properties = {

assignmentId: { attribute: "assignment-id", type: String },
groups: { type: Array },
title: String,
action: String,
subject: String,
body: String,
error: Boolean,
success: Boolean,
groupId: String,
validationError: String,
recipientsToCheck: Array,
sending: Boolean,
recipientsRequested: { type: Boolean },
title: { type: String },
action: { state: true },
subject: { state: true },
body: { state: true },
error: { state: true },
success: { state: true },
groupId: { state: true },
validationError: { state: true },
recipients: { state: true },
sending: { state: true },
recipientsRequested: { state: true },
numSent: { state: true },
};

constructor() {

super();

this.groups = [];
this.recipientsToCheck = [];
this.recipientsRequested = false;
this._i18n = {};
this.reset();
this.loadTranslations("submission-messager").then(t => this._i18n = t);
}
Expand All @@ -40,9 +36,22 @@ export class SakaiSubmissionMessager extends SakaiElement {
}

render() {
if (this.success) {
return html`
<div class="submission-messager">
<div class="alert alert-success">
<div class="fs-5 mb-2">${this._i18n.success}</div>
<p>${this.tr("messages_sent_detail", { numSent: this.numSent })}</p>
<button type="button" class="btn btn-primary" @click=${this.reset}>
${this._i18n.send_another}
</button>
</div>
</div>
`;
}

return html`
<div id="submission-messager-${this.assignmentId}" class="submission-messager">
<div class="submission-messager">
${this.validationError ? html`
<div class="alert alert-danger" role="alert">
${this.validationError}
Expand Down Expand Up @@ -117,23 +126,23 @@ export class SakaiSubmissionMessager extends SakaiElement {
</button>

${this.recipientsRequested ? html`
${this.recipientsToCheck.length > 0 ? html`
${this.recipients?.length > 0 ? html`
<div class="card mb-2">
<div class="card-header py-1 d-flex justify-content-between align-items-center">
<span class="small">${this._i18n.recipients}</span>
<span class="badge bg-secondary">${this.recipientsToCheck.length}</span>
<span class="badge bg-secondary">${this.recipients.length}</span>
</div>
<div class="card-body p-0" style="max-height: 100px; overflow-y: auto;">
<div class="list-group list-group-flush small">
${this.recipientsToCheck.map(r => html`
${this.recipients.map(r => html`
<div class="list-group-item py-1">${r.displayName}</div>
`)}
</div>
</div>
</div>
` : html`
<div class="alert alert-warning py-1 small mb-2">
${this._i18n.no_recipients || "No recipients found"}
${this._i18n.no_recipients}
</div>
`}
` : nothing}
Expand All @@ -150,9 +159,6 @@ export class SakaiSubmissionMessager extends SakaiElement {
<sp-progress-bar aria-label="Sending message" indeterminate></sp-progress-bar>
</div>
` : nothing}
${this.success ? html`
<div class="alert alert-success mb-0 py-2">${this._i18n.success}</div>
` : nothing}
${this.error ? html`
<div class="alert alert-danger mb-0 py-2">${this._i18n.error}</div>
` : nothing}
Expand All @@ -162,25 +168,25 @@ export class SakaiSubmissionMessager extends SakaiElement {
}

actionChanged(e) {
this.recipientsToCheck = [];
this.recipients = [];
this.recipientsRequested = false;
this.action = e.target.value;
}

minScoreChanged(e) {
this.recipientsToCheck = [];
this.recipients = [];
this.recipientsRequested = false;
this.minScore = e.target.value;
}

maxScoreChanged(e) {
this.recipientsToCheck = [];
this.recipients = [];
this.recipientsRequested = false;
this.maxScore = e.target.value;
}

groupSelected(e) {
this.recipientsToCheck = [];
this.recipients = [];
this.recipientsRequested = false;
this.groupId = e.detail.value[0];
}
Expand All @@ -191,13 +197,14 @@ export class SakaiSubmissionMessager extends SakaiElement {
this.action = "1";
this.subject = "";
this.body = "";
this.success = false;
this.error = false;
this.recipientsToCheck = [];
this.recipients = [];
this.minScore = "";
this.maxScore = "";
this.validationError = "";
this.recipientsRequested = false;
this.numSent = 0;
this.success = false;
}

getFormData() {
Expand All @@ -220,15 +227,13 @@ export class SakaiSubmissionMessager extends SakaiElement {

fetch("/direct/gbng/listMessageRecipients.json", { method: "POST", cache: "no-cache", credentials: "same-origin", body: formData })
.then(r => r.json())
.then(data => {
this.recipientsToCheck = data;
});
.then(data => this.recipients = data);
}

sendMessage() {

if (!this.subject || !this.body) {
this.validationError = this._i18n.validation_error || "You need to supply a subject and body!";
this.validationError = this._i18n.validation_error;
return;
}

Expand All @@ -244,17 +249,13 @@ export class SakaiSubmissionMessager extends SakaiElement {
return r.json();
}
this.error = true;

})
.then(data => {

if (data.result) {
this.success = true;
if (data.result === "SUCCESS") {
this.numSent = data.num_sent;
this.sending = false;
window.setTimeout(() => {
this.success = false;
this.reset();
}, 1500);
this.success = true;
}
});
}
Expand Down
Loading