From 60fcff5ac534f11c21f38ce696275521d9523855 Mon Sep 17 00:00:00 2001 From: orakili Date: Fri, 27 Oct 2023 05:17:04 +0000 Subject: [PATCH 1/2] chore: mark report sources as inactive or archive Refs: RW-798 --- .../reliefweb_entities/src/DocumentTrait.php | 33 ++++++++++ .../reliefweb_entities/src/Entity/Report.php | 3 + .../src/OpportunityDocumentTrait.php | 33 ---------- .../Commands/ReliefWebModerationCommands.php | 64 ++++++++++++------- 4 files changed, 76 insertions(+), 57 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/DocumentTrait.php b/html/modules/custom/reliefweb_entities/src/DocumentTrait.php index 6bb8da3f5..0af0b30ab 100644 --- a/html/modules/custom/reliefweb_entities/src/DocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/DocumentTrait.php @@ -3,9 +3,11 @@ namespace Drupal\reliefweb_entities; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; +use Drupal\reliefweb_entities\Entity\Source; use Drupal\reliefweb_moderation\EntityModeratedInterface; use Drupal\reliefweb_rivers\RiverServiceBase; use Drupal\reliefweb_utility\Helpers\HtmlSummarizer; @@ -358,4 +360,35 @@ public function createDate($date) { return new \DateTime($date, new \DateTimeZone('UTC')); } + /** + * Update the status of the sources when publishing an opportunity. + */ + protected function updateSourceModerationStatus() { + if (!$this->hasField('field_source') || $this->field_source->isEmpty()) { + return; + } + + if (!($this instanceof EntityPublishedInterface) || !$this->isPublished()) { + return; + } + + // Make the inactive or archive sources active when the node is published. + foreach ($this->field_source as $item) { + $source = $item->entity; + if (empty($source) || !($source instanceof Source)) { + continue; + } + + if (in_array($source->getModerationStatus(), ['inactive', 'archive'])) { + $source->notifications_content_disable = TRUE; + $source->setModerationStatus('active'); + $source->setNewRevision(TRUE); + $source->setRevisionLogMessage('Automatic status update due to publication of node ' . $this->id()); + $source->setRevisionUserId(2); + $source->setRevisionCreationTime(time()); + $source->save(); + } + } + } + } diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 0f24e4782..5e115009e 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -257,6 +257,9 @@ public function preSave(EntityStorageInterface $storage) { public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); + // Make the sources active. + $this->updateSourceModerationStatus(); + $this->sendPublicationNotification(); } diff --git a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php index d946ca5b3..44ed15aa7 100644 --- a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php @@ -2,7 +2,6 @@ namespace Drupal\reliefweb_entities; -use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Entity\RevisionLogInterface; use Drupal\reliefweb_entities\Entity\Source; use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; @@ -169,36 +168,4 @@ protected function updateDateWhenPublished() { } } - /** - * Update the status of the sources when publishing an opportunity. - */ - protected function updateSourceModerationStatus() { - if (!$this->hasField('field_source') || $this->field_source->isEmpty()) { - return; - } - - if (!($this instanceof EntityPublishedInterface) || !$this->isPublished()) { - return; - } - - // Make the inactive or archive sources active when an apportunity is - // published. - foreach ($this->field_source as $item) { - $source = $item->entity; - if (empty($source) || !($source instanceof Source)) { - continue; - } - - if (in_array($source->getModerationStatus(), ['inactive', 'archive'])) { - $source->notifications_content_disable = TRUE; - $source->setModerationStatus('active'); - $source->setNewRevision(TRUE); - $source->setRevisionLogMessage('Automatic status update due to publication of node ' . $this->id()); - $source->setRevisionUserId(2); - $source->setRevisionCreationTime(time()); - $source->save(); - } - } - } - } diff --git a/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php b/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php index e37242180..2b9cacc02 100644 --- a/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php +++ b/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php @@ -80,40 +80,56 @@ public function updateInactiveSources(array $options = [ return TRUE; } + // Retrieve sources with status to update. $sources = $this->database->query(" - SELECT query.id, query.status + SELECT + query.id AS id, + query.current_status, + query.status AS status FROM ( - SELECT subquery.id, - CASE - WHEN subquery.published > 0 THEN 'active' - WHEN subquery.recent > 0 THEN 'inactive' - ELSE 'archive' - END AS status + SELECT + subquery.id, + CASE + WHEN subquery.active > 0 THEN 'active' + WHEN subquery.inactive > 0 THEN 'inactive' + ELSE 'archive' + END AS status, + subquery.current_status AS current_status FROM ( - SELECT fs.field_source_target_id AS id, - SUM(CASE - WHEN n.type = 'job' AND UNIX_TIMESTAMP(fjcd.field_job_closing_date_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 - WHEN n.type = 'training' AND UNIX_TIMESTAMP(frd.field_registration_deadline_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 - ELSE 0 - END) AS recent, - SUM(IF(n.moderation_status = 'published', 1, 0)) AS published - FROM node__field_source AS fs - INNER JOIN node_field_data AS n + SELECT + tfd.tid AS id, + tfd.moderation_status AS current_status, + SUM(CASE + WHEN n.type IN ('job', 'training') AND n.status = 1 THEN 1 + WHEN n.type = 'job' AND UNIX_TIMESTAMP(fjcd.field_job_closing_date_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH) THEN 1 + WHEN n.type = 'training' AND UNIX_TIMESTAMP(frd.field_registration_deadline_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH) THEN 1 + WHEN n.type = 'report' AND n.created > UNIX_TIMESTAMP(NOW() - INTERVAL 3 YEAR) THEN 1 + ELSE 0 + END) AS active, + SUM(CASE + WHEN n.type = 'job' AND UNIX_TIMESTAMP(fjcd.field_job_closing_date_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 + WHEN n.type = 'training' AND UNIX_TIMESTAMP(frd.field_registration_deadline_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 + WHEN n.type = 'report' AND n.status = 1 THEN 1 + ELSE 0 + END) AS inactive + FROM taxonomy_term_field_data AS tfd + LEFT JOIN node__field_source AS fs + ON fs.field_source_target_id = tfd.tid + LEFT JOIN node_field_data AS n ON n.nid = fs.entity_id - AND n.type IN ('job', 'training') + AND n.type IN ('job', 'training', 'report') LEFT JOIN node__field_job_closing_date AS fjcd ON fjcd.entity_id = fs.entity_id LEFT JOIN node__field_registration_deadline AS frd ON frd.entity_id = fs.entity_id - WHERE fs.field_source_target_id NOT IN (SELECT field_source_target_id FROM node__field_source WHERE bundle = 'report') - GROUP BY fs.field_source_target_id + WHERE + tfd.vid = 'source' AND + tfd.moderation_status IN ('active', 'inactive', 'archive') AND + tfd.created < UNIX_TIMESTAMP(NOW() - INTERVAL 2 WEEK) + GROUP BY tfd.tid ) AS subquery ) AS query - INNER JOIN taxonomy_term_field_data AS tfd - ON tfd.tid = query.id - AND tfd.vid = 'source' - AND tfd.moderation_status NOT IN ('duplicate', 'blocked', query.status) - WHERE query.status <> 'active' + WHERE query.status IN ('inactive', 'archive') AND query.status <> query.current_status ")?->fetchAllKeyed(0, 1) ?? []; if (empty($sources)) { From e6d7e357263ed3d5826da1776781358acf27e224 Mon Sep 17 00:00:00 2001 From: orakili Date: Thu, 2 Nov 2023 00:16:19 +0000 Subject: [PATCH 2/2] fix: also handle ongoing training cases to decide status of organization Refs: RW-798 --- .../Commands/ReliefWebModerationCommands.php | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php b/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php index 2b9cacc02..43814b265 100644 --- a/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php +++ b/html/modules/custom/reliefweb_moderation/src/Commands/ReliefWebModerationCommands.php @@ -84,7 +84,6 @@ public function updateInactiveSources(array $options = [ $sources = $this->database->query(" SELECT query.id AS id, - query.current_status, query.status AS status FROM ( SELECT @@ -100,16 +99,41 @@ public function updateInactiveSources(array $options = [ tfd.tid AS id, tfd.moderation_status AS current_status, SUM(CASE + # Published jobs or training. WHEN n.type IN ('job', 'training') AND n.status = 1 THEN 1 + # Jobs that were open during the past 2 months. WHEN n.type = 'job' AND UNIX_TIMESTAMP(fjcd.field_job_closing_date_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH) THEN 1 + # Training that were open during the past 2 months. WHEN n.type = 'training' AND UNIX_TIMESTAMP(frd.field_registration_deadline_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH) THEN 1 + # Reports created during the past 3 years. WHEN n.type = 'report' AND n.created > UNIX_TIMESTAMP(NOW() - INTERVAL 3 YEAR) THEN 1 + # Ongoing training that were published during the past 2 months. + WHEN n.type = 'training' AND frd.field_registration_deadline_value IS NULL AND ( + SELECT MAX(tnr.revision_timestamp) + FROM node_field_revision AS tnfr + INNER JOIN node_revision AS tnr + ON tnr.vid = tnfr.vid + WHERE tnfr.moderation_status = 'published' + AND tnfr.nid = n.nid + ) > UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH) THEN 1 ELSE 0 END) AS active, SUM(CASE + # Jobs that were open during the past 1 year. WHEN n.type = 'job' AND UNIX_TIMESTAMP(fjcd.field_job_closing_date_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 + # Training that were open during the past 1 year. WHEN n.type = 'training' AND UNIX_TIMESTAMP(frd.field_registration_deadline_value) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 + # Published reports. WHEN n.type = 'report' AND n.status = 1 THEN 1 + # Ongoing training that were published during the past year. + WHEN n.type = 'training' AND frd.field_registration_deadline_value IS NULL AND ( + SELECT MAX(tnr.revision_timestamp) + FROM node_field_revision AS tnfr + INNER JOIN node_revision AS tnr + ON tnr.vid = tnfr.vid + WHERE tnfr.moderation_status = 'published' + AND tnfr.nid = n.nid + ) > UNIX_TIMESTAMP(NOW() - INTERVAL 1 YEAR) THEN 1 ELSE 0 END) AS inactive FROM taxonomy_term_field_data AS tfd @@ -122,10 +146,11 @@ public function updateInactiveSources(array $options = [ ON fjcd.entity_id = fs.entity_id LEFT JOIN node__field_registration_deadline AS frd ON frd.entity_id = fs.entity_id - WHERE - tfd.vid = 'source' AND - tfd.moderation_status IN ('active', 'inactive', 'archive') AND - tfd.created < UNIX_TIMESTAMP(NOW() - INTERVAL 2 WEEK) + WHERE tfd.vid = 'source' + # Skip blocked or duplicate sources. + AND tfd.moderation_status IN ('active', 'inactive', 'archive') + # Skip recently created organizations. + AND tfd.created < UNIX_TIMESTAMP(NOW() - INTERVAL 2 WEEK) GROUP BY tfd.tid ) AS subquery ) AS query