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

[RW-798] Mark report sources as inactive or archive #672

Merged
merged 3 commits into from
Nov 14, 2023
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
33 changes: 33 additions & 0 deletions html/modules/custom/reliefweb_entities/src/DocumentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

}
3 changes: 3 additions & 0 deletions html/modules/custom/reliefweb_entities/src/Entity/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,81 @@ 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.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
# 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
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'
# 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
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)) {
Expand Down