first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
define("qbank_usage/usage",["exports","core/fragment","core/modal_cancel","core/notification","core/str"],(function(_exports,_fragment,_modal_cancel,_notification,Str){function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}
/**
* Usage column selector js.
*
* @module qbank_usage/usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_fragment=_interopRequireDefault(_fragment),_modal_cancel=_interopRequireDefault(_modal_cancel),_notification=_interopRequireDefault(_notification),Str=function(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}newObj.default=obj,cache&&cache.set(obj,newObj);return newObj}(Str);let modal=null;const usageEvent=async(questionId,contextId,specificVersion)=>{const args={questionid:questionId,specificversion:specificVersion};if(null===modal){try{modal=await _modal_cancel.default.create({title:Str.get_string("usageheader","qbank_usage"),body:_fragment.default.loadFragment("qbank_usage","question_usage",contextId,args),large:!0,show:!0})}catch(e){return void _notification.default.exception(e)}modal.getRoot().on("click","a[href].page-link",(function(e){e.preventDefault();let attr=e.target.getAttribute("href");"#"!==attr&&(args.querystring=attr,modal.setBody(_fragment.default.loadFragment("qbank_usage","question_usage",contextId,args)))})),modal.getRoot().on("change","#question_usage_version_dropdown",(function(e){args.questionid=e.target.value,modal.setBody(_fragment.default.loadFragment("qbank_usage","question_usage",contextId,args))}))}else modal.setBody(_fragment.default.loadFragment("qbank_usage","question_usage",contextId,args)),modal.show()};_exports.init=function(){let specificVersion=arguments.length>0&&void 0!==arguments[0]&&arguments[0];const target=document.querySelector("#categoryquestions");null!==target&&target.addEventListener("click",(e=>{e.target.dataset.target&&e.target.dataset.target.includes("questionusagepreview")&&usageEvent(e.target.dataset.questionid,e.target.dataset.contextid,specificVersion)}))}}));
//# sourceMappingURL=usage.min.js.map
@@ -0,0 +1 @@
{"version":3,"file":"usage.min.js","sources":["../src/usage.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Usage column selector js.\n *\n * @module qbank_usage/usage\n * @copyright 2021 Catalyst IT Australia Pty Ltd\n * @author Safat Shahin <safatshahin@catalyst-au.net>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Fragment from 'core/fragment';\nimport ModalCancel from 'core/modal_cancel';\nimport Notification from 'core/notification';\nimport * as Str from 'core/str';\n\nlet modal = null;\n\n/**\n * Event listeners for the module.\n *\n * @method clickEvent\n * @param {int} questionId\n * @param {int} contextId\n * @param {boolean} specificVersion Is the view listing specific question versions?\n */\nconst usageEvent = async(questionId, contextId, specificVersion) => {\n const args = {\n questionid: questionId,\n specificversion: specificVersion,\n };\n if (modal === null) {\n try {\n modal = await ModalCancel.create({\n title: Str.get_string('usageheader', 'qbank_usage'),\n body: Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args),\n large: true,\n show: true,\n });\n } catch (e) {\n Notification.exception(e);\n return;\n }\n\n modal.getRoot().on('click', 'a[href].page-link', function(e) {\n e.preventDefault();\n let attr = e.target.getAttribute(\"href\");\n if (attr !== '#') {\n args.querystring = attr;\n modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));\n }\n });\n // Version selection event.\n modal.getRoot().on('change', '#question_usage_version_dropdown', function(e) {\n args.questionid = e.target.value;\n modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));\n });\n } else {\n modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));\n modal.show();\n }\n\n};\n\n/**\n * Entrypoint of the js.\n *\n * @method init\n * @param {boolean} specificVersion Is the view listing specific question versions?\n */\nexport const init = (specificVersion = false) => {\n const target = document.querySelector('#categoryquestions');\n if (target !== null) {\n target.addEventListener('click', (e) => {\n if (e.target.dataset.target && e.target.dataset.target.includes('questionusagepreview')) {\n // Call for the event listener to listed for clicks in any usage count row.\n usageEvent(e.target.dataset.questionid, e.target.dataset.contextid, specificVersion);\n }\n });\n }\n};\n"],"names":["modal","usageEvent","async","questionId","contextId","specificVersion","args","questionid","specificversion","ModalCancel","create","title","Str","get_string","body","Fragment","loadFragment","large","show","e","exception","getRoot","on","preventDefault","attr","target","getAttribute","querystring","setBody","value","document","querySelector","addEventListener","dataset","includes","contextid"],"mappings":";;;;;;;;g4BA6BIA,MAAQ,WAUNC,WAAaC,MAAMC,WAAYC,UAAWC,yBACtCC,KAAO,CACTC,WAAYJ,WACZK,gBAAiBH,oBAEP,OAAVL,MAAgB,KAEZA,YAAcS,sBAAYC,OAAO,CAC7BC,MAAOC,IAAIC,WAAW,cAAe,eACrCC,KAAMC,kBAASC,aAAa,cAAe,iBAAkBZ,UAAWE,MACxEW,OAAO,EACPC,MAAM,IAEZ,MAAOC,qCACQC,UAAUD,GAI3BnB,MAAMqB,UAAUC,GAAG,QAAS,qBAAqB,SAASH,GACtDA,EAAEI,qBACEC,KAAOL,EAAEM,OAAOC,aAAa,QACpB,MAATF,OACAlB,KAAKqB,YAAcH,KACnBxB,MAAM4B,QAAQb,kBAASC,aAAa,cAAe,iBAAkBZ,UAAWE,WAIxFN,MAAMqB,UAAUC,GAAG,SAAU,oCAAoC,SAASH,GACtEb,KAAKC,WAAaY,EAAEM,OAAOI,MAC3B7B,MAAM4B,QAAQb,kBAASC,aAAa,cAAe,iBAAkBZ,UAAWE,eAGpFN,MAAM4B,QAAQb,kBAASC,aAAa,cAAe,iBAAkBZ,UAAWE,OAChFN,MAAMkB,sBAWM,eAACb,8EACXoB,OAASK,SAASC,cAAc,sBACvB,OAAXN,QACAA,OAAOO,iBAAiB,SAAUb,IAC1BA,EAAEM,OAAOQ,QAAQR,QAAUN,EAAEM,OAAOQ,QAAQR,OAAOS,SAAS,yBAE5DjC,WAAWkB,EAAEM,OAAOQ,QAAQ1B,WAAYY,EAAEM,OAAOQ,QAAQE,UAAW9B"}
+94
View File
@@ -0,0 +1,94 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Usage column selector js.
*
* @module qbank_usage/usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Fragment from 'core/fragment';
import ModalCancel from 'core/modal_cancel';
import Notification from 'core/notification';
import * as Str from 'core/str';
let modal = null;
/**
* Event listeners for the module.
*
* @method clickEvent
* @param {int} questionId
* @param {int} contextId
* @param {boolean} specificVersion Is the view listing specific question versions?
*/
const usageEvent = async(questionId, contextId, specificVersion) => {
const args = {
questionid: questionId,
specificversion: specificVersion,
};
if (modal === null) {
try {
modal = await ModalCancel.create({
title: Str.get_string('usageheader', 'qbank_usage'),
body: Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args),
large: true,
show: true,
});
} catch (e) {
Notification.exception(e);
return;
}
modal.getRoot().on('click', 'a[href].page-link', function(e) {
e.preventDefault();
let attr = e.target.getAttribute("href");
if (attr !== '#') {
args.querystring = attr;
modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
}
});
// Version selection event.
modal.getRoot().on('change', '#question_usage_version_dropdown', function(e) {
args.questionid = e.target.value;
modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
});
} else {
modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
modal.show();
}
};
/**
* Entrypoint of the js.
*
* @method init
* @param {boolean} specificVersion Is the view listing specific question versions?
*/
export const init = (specificVersion = false) => {
const target = document.querySelector('#categoryquestions');
if (target !== null) {
target.addEventListener('click', (e) => {
if (e.target.dataset.target && e.target.dataset.target.includes('questionusagepreview')) {
// Call for the event listener to listed for clicks in any usage count row.
usageEvent(e.target.dataset.questionid, e.target.dataset.contextid, specificVersion);
}
});
}
};
+190
View File
@@ -0,0 +1,190 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
/**
* Helper class for usage.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Get the usage count for a question.
*
* @param \question_definition $question
* @param bool $specificversion Count usages just for this version of the question?
* @return int
*/
public static function get_question_entry_usage_count($question, bool $specificversion = false) {
global $DB;
$sql = 'SELECT COUNT(*) FROM (' . self::question_usage_sql($specificversion) . ') quizid';
$params = [$question->id, $question->questionbankentryid, 'mod_quiz', 'slot'];
if ($specificversion) {
$params[] = $question->id;
}
return $DB->count_records_sql($sql, $params);
}
/**
* Get the sql for usage data.
*
* @param bool $specificversion Count usages just for this version of the question?
* @return string
*/
public static function question_usage_sql(bool $specificversion = false): string {
$sqlset = "(". self::get_question_attempt_usage_sql($specificversion) .")".
"UNION".
"(". self::get_question_bank_usage_sql($specificversion) .")";
return $sqlset;
}
/**
* Get question attempt count for the question.
*
* @param int $questionid
* @param int $quizid
* @return int
*/
public static function get_question_attempts_count_in_quiz(int $questionid, $quizid = null): int {
global $DB;
if ($quizid) {
$sql = 'SELECT COUNT(qatt.id)
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 qatt.questionid = :questionid
AND qa.preview = 0
AND qz.id = :quizid';
$param = ['questionid' => $questionid, 'quizid' => $quizid];
} else {
$sql = 'SELECT COUNT(qatt.id)
FROM {quiz_slots} qs
JOIN {quiz_attempts} qa ON qa.quiz = qs.quizid
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 qatt.questionid = ?
AND qa.preview = 0';
$param = ['questionid' => $questionid];
}
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.
*
* @param bool $specificversion Count usages just for this version of the question?
* @return string
*/
public static function get_question_bank_usage_sql(bool $specificversion = false): 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 = ?";
if ($specificversion) {
// Only get results where the reference matches the specific question ID that was requested,
// or the question ID that's requested is the latest version, and the reference is set to null (always latest version).
$sql .= " AND qv.questionid = ?
AND (
qv.version = qr.version
OR (
qr.version IS NULL
AND qv.version = (
SELECT MAX(qv1.version)
FROM {question_versions} qv1
WHERE qv1.questionbankentryid = qbe.id
)
)
)";
}
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.
*
* @param bool $specificversion Count usages just for this version of the question?
* @return string
*/
public static function get_question_attempt_usage_sql(bool $specificversion = false): 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";
if ($specificversion) {
$sql .= "
JOIN {question} q ON q.id = qatt.questionid
WHERE qa.preview = 0
AND q.id = ?";
} else {
$sql .= "
JOIN {question_versions} qv ON qv.questionid = qatt.questionid
JOIN {question_versions} qv2 ON qv.questionbankentryid = qv2.questionbankentryid
WHERE qa.preview = 0
AND qv2.questionid = ?";
}
return $sql;
}
/**
* Get the question last used sql.
*
* @return string
*/
public static function get_question_last_used_sql(): string {
$sql = "SELECT MAX(qa.timemodified) as lastused
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;
}
}
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage\output;
/**
* Renderer for usage plugin.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/**
* Render the html fragment for usage modal.
*
* @param array $displaydata
* @return string
*/
public function render_usage_fragment(array $displaydata): string {
return $this->render_from_template('qbank_usage/usage_modal', $displaydata);
}
/**
* Render the question usage column.
*
* @param array $displaydata last used date or never
* @return string
*/
public function render_last_used_column(array $displaydata): string {
return $this->render_from_template('qbank_usage/last_used', $displaydata);
}
}
@@ -0,0 +1,38 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
use core_question\local\bank\view;
/**
* Class plugin_feature is the entrypoint for the columns.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_feature extends \core_question\local\bank\plugin_features_base {
public function get_question_columns(view $qbank): array {
return [
new question_usage_column($qbank),
new question_last_used_column($qbank)
];
}
}
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage\privacy;
/**
* Privacy Subsystem for qbank_usage implementing null_provider.
*
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
use core_question\local\bank\column_base;
/**
* Question bank column for the question last used.
*
* @package qbank_usage
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_last_used_column extends column_base {
public function get_name(): string {
return 'questionlastused';
}
public function get_title(): string {
return get_string('questionlastused', 'qbank_usage');
}
public function help_icon(): ?\help_icon {
return new \help_icon('questionlastused', 'qbank_usage');
}
protected function display_content($question, $rowclasses): void {
global $DB, $PAGE;
$displaydata = [];
$questionusage = $DB->get_record_sql(helper::get_question_last_used_sql(), [$question->id]);
$displaydata['lastused'] = get_string('notused', 'qbank_usage');
if (!empty($questionusage->lastused)) {
$displaydata['lastused'] = userdate($questionusage->lastused);
}
echo $PAGE->get_renderer('qbank_usage')->render_last_used_column($displaydata);
}
public function get_extra_classes(): array {
return ['pr-3'];
}
}
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
use core_question\local\bank\column_base;
/**
* A column type for the name of the question type.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_usage_column extends column_base {
/**
* Include Javascript module.
*
* @return void
*/
public function init(): void {
parent::init();
global $PAGE;
$PAGE->requires->js_call_amd('qbank_usage/usage', 'init', [
$this->qbank->is_listing_specific_versions()
]);
}
public function get_name(): string {
return 'questionusage';
}
public function get_title(): string {
return get_string('questionusage', 'qbank_usage');
}
public function help_icon(): ?\help_icon {
return new \help_icon('questionusage', 'qbank_usage');
}
protected function display_content($question, $rowclasses): void {
$usagecount = helper::get_question_entry_usage_count($question, $this->qbank->is_listing_specific_versions());
$attributes = [];
if (question_has_capability_on($question, 'view')) {
$target = 'questionusagepreview_' . $question->id;
$attributes = [
'href' => '#',
'data-target' => $target,
'data-questionid' => $question->id,
'data-courseid' => $this->qbank->course->id,
'data-contextid' => $question->contextid,
];
}
echo \html_writer::tag('a', $usagecount, $attributes);
}
public function get_extra_classes(): array {
return ['pr-3'];
}
}
@@ -0,0 +1,136 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
namespace qbank_usage\tables;
global $CFG;
require_once($CFG->libdir.'/tablelib.php');
use context_course;
use html_writer;
use moodle_url;
use qbank_usage\helper;
use table_sql;
/**
* Class question_usage_table.
* An extension of regular Moodle table.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_usage_table extends table_sql {
/**
* Search string.
*
* @var string $search
*/
public $search = '';
/**
* Question id.
*
* @var \question_definition $question
*/
public $question;
/**
* @var bool $specificversion Are we displaying the usage for a specific version, rather than all versions of the question?
*/
protected $specificversion;
/**
* constructor.
* Sets the SQL for the table and the pagination.
*
* @param string $uniqueid
* @param \question_definition $question
*/
public function __construct(string $uniqueid, \question_definition $question, bool $specificversion = false) {
global $PAGE;
parent::__construct($uniqueid);
$this->question = $question;
$columns = ['modulename', 'coursename', 'attempts'];
$headers = [
get_string('modulename', 'qbank_usage'),
get_string('coursename', 'qbank_usage'),
get_string('attempts', 'qbank_usage')
];
$this->is_collapsible = false;
$this->no_sorting('modulename');
$this->no_sorting('coursename');
$this->no_sorting('attempts');
$this->define_columns($columns);
$this->define_headers($headers);
$this->define_baseurl($PAGE->url);
$this->specificversion = $specificversion;
$this->set_attribute('id', 'question_usage_table');
}
public function query_db($pagesize, $useinitialsbar = true) {
global $DB;
if (!$this->is_downloading()) {
$total = helper::get_question_entry_usage_count($this->question, $this->specificversion);
$this->pagesize($pagesize, $total);
}
$sql = helper::question_usage_sql($this->specificversion);
$params = [$this->question->id, $this->question->questionbankentryid, 'mod_quiz', 'slot'];
if ($this->specificversion) {
$params[] = $this->question->id;
}
if (!$this->is_downloading()) {
$this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
} else {
$this->rawdata = $DB->get_records_sql($sql, $params);
}
}
public function col_modulename(\stdClass $values): string {
$cm = get_fast_modinfo($values->courseid)->instances['quiz'][$values->quizid];
return html_writer::link(new moodle_url('/mod/quiz/view.php', ['q' => $values->quizid]), $cm->get_formatted_name());
}
public function col_coursename(\stdClass $values): string {
$course = get_course($values->courseid);
$context = context_course::instance($course->id);
return html_writer::link(course_get_url($course), format_string($course->fullname, true, [
'context' => $context,
]));
}
public function col_attempts(\stdClass $values): string {
return helper::get_question_attempts_count_in_quiz($this->question->id, $values->quizid);
}
/**
* Export this data so it can be used as the context for a mustache template/fragment.
*
* @return string
*/
public function export_for_fragment(): string {
ob_start();
$this->out(10, true);
return ob_get_clean();
}
}
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component qbank_usage, language 'en'
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Question usage';
$string['notused'] = 'Never';
$string['privacy:metadata'] = 'The Question usage question bank plugin does not store any personal data.';
$string['questionusage'] = 'Usage';
$string['questionusage_help'] = 'The number of quizzes in which the question is used, with a link to open a window listing the quizzes and the number of attempts.';
$string['questionlastused'] = 'Last used';
$string['questionlastused_help'] = 'The date when the question was last attempted.';
$string['usageheader'] = 'Question usage';
// Table.
$string['modulename'] = 'Activity name';
$string['coursename'] = 'Course name';
$string['versions'] = 'Version';
$string['state'] = 'State';
$string['attempts'] = 'Attempts';
$string['questionusageversion'] = 'v{$a}';
+69
View File
@@ -0,0 +1,69 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper functions and callbacks.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Question usage fragment callback.
*
* @param array $args
* @return string rendered output
*/
function qbank_usage_output_fragment_question_usage(array $args): string {
global $USER, $PAGE, $CFG, $DB;
require_once($CFG->dirroot . '/question/engine/bank.php');
$displaydata = [];
$question = question_bank::load_question($args['questionid']);
$quba = question_engine::make_questions_usage_by_activity('core_question_preview', context_user::instance($USER->id));
$options = new \qbank_previewquestion\question_preview_options($question);
$quba->set_preferred_behaviour($options->behaviour);
$slot = $quba->add_question($question, $options->maxmark);
$quba->start_question($slot, $options->variant);
$transaction = $DB->start_delegated_transaction();
question_engine::save_questions_usage_by_activity($quba);
$transaction->allow_commit();
$displaydata['question'] = $quba->render_question($slot, $options, '1');
$specificversion = clean_param($args['specificversion'] ?? false, PARAM_BOOL);
$questionusagetable = new \qbank_usage\tables\question_usage_table('question_usage_table', $question, $specificversion);
$questionusagetable->baseurl = new moodle_url('');
if (isset($args['querystring'])) {
$querystring = preg_replace('/^\?/', '', $args['querystring']);
$params = [];
parse_str($querystring, $params);
if (isset($params['page'])) {
$questionusagetable->currpage = $params['page'];
}
}
$displaydata['tablesql'] = $questionusagetable->export_for_fragment();
$selector = \core_question\output\question_version_selection::make_for_question('question_usage_version_dropdown',
$args['questionid']);
$qbankrenderer = $PAGE->get_renderer('core_question', 'bank');
$displaydata['versionselection'] = $selector->export_for_template($qbankrenderer);
return $PAGE->get_renderer('qbank_usage')->render_usage_fragment($displaydata);
}
+4
View File
@@ -0,0 +1,4 @@
.question-bank-table td.questionlastused span.date {
font-weight: 400;
font-size: .8em;
}
@@ -0,0 +1,32 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template qbank_usage/last_used
Example context (json):
{
"displaydata": [
{
"lastused": "Never"
}
]
}
}}
<span class="date">
{{lastused}}
</span>
@@ -0,0 +1,38 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template qbank_usage/usage_modal
Example context (json):
{
"usagedata": [
{
"question": "question html"
}
]
}
}}
{{#versionselection}}
{{> core_question/question_version_selection }}
{{/versionselection}}
<div class="question-usage-preview">
{{{question}}}
</div>
<div id="question-usage_table">
{{{tablesql}}}
</div>
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../../../tests/behat/behat_question_base.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
/**
* Steps definitions to deal with the usage in question.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_qbank_usage extends behat_question_base {
/**
* Looks for a table, then looks for a row that contains the given text.
* Once it finds the right row, it clicks a link in that row.
*
* @When I click :arg1 on the usage column
* @param string $linkname
*/
public function i_click_on_the_usage_column($linkname) {
$exception = new ElementNotFoundException($this->getSession(),
'Cannot find any row on the page containing the text ' . $linkname);
$row = $this->find('css', sprintf('table tbody tr td.questionusage a:contains("%s")', $linkname), $exception);
$row->click();
}
/**
* Looks for the appropriate usage count in the column.
*
* @Then I should see :arg1 on the usage column
* @param string $linkdata
*/
public function i_should_see_on_the_usage_column($linkdata) {
$exception = new ElementNotFoundException($this->getSession(),
'Cannot find any row with the usage count of ' . $linkdata . ' on the column named Usage');
$this->find('css', sprintf('table tbody tr td.questionusage a:contains("%s")', $linkdata), $exception);
}
}
@@ -0,0 +1,31 @@
@qbank @qbank_usage @javascript
Feature: Use the qbank plugin manager page for question last used
In order to check the plugin behaviour with enable and disable
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "activities" exist:
| activity | name | course | idnumber |
| quiz | Test quiz | C1 | quiz1 |
And the following "question categories" exist:
| contextlevel | reference | name |
| Course | C1 | Test questions |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | First question | Answer the first question |
Scenario: Enable/disable question last usage column from the base view
Given I log in as "admin"
And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
And I should see "Question usage"
When I click on "Disable" "link" in the "Question usage" "table_row"
And I am on the "Test quiz" "quiz activity" page
And I navigate to "Question bank" in current page administration
Then I should not see "Last used"
And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
And I click on "Enable" "link" in the "Question usage" "table_row"
And I am on the "Test quiz" "quiz activity" page
And I navigate to "Question bank" in current page administration
And I should see "Last used"
@@ -0,0 +1,53 @@
@qbank @qbank_usage
Feature: Use the qbank plugin manager page for question usage
In order to check the plugin behaviour with enable and disable
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "activities" exist:
| activity | name | course | idnumber |
| quiz | Test quiz | C1 | quiz1 |
And the following "question categories" exist:
| contextlevel | reference | name |
| Course | C1 | Test questions |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | First question | Answer the first question |
Scenario: Enable/disable question usage column from the base view
Given I log in as "admin"
And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
And I should see "Question usage"
When I click on "Disable" "link" in the "Question usage" "table_row"
And I am on the "Test quiz" "mod_quiz > question bank" page
Then I should not see "Usage"
And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
And I click on "Enable" "link" in the "Question usage" "table_row"
And I am on the "Test quiz" "mod_quiz > question bank" page
And I should see "Usage"
@javascript
Scenario: Question usage modal should work without any usage data
And I am on the "Test quiz" "mod_quiz > question bank" page logged in as "admin"
And I apply question bank filter "Category" with value "Test questions"
And I should see "Test questions"
And I should see "0" on the usage column
When I click "0" on the usage column
Then I should see "Version 1"
And I should see "v1 (latest)" in the "Question 1" "question"
And I click on "Close" "button" in the ".modal-dialog" "css_element"
And I should see "0" on the usage column
@javascript
Scenario: Question usage modal should work with usage data
Given quiz "Test quiz" contains the following questions:
| question | page |
| First question | 1 |
And I am on the "Test quiz" "mod_quiz > question bank" page logged in as "admin"
And I apply question bank filter "Category" with value "Test questions"
And I should see "Test questions"
And I should see "1" on the usage column
When I click "1" on the usage column
Then "Test quiz" "table_row" should exist in the "question-usage_table" "region"
+260
View File
@@ -0,0 +1,260 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
defined('MOODLE_INTERNAL') || die();
use mod_quiz\quiz_attempt;
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
/**
* Helper test.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \qbank_usage\helper
*/
class helper_test extends \advanced_testcase {
use \quiz_question_helper_test_trait;
/**
* @var \stdClass $quiz
*/
protected $quiz;
/**
* @var \stdClass $user
*/
protected $user;
/**
* @var \core_question_generator $questiongenerator
*/
protected $questiongenerator;
/**
* @var array $questions
*/
protected $questions = [];
/**
* Test setup.
*/
public function setup(): void {
$this->resetAfterTest();
$layout = '1,2,0';
// Make a user to do the quiz.
$this->user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$this->quiz = $quizgenerator->create_instance(['course' => $course->id,
'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
$this->questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $this->questiongenerator->create_question_category();
$page = 1;
foreach (explode(',', $layout) as $slot) {
if ($slot == 0) {
$page += 1;
continue;
}
$question = $this->questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $this->quiz, $page);
$this->questions [] = $question;
}
}
/**
* Record a quiz attempt.
*
* @return void
*/
protected function attempt_quiz(): void {
$quizobj = \mod_quiz\quiz_settings::create($this->quiz->id, $this->user->id);
$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->user->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
quiz_attempt::create($attempt->id);
}
/**
* Test question attempt count.
*
* @covers ::get_question_attempts_count_in_quiz
*/
public function test_get_question_attempts_count_in_quiz(): void {
$this->attempt_quiz();
foreach ($this->questions as $question) {
$questionattemptcount = helper::get_question_attempts_count_in_quiz($question->id, $this->quiz->id);
// Test the attempt count matches the usage count, each question should have one count.
$this->assertEquals(1, $questionattemptcount);
}
}
/**
* Test test usage data.
*
* @covers ::get_question_entry_usage_count
*/
public function test_get_question_entry_usage_count(): void {
foreach ($this->questions as $question) {
$count = helper::get_question_entry_usage_count(\question_bank::load_question($question->id));
// Test that the attempt data matches the usage data for the count.
$this->assertEquals(1, $count);
}
}
/**
* If a question has been included via a random question attempt, this should be counted as a usage.
*
* @covers ::get_question_entry_usage_count
* @return void
*/
public function test_get_random_question_attempts_usage_count(): void {
$this->setAdminUser();
$cat = $this->questiongenerator->create_question_category();
$question = $this->questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
$this->add_random_questions($this->quiz->id, 1, $cat->id, 1);
$qdef = \question_bank::load_question($question->id);
$count = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(0, $count);
$this->attempt_quiz();
$count = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(1, $count);
}
/**
* When a question referenced directly is edited, the usage count of all versions remains the same.
*
* When checking usage of separate versions, the new version should show usages but the original version should not.
*
* @covers ::get_question_entry_usage_count
* @return void
*/
public function test_edited_question_usage_counts(): void {
foreach ($this->questions as $question) {
$qdef = \question_bank::load_question($question->id);
$count1 = helper::get_question_entry_usage_count($qdef);
// Each question should have 1 usage.
$this->assertEquals(1, $count1);
$newversion = $this->questiongenerator->update_question($question);
$newqdef = \question_bank::load_question($newversion->id);
// Either version should return the same count if not checking a specific version.
$count2 = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(1, $count2);
$count3 = helper::get_question_entry_usage_count($newqdef);
$this->assertEquals(1, $count3);
// Checking the specific version count should return the counts for each version.
// The original version is no longer included in the quiz, so has 0 usages.
$count4 = helper::get_question_entry_usage_count($qdef, true);
$this->assertEquals(0, $count4);
// The new version is now included in the quiz, so has 1 usage.
$count5 = helper::get_question_entry_usage_count($newqdef, true);
$this->assertEquals(1, $count5);
}
}
/**
* When a question referenced directly with attempts is edited, the usage count of all versions remains the same.
*
* When checking usage of separate versions, both versions should show usage.
*
* @covers ::get_question_entry_usage_count
* @return void
*/
public function test_edited_attempted_question_usage_counts(): void {
$this->attempt_quiz();
foreach ($this->questions as $question) {
$qdef = \question_bank::load_question($question->id);
$count1 = helper::get_question_entry_usage_count($qdef);
// Each question should have 1 usage.
$this->assertEquals(1, $count1);
$newversion = $this->questiongenerator->update_question($question);
$newqdef = \question_bank::load_question($newversion->id);
// Either version should return the same count if not checking a specific version.
$count2 = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(1, $count2);
$count3 = helper::get_question_entry_usage_count($newqdef);
$this->assertEquals(1, $count3);
// Checking the specific version count should return the counts for each version.
// The original version is no longer included in the quiz. However, the is still an attempt using this question version,
// so it has 1 usage.
$count4 = helper::get_question_entry_usage_count($qdef, true);
$this->assertEquals(1, $count4);
// The new version is now included in the quiz, so has 1 usage.
$count5 = helper::get_question_entry_usage_count($newqdef, true);
$this->assertEquals(1, $count5);
}
}
/**
* When a random question with attempts is edited, it should still have the same usage count.
*
* When checking usage of separate versions, the original version should still show usage but the new version should not.
*
* @covers ::get_question_entry_usage_count
* @return void
*/
public function test_edited_attempted_random_question_usage_count(): void {
$this->setAdminUser();
$cat = $this->questiongenerator->create_question_category();
$question = $this->questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
$this->add_random_questions($this->quiz->id, 1, $cat->id, 1);
$this->attempt_quiz();
$qdef = \question_bank::load_question($question->id);
$count1 = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(1, $count1);
$newversion = $this->questiongenerator->update_question($question);
$newqdef = \question_bank::load_question($newversion->id);
// Either version should return the same count if not checking a specific version.
$count2 = helper::get_question_entry_usage_count($qdef);
$this->assertEquals(1, $count2);
$count3 = helper::get_question_entry_usage_count($newqdef);
$this->assertEquals(1, $count3);
// Checking the specific version count should return the counts for each version.
// There is still an attempt of the original version has part of the random question attempt, so it has 1 usage.
$count4 = helper::get_question_entry_usage_count($qdef, true);
$this->assertEquals(1, $count4);
// There is no attempt of the new version, so it has 0 usages.
$count5 = helper::get_question_entry_usage_count($newqdef, true);
$this->assertEquals(0, $count5);
}
}
@@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_usage;
use mod_quiz\quiz_attempt;
/**
* Tests for the data of question usage from differnet areas like helper or usage table.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \qbank_usage\tables\question_usage_table
* @covers \qbank_usage_output_fragment_question_usage
*/
class question_usage_test extends \advanced_testcase {
/**
* Test question usage data.
*/
public function test_question_usage(): void {
global $PAGE;
$this->resetAfterTest(true);
$layout = '1,2,0';
// Make a user to do the quiz.
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(['course' => $course->id,
'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
$quizobj = \mod_quiz\quiz_settings::create($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);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$questions = [];
$page = 1;
foreach (explode(',', $layout) as $slot) {
if ($slot == 0) {
$page += 1;
continue;
}
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $quiz, $page);
$questions[] = $question;
}
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
$attemptdata = quiz_attempt::create($attempt->id);
$this->setAdminUser();
$PAGE->set_url(new \moodle_url('/'));
foreach ($questions as $question) {
$questionusagetable = qbank_usage_output_fragment_question_usage(['questionid' => $question->id]);
// Test usage table contains the quiz data which was attempted.
$this->assertStringContainsString($quiz->name, $questionusagetable);
// Test usage table contains the course data where the quiz was attempted.
$this->assertStringContainsString($course->fullname, $questionusagetable);
}
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information for qbank_usage.
*
* @package qbank_usage
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qbank_usage';
$plugin->version = 2024042200;
$plugin->requires = 2024041600;
$plugin->maturity = MATURITY_STABLE;