You are here

function lingotek_get_and_update_target_progress in Lingotek Translation 7.4

Same name and namespace in other branches
  1. 7.7 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  2. 7.5 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  3. 7.6 lingotek.sync.inc \lingotek_get_and_update_target_progress()

Updates the 'target_sync_progress_[lang-code]' field for every target in the lingotek table with the overall progress returned by TMS

Parameters

int array $document_ids: array of Document IDs that you want to update

3 calls to lingotek_get_and_update_target_progress()
LingotekSync::getDownloadableReport in lib/Drupal/lingotek/LingotekSync.php
lingotek_grid_action_submit in ./lingotek.bulk_grid.inc
Submit function for The Grid's actions The action corresponds to the key of the option selected Often redirects to batch operations or to other pages entirely
lingotek_notifications in ./lingotek.sync.inc
Registers the site translation notfication callback.
1 string reference to 'lingotek_get_and_update_target_progress'
lingotek_update_target_progress_batch_create in ./lingotek.batch.inc

File

./lingotek.sync.inc, line 801
Sync and management

Code

function lingotek_get_and_update_target_progress($document_ids) {
  $api = LingotekApi::Instance();
  if (empty($document_ids)) {
    return;
  }
  if (!is_array($document_ids)) {
    $document_ids = array(
      $document_ids,
    );
  }
  $project_id = variable_get('lingotek_project', NULL);
  $progress_report = $api
    ->getProgressReport($project_id, $document_ids);
  $targets_count = LingotekSync::getTargetCountByDocumentIds($document_ids);
  if (isset($progress_report) && $progress_report->results == 'success') {
    $delete_nids_maybe = array();
    $delete_cids_maybe = array();
    $node_values = array();
    $cfg_values = array();
    $trans_obj = NULL;
    foreach ($progress_report->byDocumentIdAndTargetLocale as $doc_id => $completion) {
      $nid = LingotekSync::getNodeIdFromDocId($doc_id);

      // if no $nid is found for the doc ID, it must be config translation
      if (!$nid) {
        $trans_obj = LingotekConfigChunk::loadByLingotekDocumentId($doc_id, NULL, $project_id);
        if (!$trans_obj) {
          LingotekLog::error("Lingotek doc ID '@doc_id' not found", array(
            '@doc_id' => $doc_id,
          ));
          continue;
        }

        // re-assign the chunk ID to be a node ID for now...
        $nid = $trans_obj->cid;
        $delete_cids_maybe[] = $nid;
      }
      else {
        $delete_nids_maybe[] = $nid;
        $target_number = $targets_count[$nid]->targets;
      }
      $total_percentage = 0;
      foreach ($completion as $language => $percent) {
        if ($trans_obj) {
          $values =& $cfg_values;
        }
        else {
          $values =& $node_values;
        }
        $values[] = array(
          $nid,
          'target_sync_progress_' . $language,
          $percent,
        );
        $values[] = array(
          $nid,
          'target_sync_last_progress_updated_' . $language,
          time(),
        );
        $total_percentage += $percent;
        $status = LingotekSync::getTargetStatus($doc_id, $language);
        if (isset($progress_report->workflowCompletedByDocumentIdAndTargetLocale->{$doc_id}->{$language})) {
          if ($progress_report->workflowCompletedByDocumentIdAndTargetLocale->{$doc_id}->{$language}) {

            // If the workflow is complete
            if ($status != LingotekSync::STATUS_CURRENT) {

              // If the status is not current
              $to_status = LingotekSync::STATUS_READY;

              // Set it to ready
            }
            else {
              $to_status = LingotekSync::STATUS_CURRENT;

              // Otherwise keep it at current
            }
          }
          else {

            // If the workflow is not complete
            $to_status = LingotekSync::STATUS_PENDING;

            // Set it to pending
          }
          $values[] = array(
            $nid,
            'target_sync_status_' . $language,
            $to_status,
          );
        }
      }

      // for now just report on node translation progress...
      if (!$trans_obj) {
        $node_values[] = array(
          'nid' => $nid,
          'lingokey' => 'translation_progress',
          'lingovalue' => $total_percentage / $target_number,
        );
      }
    }
    if ($delete_nids_maybe) {
      lingotek_lingonode_variable_delete_multiple($delete_nids_maybe, 'target_sync_progress_%', 'LIKE');
      lingotek_lingonode_variable_delete_multiple($delete_nids_maybe, 'target_sync_last_progress_updated_%', 'LIKE');
      lingotek_lingonode_variable_delete_multiple($delete_nids_maybe, 'translation_progress');
    }
    if ($delete_cids_maybe) {
      foreach ($delete_cids_maybe as $cid) {
        if ($trans_obj = LingotekConfigChunk::loadById($cid)) {
          $trans_obj
            ->deleteMetadataValue('target_sync_progress_%');
          $trans_obj
            ->deleteMetadataValue('target_sync_last_progress_updated_%');
          $trans_obj
            ->deleteMetadataValue('translation_progress');
        }
      }
    }

    // merge status info for nodes
    foreach ($node_values as $record) {
      $nid = isset($record['nid']) ? $record['nid'] : $record[0];
      $lingokey = isset($record['lingokey']) ? $record['lingokey'] : $record[1];
      $lingovalue = isset($record['lingovalue']) ? $record['lingovalue'] : $record[2];
      $query = db_merge('lingotek')
        ->key(array(
        'nid' => $nid,
        'lingokey' => $lingokey,
      ))
        ->fields(array(
        'nid' => $nid,
        'lingokey' => $lingokey,
        'lingovalue' => $lingovalue,
      ))
        ->execute();
    }

    // insert status info for config
    foreach ($cfg_values as $record) {
      $query = db_merge('lingotek_config_metadata')
        ->key(array(
        'id' => $record[0],
        'config_key' => $record[1],
      ))
        ->fields(array(
        'id' => $record[0],
        'config_key' => $record[1],
        'value' => $record[2],
      ))
        ->execute();
    }
    return $progress_report;
  }
  else {
    $error_message = t("API Error(s):") . " <ul>";
    if (is_array($progress_report->errors)) {
      foreach ($progress_report->errors as $error) {
        $error_message .= "<li>" . $error . "</li>";
      }
    }
    $error_message .= "</ul><i>For additional information, check your recent log messages</i>";
    drupal_set_message(t('@error_message', array(
      '@error_message' => $error_message,
    )), 'error');
  }
}