本文整理汇总了PHP中quiz_add_quiz_question函数的典型用法代码示例。如果您正苦于以下问题:PHP quiz_add_quiz_question函数的具体用法?PHP quiz_add_quiz_question怎么用?PHP quiz_add_quiz_question使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quiz_add_quiz_question函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_quiz_report_overview_report_forcesubmit_specific_attempt
public function test_quiz_report_overview_report_forcesubmit_specific_attempt()
{
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$quizgenerator = $generator->get_plugin_generator('mod_quiz');
$questiongenerator = $generator->get_plugin_generator('core_question');
// Make a user to do the quiz.
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
// Create our course.
$course = $generator->create_course(array('visible' => true));
// Create the quiz.
$quiz = $quizgenerator->create_instance(array('course' => $course->id, 'visible' => true, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));
// Create two questions.
$cat = $questiongenerator->create_question_category();
$saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
// Add the questions to the quiz.
quiz_add_quiz_question($saq->id, $quiz);
quiz_add_quiz_question($numq->id, $quiz);
// Get a quiz object with user access overrides.
$quizobj = quiz::create($quiz->id, $user1->id);
$quizobj2 = quiz::create($quiz->id, $user2->id);
$quizobj3 = quiz::create($quiz->id, $user3->id);
// Start the attempt.
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$quba2 = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2->get_context());
$quba2->set_preferred_behaviour($quizobj2->get_quiz()->preferredbehaviour);
$quba3 = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj3->get_context());
$quba3->set_preferred_behaviour($quizobj3->get_quiz()->preferredbehaviour);
// Create a quiz attempt.
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
$attempt2 = quiz_create_attempt($quizobj2, 1, false, $timenow, false, $user2->id);
$attempt3 = quiz_create_attempt($quizobj3, 1, false, $timenow, false, $user3->id);
// Start the attempt.
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
quiz_start_new_attempt($quizobj2, $quba2, $attempt2, 1, $timenow);
quiz_attempt_save_started($quizobj2, $quba2, $attempt2);
quiz_start_new_attempt($quizobj3, $quba3, $attempt3, 1, $timenow);
quiz_attempt_save_started($quizobj3, $quba3, $attempt3);
// Answer first question and set it overdue.
$tosubmit = array(1 => array('answer' => 'frog'));
$tosubmit2 = array(1 => array('answer' => 'tiger'));
$tosubmit3 = array(1 => array('answer' => 'tiger'));
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_submitted_actions($timenow, true, $tosubmit);
$attemptobj2 = quiz_attempt::create($attempt2->id);
$attemptobj2->process_submitted_actions($timenow, true, $tosubmit2);
$attemptobj3 = quiz_attempt::create($attempt3->id);
$attemptobj3->process_submitted_actions($timenow, true, $tosubmit3);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
$attemptobj->process_abandon($timenow, false);
// Re-load quiz attempt2 data.
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj2 = quiz_attempt::create($attempt2->id);
$attemptobj3 = quiz_attempt::create($attempt3->id);
// Check that the state of the attempt is as expected.
$this->assertEquals(1, $attemptobj->get_attempt_number());
$this->assertEquals(quiz_attempt::ABANDONED, $attemptobj->get_state());
$this->assertEquals($user1->id, $attemptobj->get_userid());
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
// Check that the state of the attempt2 is as expected.
$this->assertEquals(1, $attemptobj2->get_attempt_number());
$this->assertEquals(quiz_attempt::OVERDUE, $attemptobj2->get_state());
$this->assertEquals($user2->id, $attemptobj2->get_userid());
$this->assertTrue($attemptobj2->has_response_to_at_least_one_graded_question());
// Check that the state of the attempt3 is as expected.
$this->assertEquals(1, $attemptobj3->get_attempt_number());
$this->assertEquals(quiz_attempt::OVERDUE, $attemptobj3->get_state());
$this->assertEquals($user3->id, $attemptobj3->get_userid());
$this->assertTrue($attemptobj3->has_response_to_at_least_one_graded_question());
// Force submit the attempts.
$overviewreport = new quiz_overview_report_testable();
$overviewreport->forcesubmit_attempts($quiz, false, array(), array($attempt->id, $attempt3->id));
// Check that it is now finished.
$attemptobj = quiz_attempt::create($attempt->id);
$this->assertEquals(quiz_attempt::FINISHED, $attemptobj->get_state());
$attemptobj2 = quiz_attempt::create($attempt2->id);
$this->assertEquals(quiz_attempt::OVERDUE, $attemptobj2->get_state());
$attemptobj3 = quiz_attempt::create($attempt3->id);
$this->assertEquals(quiz_attempt::FINISHED, $attemptobj3->get_state());
}
示例2: quiz_contains_the_following_questions
/**
* Put the specified questions on the specified pages of a given quiz.
*
* Give the question name in the first column, and that page number in the
* second column. You may optionally give the desired maximum mark for each
* question in a third column.
*
* @param string $quizname the name of the quiz to add questions to.
* @param TableNode $data information about the questions to add.
*
* @Given /^quiz "([^"]*)" contains the following questions:$/
*/
public function quiz_contains_the_following_questions($quizname, TableNode $data)
{
global $DB;
$quiz = $DB->get_record('quiz', array('name' => $quizname), '*', MUST_EXIST);
// The action depends on the field type.
foreach ($data->getRows() as $questiondata) {
if (count($questiondata) < 2 || count($questiondata) > 3) {
throw new ExpectationException('When adding questions to a quiz, you should give 2 or three 3 things: ' . ' the question name, the page number, and optionally a the maxiumum mark. ' . count($questiondata) . ' values passed.', $this->getSession());
}
list($questionname, $rawpage) = $questiondata;
if (!isset($questiondata[2]) || $questiondata[2] === '') {
$maxmark = null;
} else {
$maxmark = clean_param($questiondata[2], PARAM_FLOAT);
if (!is_numeric($questiondata[2]) || $maxmark < 0) {
throw new ExpectationException('When adding questions to a quiz, the max mark must be a positive number.', $this->getSession());
}
}
$page = clean_param($rawpage, PARAM_INT);
if ($page <= 0 || (string) $page !== $rawpage) {
throw new ExpectationException('When adding questions to a quiz, the page number must be a positive integer.', $this->getSession());
}
$questionid = $DB->get_field('question', 'id', array('name' => $questionname), MUST_EXIST);
quiz_add_quiz_question($questionid, $quiz, $page, $maxmark);
}
quiz_update_sumgrades($quiz);
}
示例3: prepare_quiz_data
protected function prepare_quiz_data()
{
$this->resetAfterTest(true);
// Create a course
$course = $this->getDataGenerator()->create_course();
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id);
// Create a couple of questions.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
// Add them to the quiz.
quiz_add_quiz_question($saq->id, $quiz);
quiz_add_quiz_question($numq->id, $quiz);
// Make a user to do the quiz.
$user1 = $this->getDataGenerator()->create_user();
$this->setUser($user1);
$quizobj = quiz::create($quiz->id, $user1->id);
// Start the attempt.
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
return array($quizobj, $quba, $attempt);
}
示例4: add_quiz_to_course
/**
* Utility method to add a quiz to a course.
* @param $course
* @return testable_assign
*/
private function add_quiz_to_course($course)
{
// Add a quiz to the course.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));
// Create a couple of questions using test data in mod_quiz.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
// Add them to the quiz.
quiz_add_quiz_question($saq->id, $quiz);
quiz_add_quiz_question($numq->id, $quiz);
return $quiz;
}
示例5: setup_quiz_and_questions
/**
* Setup a course, a quiz, a question category and a question for testing.
*
* @param string $type The type of question category to create.
* @return array The created data objects
*/
public function setup_quiz_and_questions($type = 'module')
{
// Create course category.
$category = $this->getDataGenerator()->create_category();
// Create course.
$course = $this->getDataGenerator()->create_course(array('numsections' => 5));
$options = array('course' => $course->id, 'duedate' => time());
// Generate an assignment with due date (will generate a course event).
$quiz = $this->getDataGenerator()->create_module('quiz', $options);
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
if ('course' == $type) {
$context = context_course::instance($course->id);
} else {
if ('category' == $type) {
$context = context_coursecat::instance($category->id);
} else {
$context = context_module::instance($quiz->cmid);
}
}
$qcat = $qgen->create_question_category(array('contextid' => $context->id));
$questions = array($qgen->create_question('shortanswer', null, array('category' => $qcat->id)), $qgen->create_question('shortanswer', null, array('category' => $qcat->id)));
quiz_add_quiz_question($questions[0]->id, $quiz);
return array($category, $course, $quiz, $qcat, $questions);
}
示例6: make_unique_id_code
// on whether to include questions in subcategories
$form->questiontextformat = 0;
$form->image = '';
$form->defaultgrade = 1;
$form->hidden = 1;
for ($i = 0; $i < $randomcount; $i++) {
$form->category = "{$category->id},{$category->contextid}";
$form->stamp = make_unique_id_code();
// Set the unique code (not to be changed)
$question = new stdClass();
$question->qtype = RANDOM;
$question = $QTYPES[RANDOM]->save_question($question, $form, $course);
if (!isset($question->id)) {
error('Could not insert new random question!');
}
quiz_add_quiz_question($question->id, $quiz);
}
}
$significantchangemade = true;
}
if (optional_param('repaginate', false, PARAM_BOOL) and confirm_sesskey()) {
/// Re-paginate the quiz
$questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
if ($questionsperpage != $quiz->questionsperpage) {
$quiz->questionsperpage = $questionsperpage;
if (!set_field('quiz', 'questionsperpage', $quiz->questionsperpage, 'id', $quiz->id)) {
error('Could not save number of questions per page');
}
}
$quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) {
示例7: verbose
$questions[$courseid][] = $question->generate_test("question{$qtype}-{$i}", $courseid);
verbose("Generated a question of type {$qtype} for course id {$courseid}.");
}
}
// Assign questions to quizzes, if such exist
if (!empty($quizzes) && !empty($questions) && !empty($settings['questions-per-quiz'])) {
// Cannot assign more questions per quiz than are available, so determine which is the largest
$questions_per_quiz = max(count($questions), $settings['questions-per-quiz']);
foreach ($quizzes as $quiz) {
$questions_added = array();
for ($i = 0; $i < $questions_per_quiz; $i++) {
// Add a random question to the quiz
do {
$random = rand(0, count($questions[$quiz->course]));
} while (in_array($random, $questions_added) || !array_key_exists($random, $questions[$quiz->course]));
if (!quiz_add_quiz_question($questions[$quiz->course][$random]->id, $quiz)) {
// Could not add question to quiz!! report error
echo "WARNING: Could not add question id {$random} to quiz id {$quiz->id}{$settings['eolchar']}";
} else {
verbose("Adding question id {$random} to quiz id {$quiz->id}.");
$questions_added[] = $random;
}
}
}
}
}
/**
* ROLE ASSIGNMENTS
*/
if (count($courses) > 0) {
verbose("Inserting student->course role assignments...");
示例8: make_unique_id_code
$form->questiontextformat = 0;
$form->image = '';
$form->defaultgrade = 1;
$form->hidden = 1;
for ($i = 0; $i < $randomcount; $i++) {
$form->category = $category->id . ',' . $category->contextid;
$form->stamp = make_unique_id_code();
// Set the unique
//code (not to be changed)
$question = new stdClass();
$question->qtype = RANDOM;
$question = $QTYPES[RANDOM]->save_question($question, $form, $course);
if (!isset($question->id)) {
print_error('cannotinsertrandomquestion', 'quiz');
}
quiz_add_quiz_question($question->id, $quiz, $addonpage);
}
}
quiz_update_sumgrades($quiz);
quiz_delete_previews($quiz);
redirect($thispageurl->out());
}
if (optional_param('addnewpagesafterselected', null) && !empty($selectedquestionids) && confirm_sesskey()) {
foreach ($selectedquestionids as $questionid) {
$quiz->questions = quiz_add_page_break_after($quiz->questions, $questionid);
}
quiz_save_new_layout($quiz);
redirect($thispageurl->out());
}
$addpage = optional_param('addpage', false, PARAM_INT);
if ($addpage !== false && confirm_sesskey()) {
示例9: add_question_to_quiz
/**
* This adds a particular question to the supplied quiz. Based on /mod/quiz/edit.php
*
* @param int $questionid
* @param stdClass $quiz
* @return void
*/
public function add_question_to_quiz($questionid, $quiz)
{
quiz_require_question_use($questionid);
quiz_add_quiz_question($questionid, $quiz, 0);
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
}
开发者ID:nadavkav,项目名称:moodle-block_ajax_marking,代码行数:14,代码来源:block_ajax_marking_mod_quiz_generator.class.php
示例10: test_question_remove_stale_questions_from_category
public function test_question_remove_stale_questions_from_category()
{
global $DB;
$this->resetAfterTest(true);
$dg = $this->getDataGenerator();
$course = $dg->create_course();
$quiz = $dg->create_module('quiz', ['course' => $course->id]);
$qgen = $dg->get_plugin_generator('core_question');
$context = context_system::instance();
$qcat1 = $qgen->create_question_category(['contextid' => $context->id]);
$q1a = $qgen->create_question('shortanswer', null, ['category' => $qcat1->id]);
// Will be hidden.
$q1b = $qgen->create_question('random', null, ['category' => $qcat1->id]);
// Will not be used.
$DB->set_field('question', 'hidden', 1, ['id' => $q1a->id]);
$qcat2 = $qgen->create_question_category(['contextid' => $context->id]);
$q2a = $qgen->create_question('shortanswer', null, ['category' => $qcat2->id]);
// Will be hidden.
$q2b = $qgen->create_question('shortanswer', null, ['category' => $qcat2->id]);
// Will be hidden but used.
$q2c = $qgen->create_question('random', null, ['category' => $qcat2->id]);
// Will not be used.
$q2d = $qgen->create_question('random', null, ['category' => $qcat2->id]);
// Will be used.
$DB->set_field('question', 'hidden', 1, ['id' => $q2a->id]);
$DB->set_field('question', 'hidden', 1, ['id' => $q2b->id]);
quiz_add_quiz_question($q2b->id, $quiz);
quiz_add_quiz_question($q2d->id, $quiz);
$this->assertEquals(2, $DB->count_records('question', ['category' => $qcat1->id]));
$this->assertEquals(4, $DB->count_records('question', ['category' => $qcat2->id]));
// Non-existing category, nothing will happen.
question_remove_stale_questions_from_category(0);
$this->assertEquals(2, $DB->count_records('question', ['category' => $qcat1->id]));
$this->assertEquals(4, $DB->count_records('question', ['category' => $qcat2->id]));
// First category, should be empty afterwards.
question_remove_stale_questions_from_category($qcat1->id);
$this->assertEquals(0, $DB->count_records('question', ['category' => $qcat1->id]));
$this->assertEquals(4, $DB->count_records('question', ['category' => $qcat2->id]));
$this->assertFalse($DB->record_exists('question', ['id' => $q1a->id]));
$this->assertFalse($DB->record_exists('question', ['id' => $q1b->id]));
// Second category, used questions should be left untouched.
question_remove_stale_questions_from_category($qcat2->id);
$this->assertEquals(0, $DB->count_records('question', ['category' => $qcat1->id]));
$this->assertEquals(2, $DB->count_records('question', ['category' => $qcat2->id]));
$this->assertFalse($DB->record_exists('question', ['id' => $q2a->id]));
$this->assertTrue($DB->record_exists('question', ['id' => $q2b->id]));
$this->assertFalse($DB->record_exists('question', ['id' => $q2c->id]));
$this->assertTrue($DB->record_exists('question', ['id' => $q2d->id]));
}
示例11: test_quiz_delete_instance
/**
* Test deleting a quiz instance.
*/
public function test_quiz_delete_instance()
{
global $SITE, $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup a quiz with 1 standard and 1 random question.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
quiz_add_quiz_question($standardq->id, $quiz);
quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
// Get the random question.
$randomq = $DB->get_record('question', array('qtype' => 'random'));
quiz_delete_instance($quiz->id);
// Check that the random question was deleted.
$count = $DB->count_records('question', array('id' => $randomq->id));
$this->assertEquals(0, $count);
// Check that the standard question was not deleted.
$count = $DB->count_records('question', array('id' => $standardq->id));
$this->assertEquals(1, $count);
// Check that all the slots were removed.
$count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id));
$this->assertEquals(0, $count);
// Check that the quiz was removed.
$count = $DB->count_records('quiz', array('id' => $quiz->id));
$this->assertEquals(0, $count);
}
示例12: get_quiz_object
/**
* Create a quiz, add five questions to the quiz
* which are all on one page and return the quiz object.
*/
private function get_quiz_object() {
global $SITE;
$this->resetAfterTest(true);
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array(
'course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));
// Create five questions.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$shortanswer = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
$numerical = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
$essay = $questiongenerator->create_question('essay', null, array('category' => $cat->id));
$truefalse = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id));
$match = $questiongenerator->create_question('match', null, array('category' => $cat->id));
// Add them to the quiz.
quiz_add_quiz_question($shortanswer->id, $quiz);
quiz_add_quiz_question($numerical->id, $quiz);
quiz_add_quiz_question($essay->id, $quiz);
quiz_add_quiz_question($truefalse->id, $quiz);
quiz_add_quiz_question($match->id, $quiz);
// Return the quiz object.
return \mod_quiz\structure::create_for($quiz);
}
示例13: generate_questions
public function generate_questions($courses, $modules)
{
global $DB, $CFG;
if (!is_null($this->get('questions_per_course')) && count($courses) > 0 && is_array($courses)) {
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->dirroot . '/mod/quiz/editlib.php';
$questions = array();
$questionsmenu = question_type_menu();
$questiontypes = array();
foreach ($questionsmenu as $qtype => $qname) {
$questiontypes[] = $qtype;
}
// Add the questions
foreach ($courses as $courseid) {
$questions[$courseid] = array();
for ($i = 0; $i < $this->get('questions_per_course'); $i++) {
$qtype = $questiontypes[array_rand($questiontypes)];
// Only the following types are supported right now. Hang around for more!
$supported_types = array('match', 'essay', 'multianswer', 'multichoice', 'shortanswer', 'numerical', 'truefalse', 'calculated');
$qtype = $supported_types[array_rand($supported_types)];
if ($qtype == 'calculated') {
continue;
}
$classname = "question_{$qtype}_qtype";
if ($qtype == 'multianswer') {
$classname = "embedded_cloze_qtype";
}
$question = new $classname();
$question->qtype = $qtype;
$questions[$courseid][] = $question->generate_test("question{$qtype}-{$i}", $courseid);
$this->verbose("Generated a question of type {$qtype} for course id {$courseid}.");
}
}
// Assign questions to quizzes, if such exist
if (!empty($modules['quiz']) && !empty($questions) && !is_null($this->get('questions_per_quiz'))) {
$quizzes = $modules['quiz'];
// Cannot assign more questions per quiz than are available, so determine which is the largest
$questions_per_quiz = max(count($questions), $this->get('questions_per_quiz'));
foreach ($quizzes as $quiz) {
$questions_added = array();
for ($i = 0; $i < $questions_per_quiz; $i++) {
// Add a random question to the quiz
do {
if (empty($quiz->course)) {
print_object($quizzes);
die;
}
$random = rand(0, count($questions[$quiz->course]));
} while (in_array($random, $questions_added) || !array_key_exists($random, $questions[$quiz->course]));
if (!quiz_add_quiz_question($questions[$quiz->course][$random]->id, $quiz)) {
// Could not add question to quiz!! report error
if (!$this->get('quiet')) {
echo "WARNING: Could not add question id {$random} to quiz id {$quiz->id}{$this->eolchar}";
}
} else {
$this->verbose("Adding question id {$random} to quiz id {$quiz->id}.");
$questions_added[] = $random;
}
}
}
}
return $questions;
}
return null;
}
示例14: quiz_delete_previews
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
$thispageurl->param('lastchanged', $addquestion);
redirect($afteractionurl);
}
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
$structure->check_can_be_edited();
$addonpage = optional_param('addonpage', 0, PARAM_INT);
// Add selected questions to the current quiz.
$rawdata = (array) data_submitted();
foreach ($rawdata as $key => $value) {
// Parse input for question ids.
if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
$key = $matches[1];
quiz_require_question_use($key);
quiz_add_quiz_question($key, $quiz, $addonpage);
}
}
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($afteractionurl);
}
if (optional_param('addrandom', false, PARAM_BOOL) && confirm_sesskey()) {
// Add random questions to the quiz.
$structure->check_can_be_edited();
$recurse = optional_param('recurse', 0, PARAM_BOOL);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$randomcount = required_param('randomcount', PARAM_INT);
quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
quiz_delete_previews($quiz);
示例15: addRandomQuizQuestions
function addRandomQuizQuestions($cmid, $cat_id)
{
global $QTYPES;
$result = true;
$recurse = 1;
list($quiz, $cm) = get_module_from_cmid($cmid);
if ($rs = get_records('question_categories', 'parent', $cat_id, 'sortorder')) {
$course = get_record('course', 'id', $quiz->course);
foreach ($rs as $cat) {
$categoryid = $cat->id;
$randomcount = 1;
// load category
if (!($category = get_record('question_categories', 'id', $categoryid))) {
error('Category ID is incorrect');
}
$catcontext = get_context_instance_by_id($category->contextid);
require_capability('moodle/question:useall', $catcontext);
$category->name = addslashes($category->name);
// Find existing random questions in this category that are not used by any quiz.
if ($existingquestions = get_records_sql("SELECT * FROM " . $CFG->prefix . "question q\n WHERE qtype = '" . RANDOM . "'\n AND category = {$category->id}\n AND " . sql_compare_text('questiontext') . " = '{$recurse}'\n AND NOT EXISTS (SELECT * FROM " . $CFG->prefix . "quiz_question_instances WHERE question = q.id)\n ORDER BY id")) {
// Take as many of these as needed.
while ($existingquestion = array_shift($existingquestions) and $randomcount > 0) {
if (!quiz_add_quiz_question($existingquestion->id, $quiz)) {
$result = false;
}
$randomcount--;
}
}
// If more are needed, create them.
if ($randomcount > 0) {
//echo "NOT EXISTING:".$cat->id."<br/>";
$form->questiontext = $recurse;
// we use the questiontext field to store the info
// on whether to include questions in subcategories
$form->questiontextformat = 0;
$form->image = '';
$form->defaultgrade = 1;
$form->hidden = 1;
for ($i = 0; $i < $randomcount; $i++) {
$form->category = "{$category->id},{$category->contextid}";
$form->stamp = make_unique_id_code();
// Set the unique code (not to be changed)
$question = new stdClass();
$question->qtype = RANDOM;
$question = $QTYPES[RANDOM]->save_question($question, $form, $course);
if (!isset($question->id)) {
error('Could not insert new random question!');
$result = false;
}
//quiz_add_quiz_question($question->id, $quiz);
if (!quiz_add_quiz_question($question->id, $quiz)) {
$result = false;
}
}
}
$significantchangemade = true;
}
}
return $result;
/*
$result = true;
$recurse = 1;
list($quiz, $cm) = get_module_from_cmid($cmid);
if ($rs = get_recordset('question_categories','parent',$cat_id)) {
$course = get_record('course','id',$quiz->course);
foreach ($rs as $category) {
echo $category->name."<br/>";
/// Add random questions to the quiz
// - Source from: /mod/quiz/edit.php
$randomcount = 1;
// load category
//$catcontext = get_context_instance_by_id($category->contextid);
//require_capability('moodle/question:useall', $catcontext);
$category->name = $category->name;
// Find existing random questions in this category that are
// not used by any quiz.
if ($existingquestions = get_records_sql(
"SELECT q.id,q.qtype FROM {question} q
WHERE qtype = '" . RANDOM . "'
AND category = ?
AND " . $DB->sql_compare_text('questiontext') . " = ?
AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id)
ORDER BY id", array($category->id, $recurse))) {
// Take as many of these as needed.
while (($existingquestion = array_shift($existingquestions)) && $randomcount > 0) {
//quiz_add_quiz_question($existingquestion->id, $quiz);
if(!quiz_add_quiz_question($existingquestion->id, $quiz)){
$result = false;
}
$randomcount--;
}
}
// If more are needed, create them.
if ($randomcount > 0) {
$form->questiontext = $recurse; // we use the questiontext field
// to store the info on whether to include
// questions in subcategories
$form->questiontextformat = 0;
$form->image = '';
$form->defaultgrade = 1;
//.........这里部分代码省略.........