Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace mod_qbank\task;
18
 
19
use core\context;
20
use core\task\adhoc_task;
21
 
22
/**
23
 * Move all the question files and tags under a given question category to a new context.
24
 *
25
 * An instance of this task will be created for each category moved to a new context in
26
 * {@see transfer_question_categories}, allowing the heavy lifting of moving the data for each
27
 * question to be parallelised.
28
 *
29
 * @package    mod_qbank
30
 * @copyright  2025 onwards Catalyst IT EU {@link https://catalyst-eu.net}
31
 * @author     Mark Johnson <mark.johnson@catalyst-eu.net>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class transfer_questions extends adhoc_task {
35
 
36
    /**
37
     * Find the questions in the category, move their files and tags to the new context.
38
     *
39
     * @return void
40
     */
41
    public function execute() {
42
        global $DB, $CFG;
43
 
44
        require_once($CFG->dirroot . '/question/engine/lib.php');
45
 
46
        $data = $this->get_custom_data();
47
 
48
        $newcontextid = $DB->get_field('question_categories', 'contextid', ['id' => $data->categoryid]);
49
 
50
        if (!$newcontextid) {
51
            mtrace("Could not find a category record for id {$data->categoryid}. Terminating task.");
52
            return;
53
        }
54
 
55
        $newcontext = context::instance_by_id($newcontextid);
56
 
57
        $sql = "SELECT q.id, q.qtype
58
                  FROM {question} q
59
                  JOIN {question_versions} qv ON qv.questionid = q.id
60
                  JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
61
                 WHERE qbe.questioncategoryid = ?";
62
 
63
        $questions = $DB->get_records_sql($sql, [$data->categoryid]);
64
        $questioncount = count($questions);
65
        mtrace("Moving files and tags for {$questioncount} questions in category {$data->categoryid}.");
66
        $transaction = $DB->start_delegated_transaction();
67
        foreach ($questions as $question) {
68
            \question_bank::get_qtype($question->qtype, false)->move_files($question->id, $data->contextid, $newcontext->id);
69
            // Purge this question from the cache.
70
            \question_bank::notify_question_edited($question->id);
71
        }
72
 
73
        question_move_question_tags_to_new_context($questions, $newcontext);
74
        $transaction->allow_commit();
75
    }
76
}