@@ -993,45 +993,109 @@ function showStatus($logmsg) {
993
993
*/
994
994
function access_affinitygroup_cron() {
995
995
$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)) {
998
1004
$email_recruit = TRUE;
1005
+ \Drupal::state()->set('access_affinitygroup.last_recruit_email', $current_time);
999
1006
}
1000
1007
1001
1008
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
1007
1022
$query = \Drupal::entityQuery('node')
1008
1023
->condition('type', 'mentorship_engagement')
1009
- ->condition('field_me_state', $term_id )
1024
+ ->condition('field_me_state', $term_ids, 'IN' )
1010
1025
->condition('field_ccmnet_approved', 1)
1011
- ->condition('changed ', strtotime('-1 day'), '>' )
1026
+ ->condition('status ', 1 )
1012
1027
->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) {
1017
1044
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
1018
1045
$node_title = $node->getTitle();
1019
1046
$options = ['absolute' => FALSE];
1020
1047
$link_build = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
1021
1048
$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>";
1023
1064
}
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');
1028
1072
$node = \Drupal::entityTypeManager()->getStorage('node')->load(5951);
1029
1073
$ccListIds = [$node->get('field_list_id')->getValue()[0]['value']];
1030
1074
$agNames = [$node->getTitle()];
1031
1075
$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 ';
1033
1077
1034
1078
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)]);
1035
1099
}
1036
1100
}
1037
1101
@@ -1168,7 +1232,6 @@ function isCCEnabled() {
1168
1232
* If true, we use community template, otherwise use the access support template.
1169
1233
*/
1170
1234
function emailToAffinityGroups($emailText, $emailTitle, $pubDate, $agNames, $newsUrl, $ccListIds, $communityTemplate, $logoUrl) {
1171
-
1172
1235
// For CC, must be unique name for campaign.
1173
1236
$campaignName = uniqid('Access Announcement: ' . $agNames[0] . '- ' . $pubDate . ' ', FALSE);
1174
1237
0 commit comments