Skip to content

Commit 89adee1

Browse files
authored
Merge pull request #330 from necyberteam/d8-2509
D8 2509
2 parents 6d88ce1 + d233895 commit 89adee1

File tree

3 files changed

+137
-20
lines changed

3 files changed

+137
-20
lines changed

modules/access_affinitygroup/access_affinitygroup.install

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,41 @@ function access_affinitygroup_update_10000() {
1717
\Drupal::state()->set('access_affinitygroup.refresh_token', null);
1818
}
1919
}
20+
21+
/**
22+
* Initialize notified_mentorships state with existing recruiting mentorships (approved and published).
23+
*/
24+
function access_affinitygroup_update_10002() {
25+
// Get 'state' taxonomy term IDs for recruiting states.
26+
$recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'recruiting']);
27+
$in_progress_recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'In Progress and Recruiting']);
28+
29+
$term_ids = [];
30+
if ($recruiting_term) {
31+
$term_ids[] = array_keys($recruiting_term)[0];
32+
}
33+
if ($in_progress_recruiting_term) {
34+
$term_ids[] = array_keys($in_progress_recruiting_term)[0];
35+
}
36+
37+
if (!empty($term_ids)) {
38+
// Get all mentorship nodes currently in recruiting states that are approved and published
39+
$query = \Drupal::entityQuery('node')
40+
->condition('type', 'mentorship_engagement')
41+
->condition('field_me_state', $term_ids, 'IN')
42+
->condition('field_ccmnet_approved', 1)
43+
->condition('status', 1)
44+
->accessCheck(FALSE);
45+
$existing_recruiting_nids = $query->execute();
46+
47+
// Set these as already notified to prevent sending emails for existing mentorships
48+
\Drupal::state()->set('access_affinitygroup.notified_mentorships', array_values($existing_recruiting_nids));
49+
50+
$count = count($existing_recruiting_nids);
51+
\Drupal::logger('access_affinitygroup')->info('Update 10002: Marked @count existing recruiting mentorships (approved and published) as already notified to prevent duplicate emails.', ['@count' => $count]);
52+
53+
return "Marked $count existing recruiting mentorships (approved and published) as already notified.";
54+
}
55+
56+
return 'No recruiting mentorships found to mark as notified.';
57+
}

modules/access_affinitygroup/access_affinitygroup.module

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -993,45 +993,109 @@ function showStatus($logmsg) {
993993
*/
994994
function access_affinitygroup_cron() {
995995
$email_recruit = FALSE;
996-
// Adding for logging purposes to check server time.
997-
if ((date('G', time()) == 10) && (date('i', time()) < 9)) {
996+
// Check if we should send recruitment emails (once per day)
997+
$last_recruit_run = \Drupal::state()->get('access_affinitygroup.last_recruit_email', 0);
998+
$current_time = time();
999+
$hours_since_last = ($current_time - $last_recruit_run) / 3600;
1000+
1001+
// Send if it's been more than 23 hours since last run (or force for testing on non-live)
1002+
$env = getenv('PANTHEON_ENVIRONMENT');
1003+
if ($hours_since_last > 23 || ($env !== 'live' && $hours_since_last > 0.1)) {
9981004
$email_recruit = TRUE;
1005+
\Drupal::state()->set('access_affinitygroup.last_recruit_email', $current_time);
9991006
}
10001007

10011008
if ($email_recruit) {
1002-
// Get 'state' taxonomy with term name 'recruiting' tid.
1003-
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'recruiting']);
1004-
$term_id = array_keys($term)[0];
1005-
// EntityQuery to get all mentorship nodes updated in the last 24 hours with
1006-
// recruiting.
1009+
// Get 'state' taxonomy term IDs for recruiting states.
1010+
$recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'recruiting']);
1011+
$in_progress_recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'In Progress and Recruiting']);
1012+
1013+
$term_ids = [];
1014+
if ($recruiting_term) {
1015+
$term_ids[] = array_keys($recruiting_term)[0];
1016+
}
1017+
if ($in_progress_recruiting_term) {
1018+
$term_ids[] = array_keys($in_progress_recruiting_term)[0];
1019+
}
1020+
1021+
// Get all mentorship nodes currently in recruiting states that are approved and published
10071022
$query = \Drupal::entityQuery('node')
10081023
->condition('type', 'mentorship_engagement')
1009-
->condition('field_me_state', $term_id)
1024+
->condition('field_me_state', $term_ids, 'IN')
10101025
->condition('field_ccmnet_approved', 1)
1011-
->condition('changed', strtotime('-1 day'), '>')
1026+
->condition('status', 1)
10121027
->accessCheck(FALSE);
1013-
$nids = $query->execute();
1014-
if ($nids) {
1015-
$body = t("New mentorship engagements have been requested on the CCMNet portal") . ": <ul>";
1016-
foreach ($nids as $nid) {
1028+
$all_recruiting_nids = $query->execute();
1029+
1030+
// Get list of mentorships we've already notified about
1031+
$notified_mentorships = \Drupal::state()->get('access_affinitygroup.notified_mentorships', []);
1032+
1033+
// Only include mentorships we haven't notified about yet
1034+
$new_nids = array_diff($all_recruiting_nids, $notified_mentorships);
1035+
1036+
if ($new_nids) {
1037+
\Drupal::logger('access_affinitygroup')->info('Found @count NEW mentorship engagements in recruiting states for email notification', ['@count' => count($new_nids)]);
1038+
1039+
// Sort mentorships by whether they're looking for mentors or mentees
1040+
$seeking_mentors = [];
1041+
$seeking_mentees = [];
1042+
1043+
foreach ($new_nids as $nid) {
10171044
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
10181045
$node_title = $node->getTitle();
10191046
$options = ['absolute' => FALSE];
10201047
$link_build = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
10211048
$url = 'https://ccmnet.org' . $link_build->tostring(TRUE)->getGeneratedUrl();
1022-
$body .= '<li><a href="' . $url . '">' . $node_title . '</a></li>';
1049+
$link_item = '<li><a href="' . $url . '">' . $node_title . '</a></li>';
1050+
1051+
// Check what they're looking for
1052+
$looking_for = $node->get('field_me_looking_for')->getValue();
1053+
if (!empty($looking_for) && $looking_for[0]['value'] === 'mentor') {
1054+
$seeking_mentors[] = $link_item;
1055+
} else {
1056+
$seeking_mentees[] = $link_item;
1057+
}
1058+
}
1059+
1060+
$body = "";
1061+
1062+
if (!empty($seeking_mentors)) {
1063+
$body .= "<h3>Looking for Mentors:</h3><ul>" . implode('', $seeking_mentors) . "</ul>";
10231064
}
1024-
$body .= "</ul>";
1025-
// If there are any mentorship engagements in recruiting state, send email.
1026-
$title = t('New Mentorship Engagements set to recruiting');
1027-
// Load 'CCMNet Mentors' AG node.
1065+
1066+
if (!empty($seeking_mentees)) {
1067+
$body .= "<h3>Looking for Mentees:</h3><ul>" . implode('', $seeking_mentees) . "</ul>";
1068+
}
1069+
1070+
// Send email notification
1071+
$title = t('New mentorship engagements are available on the CCMNet portal');
10281072
$node = \Drupal::entityTypeManager()->getStorage('node')->load(5951);
10291073
$ccListIds = [$node->get('field_list_id')->getValue()[0]['value']];
10301074
$agNames = [$node->getTitle()];
10311075
$pubDate = date('m/d/Y');
1032-
$email_link = 'https://ccmnet.org/node/5951';
1076+
$email_link = 'https://ccmnet.org/mentorships?f%5B0%5D=state%3A827&f%5B1%5D=state%3A886';
10331077

10341078
emailToAffinityGroups($body, "$title", $pubDate, $agNames, $email_link, $ccListIds, 1, NULL);
1079+
\Drupal::logger('access_affinitygroup')->info('Sent CCMNet recruitment email to affinity group: @group for @count new mentorships', ['@group' => implode(', ', $agNames), '@count' => count($new_nids)]);
1080+
1081+
// Update our list of notified mentorships (add the new ones)
1082+
$updated_notified = array_merge($notified_mentorships, $new_nids);
1083+
\Drupal::state()->set('access_affinitygroup.notified_mentorships', $updated_notified);
1084+
}
1085+
else {
1086+
\Drupal::logger('access_affinitygroup')->info('No NEW mentorship engagements found in recruiting states for email notification (total recruiting: @total, already notified: @notified)', [
1087+
'@total' => count($all_recruiting_nids),
1088+
'@notified' => count($notified_mentorships)
1089+
]);
1090+
}
1091+
1092+
// Clean up old notified mentorships that are no longer in recruiting states
1093+
// This prevents the list from growing indefinitely
1094+
$current_notified = \Drupal::state()->get('access_affinitygroup.notified_mentorships', []);
1095+
$still_valid_notified = array_intersect($current_notified, $all_recruiting_nids);
1096+
if (count($still_valid_notified) != count($current_notified)) {
1097+
\Drupal::state()->set('access_affinitygroup.notified_mentorships', $still_valid_notified);
1098+
\Drupal::logger('access_affinitygroup')->info('Cleaned up notified mentorships list: removed @removed items', ['@removed' => count($current_notified) - count($still_valid_notified)]);
10351099
}
10361100
}
10371101

@@ -1168,7 +1232,6 @@ function isCCEnabled() {
11681232
* If true, we use community template, otherwise use the access support template.
11691233
*/
11701234
function emailToAffinityGroups($emailText, $emailTitle, $pubDate, $agNames, $newsUrl, $ccListIds, $communityTemplate, $logoUrl) {
1171-
11721235
// For CC, must be unique name for campaign.
11731236
$campaignName = uniqid('Access Announcement: ' . $agNames[0] . '- ' . $pubDate . ' ', FALSE);
11741237

modules/ccmnet/src/Plugin/EmailBuilder/MentorshipEmailBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,21 @@
2929
class MentorshipEmailBuilder extends EmailBuilderBase {
3030
public function build(EmailInterface $email) {
3131
$email->setFrom('[email protected]');
32+
33+
// Handle HTML content from variables (like 'extra' containing HTML lists)
34+
$variables = $email->getVariables();
35+
if (isset($variables['extra']) && is_string($variables['extra'])) {
36+
// If 'extra' contains HTML, set it as the body
37+
$email->setBody($variables['extra']);
38+
} else {
39+
// Set HTML content type for proper rendering
40+
$body = $email->getBody();
41+
if (is_array($body)) {
42+
$body = implode("\n", $body);
43+
}
44+
if (!empty($body)) {
45+
$email->setBody($body);
46+
}
47+
}
3248
}
3349
}

0 commit comments

Comments
 (0)