| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
// This file is part of Moodle - http://moodle.org/
|
|
|
4 |
//
|
|
|
5 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
6 |
// it under the terms of the GNU General Public License as published by
|
|
|
7 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
8 |
// (at your option) any later version.
|
|
|
9 |
//
|
|
|
10 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
13 |
// GNU General Public License for more details.
|
|
|
14 |
//
|
|
|
15 |
// You should have received a copy of the GNU General Public License
|
|
|
16 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* @package moodlecore
|
|
|
20 |
* @subpackage backup-dbops
|
|
|
21 |
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Non instantiable helper class providing DB support to the questions backup stuff
|
|
|
27 |
*
|
|
|
28 |
* This class contains various static methods available for all the DB operations
|
|
|
29 |
* performed by questions stuff
|
|
|
30 |
*
|
|
|
31 |
* TODO: Finish phpdocs
|
|
|
32 |
*/
|
|
|
33 |
abstract class backup_question_dbops extends backup_dbops {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Calculates all the question_categories to be included
|
|
|
37 |
* in backup, based in a given context (course/module) and
|
|
|
38 |
* the already annotated questions present in backup_ids_temp
|
|
|
39 |
*/
|
|
|
40 |
public static function calculate_question_categories($backupid, $contextid) {
|
|
|
41 |
global $DB;
|
|
|
42 |
|
| 1441 |
ariadna |
43 |
// First step, get all the categories for the given context (course/module)
|
| 1 |
efrain |
44 |
// i.e. the whole context questions bank
|
| 1441 |
ariadna |
45 |
$contextcategories = $DB->get_records_menu('question_categories', ['contextid' => $contextid], '', 'id, parent');
|
| 1 |
efrain |
46 |
|
| 1441 |
ariadna |
47 |
// Now, based in the annotated question bank entries, get all the categories they
|
|
|
48 |
// belong to.
|
|
|
49 |
$questioncategories = $DB->get_records_sql_menu(
|
|
|
50 |
"SELECT DISTINCT qc2.id, qc2.parent
|
|
|
51 |
FROM {question_categories} qc2
|
|
|
52 |
JOIN {question_bank_entries} qbe ON qbe.questioncategoryid = qc2.id
|
|
|
53 |
JOIN {backup_ids_temp} bi ON bi.itemid = qbe.id
|
|
|
54 |
WHERE bi.backupid = ?
|
|
|
55 |
AND bi.itemname = 'question_bank_entry'
|
|
|
56 |
AND qc2.contextid != ?",
|
|
|
57 |
[$backupid, $contextid]
|
|
|
58 |
);
|
| 1 |
efrain |
59 |
|
| 1441 |
ariadna |
60 |
// These are all the question categories we want to include in the backup.
|
|
|
61 |
$categories = $contextcategories + $questioncategories;
|
|
|
62 |
// If we're not already including the parents of a category, add them in.
|
|
|
63 |
foreach ($categories as $parentid) {
|
|
|
64 |
if (!array_key_exists($parentid, $categories)) {
|
|
|
65 |
$categories += self::get_parent_categories($parentid);
|
| 1 |
efrain |
66 |
}
|
|
|
67 |
}
|
| 1441 |
ariadna |
68 |
// Insert annotations of the found categories.
|
|
|
69 |
foreach (array_keys($categories) as $categoryid) {
|
|
|
70 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category', $categoryid);
|
| 1 |
efrain |
71 |
}
|
| 1441 |
ariadna |
72 |
// For these categories, we want to include all questions.
|
|
|
73 |
foreach (array_keys($contextcategories) as $categoryid) {
|
|
|
74 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category_complete', $categoryid);
|
| 1 |
efrain |
75 |
}
|
| 1441 |
ariadna |
76 |
// For these categories, we only want to include the questions that have been annotated.
|
|
|
77 |
// Exclude those where we're already including all questions.
|
|
|
78 |
$partialcategories = array_diff(
|
|
|
79 |
array_keys($questioncategories),
|
|
|
80 |
array_keys($contextcategories),
|
|
|
81 |
);
|
|
|
82 |
foreach ($partialcategories as $categoryid) {
|
|
|
83 |
backup_structure_dbops::insert_backup_ids_record($backupid, 'question_category_partial', $categoryid);
|
|
|
84 |
}
|
| 1 |
efrain |
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
| 1441 |
ariadna |
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;
|
|
|
95 |
$parentcategories = [];
|
|
|
96 |
$parentid = $DB->get_field('question_categories', 'parent', ['id' => $categoryid]);
|
|
|
97 |
$parentcategories[$categoryid] = $parentid;
|
|
|
98 |
// If this is not a top category, keep going.
|
|
|
99 |
if ($parentid > 0) {
|
|
|
100 |
array_merge($parentcategories, self::get_parent_categories($parentid));
|
|
|
101 |
}
|
|
|
102 |
return $parentcategories;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
| 1 |
efrain |
106 |
* Delete all the annotated questions present in backup_ids_temp
|
|
|
107 |
*/
|
|
|
108 |
public static function delete_temp_questions($backupid) {
|
|
|
109 |
global $DB;
|
|
|
110 |
$DB->delete_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'question'));
|
|
|
111 |
}
|
|
|
112 |
}
|