function quiz_generate in Quiz 6.x
Same name and namespace in other branches
- 8.6 quiz.devel.inc \quiz_generate()
- 8.5 quiz.devel.inc \quiz_generate()
- 7.6 quiz.devel.inc \quiz_generate()
- 7.5 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 95
Code
function quiz_generate($quiz_type, $question_types, $question_limit, $quiz_results) {
$users = array_filter(devel_get_users());
$quiz = (object) [
'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 = \Drupal::service('entity_type.manager')
->getStorage('quiz_result')
->create([
'type' => 'quiz_result',
'nid' => $quiz->nid,
'vid' => $quiz->vid,
'uid' => $users[array_rand($users)],
]);
$quiz_result
->save();
// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
\Drupal::database()
->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();
// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
\Drupal::database()
->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));
}