| Línea 38... |
Línea 38... |
| 38 |
* the already annotated questions present in backup_ids_temp
|
38 |
* the already annotated questions present in backup_ids_temp
|
| 39 |
*/
|
39 |
*/
|
| 40 |
public static function calculate_question_categories($backupid, $contextid) {
|
40 |
public static function calculate_question_categories($backupid, $contextid) {
|
| 41 |
global $DB;
|
41 |
global $DB;
|
| Línea 42... |
Línea 42... |
| 42 |
|
42 |
|
| 43 |
// First step, annotate all the categories for the given context (course/module)
|
43 |
// First step, get all the categories for the given context (course/module)
|
| 44 |
// i.e. the whole context questions bank
|
44 |
// i.e. the whole context questions bank
|
| 45 |
$DB->execute("INSERT INTO {backup_ids_temp} (backupid, itemname, itemid)
|
- |
|
| 46 |
SELECT ?, 'question_category', id
|
- |
|
| 47 |
FROM {question_categories}
|
- |
|
| Línea 48... |
Línea 45... |
| 48 |
WHERE contextid = ?", array($backupid, $contextid));
|
45 |
$contextcategories = $DB->get_records_menu('question_categories', ['contextid' => $contextid], '', 'id, parent');
|
| 49 |
|
46 |
|
| 50 |
// Now, based in the annotated questions, annotate all the categories they
|
- |
|
| 51 |
// belong to (whole context question banks too)
|
47 |
// Now, based in the annotated question bank entries, get all the categories they
|
| 52 |
// First, get all the contexts we are going to save their question bank (no matter
|
48 |
// belong to.
|
| 53 |
// where they are in the contexts hierarchy, transversals... whatever)
|
49 |
$questioncategories = $DB->get_records_sql_menu(
|
| 54 |
$contexts = $DB->get_fieldset_sql("SELECT DISTINCT qc2.contextid
|
50 |
"SELECT DISTINCT qc2.id, qc2.parent
|
| 55 |
FROM {question_categories} qc2
|
- |
|
| 56 |
JOIN {question_bank_entries} qbe ON qbe.questioncategoryid = qc2.id
|
- |
|
| 57 |
JOIN {question_versions} qv ON qv.questionbankentryid = qbe.id
|
51 |
FROM {question_categories} qc2
|
| 58 |
JOIN {question} q ON q.id = qv.questionid
|
52 |
JOIN {question_bank_entries} qbe ON qbe.questioncategoryid = qc2.id
|
| 59 |
JOIN {backup_ids_temp} bi ON bi.itemid = q.id
|
53 |
JOIN {backup_ids_temp} bi ON bi.itemid = qbe.id
|
| 60 |
WHERE bi.backupid = ?
|
54 |
WHERE bi.backupid = ?
|
| - |
|
55 |
AND bi.itemname = 'question_bank_entry'
|
| - |
|
56 |
AND qc2.contextid != ?",
|
| Línea 61... |
Línea 57... |
| 61 |
AND bi.itemname = 'question'
|
57 |
[$backupid, $contextid]
|
| 62 |
AND qc2.contextid != ?", array($backupid, $contextid));
|
58 |
);
|
| 63 |
|
- |
|
| 64 |
// Calculate and get the set reference records.
|
- |
|
| 65 |
$setreferencecontexts = $DB->get_fieldset_sql("
|
59 |
|
| 66 |
SELECT DISTINCT qc.contextid
|
- |
|
| 67 |
FROM {question_categories} qc
|
60 |
// These are all the question categories we want to include in the backup.
|
| 68 |
JOIN {question_set_references} qsr ON qsr.questionscontextid = qc.contextid
|
61 |
$categories = $contextcategories + $questioncategories;
|
| 69 |
WHERE qsr.usingcontextid = ?", [$contextid]);
|
62 |
// If we're not already including the parents of a category, add them in.
|
| 70 |
foreach ($setreferencecontexts as $setreferencecontext) {
|
63 |
foreach ($categories as $parentid) {
|
| 71 |
if (!in_array($setreferencecontext, $contexts) && (int)$setreferencecontext !== $contextid) {
|
64 |
if (!array_key_exists($parentid, $categories)) {
|
| 72 |
$contexts [] = $setreferencecontext;
|
- |
|
| 73 |
}
|
65 |
$categories += self::get_parent_categories($parentid);
|
| 74 |
}
|
- |
|
| 75 |
|
- |
|
| 76 |
// Calculate the get the reference records.
|
66 |
}
|
| 77 |
$referencecontexts = $DB->get_fieldset_sql("
|
67 |
}
|
| 78 |
SELECT DISTINCT qc.contextid
|
68 |
// Insert annotations of the found categories.
|
| 79 |
FROM {question_categories} qc
|
69 |
foreach (array_keys($categories) as $categoryid) {
|
| 80 |
JOIN {question_bank_entries} qbe ON qbe.questioncategoryid = qc.id
|
70 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category', $categoryid);
|
| 81 |
JOIN {question_references} qr ON qr.questionbankentryid = qbe.id
|
71 |
}
|
| 82 |
WHERE qr.usingcontextid =?", [$contextid]);
|
- |
|
| 83 |
foreach ($referencecontexts as $referencecontext) {
|
- |
|
| 84 |
if (!in_array($referencecontext, $contexts) && (int)$referencecontext !== $contextid) {
|
72 |
// For these categories, we want to include all questions.
|
| 85 |
$contexts [] = $referencecontext;
|
73 |
foreach (array_keys($contextcategories) as $categoryid) {
|
| 86 |
}
|
74 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category_complete', $categoryid);
|
| - |
|
75 |
}
|
| 87 |
}
|
76 |
// For these categories, we only want to include the questions that have been annotated.
|
| 88 |
// And now, simply insert all the question categories (complete question bank)
|
77 |
// Exclude those where we're already including all questions.
|
| - |
|
78 |
$partialcategories = array_diff(
|
| 89 |
// for those contexts if we have found any
|
79 |
array_keys($questioncategories),
|
| 90 |
if ($contexts) {
|
80 |
array_keys($contextcategories),
|
| - |
|
81 |
);
|
| - |
|
82 |
foreach ($partialcategories as $categoryid) {
|
| - |
|
83 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category_partial', $categoryid);
|
| - |
|
84 |
}
|
| - |
|
85 |
}
|
| - |
|
86 |
|
| - |
|
87 |
/**
|
| 91 |
list($contextssql, $contextparams) = $DB->get_in_or_equal($contexts);
|
88 |
* Recursively find the parents and ancestors of the given category
|
| - |
|
89 |
*
|
| - |
|
90 |
* @param int $categoryid The category we want to find parents for.
|
| - |
|
91 |
* @return array id => parentid for each category
|
| - |
|
92 |
*/
|
| - |
|
93 |
protected static function get_parent_categories(int $categoryid): array {
|
| - |
|
94 |
global $DB;
|
| 92 |
$params = array_merge(array($backupid), $contextparams);
|
95 |
$parentcategories = [];
|
| - |
|
96 |
$parentid = $DB->get_field('question_categories', 'parent', ['id' => $categoryid]);
|
| 93 |
$DB->execute("INSERT INTO {backup_ids_temp} (backupid, itemname, itemid)
|
97 |
$parentcategories[$categoryid] = $parentid;
|
| 94 |
SELECT ?, 'question_category', id
|
98 |
// If this is not a top category, keep going.
|
| - |
|
99 |
if ($parentid > 0) {
|
| 95 |
FROM {question_categories}
|
100 |
array_merge($parentcategories, self::get_parent_categories($parentid));
|
| Línea 96... |
Línea 101... |
| 96 |
WHERE contextid $contextssql", $params);
|
101 |
}
|
| 97 |
}
|
102 |
return $parentcategories;
|
| 98 |
}
|
103 |
}
|