You are here

public function TrueFalseActivityAnswer::getScore in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Plugin/ActivityAnswer/TrueFalseActivityAnswer.php \Drupal\opigno_module\Plugin\ActivityAnswer\TrueFalseActivityAnswer::getScore()

Score logic for specified activity.

Overrides ActivityAnswerPluginBase::getScore

File

src/Plugin/ActivityAnswer/TrueFalseActivityAnswer.php, line 28

Class

TrueFalseActivityAnswer
Class TrueFalseActivityAnswer.

Namespace

Drupal\opigno_module\Plugin\ActivityAnswer

Code

public function getScore(OpignoAnswerInterface $answer) {

  /* @var $db_connection \Drupal\Core\Database\Connection */
  $db_connection = \Drupal::service('database');
  $score = 0;

  /** @var \Drupal\opigno_module\Entity\OpignoActivityInterface $activity */

  /** @var \Drupal\opigno_module\Entity\OpignoAnswer $answer */
  $activity = $answer
    ->getActivity();
  if ($answer
    ->hasField('field_true_false') && $activity
    ->hasField('field_true_false') && $answer
    ->get('field_true_false')->value == $activity
    ->get('field_true_false')->value) {

    /** @var \Drupal\opigno_module\Entity\OpignoModuleInterface $module */
    $module = $answer
      ->getModule();
    $score_query = $db_connection
      ->select('opigno_module_relationship', 'omr')
      ->fields('omr', [
      'max_score',
    ])
      ->condition('omr.parent_id', $module
      ->id())
      ->condition('omr.parent_vid', $module
      ->getRevisionId())
      ->condition('omr.child_id', $activity
      ->id())
      ->condition('omr.child_vid', $activity
      ->getRevisionId());
    $score_result = $score_query
      ->execute()
      ->fetchObject();
    if ($score_result) {
      $score = $score_result->max_score;
    }
  }
  return $score;
}