zaya-1.0.x-dev/modules/zaya_events/zaya_events.module

modules/zaya_events/zaya_events.module
<?php

/**
 * @file
 * Primary module hooks for Zaya events module.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\node\Entity\Node;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\zaya\Entity\Node\ZayaProgress;

/**
 * Implements hook_ENTITY_TYPE_view_alter() for group_relationship entities.
 */
function zaya_events_group_relationship_view_alter(array &$build, EntityInterface $group_relationship, EntityViewDisplayInterface $display) {
  if ($group_relationship->isNew() || isset($group_relationship->in_preview)) {
    return;
  }
  // Update the history table, stating that this user viewed this
  // group_relationship.
  if ($display->getOriginalMode() === 'full') {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
    if (\Drupal::currentUser()->isAuthenticated() && $group_relationship->getPluginId() == "group_node:zaya_chapter" && $group_relationship->getEntity()->access('view')) {
      $user = \Drupal::currentUser();
      $build['mark_completed_form'] = \Drupal::formBuilder()->getForm('Drupal\zaya_events\Form\MarkChapterCompletedForm', $group_relationship, $user);
      // @todo move to js attached, and set visited using controller
      // as in history.module.
      // @todo add config flag
      $log_visits = FALSE;
      if ($log_visits) {
        zaya_events_set_chapter_visited($group_relationship, $user);
      }
    }

  }
  if (\Drupal::currentUser()->isAuthenticated() && $group_relationship->getPluginId() == "group_node:zaya_resource" && $group_relationship->getEntity()->access('view')) {
    $user = \Drupal::currentUser();
    $build['mark_completed_form'] = \Drupal::formBuilder()->getForm('Drupal\zaya_events\Form\MarkResourceCompletedForm', $group_relationship, $user);
  }
}

/**
 * Implements hook_ENTITY_TYPE_view_alter() for group entities.
 */
function zaya_events_group_view_alter(array &$build, EntityInterface $group, EntityViewDisplayInterface $display) {
  if ($group->isNew() || isset($group->in_preview)) {
    return;
  }
  // Update the history table, stating that this user viewed this group.
  if ($display->getOriginalMode() === 'full') {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
    if (\Drupal::currentUser()->isAuthenticated() && $group->bundle() == "zaya_itinerary") {
      $user = \Drupal::currentUser();
      $build['mark_completed_form'] = \Drupal::formBuilder()->getForm('Drupal\zaya_events\Form\MarkItineraryCompletedForm', $group, $user);
      // zaya_events_set_group_visited(); # Possible event group visited.
    }
  }
}

/**
 * Add visit progress node.
 *
 * We are doing directly, maybe in the future we could use dedicated events.
 */
function zaya_events_set_chapter_visited(?GroupRelationshipInterface $relationship = NULL, ?AccountInterface $account = NULL) {
  $completion = $relationship->getCompletionState();
  // Mark chapter as visited just when not completed.
  if ($completion === ZayaProgress::UNCOMPLETED) {
    $itinerary = $relationship->getGroup();
    // $membership = GroupMembership::loadSingle($itinerary, $account);
    $chapter = $relationship->getEntity();
    $progress_values = [
      "title" => "visit of itinerary({$itinerary->label()}) chapter: {$chapter->label()}",
      "type" => 'zaya_progress',
      "zaya_itinerary" => ['entity' => $itinerary],
      "zaya_itinerary_uuid" => $itinerary->uuid(),
      "zaya_chapter" => ['entity' => $chapter],
      "zaya_chapter_uuid" => $chapter->uuid(),
      "uid" => $account->id(),
      "zaya_learning_entity_bundle" => "zaya_chapter",
      "zaya_learning_entity_type" => "node",
      "zaya_event" => 'visit chapter',
    ];
    $progress_node = Node::create($progress_values);
    $progress_node->save();
  }
}

/**
 * Add webform submission progress node.
 *
 * We are doing directly, maybe in the future we could use dedicated events or
 * WebformHandler submit method.
 */
function zaya_events_webform_submission_insert(WebformSubmissionInterface $webform_submission) {
  _zaya_events_webform_submission_evaluation_progress_log($webform_submission);
}

/**
 * Add webform submission progress node coming from quiz webform.
 *
 * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
 *   A webform submission.
 */
function _zaya_events_webform_submission_evaluation_progress_log(WebformSubmissionInterface $webform_submission) {
  $webform = $webform_submission->getWebform();
  $webform_quiz_settings = $webform->getThirdPartySetting('webform_quiz', 'settings');

  // If the webform is not a quiz, everything below the if statement is
  // irrelevant.
  if (!$webform || empty($webform_quiz_settings['is_this_a_quiz']) || !$webform_quiz_settings['is_this_a_quiz']) {
    return;
  }

  /*
  //NodeInterface
  $source_entity = $webform_submission->getSourceEntity();
  if (!$source_entity instanceof WebformSubmissionInterface
  || $source_entity->getWebform()->id() !== 'demo_application') {
  return;
  }*/

  $webform_data = $webform_submission->getData();

  $event = "quiz passing";
  if ($webform_data['webform_quiz_score'] < $webform_quiz_settings['passing_score']) {
    $event = "quiz failing";
  }
  $relationship = \Drupal::routeMatch()->getParameter('group_relationship');
  $itinerary = \Drupal::routeMatch()->getParameter('group');
  $account = \Drupal::currentUser();
  if ($relationship && $itinerary) {
    // $membership = GroupMembership::loadSingle($itinerary, $account);
    $resource = $relationship->getEntity();
    $progress_values = [
      "title" => $event . " of itinerary({$itinerary->label()}) resource: {$resource->label()} submission {$webform_submission->id()}",
      "type" => 'zaya_progress',
      "zaya_itinerary" => ['entity' => $itinerary],
      "zaya_itinerary_uuid" => $itinerary->uuid(),
      "zaya_resource" => ['entity' => $resource],
      "zaya_resource_uuid" => $resource->uuid(),
      "uid" => $account->id(),
      "zaya_learning_entity_bundle" => "zaya_resource",
      "zaya_learning_entity_type" => "node",
      "zaya_event" => $event,
      "zaya_progress_status" => ($event === 'quiz passing') ? ZayaProgress::COMPLETED : ZayaProgress::UNCOMPLETED,
    ];
    $progress_node = Node::create($progress_values);
    $progress_node->save();
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc