You are here

public function LearningPathContentController::deleteActivity in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/Controller/LearningPathContentController.php \Drupal\opigno_learning_path\Controller\LearningPathContentController::deleteActivity()

This method is called on learning path load.

It will update an existing activity relation.

1 string reference to 'LearningPathContentController::deleteActivity'
opigno_learning_path.routing.yml in ./opigno_learning_path.routing.yml
opigno_learning_path.routing.yml

File

src/Controller/LearningPathContentController.php, line 570

Class

LearningPathContentController
Controller for all the actions of the Learning Path content.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function deleteActivity(OpignoModule $opigno_module, Request $request) {

  // First, check the params.
  $datas = json_decode($request
    ->getContent());
  if (empty($datas->omr_id)) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

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

  // Load Activity before deleting relationship.
  $relationship = $db_connection
    ->select('opigno_module_relationship', 'omr')
    ->fields('omr', [
    'child_id',
    'group_id',
  ])
    ->condition('omr_id', $datas->omr_id)
    ->execute()
    ->fetchObject();
  if (!empty($relationship->child_id)) {
    $opigno_activity = OpignoActivity::load($relationship->child_id);

    // Allow other modules to take actions.
    \Drupal::moduleHandler()
      ->invokeAll('opigno_learning_path_activity_delete', [
      $opigno_module,
      $opigno_activity,
    ]);

    // Delete relationship.
    $delete_query = $db_connection
      ->delete('opigno_module_relationship');
    $delete_query
      ->condition('omr_id', $datas->omr_id);
    $delete_query
      ->execute();
    if (!empty($relationship->group_id)) {
      $links = OpignoGroupManagedLink::loadByProperties([
        'group_id' => $relationship->group_id,
        'parent_content_id' => $opigno_module
          ->id(),
      ]);
      $added_activities = $opigno_module
        ->getModuleActivities();

      // Remove conditions if no activities;
      foreach ($links as $link) {
        if (empty($added_activities)) {
          $link
            ->set('required_activities', null);
          $link
            ->set('required_score', 0);
          $link
            ->save();
        }
        else {
          $activity_params = $link
            ->get('required_activities')
            ->getString();
          $activity_params = unserialize($activity_params);
          foreach ($activity_params as $param) {
            $options = explode('-', $param);
            if ($options[0] == $relationship->child_id) {
              $link
                ->set('required_activities', null)
                ->save();
              break;
            }
          }
        }
      }
    }
  }
  return new JsonResponse(NULL, Response::HTTP_OK);
}