From 9c89fba92a4c7f7735b720d45efb6152c153d0c2 Mon Sep 17 00:00:00 2001 From: Safat Shahin Date: Thu, 28 Jul 2022 16:58:54 +1000 Subject: [PATCH] MDL-75306 qbank_usage: Last used column This commit will implement Last used column and filterable object. --- question/bank/usage/classes/helper.php | 18 ++++++ .../bank/usage/classes/output/renderer.php | 12 +++- .../bank/usage/classes/plugin_feature.php | 8 ++- .../classes/question_last_used_column.php | 58 +++++++++++++++++++ question/bank/usage/lang/en/qbank_usage.php | 3 + question/bank/usage/styles.css | 4 ++ .../bank/usage/templates/last_used.mustache | 32 ++++++++++ .../behat/question_last_used_column.feature | 31 ++++++++++ 8 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 question/bank/usage/classes/question_last_used_column.php create mode 100644 question/bank/usage/styles.css create mode 100644 question/bank/usage/templates/last_used.mustache create mode 100644 question/bank/usage/tests/behat/question_last_used_column.feature diff --git a/question/bank/usage/classes/helper.php b/question/bank/usage/classes/helper.php index 9f523badde6..6f935fc1878 100644 --- a/question/bank/usage/classes/helper.php +++ b/question/bank/usage/classes/helper.php @@ -134,4 +134,22 @@ class helper { 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; + } + } diff --git a/question/bank/usage/classes/output/renderer.php b/question/bank/usage/classes/output/renderer.php index 58c511b465f..33ea2364526 100644 --- a/question/bank/usage/classes/output/renderer.php +++ b/question/bank/usage/classes/output/renderer.php @@ -17,7 +17,7 @@ namespace qbank_usage\output; /** - * Class renderer + * Renderer for usage plugin. * * @package qbank_usage * @copyright 2021 Catalyst IT Australia Pty Ltd @@ -36,4 +36,14 @@ class renderer extends \plugin_renderer_base { 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); + } + } diff --git a/question/bank/usage/classes/plugin_feature.php b/question/bank/usage/classes/plugin_feature.php index 21a39ce9967..31ba4b390da 100644 --- a/question/bank/usage/classes/plugin_feature.php +++ b/question/bank/usage/classes/plugin_feature.php @@ -16,6 +16,8 @@ namespace qbank_usage; +use core_question\local\bank\view; + /** * Class plugin_feature is the entrypoint for the columns. * @@ -26,9 +28,11 @@ namespace qbank_usage; */ class plugin_feature extends \core_question\local\bank\plugin_features_base { - public function get_question_columns($qbank): array { + public function get_question_columns(view $qbank): array { return [ - new question_usage_column($qbank) + new question_usage_column($qbank), + new question_last_used_column($qbank) ]; } + } diff --git a/question/bank/usage/classes/question_last_used_column.php b/question/bank/usage/classes/question_last_used_column.php new file mode 100644 index 00000000000..e87bce1b745 --- /dev/null +++ b/question/bank/usage/classes/question_last_used_column.php @@ -0,0 +1,58 @@ +. + +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 + * @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']; + } + +} diff --git a/question/bank/usage/lang/en/qbank_usage.php b/question/bank/usage/lang/en/qbank_usage.php index 617fa70ae7a..4d8c01163c7 100644 --- a/question/bank/usage/lang/en/qbank_usage.php +++ b/question/bank/usage/lang/en/qbank_usage.php @@ -24,9 +24,12 @@ */ $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'] = 'When was the question last attempted by a student.'; $string['usageheader'] = 'Question usage'; // Table. diff --git a/question/bank/usage/styles.css b/question/bank/usage/styles.css new file mode 100644 index 00000000000..3f9e0ea49a2 --- /dev/null +++ b/question/bank/usage/styles.css @@ -0,0 +1,4 @@ +#categoryquestions td.questionlastused span.date { + font-weight: 400; + font-size: .8em; +} diff --git a/question/bank/usage/templates/last_used.mustache b/question/bank/usage/templates/last_used.mustache new file mode 100644 index 00000000000..652c9001fd8 --- /dev/null +++ b/question/bank/usage/templates/last_used.mustache @@ -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 . +}} +{{! + @template qbank_usage/last_used + + Example context (json): + { + "displaydata": [ + { + "lastused": "Never" + } + ] + } +}} + + + {{lastused}} + diff --git a/question/bank/usage/tests/behat/question_last_used_column.feature b/question/bank/usage/tests/behat/question_last_used_column.feature new file mode 100644 index 00000000000..a6e13db01e2 --- /dev/null +++ b/question/bank/usage/tests/behat/question_last_used_column.feature @@ -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"