You are here

function quiz_generate in Quiz 8.6

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

Generate random quiz data.

@todo associate results with random users

Parameters

type $quiz_type:

type $question_types:

type $question_limit:

type $quiz_results:

1 call to quiz_generate()
quiz_generate_form_submit in ./quiz.devel.inc
Submit callback for devel_generate support.

File

./quiz.devel.inc, line 94

Code

function quiz_generate($quiz_type, $question_types, $question_limit, $quiz_results) {
  $users = array_filter(devel_get_users());
  $quiz = (object) array(
    'type' => $quiz_type,
    'title' => devel_create_greeking(rand(5, 10), TRUE),
    'uid' => $users[array_rand($users)],
    'created' => \Drupal::time()
      ->getRequestTime(),
    'changed' => \Drupal::time()
      ->getRequestTime(),
  );

  // Populate all core fields on behalf of field.module.
  module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  devel_generate_fields($quiz, 'node', $quiz->type);
  node_save($quiz);
  for ($i = 1; $i <= $question_limit; $i++) {
    quiz_generate_questions($quiz, array_rand($question_types));
  }
  for ($i = 1; $i <= $quiz_results; $i++) {
    $quiz_result = entity_create('quiz_result', array(
      'type' => 'quiz_result',
      'nid' => $quiz->nid,
      'vid' => $quiz->vid,
      'uid' => $users[array_rand($users)],
    ));
    $quiz_result
      ->save();
    db_update('quiz_node_results_answers')
      ->expression('points_awarded', 'result_answer_id % 5')
      ->expression('is_correct', 'result_answer_id % 2')
      ->condition('result_id', $quiz_result->result_id)
      ->execute();
    db_update('quiz_node_results')
      ->expression('score', rand(1, 100))
      ->expression('is_evaluated', 1)
      ->expression('time_end', rand(\Drupal::time()
      ->getRequestTime() - 525600 * 60, \Drupal::time()
      ->getRequestTime()))
      ->condition('result_id', $quiz_result->result_id)
      ->execute();
  }
  \Drupal::messenger()
    ->addMessage('Generated quiz: ' . l($quiz->title, 'quiz/' . $quiz->nid));
}