Skip to content

Commit

Permalink
Revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
attiks committed Dec 10, 2024
1 parent e4ec4e5 commit 93bde1f
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
107 changes: 107 additions & 0 deletions html/modules/custom/reliefweb_job_tagger/reliefweb_job_tagger.module
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,68 @@ function reliefweb_job_tagger_node_presave(EntityInterface $node) {
if (!$node instanceof Node) {
return;
}

if ($node->bundle() != 'job') {
return;
}

// Skip queued and processed nodes.
if (!$node->hasField('reliefweb_job_tagger_status')) {
return;
}

// Track changes to AI fields.
reliefweb_job_tagger_log_manual_changes_to_tagging($node);

// Already queued, nothing left to do.
if ($node->reliefweb_job_tagger_status->value == 'queued' || $node->reliefweb_job_tagger_status->value == 'processed') {
return;
}

// Skipped, nothing to do.
if ($node->reliefweb_job_tagger_status->value == 'skipped') {
return;
}

// If queue it was queued on previous save.
if ($node->reliefweb_job_tagger_status->value == 'queue') {
// Mark it as queued.
$node->set('reliefweb_job_tagger_status', 'queued');
return;
}

if ($node->moderation_status->value != 'pending') {
$node->set('reliefweb_job_tagger_status', '');
return;
}

// Check permissions.
$user = \Drupal::currentUser();
if ($user->hasPermission('bypass ocha ai job tag')) {
return;
}

if (!$user->hasPermission('enforce ocha ai job tag')) {
return;
}

// Only queue it when fields are empty.
if (!$node->field_career_categories->isEmpty()) {
return;
}

if (!$node->field_theme->isEmpty()) {
return;
}

// Queue node when status is empty.
if ($node->reliefweb_job_tagger_status->isEmpty()) {
$node->set('reliefweb_job_tagger_status', 'queue');

$log_message = $node->getRevisionLogMessage();
$log_message .= (empty($log_message) ? '' : ' ') . 'Job has been queued for tagging.';
$node->setRevisionLogMessage($log_message);
}
}

/**
Expand Down Expand Up @@ -251,6 +313,51 @@ function reliefweb_job_tagger_menu_local_tasks_alter(&$data, $route_name, Refina
}
}

/**
* Check for manual changes to the AI tagging and log them.
*
* @param \Drupal\Entity\EntityInterface $entity
* Changed entity.
*/
function reliefweb_job_tagger_log_manual_changes_to_tagging(EntityInterface $entity) {
if (!isset($entity->original) || !$entity->original->hasField('reliefweb_job_tagger_status')) {
return;
}
$original = $entity->original;

// Skip if the original was not processed by the AI.
if ($original->reliefweb_job_tagger_status->value !== 'processed') {
return;
}

$editor = UserHelper::userHasRoles(['editor']);
$logger = \Drupal::logger('reliefweb_job_tagger');

$fields = [
'field_theme',
'field_career_categories',
];

foreach ($fields as $field) {
if ($entity->hasField($field) && !$entity->get($field)->equals($original->get($field))) {
$old = [];
$new = [];
foreach ($original->get($field) as $item) {
$old[] = $item->entity?->label() ?? $item->target_id;
}
foreach ($entity->get($field) as $item) {
$new[] = $item->entity?->label() ?? $item->target_id;
}
$logger->info(strtr('Manual change to the @field tagging by @user: @old --> @new', [
'@field' => $field,
'@user' => $editor ? 'editor' : 'non editor',
'@old' => implode(', ', $old),
'@new' => implode(', ', $new),
]));
}
}
}

/**
* Implements hook_entity_after_save().
*
Expand Down
37 changes: 37 additions & 0 deletions html/modules/custom/reliefweb_reporting/reliefweb_reporting.module
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,43 @@ function reliefweb_reporting_get_weekly_ai_tagging_stats() {
$schema = \Drupal::database()->schema();
$records = [];

// To avoid losing data when transitioning, we fetch the information from the
// previous tagging system and concatenate it with the new one.
if ($schema->tableExists('node_revision__reliefweb_job_tagger_status')) {
$old_records = $connection->query("
SELECT
nr.nid AS nid,
nr.vid As vid,
IFNULL(GROUP_CONCAT(DISTINCT ur.roles_target_id ORDER BY ur.roles_target_id SEPARATOR ','), '') AS roles,
GROUP_CONCAT(DISTINCT nfcc.field_career_categories_target_id ORDER BY nfcc.field_career_categories_target_id SEPARATOR ',') AS career_categories,
GROUP_CONCAT(DISTINCT nft.field_theme_target_id ORDER BY nft.field_theme_target_id SEPARATOR ',') AS themes
FROM node_revision AS nr
INNER JOIN node_field_data AS n
ON n.nid = nr.nid
INNER JOIN node_revision__reliefweb_job_tagger_status AS njts
ON njts.entity_id = n.nid
AND njts.revision_id = nr.vid
AND njts.reliefweb_job_tagger_status_value = 'processed'
LEFT JOIN user__roles AS ur
ON ur.entity_id = nr.revision_uid
LEFT JOIN node_revision__field_career_categories AS nfcc
ON nfcc.entity_id = nr.nid
AND nfcc.revision_id = nr.vid
LEFT JOIN node_revision__field_theme AS nft
ON nft.entity_id = nr.nid
AND nft.revision_id = nr.vid
WHERE n.type = 'job'
AND n.created >= UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW()) + 6 DAY))
AND n.created < UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW()) - 1 DAY))
GROUP BY nr.vid
ORDER BY nr.vid
")->fetchAll(\PDO::FETCH_ASSOC);

foreach ($old_records as $record) {
$records[$record['nid']][$record['vid']] = $record;
}
}

if ($schema->tableExists('ocha_content_classification_progress')) {
$new_records = $connection->query("
SELECT
Expand Down

0 comments on commit 93bde1f

Please sign in to comment.