mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 01:16:44 +02:00
MDL-48410 behat, quiz: step for efficiently adding questions
This commit is contained in:
parent
df44db5baa
commit
8fec847ca7
1 changed files with 51 additions and 1 deletions
|
@ -29,7 +29,8 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
|
|||
require_once(__DIR__ . '/../../../../question/tests/behat/behat_question_base.php');
|
||||
|
||||
use Behat\Behat\Context\Step\Given as Given,
|
||||
Behat\Gherkin\Node\TableNode as TableNode;
|
||||
Behat\Gherkin\Node\TableNode as TableNode,
|
||||
Behat\Mink\Exception\ExpectationException as ExpectationException;
|
||||
|
||||
/**
|
||||
* Steps definitions related to mod_quiz.
|
||||
|
@ -38,6 +39,55 @@ use Behat\Behat\Context\Step\Given as Given,
|
|||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_mod_quiz extends behat_question_base {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a question to the existing quiz with filling the form.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue