本文整理汇总了PHP中question_engine::make_questions_usage_by_activity方法的典型用法代码示例。如果您正苦于以下问题:PHP question_engine::make_questions_usage_by_activity方法的具体用法?PHP question_engine::make_questions_usage_by_activity怎么用?PHP question_engine::make_questions_usage_by_activity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类question_engine
的用法示例。
在下文中一共展示了question_engine::make_questions_usage_by_activity方法的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: 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);
}
示例3: test_core_question_update_flag
/**
* Test update question flag
*/
public function test_core_question_update_flag()
{
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
// Create a question category.
$cat = $questiongenerator->create_question_category();
$quba = question_engine::make_questions_usage_by_activity('core_question_update_flag', context_system::instance());
$quba->set_preferred_behaviour('deferredfeedback');
$questiondata = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
$question = question_bank::load_question($questiondata->id);
$slot = $quba->add_question($question);
$qa = $quba->get_question_attempt($slot);
self::setUser($this->student);
$quba->start_all_questions();
question_engine::save_questions_usage_by_activity($quba);
$qubaid = $quba->get_id();
$questionid = $question->id;
$qaid = $qa->get_database_id();
$checksum = md5($qubaid . "_" . $this->student->secret . "_" . $questionid . "_" . $qaid . "_" . $slot);
$flag = core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true);
$this->assertTrue($flag['status']);
// Test invalid checksum.
try {
// Using random_string to force failing.
$checksum = md5($qubaid . "_" . random_string(11) . "_" . $questionid . "_" . $qaid . "_" . $slot);
core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true);
$this->fail('Exception expected due to invalid checksum.');
} catch (moodle_exception $e) {
$this->assertEquals('errorsavingflags', $e->errorcode);
}
}
示例4: test_set_max_mark_in_attempts
/**
* We create two usages, each with two questions, a short-answer marked
* out of 5, and and essay marked out of 10. We just start these attempts.
*
* Then we change the max mark for the short-answer question in one of the
* usages to 20, using a qubaid_list, and verify.
*
* Then we change the max mark for the essay question in the other
* usage to 2, using a qubaid_join, and verify.
*/
public function test_set_max_mark_in_attempts()
{
// Set up some things the tests will need.
$this->resetAfterTest();
$dm = new question_engine_data_mapper();
// Create the questions.
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $generator->create_question_category();
$sa = $generator->create_question('shortanswer', null, array('category' => $cat->id));
$essay = $generator->create_question('essay', null, array('category' => $cat->id));
// Create the first usage.
$q = question_bank::load_question($sa->id);
$this->start_attempt_at_question($q, 'interactive', 5);
$q = question_bank::load_question($essay->id);
$this->start_attempt_at_question($q, 'interactive', 10);
$this->finish();
$this->save_quba();
$usage1id = $this->quba->get_id();
// Create the second usage.
$this->quba = question_engine::make_questions_usage_by_activity('unit_test', context_system::instance());
$q = question_bank::load_question($sa->id);
$this->start_attempt_at_question($q, 'interactive', 5);
$this->process_submission(array('answer' => 'fish'));
$q = question_bank::load_question($essay->id);
$this->start_attempt_at_question($q, 'interactive', 10);
$this->finish();
$this->save_quba();
$usage2id = $this->quba->get_id();
// Test set_max_mark_in_attempts with a qubaid_list.
$usagestoupdate = new qubaid_list(array($usage1id));
$dm->set_max_mark_in_attempts($usagestoupdate, 1, 20.0);
$quba1 = question_engine::load_questions_usage_by_activity($usage1id);
$quba2 = question_engine::load_questions_usage_by_activity($usage2id);
$this->assertEquals(20, $quba1->get_question_max_mark(1));
$this->assertEquals(10, $quba1->get_question_max_mark(2));
$this->assertEquals(5, $quba2->get_question_max_mark(1));
$this->assertEquals(10, $quba2->get_question_max_mark(2));
// Test set_max_mark_in_attempts with a qubaid_join.
$usagestoupdate = new qubaid_join('{question_usages} qu', 'qu.id', 'qu.id = :usageid', array('usageid' => $usage2id));
$dm->set_max_mark_in_attempts($usagestoupdate, 2, 2.0);
$quba1 = question_engine::load_questions_usage_by_activity($usage1id);
$quba2 = question_engine::load_questions_usage_by_activity($usage2id);
$this->assertEquals(20, $quba1->get_question_max_mark(1));
$this->assertEquals(10, $quba1->get_question_max_mark(2));
$this->assertEquals(5, $quba2->get_question_max_mark(1));
$this->assertEquals(2, $quba2->get_question_max_mark(2));
// Test the nothing to do case.
$usagestoupdate = new qubaid_join('{question_usages} qu', 'qu.id', 'qu.id = :usageid', array('usageid' => -1));
$dm->set_max_mark_in_attempts($usagestoupdate, 2, 2.0);
$quba1 = question_engine::load_questions_usage_by_activity($usage1id);
$quba2 = question_engine::load_questions_usage_by_activity($usage2id);
$this->assertEquals(20, $quba1->get_question_max_mark(1));
$this->assertEquals(10, $quba1->get_question_max_mark(2));
$this->assertEquals(5, $quba2->get_question_max_mark(1));
$this->assertEquals(2, $quba2->get_question_max_mark(2));
}
示例5: setUp
protected function setUp()
{
$this->quba = question_engine::make_questions_usage_by_activity('unit_test', context_system::instance());
$this->quba->set_preferred_behaviour('deferredfeedback');
$slot = $this->quba->add_question(test_question_maker::make_question('description'));
$this->qas[$slot] = $this->quba->get_question_attempt($slot);
$slot = $this->quba->add_question(test_question_maker::make_question('description'));
$this->qas[$slot] = $this->quba->get_question_attempt($slot);
$this->iterator = $this->quba->get_attempt_iterator();
}
示例6: setUp
public function setUp()
{
$this->quba = question_engine::make_questions_usage_by_activity('unit_test', get_context_instance(CONTEXT_SYSTEM));
$this->quba->set_preferred_behaviour('deferredfeedback');
$slot = $this->quba->add_question(test_question_maker::make_a_description_question());
$this->qas[$slot] = $this->quba->get_question_attempt($slot);
$slot = $this->quba->add_question(test_question_maker::make_a_description_question());
$this->qas[$slot] = $this->quba->get_question_attempt($slot);
$this->iterator = $this->quba->get_attempt_iterator();
}
示例7: test_reporting_queries
/**
* Test the various methods that load data for reporting.
*
* Since these methods need an expensive set-up, and then only do read-only
* operations on the data, we use a single method to do the set-up, which
* calls diffents methods to test each query.
*/
public function test_reporting_queries()
{
// We create two usages, each with two questions, a short-answer marked
// out of 5, and and essay marked out of 10.
//
// In the first usage, the student answers the short-answer
// question correctly, and enters something in the essay.
//
// In the second useage, the student answers the short-answer question
// wrongly, and leaves the essay blank.
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $generator->create_question_category();
$this->sa = $generator->create_question('shortanswer', null, array('category' => $cat->id));
$this->essay = $generator->create_question('essay', null, array('category' => $cat->id));
$this->usageids = array();
// Create the first usage.
$q = question_bank::load_question($this->sa->id);
$this->start_attempt_at_question($q, 'interactive', 5);
$this->allslots[] = $this->slot;
$this->process_submission(array('answer' => 'cat'));
$this->process_submission(array('answer' => 'frog', '-submit' => 1));
$q = question_bank::load_question($this->essay->id);
$this->start_attempt_at_question($q, 'interactive', 10);
$this->allslots[] = $this->slot;
$this->process_submission(array('answer' => '<p>The cat sat on the mat.</p>', 'answerformat' => FORMAT_HTML));
$this->finish();
$this->save_quba();
$this->usageids[] = $this->quba->get_id();
// Create the second usage.
$this->quba = question_engine::make_questions_usage_by_activity('unit_test', context_system::instance());
$q = question_bank::load_question($this->sa->id);
$this->start_attempt_at_question($q, 'interactive', 5);
$this->process_submission(array('answer' => 'fish'));
$q = question_bank::load_question($this->essay->id);
$this->start_attempt_at_question($q, 'interactive', 10);
$this->finish();
$this->save_quba();
$this->usageids[] = $this->quba->get_id();
// Set up some things the tests will need.
$this->dm = new question_engine_data_mapper();
$this->bothusages = new qubaid_list($this->usageids);
// Now test the various queries.
$this->dotest_load_questions_usages_latest_steps();
$this->dotest_load_questions_usages_question_state_summary();
$this->dotest_load_questions_usages_where_question_in_state();
$this->dotest_load_average_marks();
$this->dotest_sum_usage_marks_subquery();
$this->dotest_question_attempt_latest_state_view();
}
示例8: test_get_questions_from_categories_with_usage_counts
public function test_get_questions_from_categories_with_usage_counts()
{
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $generator->create_question_category();
$questiondata1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
$questiondata2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
$questiondata3 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
$quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
$quba->set_preferred_behaviour('deferredfeedback');
$question1 = question_bank::load_question($questiondata1->id);
$question3 = question_bank::load_question($questiondata3->id);
$quba->add_question($question1);
$quba->add_question($question1);
$quba->add_question($question3);
$quba->start_all_questions();
question_engine::save_questions_usage_by_activity($quba);
$this->assertEquals(array($questiondata2->id => 0, $questiondata3->id => 1, $questiondata1->id => 2), question_bank::get_finder()->get_questions_from_categories_with_usage_counts(array($cat->id), new qubaid_list(array($quba->get_id()))));
}
示例9: __construct
/**
* Construct the class. if a dbattempt object is passed in set it, otherwise initialize empty class
*
* @param questionmanager $questionmanager
* @param \stdClass
* @param \context_module $context
*/
public function __construct($questionmanager, $dbattempt = null, $context = null)
{
$this->questionmanager = $questionmanager;
$this->context = $context;
// if empty create new attempt
if (empty($dbattempt)) {
$this->attempt = new \stdClass();
// create a new quba since we're creating a new attempt
$this->quba = \question_engine::make_questions_usage_by_activity('mod_activequiz', $this->questionmanager->getRTQ()->getContext());
$this->quba->set_preferred_behaviour('immediatefeedback');
$attemptlayout = $this->questionmanager->add_questions_to_quba($this->quba);
// add the attempt layout to this instance
$this->attempt->qubalayout = implode(',', $attemptlayout);
} else {
// else load it up in this class instance
$this->attempt = $dbattempt;
$this->quba = \question_engine::load_questions_usage_by_activity($this->attempt->questionengid);
}
}
示例10: get_clone
public function get_clone($qinstances)
{
// The new quba doesn't have to be cloned, so we can use the parent class.
$newquba = question_engine::make_questions_usage_by_activity($this->owningcomponent, $this->context);
$newquba->set_preferred_behaviour('immediatefeedback');
foreach ($this->get_slots() as $slot) {
$slotquestion = $this->get_question($slot);
$attempt = $this->get_question_attempt($slot);
// We have to check for the type because we might have old migrated templates
// that could contain description questions.
if ($slotquestion->get_type_name() == 'multichoice' || $slotquestion->get_type_name() == 'multichoiceset') {
$order = $slotquestion->get_order($attempt);
// Order of the answers.
$order = implode(',', $order);
$newslot = $newquba->add_question($slotquestion, $qinstances[$slotquestion->id]->maxmark);
$qa = $newquba->get_question_attempt($newslot);
$qa->start('immediatefeedback', 1, array('_order' => $order));
}
}
question_engine::save_questions_usage_by_activity($newquba);
return $newquba;
}
示例11: test_load_with_unnecessary_autosaved_data
public function test_load_with_unnecessary_autosaved_data()
{
// The point here is that the somehow (probably due to two things
// happening concurrently, we have autosaved data in the database that
// has already been superceded by real data, so it should be ignored.
// There is also a second lot of redundant data to delete.
$records = new question_test_recordset(array(array('questionattemptid', 'contextid', 'questionusageid', 'slot', 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged', 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 5, -2, 'complete', null, 1256233715, 1, 'answer', '0'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 4, -1, 'complete', null, 1256233715, 1, 'answer', '0'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 2, 1, 'complete', null, 1256233705, 1, 'answer', '1'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 1, '', '', '', 1256233790, 3, 2, 'complete', null, 1256233710, 1, 'answer', '0')));
$question = test_question_maker::make_question('truefalse', 'true');
$question->id = -1;
question_bank::start_unit_test();
question_bank::load_test_question_data($question);
$observer = new testable_question_engine_unit_of_work(question_engine::make_questions_usage_by_activity('unit_test', context_system::instance()));
$qa = question_attempt::load_from_records($records, 1, $observer, 'deferredfeedback');
question_bank::end_unit_test();
$this->assertEquals($question->questiontext, $qa->get_question()->questiontext);
$this->assertEquals(3, $qa->get_num_steps());
$this->assertFalse($qa->has_autosaved_step());
$step = $qa->get_step(0);
$this->assertEquals(question_state::$todo, $step->get_state());
$this->assertNull($step->get_fraction());
$this->assertEquals(1256233700, $step->get_timecreated());
$this->assertEquals(1, $step->get_user_id());
$this->assertEquals(array(), $step->get_all_data());
$step = $qa->get_step(1);
$this->assertEquals(question_state::$complete, $step->get_state());
$this->assertNull($step->get_fraction());
$this->assertEquals(1256233705, $step->get_timecreated());
$this->assertEquals(1, $step->get_user_id());
$this->assertEquals(array('answer' => '1'), $step->get_all_data());
$step = $qa->get_step(2);
$this->assertEquals(question_state::$complete, $step->get_state());
$this->assertNull($step->get_fraction());
$this->assertEquals(1256233710, $step->get_timecreated());
$this->assertEquals(1, $step->get_user_id());
$this->assertEquals(array('answer' => '0'), $step->get_all_data());
$this->assertEquals(2, count($observer->get_steps_deleted()));
}
示例12: can_finish_during_the_attempt
/**
* Whether it is possible for another question to depend on this one finishing.
* Note that the answer is not exact, because of random questions, and sometimes
* questions cannot be depended upon because of quiz options.
* @param int $slotnumber the index of the slot in question.
* @return bool can this question finish naturally during the attempt?
*/
public function can_finish_during_the_attempt($slotnumber)
{
if ($this->quizobj->get_quiz()->shufflequestions || $this->quizobj->get_navigation_method() == QUIZ_NAVMETHOD_SEQ) {
return false;
}
if ($this->get_question_type_for_slot($slotnumber) == 'random') {
return true;
}
if (isset($this->slotsinorder[$slotnumber]->canfinish)) {
return $this->slotsinorder[$slotnumber]->canfinish;
}
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $this->quizobj->get_context());
$tempslot = $quba->add_question(\question_bank::load_question($this->slotsinorder[$slotnumber]->questionid));
$quba->set_preferred_behaviour($this->quizobj->get_quiz()->preferredbehaviour);
$quba->start_all_questions();
$this->slotsinorder[$slotnumber]->canfinish = $quba->can_question_finish_during_attempt($tempslot);
return $this->slotsinorder[$slotnumber]->canfinish;
}
示例13: test_get_attempt_access_information
/**
* Test get_attempt_access_information
*/
public function test_get_attempt_access_information()
{
global $DB;
// Create a new quiz with attempts.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$data = array('course' => $this->course->id, 'sumgrades' => 2);
$quiz = $quizgenerator->create_instance($data);
// Create some questions.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
quiz_add_quiz_question($question->id, $quiz);
$question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
quiz_add_quiz_question($question->id, $quiz);
// Add new question types in the category (for the random one).
$question = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id));
$question = $questiongenerator->create_question('essay', null, array('category' => $cat->id));
$question = $questiongenerator->create_question('random', null, array('category' => $cat->id));
quiz_add_quiz_question($question->id, $quiz);
$quizobj = quiz::create($quiz->id, $this->student->id);
// Set grade to pass.
$item = grade_item::fetch(array('courseid' => $this->course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null));
$item->gradepass = 80;
$item->update();
$this->setUser($this->student);
// Default restrictions (none).
$result = mod_quiz_external::get_attempt_access_information($quiz->id);
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_access_information_returns(), $result);
$expected = array('isfinished' => false, 'preventnewattemptreasons' => [], 'warnings' => []);
$this->assertEquals($expected, $result);
// Limited attempts.
$quiz->attempts = 1;
$DB->update_record('quiz', $quiz);
// Now, do one 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, false, $this->student->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
// Process some responses from the student.
$attemptobj = quiz_attempt::create($attempt->id);
$tosubmit = array(1 => array('answer' => '3.14'));
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
$attemptobj->process_finish($timenow, false);
// Can we start a new attempt? We shall not!
$result = mod_quiz_external::get_attempt_access_information($quiz->id, $attempt->id);
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_access_information_returns(), $result);
// Now new attemps allowed.
$this->assertCount(1, $result['preventnewattemptreasons']);
$this->assertFalse($result['ispreflightcheckrequired']);
$this->assertEquals(get_string('nomoreattempts', 'quiz'), $result['preventnewattemptreasons'][0]);
}
示例14: setUp
protected function setUp()
{
parent::setUp();
$this->resetAfterTest(true);
$this->displayoptions = new question_display_options();
$this->quba = question_engine::make_questions_usage_by_activity('unit_test', context_system::instance());
}
示例15: create_printable_copy
/**
* Creates a single printable copy of the given quiz.
*
* @return The ID of the created printable question usage.
*
*/
protected function create_printable_copy($shuffle_mode = quiz_papercopy_shuffle_modes::MODE_SHUFFLE_IGNORE_PAGES, $fix_descriptions = false, $fix_first = false, $fix_last = false)
{
//get a reference to the current user
global $USER;
//create a new usage object, which will allow us to create a psuedoquiz in the same context as the online quiz
$usage = question_engine::make_questions_usage_by_activity('mod_quiz', $this->context);
//and set the grading mode to "deferred feedback", the standard for paper quizzes
//this makes sense, since our paradigm is duriven by the idea that feedback is only offered once a paper quiz has been uploaded/graded
$usage->set_preferred_behaviour('deferredfeedback');
//get an array of questions in the current quiz
$quiz_questions = $this->quizobj->get_questions();
//randomize the question order, as requested
$quiz_questions = self::shuffle_questions($quiz_questions, $this->quiz->questions, $shuffle_mode, $fix_descriptions, $fix_first, $fix_last);
//for each question in our online quiz
foreach ($quiz_questions as $slot => $qdata) {
$question = question_bank::make_question($qdata);
//add the new question instance to our new printable copy, keeping the maximum grade from the quiz
//TODO: respect maximum marks
$usage->add_question($question);
}
//initialize each of the questions
$usage->start_all_questions();
//save the usage to the database
question_engine::save_questions_usage_by_activity($usage);
//return the ID of the newly created questions usage
return $usage->get_id();
}