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
+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();
}
}