MDL-74941 qtype_calculated: Fix for usage calculation
The patch for the helper functions is by Nathan Nguyen <[email protected]> whom I wish to thank.
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
This file describes core qbank plugin changes in /question/bank/*,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 4.1 ===
|
||||
* New functions qbank_usage\helper::get_question_bank_usage_sql and
|
||||
qbank_usage\helper::get_question_attempt_usage_sql have been implemented.
|
||||
When calling a query with the SQL those methods returned, you have to be sure
|
||||
in the accompanying $params array are in the right order.
|
||||
|
||||
=== 4.0 ===
|
||||
|
||||
* This plugin type is new in Moodle 4.0. It is for adding features to the question bank.
|
||||
|
||||
@@ -46,28 +46,9 @@ class helper {
|
||||
* @return string
|
||||
*/
|
||||
public static function question_usage_sql(): string {
|
||||
$sqlset = "(SELECT qz.id as quizid,
|
||||
qz.name as modulename,
|
||||
qz.course as courseid
|
||||
FROM {quiz} qz
|
||||
JOIN {quiz_attempts} qa ON qa.quiz = qz.id
|
||||
JOIN {question_usages} qu ON qu.id = qa.uniqueid
|
||||
JOIN {question_attempts} qatt ON qatt.questionusageid = qu.id
|
||||
JOIN {question} q ON q.id = qatt.questionid
|
||||
WHERE qa.preview = 0
|
||||
AND q.id = ?)
|
||||
UNION
|
||||
(SELECT qz.id as quizid,
|
||||
qz.name as modulename,
|
||||
qz.course as courseid
|
||||
FROM {quiz_slots} slot
|
||||
JOIN {quiz} qz ON qz.id = slot.quizid
|
||||
JOIN {question_references} qr ON qr.itemid = slot.id
|
||||
JOIN {question_bank_entries} qbe ON qbe.id = qr.questionbankentryid
|
||||
JOIN {question_versions} qv ON qv.questionbankentryid = qbe.id
|
||||
WHERE qv.questionbankentryid = ?
|
||||
AND qr.component = ?
|
||||
AND qr.questionarea = ?)";
|
||||
$sqlset = "(". self::get_question_attempt_usage_sql() .")".
|
||||
"UNION".
|
||||
"(". self::get_question_bank_usage_sql() .")";
|
||||
return $sqlset;
|
||||
}
|
||||
|
||||
@@ -105,4 +86,52 @@ class helper {
|
||||
return $DB->count_records_sql($sql, $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the question bank usage sql.
|
||||
*
|
||||
* The resulting string which represents a sql query has then to be
|
||||
* called accompanying a $params array which includes the necessary
|
||||
* parameters in the correct order which are the question id, then
|
||||
* the component and finally the question area.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_question_bank_usage_sql(): string {
|
||||
$sql = "SELECT qz.id as quizid,
|
||||
qz.name as modulename,
|
||||
qz.course as courseid
|
||||
FROM {quiz_slots} slot
|
||||
JOIN {quiz} qz ON qz.id = slot.quizid
|
||||
JOIN {question_references} qr ON qr.itemid = slot.id
|
||||
JOIN {question_bank_entries} qbe ON qbe.id = qr.questionbankentryid
|
||||
JOIN {question_versions} qv ON qv.questionbankentryid = qbe.id
|
||||
WHERE qv.questionbankentryid = ?
|
||||
AND qr.component = ?
|
||||
AND qr.questionarea = ?";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the question attempt usage sql.
|
||||
*
|
||||
* The resulting string which represents a sql query has then to be
|
||||
* called accompanying a $params array which includes the necessary
|
||||
* parameter, the question id.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_question_attempt_usage_sql(): string {
|
||||
$sql = "SELECT qz.id as quizid,
|
||||
qz.name as modulename,
|
||||
qz.course as courseid
|
||||
FROM {quiz} qz
|
||||
JOIN {quiz_attempts} qa ON qa.quiz = qz.id
|
||||
JOIN {question_usages} qu ON qu.id = qa.uniqueid
|
||||
JOIN {question_attempts} qatt ON qatt.questionusageid = qu.id
|
||||
JOIN {question} q ON q.id = qatt.questionid
|
||||
WHERE qa.preview = 0
|
||||
AND q.id = ?";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1759,13 +1759,10 @@ class qtype_calculated extends question_type {
|
||||
$line++;
|
||||
$text .= "<td align=\"left\" style=\"white-space:nowrap;\">{$questionname}</td>";
|
||||
// TODO MDL-43779 should not have quiz-specific code here.
|
||||
$nbofquiz = $DB->count_records('quiz_slots', array('questionid' => $qu->id));
|
||||
$nbofattempts = $DB->count_records_sql("
|
||||
SELECT count(1)
|
||||
FROM {quiz_slots} slot
|
||||
JOIN {quiz_attempts} quiza ON quiza.quiz = slot.quizid
|
||||
WHERE slot.questionid = ?
|
||||
AND quiza.preview = 0", array($qu->id));
|
||||
$sql = 'SELECT COUNT(*) FROM (' . qbank_usage\helper::get_question_bank_usage_sql() . ') questioncount';
|
||||
$nbofquiz = $DB->count_records_sql($sql, [$qu->id, 'mod_quiz', 'slot']);
|
||||
$sql = 'SELECT COUNT(*) FROM (' . qbank_usage\helper::get_question_attempt_usage_sql() . ') attemptcount';
|
||||
$nbofattempts = $DB->count_records_sql($sql, [$qu->id]);
|
||||
if ($nbofquiz > 0) {
|
||||
$text .= "<td align=\"center\">{$nbofquiz}</td>";
|
||||
$text .= "<td align=\"center\">{$nbofattempts}";
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
@qtype @qtype_calculatedmulti
|
||||
Feature: Test creating a Calculated multichoice question
|
||||
As a teacher
|
||||
In order to test my students
|
||||
I need to be able to create Calculated multichoice questions
|
||||
|
||||
Background:
|
||||
Given the following "users" exist:
|
||||
| username |
|
||||
| teacher |
|
||||
And the following "courses" exist:
|
||||
| fullname | shortname | category |
|
||||
| Course 1 | C1 | 0 |
|
||||
And the following "course enrolments" exist:
|
||||
| user | course | role |
|
||||
| teacher | C1 | editingteacher |
|
||||
And the following "question categories" exist:
|
||||
| contextlevel | reference | name |
|
||||
| Course | C1 | Test questions |
|
||||
|
||||
Scenario: Create a Calculated question
|
||||
When I am on the "Course 1" "core_question > course question bank" page logged in as teacher
|
||||
And I press "Create a new question ..."
|
||||
And I set the field "Calculated multichoice" to "1"
|
||||
And I press "Add"
|
||||
And I set the following fields to these values:
|
||||
| Question name | calculatedmulti-001 |
|
||||
| Question text | What is the sum of those two numbers: {a} and {b} |
|
||||
| Choice 1 | {a}+{b} |
|
||||
| Grade | 100% |
|
||||
| Choice 2 | {a}*{b} |
|
||||
| Choice 3 | {a}-{b} |
|
||||
And I press "id_submitbutton"
|
||||
And I should see "Choose wildcards dataset properties"
|
||||
And I press "id_submitbutton"
|
||||
And I should see "Edit the wildcards datasets"
|
||||
And I press "id_addbutton"
|
||||
And I set the following fields to these values:
|
||||
| id_number_2 | 3.0 |
|
||||
| id_number_1 | 4.0 |
|
||||
And I press "id_savechanges"
|
||||
# Checking that the wildcard values are there
|
||||
And I am on the "calculatedmulti-001" "core_question > edit" page logged in as teacher
|
||||
And I press "id_submitbutton"
|
||||
And I should see "Choose wildcards dataset properties"
|
||||
And I press "id_submitbutton"
|
||||
Then I should see "3+4"
|
||||
Reference in New Issue
Block a user