You are here

function quiz_generate_questions in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 quiz.devel.inc \quiz_generate_questions()
  2. 7.6 quiz.devel.inc \quiz_generate_questions()
  3. 7.5 quiz.devel.inc \quiz_generate_questions()
  4. 6.x quiz.devel.inc \quiz_generate_questions()

Generate Quiz questions.

Parameters

type $quiz:

type $question_type:

1 call to quiz_generate_questions()
quiz_generate in ./quiz.devel.inc
Generate random quiz data.

File

./quiz.devel.inc, line 143

Code

function quiz_generate_questions($quiz, $question_type) {
  $users = array_filter(devel_get_users());
  $question_array = array(
    'type' => $question_type,
    'changed' => \Drupal::time()
      ->getRequestTime(),
    'moderate' => 0,
    'promote' => 0,
    'log' => '',
    'status' => 1,
    'sticky' => 0,
    'language' => LANGUAGE_NONE,
    'title' => devel_create_greeking(rand(5, 20), TRUE),
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => devel_create_para(rand(20, 50), 1),
        ),
      ),
    ),
    'uid' => $users[array_rand($users)],
  );
  switch ($question_type) {
    case 'truefalse':
      $question_array += array(
        'correct_answer' => rand(0, 1),
      );
      break;
    case 'short_answer':
      $question_array += array(
        'correct_answer_evaluation' => rand(ShortAnswerQuestion::ANSWER_MATCH, ShortAnswerQuestion::ANSWER_MANUAL),
        'correct_answer' => devel_create_greeking(rand(10, 20)),
      );
      break;
    case 'long_answer':
      $question_array += array(
        'rubric' => devel_create_greeking(rand(10, 20)),
      );
      break;
    case 'multichoice':
      $question_array += quiz_generate_dummy_multichoice_question_info();
      break;
    case 'quiz_directions':
      break;
    default:
      \Drupal::messenger()
        ->addMessage('Unsupported question: ' . $question_type, 'error');
  }

  // Create question node.
  $question = (object) $question_array;

  // Populate all core fields on behalf of field.module.
  module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  devel_generate_fields($question, 'node', $question->type);
  node_save($question);
  _quiz_question_get_instance($question)
    ->saveRelationships($quiz->nid, $quiz->vid);
  quiz_update_max_score_properties(array(
    $quiz->vid,
  ));
}