You are here

function scale_collection_form_submit in Quiz 8.4

Same name and namespace in other branches
  1. 8.6 question_types/quiz_scale/quiz_scale.module \scale_collection_form_submit()
  2. 8.5 question_types/quiz_scale/quiz_scale.module \scale_collection_form_submit()
  3. 6.6 question_types/scale/scale.module \scale_collection_form_submit()
  4. 6.4 question_types/scale/scale.module \scale_collection_form_submit()
  5. 7.6 question_types/scale/scale.module \scale_collection_form_submit()
  6. 7 question_types/scale/scale.module \scale_collection_form_submit()
  7. 7.4 question_types/scale/scale.module \scale_collection_form_submit()
  8. 7.5 question_types/scale/scale.module \scale_collection_form_submit()

Handles the scale collection form.

_state

Parameters

$form:

1 string reference to 'scale_collection_form_submit'
scale_manage_collection_form in question_types/scale/scale.module
Form for changing and deleting the current users preset answer collections.

File

question_types/scale/scale.module, line 260
The main file for scale.

Code

function scale_collection_form_submit($form, &$form_state) {
  $user = \Drupal::currentUser();
  $changed = 0;
  $deleted = 0;
  foreach ($form_state['values'] as $key => $alternatives) {
    if ($col_id = _scale_get_col_id($key)) {
      $s_q = new ScaleQuestion(new \stdClass());
      $s_q
        ->initUtil($col_id);
      switch ($alternatives['to-do']) {

        // @todo: Rename to-do to $op
        case 0:

        //Save, but don't change
        case 1:

          //Save and change existing questions
          $new_col_id = $s_q
            ->saveAnswerCollection(FALSE, $alternatives, 1);
          if (isset($alternatives['for_all'])) {
            _scale_set_for_all($new_col_id, $alternatives['for_all']);
          }
          if ($new_col_id == $col_id) {
            break;
          }
          $changed++;

          // We save the changes, but don't change existing questions
          if ($alternatives['to-do'] == 0) {

            // The old version of the collection shall not be a preset anymore
            _scale_unpreset_collection($col_id, $user
              ->id());

            // If the old version of the collection doesn't belong to any questions it is safe to delete it.
            $s_q
              ->deleteCollectionIfNotUsed($col_id);
            if (isset($alternatives['for_all'])) {
              _scale_set_for_all($new_col_id, $alternatives['for_all']);
            }
          }
          elseif ($alternatives['to-do'] == 1) {

            // Update all the users questions where the collection is used
            $nids = db_query('SELECT nid FROM {node} WHERE uid = :uid', array(
              ':uid' => 1,
            ))
              ->fetchCol();
            db_update('quiz_scale_node_properties')
              ->fields(array(
              'answer_collection_id' => $new_col_id,
            ))
              ->condition('answer_collection_id', $nids, 'IN')
              ->execute();
            db_delete('quiz_scale_user')
              ->condition('answer_collection_id', $col_id)
              ->condition('uid', $user
              ->id())
              ->execute();
            $s_q
              ->deleteCollectionIfNotUsed($col_id);
          }
          break;
        case 2:

          //Delete
          $got_deleted = $s_q
            ->deleteCollectionIfNotUsed($col_id);
          if (!$got_deleted) {
            _scale_unpreset_collection($col_id, $user
              ->id());
          }
          $deleted++;
          break;
        case 3:

          //New
          if (drupal_strlen($alternatives['alternative0']) > 0) {
            $new_col_id = $s_q
              ->saveAnswerCollection(FALSE, $alternatives, 1);
            _scale_set_for_all($new_col_id, $alternatives['for_all']);
            drupal_set_message(t('New preset has been added'));
          }
          break;
      }
    }
  }
  if ($changed > 0) {
    drupal_set_message(t('!changed presets have been changed.', array(
      '!changed' => $changed,
    )));
  }
  if ($deleted > 0) {
    drupal_set_message(t('!deleted presets have been deleted.', array(
      '!deleted' => $deleted,
    )));
  }
}