本文整理汇总了PHP中quiz::create方法的典型用法代码示例。如果您正苦于以下问题:PHP quiz::create方法的具体用法?PHP quiz::create怎么用?PHP quiz::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类quiz
的用法示例。
在下文中一共展示了quiz::create方法的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_quiz_get_completion_state
/**
* Test checking the completion state of a quiz.
*/
public function test_quiz_get_completion_state()
{
global $CFG, $DB;
$this->resetAfterTest(true);
// Enable completion before creating modules, otherwise the completion data is not written in DB.
$CFG->enablecompletion = true;
// Create a course and student.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$passstudent = $this->getDataGenerator()->create_user();
$failstudent = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
// Enrol students.
$this->assertTrue($this->getDataGenerator()->enrol_user($passstudent->id, $course->id, $studentrole->id));
$this->assertTrue($this->getDataGenerator()->enrol_user($failstudent->id, $course->id, $studentrole->id));
// Make a scale and an outcome.
$scale = $this->getDataGenerator()->create_scale();
$data = array('courseid' => $course->id, 'fullname' => 'Team work', 'shortname' => 'Team work', 'scaleid' => $scale->id);
$outcome = $this->getDataGenerator()->create_grade_outcome($data);
// Make a quiz with the outcome on.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$data = array('course' => $course->id, 'outcome_' . $outcome->id => 1, 'grade' => 100.0, 'questionsperpage' => 0, 'sumgrades' => 1, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionpass' => 1);
$quiz = $quizgenerator->create_instance($data);
$cm = get_coursemodule_from_id('quiz', $quiz->cmid);
// Create a couple of 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);
$quizobj = quiz::create($quiz->id, $passstudent->id);
// Set grade to pass.
$item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null));
$item->gradepass = 80;
$item->update();
// Start the passing 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, $passstudent->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);
// Start the failing 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, $failstudent->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' => '0'));
$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);
// Check the results.
$this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
$this->assertFalse(quiz_get_completion_state($course, $cm, $failstudent->id, 'return'));
}
示例4: test_start_attempt
/**
* Test start_attempt
*/
public function test_start_attempt()
{
global $DB;
// Create a new quiz with attempts.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$data = array('course' => $this->course->id, 'sumgrades' => 1);
$quiz = $quizgenerator->create_instance($data);
$context = context_module::instance($quiz->cmid);
try {
mod_quiz_external::start_attempt($quiz->id);
$this->fail('Exception expected due to missing questions.');
} catch (moodle_quiz_exception $e) {
$this->assertEquals('noquestionsfound', $e->errorcode);
}
// Create a question.
$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);
$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);
// Try to open attempt in closed quiz.
$quiz->timeopen = time() - WEEKSECS;
$quiz->timeclose = time() - DAYSECS;
$DB->update_record('quiz', $quiz);
$result = mod_quiz_external::start_attempt($quiz->id);
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
$this->assertEquals([], $result['attempt']);
$this->assertCount(1, $result['warnings']);
// Now with a password.
$quiz->timeopen = 0;
$quiz->timeclose = 0;
$quiz->password = 'abc';
$DB->update_record('quiz', $quiz);
try {
mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'bad')));
$this->fail('Exception expected due to invalid passwod.');
} catch (moodle_exception $e) {
$this->assertEquals(get_string('passworderror', 'quizaccess_password'), $e->errorcode);
}
// Now, try everything correct.
$result = mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc')));
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
$this->assertEquals(1, $result['attempt']['attempt']);
$this->assertEquals($this->student->id, $result['attempt']['userid']);
$this->assertEquals($quiz->id, $result['attempt']['quiz']);
$this->assertCount(0, $result['warnings']);
$attemptid = $result['attempt']['id'];
// We are good, try to start a new attempt now.
try {
mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc')));
$this->fail('Exception expected due to attempt not finished.');
} catch (moodle_quiz_exception $e) {
$this->assertEquals('attemptstillinprogress', $e->errorcode);
}
// Finish the started attempt.
// Process some responses from the student.
$timenow = time();
$attemptobj = quiz_attempt::create($attemptid);
$tosubmit = array(1 => array('answer' => '3.14'));
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attemptid);
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
$attemptobj->process_finish($timenow, false);
// We should be able to start a new attempt.
$result = mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc')));
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
$this->assertEquals(2, $result['attempt']['attempt']);
$this->assertEquals($this->student->id, $result['attempt']['userid']);
$this->assertEquals($quiz->id, $result['attempt']['quiz']);
$this->assertCount(0, $result['warnings']);
// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $this->studentrole->id, $context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();
try {
mod_quiz_external::start_attempt($quiz->id);
$this->fail('Exception expected due to missing capability.');
} catch (required_capability_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
}
示例5: get_quiz_required_qtypes
/**
* Return the potential question types that would be required for a given quiz.
* Please note that for random question types we return the potential question types in the category choosen.
*
* @param int $quizid quiz instance id
* @return array of warnings and the access information
* @since Moodle 3.1
* @throws moodle_quiz_exception
*/
public static function get_quiz_required_qtypes($quizid)
{
global $DB, $USER;
$warnings = array();
$params = array('quizid' => $quizid);
$params = self::validate_parameters(self::get_quiz_required_qtypes_parameters(), $params);
list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']);
$quizobj = quiz::create($cm->instance, $USER->id);
$quizobj->preload_questions();
$quizobj->load_questions();
// Question types used.
$result = array();
$result['questiontypes'] = $quizobj->get_all_question_types_used(true);
$result['warnings'] = $warnings;
return $result;
}
示例6: 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]);
}
示例7: print_error
}
if (!($cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id))) {
print_error('invalidcoursemodule');
}
}
/// Check login and get context.
require_login($course->id, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/quiz:view', $context);
/// Cache some other capabilities we use several times.
$canattempt = has_capability('mod/quiz:attempt', $context);
$canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
$canpreview = has_capability('mod/quiz:preview', $context);
/// Create an object to manage all the other (non-roles) access rules.
$timenow = time();
$accessmanager = new quiz_access_manager(quiz::create($quiz->id, $USER->id), $timenow, has_capability('mod/quiz:ignoretimelimits', $context, NULL, false));
/// If no questions have been set up yet redirect to edit.php
if (!$quiz->questions && has_capability('mod/quiz:manage', $context)) {
redirect($CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $cm->id);
}
/// Log this request.
add_to_log($course->id, "quiz", "view", "view.php?id={$cm->id}", $quiz->id, $cm->id);
// Mark module as viewed
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
/// Initialize $PAGE, compute blocks
$PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
$edit = optional_param('edit', -1, PARAM_BOOL);
if ($edit != -1 && $PAGE->user_allowed_editing()) {
$USER->editing = $edit;
}
示例8: get_quizzes_by_courses
/**
* Returns a list of quizzes in a provided list of courses,
* if no list is provided all quizzes that the user can view will be returned.
*
* @param array $courseids Array of course ids
* @return array of quizzes details
* @since Moodle 3.1
*/
public static function get_quizzes_by_courses($courseids = array())
{
global $USER;
$warnings = array();
$returnedquizzes = array();
$params = array('courseids' => $courseids);
$params = self::validate_parameters(self::get_quizzes_by_courses_parameters(), $params);
$mycourses = array();
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
// Get the quizzes in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$quizzes = get_all_instances_in_courses("quiz", $courses);
foreach ($quizzes as $quiz) {
$context = context_module::instance($quiz->coursemodule);
// Update quiz with override information.
$quiz = quiz_update_effective_access($quiz, $USER->id);
// Entry to return.
$quizdetails = array();
// First, we return information that any user can see in the web interface.
$quizdetails['id'] = $quiz->id;
$quizdetails['coursemodule'] = $quiz->coursemodule;
$quizdetails['course'] = $quiz->course;
$quizdetails['name'] = external_format_string($quiz->name, $context->id);
if (has_capability('mod/quiz:view', $context)) {
// Format intro.
list($quizdetails['intro'], $quizdetails['introformat']) = external_format_text($quiz->intro, $quiz->introformat, $context->id, 'mod_quiz', 'intro', null);
$viewablefields = array('timeopen', 'timeclose', 'grademethod', 'section', 'visible', 'groupmode', 'groupingid');
$timenow = time();
$quizobj = quiz::create($quiz->id, $USER->id);
$accessmanager = new quiz_access_manager($quizobj, $timenow, has_capability('mod/quiz:ignoretimelimits', $context, null, false));
// Fields the user could see if have access to the quiz.
if (!$accessmanager->prevent_access()) {
// Some times this function returns just empty.
$hasfeedback = quiz_has_feedback($quiz);
$quizdetails['hasfeedback'] = !empty($hasfeedback) ? 1 : 0;
$quizdetails['hasquestions'] = (int) $quizobj->has_questions();
$quizdetails['autosaveperiod'] = get_config('quiz', 'autosaveperiod');
$additionalfields = array('timelimit', 'attempts', 'attemptonlast', 'grademethod', 'decimalpoints', 'questiondecimalpoints', 'reviewattempt', 'reviewcorrectness', 'reviewmarks', 'reviewspecificfeedback', 'reviewgeneralfeedback', 'reviewrightanswer', 'reviewoverallfeedback', 'questionsperpage', 'navmethod', 'sumgrades', 'grade', 'browsersecurity', 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass', 'overduehandling', 'graceperiod', 'preferredbehaviour', 'canredoquestions');
$viewablefields = array_merge($viewablefields, $additionalfields);
}
// Fields only for managers.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('shuffleanswers', 'timecreated', 'timemodified', 'password', 'subnet');
$viewablefields = array_merge($viewablefields, $additionalfields);
}
foreach ($viewablefields as $field) {
$quizdetails[$field] = $quiz->{$field};
}
}
$returnedquizzes[] = $quizdetails;
}
}
$result = array();
$result['quizzes'] = $returnedquizzes;
$result['warnings'] = $warnings;
return $result;
}
示例9: emarking_get_enroled_students
}
// Get the users enrolled
$users = emarking_get_enroled_students($course->id);
$pbar = new progress_bar();
$pbar->create();
if ($create) {
quiz_delete_all_attempts($quiz);
}
$cur = 1;
$total = count($users);
// Insert answers or finish the attempt for each student
foreach ($users as $user) {
$pbar->update($cur, $total, get_string('processing', 'mod_emarking') . $user->lastname . $user->firstname);
flush();
// Get the quiz instance for the specific student
$quizobj = quiz::create($cm->instance, $user->id);
// Get all the attempts
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $user->id, 'all');
if ($create) {
emarking_add_user_attempt($cm, $user);
} else {
// For each attempt insert the answers or finish
foreach ($attempts as $attempt) {
if ($finish) {
emarking_finish_user_attempt($attempt->id);
}
}
}
$cur++;
}
$pbar->update_full(100, get_string('finished', 'mod_emarking'));
示例10: require_login
}
}
/// Check login and get context.
require_login($course->id, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/quiz:view', $context);
/// Cache some other capabilities we use several times.
$canattempt = has_capability('mod/quiz:attempt', $context);
$canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
$canpreview = has_capability('mod/quiz:preview', $context);
/// Create an object to manage all the other (non-roles) access rules.
$timenow = time();
$accessmanager = new quiz_access_manager(quiz::create($quiz->id, $USER->id), $timenow,
has_capability('mod/quiz:ignoretimelimits', $context, NULL, false));
/// If no questions have been set up yet redirect to edit.php
if (!$quiz->questions && has_capability('mod/quiz:manage', $context)) {
redirect($CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $cm->id);
}
/// Log this request.
add_to_log($course->id, "quiz", "view", "view.php?id=$cm->id", $quiz->id, $cm->id);
/// Initialize $PAGE, compute blocks
$PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
$edit = optional_param('edit', -1, PARAM_BOOL);
if ($edit != -1 && $PAGE->user_allowed_editing()) {
示例11: walkthrough_attempts
/**
* @param $steps PHPUnit_Extensions_Database_DataSet_ITable the step data from the csv file.
* @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
*/
protected function walkthrough_attempts($steps)
{
global $DB;
$attemptids = array();
for ($rowno = 0; $rowno < $steps->getRowCount(); $rowno++) {
$step = $this->explode_dot_separated_keys_to_make_subindexs($steps->getRow($rowno));
// Find existing user or make a new user to do the quiz.
$username = array('firstname' => $step['firstname'], 'lastname' => $step['lastname']);
if (!($user = $DB->get_record('user', $username))) {
$user = $this->getDataGenerator()->create_user($username);
}
if (!isset($attemptids[$step['quizattempt']])) {
// Start the attempt.
$quizobj = quiz::create($this->quiz->id, $user->id);
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$prevattempts = quiz_get_user_attempts($this->quiz->id, $user->id, 'all', true);
$attemptnumber = count($prevattempts) + 1;
$timenow = time();
$attempt = quiz_create_attempt($quizobj, $attemptnumber, false, $timenow, false, $user->id);
// Select variant and / or random sub question.
if (!isset($step['variants'])) {
$step['variants'] = array();
}
if (isset($step['randqs'])) {
// Replace 'names' with ids.
foreach ($step['randqs'] as $slotno => $randqname) {
$step['randqs'][$slotno] = $this->randqids[$slotno][$randqname];
}
} else {
$step['randqs'] = array();
}
quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow, $step['randqs'], $step['variants']);
quiz_attempt_save_started($quizobj, $quba, $attempt);
$attemptid = $attemptids[$step['quizattempt']] = $attempt->id;
} else {
$attemptid = $attemptids[$step['quizattempt']];
}
// Process some responses from the student.
$attemptobj = quiz_attempt::create($attemptid);
$attemptobj->process_submitted_actions($timenow, false, $step['responses']);
// Finish the attempt.
if (!isset($step['finished']) || $step['finished'] == 1) {
$attemptobj = quiz_attempt::create($attemptid);
$attemptobj->process_finish($timenow, false);
}
}
return $attemptids;
}
示例12: exportQuestionMedia
function exportQuestionMedia()
{
global $DB, $CFG, $USER, $QUIZ_CACHE, $CFG;
$cm = get_coursemodule_from_id('quiz', $this->id);
$context = context_module::instance($cm->id);
$quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
$quizobj = quiz::create($cm->instance, $USER->id);
try {
$quizobj->preload_questions();
$quizobj->load_questions();
$qs = $quizobj->get_questions();
foreach ($qs as $q) {
$this->extractMedia($q->id, $q->questiontext);
}
} catch (moodle_exception $me) {
return;
}
}
示例13: get_employeescore
function get_employeescore($id,$course,$userid) {
$cm = get_coursemodule_from_instance("quiz", $id, $course);
$quizobj = quiz::create($cm->instance, $userid);
$quiz = $quizobj->get_quiz();
$viewobj = new mod_quiz_view_object();
$attempts = quiz_get_user_attempts($id, $userid, 'finished', true);
$lastfinishedattempt = end($attempts);
$numattempts = count($attempts);
$viewobj->attempts = $attempts;
$viewobj->attemptobjs = array();
foreach ($attempts as $attempt) {
$viewobj->attemptobjs[] = new quiz_attempt($attempt, $quiz, $cm, $quiz->course, false);
}
/*Function to display attemps and grades */
$attemptgrade='';
foreach ($viewobj->attemptobjs as $attemptobj) {
$attemptgrade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
}
return $attemptgrade;
}
示例14: is_quiz_finished
function is_quiz_finished($cmid)
{
global $USER, $CFG;
require_once $CFG->dirroot . '/mod/quiz/locallib.php';
require_once $CFG->dirroot . '/mod/quiz/lib.php';
$cm = get_coursemodule_from_id('quiz', $cmid);
$quizobj = quiz::create($cm->instance, $USER->id);
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true);
foreach ($attempts as $attempt) {
if ($attempt && $attempt->state == quiz_attempt::FINISHED && $attempt->sumgrades == 1) {
return true;
}
}
return false;
}
示例15: quiz_get_completion_state
/**
* Obtains the automatic completion state for this quiz on any conditions
* in quiz settings, such as if all attempts are used or a certain grade is achieved.
*
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not. (If no conditions, then return
* value depends on comparison type)
*/
function quiz_get_completion_state($course, $cm, $userid, $type)
{
global $DB;
global $CFG;
$quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
if (!$quiz->completionattemptsexhausted && !$quiz->completionpass) {
return $type;
}
// Check if the user has used up all attempts.
if ($quiz->completionattemptsexhausted) {
$attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished', true);
if ($attempts) {
$lastfinishedattempt = end($attempts);
$context = context_module::instance($cm->id);
$quizobj = quiz::create($quiz->id, $userid);
$accessmanager = new quiz_access_manager($quizobj, time(), has_capability('mod/quiz:ignoretimelimits', $context, $userid, false));
if ($accessmanager->is_finished(count($attempts), $lastfinishedattempt)) {
return true;
}
}
}
// Check for passing grade.
if ($quiz->completionpass) {
require_once $CFG->libdir . '/gradelib.php';
$item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $cm->instance, 'outcomeid' => null));
if ($item) {
$grades = grade_grade::fetch_users_grades($item, array($userid), false);
if (!empty($grades[$userid])) {
return $grades[$userid]->is_passed($item);
}
}
}
return false;
}