You are here

public static function ProblemsStorage::save_node_problems in Node Accessibility 8

Saves the node report data to the database.

Parameters

int $nid: The node id of the node associated with the given reports.

int $vid: The node revision id of the node associated with the given reports.

array $reports: The reports array as returned by the quail library quail api reporter.

Return value

bool TRUE on success, FALSE otherwise.

2 calls to ProblemsStorage::save_node_problems()
PerformValidation::nodes in src/PerformValidation.php
Performs validation on the given nodes and stores the results.
PerformValidation::node_revisions in src/PerformValidation.php
Performs validation on the given nodes and stores the results.

File

src/ProblemsStorage.php, line 30

Class

ProblemsStorage
Class DatabaseStorage.

Namespace

Drupal\node_accessibility

Code

public static function save_node_problems($nid, $vid, $reports) {
  if (!is_array($reports)) {
    return FALSE;
  }
  $tests_known = (array) QuailApiBase::load_tests([], 'machine_name');
  $problems = [];
  foreach ($reports as $severity => $severity_results) {
    if ($severity == 'total') {
      continue;
    }
    foreach ($severity_results as $test_name => $test_results) {
      if ($test_name == 'total') {
        continue;
      }
      if (!array_key_exists($test_name, $tests_known)) {
        if (empty($test_results['body']['title']) || empty($test_results['body']['description'])) {
          continue;
        }
        $test_data = [];
        $test_data['machine_name'] = $test_name;
        $test_data['severity'] = $severity;
        $test_data['human_name'] = $test_results['body']['title'];
        $test_data['description'] = $test_results['body']['description'];
        $results = QuailApiBase::save_test($test_data);
        if ($results === FALSE) {
          \Drupal::logger('node_accessibility')
            ->error("Failed to insert @machine_name into quail api tests database table.", [
            '@machine_name' => $test_name,
          ]);
          continue;
        }

        // The row must be loaded from the database so that the id can be retrieved
        $loaded_test = QuailApiBase::load_tests([
          'machine_name' => $test_name,
        ], NULL);
        if (!isset($loaded_test['0']) || !is_object($loaded_test['0'])) {
          \Drupal::logger('node_accessibility')
            ->error("Failed to insert @machine_name problems into node accessibility tests database table because is not a valid object.", [
            '@machine_name' => $test_name,
          ]);
          continue;
        }
        $tests_known[$test_name] = $loaded_test['0'];
      }
      foreach ($test_results['problems'] as $problem_name => $problem) {
        if (empty($problem['line']) || empty($problem['element'])) {
          continue;
        }
        $problem_data = [];
        $problem_data['nid'] = $nid;
        $problem_data['vid'] = $vid;
        $problem_data['test_id'] = $tests_known[$test_name]->id;
        $problem_data['test_severity'] = $tests_known[$test_name]->severity;
        $problem_data['line'] = $problem['line'];
        $problem_data['element'] = $problem['element'];
        $problems[] = $problem_data;
      }
    }
  }
  if (!empty($problems)) {
    $results = self::replace_problem($nid, $vid, $problems);
    if ($results === FALSE) {
      \Drupal::logger('node_accessibility')
        ->error("Failed to insert @machine_name problems into node accessibility problems database table.", [
        '@machine_name' => $test_name,
      ]);
    }
  }
  $account = \Drupal::currentUser();
  $results = self::replace_problem_stats((int) $account
    ->id(), (int) $nid, (int) $vid, (int) microtime(TRUE));
  if ($results === FALSE) {
    \Drupal::logger('node_accessibility')
      ->error("Failed to update the accessibility problem statistics database table for node @nid with reveision @vid.", [
      '@nid' => $nid,
      '@vid' => $vid,
    ]);
  }
}