You are here

public function OpignoTincanQuestionTypeQuestion::saveNodeProperties in Opigno TinCan Question Type 7

Save question type specific node properties.

Parameters

bool $is_new: Indicate if the node is a new one or an update.

File

includes/opigno_tincan_question_type.question.inc, line 40
This file contains the class OpignoTincanQuestionTypeQuestion.

Class

OpignoTincanQuestionTypeQuestion
This class goal is to provide specific TinCan question information.

Code

public function saveNodeProperties($is_new = FALSE) {

  // Check first if the file is a new one or an updated one.
  // If the file didn't change, leave this method.
  if (isset($this->node->fid)) {
    $is_file_updated = $this->node->fid != $this->node->file;
    if (!$is_new && !$is_file_updated) {
      return;
    }
  }
  else {
    $is_file_updated = FALSE;
  }
  if (!isset($_SESSION[self::SESSIONKEY_FILE])) {
    form_set_error('file', t('Error while uploading the file.'));
    return;
  }
  $file = file_load($_SESSION[self::SESSIONKEY_FILE]);
  if (!$file) {
    form_set_error('file', t('Error while opening the file.'));
    return;
  }
  $package_info = self::getInfoFromExtractedPackage($file);
  if ($package_info === FALSE) {
    form_set_error('file', t('The package does not contain an activity ID or a launch file'));
    return;
  }

  // Delete the old file if there is a new one.
  if ($is_file_updated) {
    $old_file = file_load($this->node->fid);
    if ($old_file) {
      file_usage_delete($old_file, 'opigno_tincan_question_type', 'node', $this->node->nid);
      file_delete($old_file);
    }
  }
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  file_usage_add($file, 'opigno_tincan_question_type', 'node', $this->node->nid);
  $activity_id = $package_info['id'];
  $launch_filename = $package_info['launch'];
  db_merge(OpignoTincanQuestionTypePropertiesDatabase::NAME)
    ->key(array(
    'nid' => $this->node->nid,
    'vid' => $this->node->vid,
  ))
    ->fields(array(
    'fid' => $_SESSION[self::SESSIONKEY_FILE],
    'activity_id' => $activity_id,
    'launch_filename' => $launch_filename,
  ))
    ->execute();
}