mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
Merge branch 'master_MDL-71696-versioning-integration' of https://github.com/catalyst/moodle-MDL-70329
This commit is contained in:
commit
b841a811be
223 changed files with 7768 additions and 2899 deletions
|
@ -42,25 +42,53 @@ $qtypes = question_bank::get_all_qtypes();
|
|||
$pluginmanager = core_plugin_manager::instance();
|
||||
|
||||
// Get some data we will need - question counts and which types are needed.
|
||||
$counts = $DB->get_records_sql("
|
||||
SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
|
||||
FROM {question} GROUP BY qtype", array());
|
||||
$needed = array();
|
||||
$hiddenstatus = \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN;
|
||||
$draftstatus = \core_question\local\bank\question_version_status::QUESTION_STATUS_DRAFT;
|
||||
|
||||
$sql = "SELECT result.qtype,
|
||||
SUM(result.numquestions) AS numquestions,
|
||||
SUM(result.numhidden) AS numhidden,
|
||||
SUM(result.numdraft) AS numdraft
|
||||
FROM (SELECT data.qtype,
|
||||
COUNT(data.numquestions) AS numquestions,
|
||||
(SELECT COUNT(qv.id)
|
||||
FROM {question_versions} qv
|
||||
WHERE qv.id = data.versionid
|
||||
AND qv.status = :hiddenstatus) AS numhidden,
|
||||
(SELECT COUNT(qv.id)
|
||||
FROM {question_versions} qv
|
||||
WHERE qv.id = data.versionid
|
||||
AND qv.status = :draftstatus) AS numdraft
|
||||
FROM (SELECT q.qtype, qv.id AS versionid, 1 AS numquestions
|
||||
FROM {question} q
|
||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
||||
AND qv.version = (SELECT MAX(v.version)
|
||||
FROM {question_versions} v
|
||||
JOIN {question_bank_entries} be ON be.id = v.questionbankentryid
|
||||
WHERE be.id = qbe.id)) data
|
||||
GROUP BY data.qtype, data.versionid) result
|
||||
GROUP BY result.qtype";
|
||||
|
||||
$counts = $DB->get_records_sql($sql, ['hiddenstatus' => $hiddenstatus, 'draftstatus' => $draftstatus]);
|
||||
$needed = [];
|
||||
foreach ($qtypes as $qtypename => $qtype) {
|
||||
if (!isset($counts[$qtypename])) {
|
||||
$counts[$qtypename] = new stdClass;
|
||||
$counts[$qtypename]->numquestions = 0;
|
||||
$counts[$qtypename]->numhidden = 0;
|
||||
$counts[$qtypename]->numdraft = 0;
|
||||
}
|
||||
$needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
|
||||
$pluginmanager->other_plugins_that_require($qtype->plugin_name());
|
||||
$counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
|
||||
$counts[$qtypename]->numquestions -= ($counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft);
|
||||
}
|
||||
$needed['missingtype'] = true; // The system needs the missing question type.
|
||||
foreach ($counts as $qtypename => $count) {
|
||||
if (!isset($qtypes[$qtypename])) {
|
||||
$counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
|
||||
$counts['missingtype']->numquestions += $count->numquestions - ($count->numhidden + $count->numdraft);
|
||||
$counts['missingtype']->numhidden += $count->numhidden;
|
||||
$counts['missingtype']->numdraft += $count->numdraft;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,8 +181,8 @@ foreach ($sortedqtypes as $qtypename => $localname) {
|
|||
$row[] = $icon . ' ' . $localname;
|
||||
|
||||
// Number of questions of this type.
|
||||
if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
|
||||
if ($counts[$qtypename]->numhidden > 0) {
|
||||
if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft > 0) {
|
||||
if ($counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft > 0) {
|
||||
$strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
|
||||
} else {
|
||||
$strcount = $counts[$qtypename]->numquestions;
|
||||
|
|
|
@ -20,9 +20,9 @@ use core\event\question_created;
|
|||
use core\event\question_updated;
|
||||
|
||||
/**
|
||||
* Base class for various question-related areas
|
||||
* Base class for various question-related areas.
|
||||
*
|
||||
* This is an abstract class so it will be skipped by manager when it finds all areas
|
||||
* This is an abstract class so it will be skipped by manager when it finds all areas.
|
||||
*
|
||||
* @package tool_brickfield
|
||||
* @copyright 2020 onward: Brickfield Education Labs, www.brickfield.ie
|
||||
|
@ -32,6 +32,7 @@ abstract class answerbase extends base {
|
|||
|
||||
/**
|
||||
* Get table name reference.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_ref_tablename(): string {
|
||||
|
@ -40,34 +41,38 @@ abstract class answerbase extends base {
|
|||
|
||||
/**
|
||||
* Find recordset of the relevant areas.
|
||||
*
|
||||
* @param \core\event\base $event
|
||||
* @return \moodle_recordset|null
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
|
||||
global $DB;
|
||||
|
||||
if (($event instanceof question_created) || ($event instanceof question_updated)) {
|
||||
$rs = $DB->get_recordset_sql(
|
||||
"SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
a.id AS itemid,
|
||||
{$this->get_reftable_field_sql()}
|
||||
t.id AS refid,
|
||||
{$this->get_course_and_cat_sql($event)}
|
||||
a.{$this->get_fieldname()} AS content
|
||||
FROM {question} t
|
||||
INNER JOIN {question_answers} a ON a.question = t.id
|
||||
INNER JOIN {question_categories} qc ON qc.id = t.category
|
||||
INNER JOIN {context} ctx ON ctx.id = qc.contextid
|
||||
WHERE (t.id = :refid)
|
||||
ORDER BY a.id",
|
||||
[
|
||||
'refid' => $event->objectid,
|
||||
]);
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
a.id AS itemid,
|
||||
{$this->get_reftable_field_sql()}
|
||||
q.id AS refid,
|
||||
{$this->get_course_and_cat_sql($event)}
|
||||
a.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_answers} a
|
||||
ON a.question = q.id
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
WHERE (q.id = :refid)
|
||||
ORDER BY a.id";
|
||||
|
||||
$rs = $DB->get_recordset_sql($sql, ['refid' => $event->objectid]);
|
||||
return $rs;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -75,61 +80,90 @@ abstract class answerbase extends base {
|
|||
/**
|
||||
* Return an array of area objects that contain content at the site and system levels only. This would be question content from
|
||||
* question categories at the system context, or course category context.
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_system_areas(): ?\moodle_recordset {
|
||||
global $DB;
|
||||
$select = 'SELECT ' . $this->get_type() . ' AS type, qc.contextid AS contextid, ' . $this->get_standard_area_fields_sql() .
|
||||
' a.id AS itemid, ' . $this->get_reftable_field_sql() . 't.id AS refid, '.
|
||||
SITEID . ' as courseid, cc.id as categoryid, a.'.$this->get_fieldname().' AS content ';
|
||||
$from = 'FROM {question} t ' .
|
||||
'INNER JOIN {question_answers} a ON a.question = t.id ' .
|
||||
'INNER JOIN {question_categories} qc ON qc.id = t.category ' .
|
||||
'INNER JOIN {context} ctx ON ctx.id = qc.contextid ' .
|
||||
'LEFT JOIN {course_categories} cc ON cc.id = ctx.instanceid AND ctx.contextlevel = :coursecat ';
|
||||
$where = 'WHERE (ctx.contextlevel = :syscontext) OR (ctx.contextlevel = :coursecat2) ';
|
||||
$order = 'ORDER BY a.id';
|
||||
$params = [
|
||||
'syscontext' => CONTEXT_SYSTEM,
|
||||
'coursecat' => CONTEXT_COURSECAT,
|
||||
'coursecat2' => CONTEXT_COURSECAT,
|
||||
];
|
||||
|
||||
return $DB->get_recordset_sql($select . $from . $where . $order, $params);
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
qc.contextid AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
a.id AS itemid,
|
||||
{$this->get_reftable_field_sql()}
|
||||
q.id AS refid,
|
||||
" . SITEID . " as courseid,
|
||||
cc.id as categoryid,
|
||||
a.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_answers} a
|
||||
ON a.question = q.id
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
LEFT JOIN {course_categories} cc
|
||||
ON cc.id = ctx.instanceid
|
||||
AND ctx.contextlevel = :coursecat
|
||||
WHERE (ctx.contextlevel = :syscontext)
|
||||
OR (ctx.contextlevel = :coursecat2)
|
||||
ORDER BY a.id";
|
||||
|
||||
return $DB->get_recordset_sql($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find recordset of the course areas.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @return \moodle_recordset
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_course_areas(int $courseid): ?\moodle_recordset {
|
||||
global $DB;
|
||||
|
||||
$coursecontext = \context_course::instance($courseid);
|
||||
return $DB->get_recordset_sql(
|
||||
"SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
a.id AS itemid,
|
||||
{$this->get_reftable_field_sql()}
|
||||
t.id AS refid,
|
||||
{$courseid} AS courseid,
|
||||
a.{$this->get_fieldname()} AS content
|
||||
FROM {question} t
|
||||
INNER JOIN {question_answers} a ON a.question = t.id
|
||||
INNER JOIN {question_categories} qc ON qc.id = t.category
|
||||
INNER JOIN {context} ctx ON ctx.id = qc.contextid
|
||||
WHERE (ctx.contextlevel = :ctxcourse AND ctx.id = qc.contextid AND ctx.instanceid = :courseid) OR
|
||||
(ctx.contextlevel = :module AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
|
||||
ORDER BY a.id",
|
||||
['ctxcourse' => CONTEXT_COURSE,
|
||||
'courseid' => $courseid,
|
||||
'module' => CONTEXT_MODULE,
|
||||
'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
|
||||
]);
|
||||
$param = [
|
||||
'ctxcourse' => CONTEXT_COURSE,
|
||||
'courseid' => $courseid,
|
||||
'module' => CONTEXT_MODULE,
|
||||
'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
|
||||
];
|
||||
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
a.id AS itemid,
|
||||
{$this->get_reftable_field_sql()}
|
||||
q.id AS refid,
|
||||
{$courseid} AS courseid,
|
||||
a.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_answers} a
|
||||
ON a.question = q.id
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
WHERE (ctx.contextlevel = :ctxcourse
|
||||
AND ctx.id = qc.contextid
|
||||
AND ctx.instanceid = :courseid)
|
||||
OR (ctx.contextlevel = :module
|
||||
AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
|
||||
ORDER BY a.id ASC";
|
||||
|
||||
return $DB->get_recordset_sql($sql, $param);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ use core\event\question_updated;
|
|||
use tool_brickfield\area_base;
|
||||
|
||||
/**
|
||||
* Base class for various question-related areas
|
||||
* Base class for various question-related areas.
|
||||
*
|
||||
* This is an abstract class so it will be skipped by manager when it finds all areas
|
||||
*
|
||||
|
@ -33,30 +33,32 @@ abstract class base extends area_base {
|
|||
|
||||
/**
|
||||
* Find recordset of the relevant areas.
|
||||
*
|
||||
* @param \core\event\base $event
|
||||
* @return \moodle_recordset|null
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
|
||||
global $DB;
|
||||
|
||||
if (($event instanceof question_created) || ($event instanceof question_updated)) {
|
||||
$rs = $DB->get_recordset_sql(
|
||||
"SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
t.id AS itemid,
|
||||
{$this->get_course_and_cat_sql($event)}
|
||||
t.{$this->get_fieldname()} AS content
|
||||
FROM {question} t
|
||||
INNER JOIN {question_categories} qc ON qc.id = t.category
|
||||
INNER JOIN {context} ctx ON ctx.id = qc.contextid
|
||||
WHERE (t.id = :refid)
|
||||
ORDER BY t.id",
|
||||
[
|
||||
'refid' => $event->objectid,
|
||||
]);
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
q.id AS itemid,
|
||||
{$this->get_course_and_cat_sql($event)}
|
||||
q.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
WHERE (q.id = :refid)
|
||||
ORDER BY q.id";
|
||||
|
||||
$rs = $DB->get_recordset_sql($sql, ['refid' => $event->objectid]);
|
||||
return $rs;
|
||||
}
|
||||
return null;
|
||||
|
@ -64,75 +66,97 @@ abstract class base extends area_base {
|
|||
|
||||
/**
|
||||
* Find recordset of the course areas.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @return \moodle_recordset
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_course_areas(int $courseid): ?\moodle_recordset {
|
||||
global $DB;
|
||||
|
||||
$coursecontext = \context_course::instance($courseid);
|
||||
return $DB->get_recordset_sql(
|
||||
"SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
t.id AS itemid,
|
||||
{$courseid} AS courseid,
|
||||
null AS categoryid,
|
||||
t.{$this->get_fieldname()} AS content
|
||||
FROM {question} t
|
||||
INNER JOIN {question_categories} qc ON qc.id = t.category
|
||||
INNER JOIN {context} ctx ON ctx.id = qc.contextid
|
||||
WHERE (ctx.contextlevel = :ctxcourse AND ctx.id = qc.contextid AND ctx.instanceid = :courseid) OR
|
||||
(ctx.contextlevel = :module AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
|
||||
ORDER BY t.id ASC",
|
||||
[
|
||||
'ctxcourse' => CONTEXT_COURSE,
|
||||
'courseid' => $courseid,
|
||||
'module' => CONTEXT_MODULE,
|
||||
'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
|
||||
]);
|
||||
$param = [
|
||||
'ctxcourse' => CONTEXT_COURSE,
|
||||
'courseid' => $courseid,
|
||||
'module' => CONTEXT_MODULE,
|
||||
'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
|
||||
];
|
||||
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
ctx.id AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
q.id AS itemid,
|
||||
{$courseid} AS courseid,
|
||||
null AS categoryid,
|
||||
q.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
WHERE (ctx.contextlevel = :ctxcourse
|
||||
AND ctx.id = qc.contextid
|
||||
AND ctx.instanceid = :courseid)
|
||||
OR (ctx.contextlevel = :module
|
||||
AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
|
||||
ORDER BY q.id ASC";
|
||||
|
||||
return $DB->get_recordset_sql($sql, $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of area objects that contain content at the site and system levels only. This would be question content from
|
||||
* question categories at the system context only.
|
||||
*
|
||||
* @return \moodle_recordset
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function find_system_areas(): ?\moodle_recordset {
|
||||
global $DB;
|
||||
|
||||
$select = 'SELECT ' . $this->get_type() . ' AS type, qc.contextid AS contextid, ' . $this->get_standard_area_fields_sql() .
|
||||
' t.id AS itemid, ' . SITEID . ' as courseid, cc.id as categoryid,' .
|
||||
' t.'.$this->get_fieldname().' AS content ';
|
||||
$from = 'FROM {question} t ' .
|
||||
'INNER JOIN {question_categories} qc ON qc.id = t.category ' .
|
||||
'INNER JOIN {context} ctx ON ctx.id = qc.contextid ' .
|
||||
'LEFT JOIN {course_categories} cc ON cc.id = ctx.instanceid AND ctx.contextlevel = :coursecat ';
|
||||
$where = 'WHERE (ctx.contextlevel = :syscontext) OR (ctx.contextlevel = :coursecat2) ';
|
||||
$order = 'ORDER BY t.id';
|
||||
$params = [
|
||||
'syscontext' => CONTEXT_SYSTEM,
|
||||
'coursecat' => CONTEXT_COURSECAT,
|
||||
'coursecat2' => CONTEXT_COURSECAT,
|
||||
];
|
||||
|
||||
return $DB->get_recordset_sql($select . $from . $where . $order, $params);
|
||||
$sql = "SELECT {$this->get_type()} AS type,
|
||||
qc.contextid AS contextid,
|
||||
{$this->get_standard_area_fields_sql()}
|
||||
q.id AS itemid,
|
||||
" . SITEID . " as courseid,
|
||||
cc.id as categoryid,
|
||||
q.{$this->get_fieldname()} AS content
|
||||
FROM {question} q
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
LEFT JOIN {course_categories} cc
|
||||
ON cc.id = ctx.instanceid
|
||||
AND ctx.contextlevel = :coursecat
|
||||
WHERE (ctx.contextlevel = :syscontext)
|
||||
OR (ctx.contextlevel = :coursecat2)
|
||||
ORDER BY q.id";
|
||||
|
||||
return $DB->get_recordset_sql($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the moodle_url of the page to edit the error.
|
||||
*
|
||||
* @param \stdClass $componentinfo
|
||||
* @return \moodle_url
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public static function get_edit_url(\stdClass $componentinfo): \moodle_url {
|
||||
$questionid = $componentinfo->itemid;
|
||||
// Question answers are editable on main question page
|
||||
// Question answers are editable on main question page.
|
||||
// Hence, use refid for these links.
|
||||
if ($componentinfo->tablename == 'question_answers') {
|
||||
if ($componentinfo->tablename === 'question_answers') {
|
||||
$questionid = $componentinfo->refid;
|
||||
}
|
||||
// Default to SITEID if courseid is null, i.e. system or category level questions.
|
||||
|
@ -142,30 +166,15 @@ abstract class base extends area_base {
|
|||
|
||||
/**
|
||||
* Determine the course and category id SQL depending on the specific context associated with question data.
|
||||
*
|
||||
* @param \core\event\base $event
|
||||
* @return string
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
protected function get_course_and_cat_sql(\core\event\base $event): string {
|
||||
global $DB;
|
||||
|
||||
$courseid = 'null';
|
||||
$catid = 'null';
|
||||
|
||||
$sql = "
|
||||
SELECT ctx.instanceid, cm.course as courseid, ctx.contextlevel
|
||||
FROM {question} q
|
||||
INNER JOIN {question_categories} qc ON qc.id = q.category
|
||||
INNER JOIN {context} ctx ON ctx.id = qc.contextid
|
||||
LEFT JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :coursemodule
|
||||
WHERE q.id = :refid
|
||||
";
|
||||
$params = [
|
||||
'coursemodule' => CONTEXT_MODULE,
|
||||
'refid' => $event->objectid,
|
||||
];
|
||||
|
||||
if ($record = $DB->get_record_sql($sql, $params)) {
|
||||
if ($record = self::get_course_and_category(CONTEXT_MODULE, $event->objectid)) {
|
||||
if ($record->contextlevel == CONTEXT_MODULE) {
|
||||
$courseid = $record->courseid;
|
||||
} else if ($record->contextlevel == CONTEXT_COURSE) {
|
||||
|
@ -182,4 +191,37 @@ abstract class base extends area_base {
|
|||
{$catid} AS categoryid,
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course and category data for the question.
|
||||
*
|
||||
* @param int $coursemodule
|
||||
* @param int $refid
|
||||
* @return \stdClass|false
|
||||
*/
|
||||
public static function get_course_and_category($coursemodule, $refid) {
|
||||
global $DB;
|
||||
|
||||
$sql = 'SELECT ctx.instanceid,
|
||||
cm.course as courseid,
|
||||
ctx.contextlevel
|
||||
FROM {question} q
|
||||
INNER JOIN {question_versions} qv
|
||||
ON qv.questionid = q.id
|
||||
INNER JOIN {question_bank_entries} qbe
|
||||
ON qbe.id = qv.questionbankentryid
|
||||
INNER JOIN {question_categories} qc
|
||||
ON qc.id = qbe.questioncategoryid
|
||||
INNER JOIN {context} ctx
|
||||
ON ctx.id = qc.contextid
|
||||
LEFT JOIN {course_modules} cm
|
||||
ON cm.id = ctx.instanceid
|
||||
AND ctx.contextlevel = :coursemodule
|
||||
WHERE q.id = :refid';
|
||||
$params = [
|
||||
'coursemodule' => $coursemodule,
|
||||
'refid' => $refid
|
||||
];
|
||||
return $DB->get_record_sql($sql, $params);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ use tool_brickfield\area_test_base;
|
|||
* @package tool_brickfield
|
||||
* @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass \tool_brickfield\local\areas\core_question\base
|
||||
*/
|
||||
class questiontext_test extends area_test_base {
|
||||
/**
|
||||
|
@ -190,4 +191,32 @@ class questiontext_test extends area_test_base {
|
|||
$category->id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get course and category.
|
||||
*
|
||||
* @covers ::get_course_and_category
|
||||
*/
|
||||
public function test_get_course_and_category() {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$coursecontext = \context_course::instance($course->id);
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
|
||||
$question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
|
||||
$event = \core\event\question_updated::create_from_question_instance($question1,
|
||||
\context_course::instance($course->id));
|
||||
$rs = base::get_course_and_category(CONTEXT_COURSE, $event->objectid);
|
||||
$this->assertNotNull($rs);
|
||||
$this->assertEquals(CONTEXT_COURSE, $rs->contextlevel);
|
||||
$this->assertNotEquals(CONTEXT_MODULE, $rs->contextlevel);
|
||||
// Invalid objectid.
|
||||
$rs = base::get_course_and_category(CONTEXT_COURSE, 0);
|
||||
$this->assertFalse($rs);
|
||||
// Incorrect objectid.
|
||||
$rs = base::get_course_and_category(CONTEXT_COURSE, 100);
|
||||
$this->assertFalse($rs);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue