Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 qbank_bulkmove;
18
 
19
/**
20
 * Bulk move helper.
21
 *
22
 * @package    qbank_bulkmove
23
 * @copyright  2021 Catalyst IT Australia Pty Ltd
24
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class helper {
28
 
29
    /**
30
     * Bulk move questions to a category.
31
     *
32
     * @param string $movequestionselected comma separated string of questions to be moved.
33
     * @param \stdClass $tocategory the category where the questions will be moved to.
34
     */
35
    public static function bulk_move_questions(string $movequestionselected, \stdClass $tocategory): void {
1441 ariadna 36
        global $DB, $CFG;
37
        require_once($CFG->libdir .'/questionlib.php');
1 efrain 38
        if ($questionids = explode(',', $movequestionselected)) {
1441 ariadna 39
            [$usql, $params] = $DB->get_in_or_equal($questionids);
1 efrain 40
            $sql = "SELECT q.*, c.contextid
41
                      FROM {question} q
42
                      JOIN {question_versions} qv ON qv.questionid = q.id
43
                      JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
44
                      JOIN {question_categories} c ON c.id = qbe.questioncategoryid
45
                     WHERE q.id
46
                     {$usql}";
47
            $questions = $DB->get_records_sql($sql, $params);
48
            foreach ($questions as $question) {
49
                question_require_capability_on($question, 'move');
50
            }
51
            question_move_questions_to_category($questionids, $tocategory->id);
52
        }
53
    }
54
 
55
    /**
56
     * Get the display data for the move form.
57
     *
58
     * @param array $addcontexts the array of contexts to be considered in order to render the category select menu.
59
     * @param \moodle_url $moveurl the url where the move script will point to.
60
     * @param \moodle_url $returnurl return url in case the form is cancelled.
61
     * @return array the data to be rendered in the mustache where it contains the dropdown, move url and return url.
1441 ariadna 62
     * @deprecated since Moodle 5.0.
63
     * @todo MDL-82413 Final deprecation in Moodle 6.0.
1 efrain 64
     */
1441 ariadna 65
    #[\core\attribute\deprecated(
66
        replacement: 'replaced by a modal and webservice.
67
        See qbank_bulkmove/modal_question_bank_bulkmove and core_question_external\move_questions',
68
        since: '5.0',
69
        mdl: 'MDL-71378'
70
    )]
1 efrain 71
    public static function get_displaydata(array $addcontexts, \moodle_url $moveurl, \moodle_url $returnurl): array {
1441 ariadna 72
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 73
        $displaydata = [];
74
        $displaydata ['categorydropdown'] = \qbank_managecategories\helper::question_category_select_menu($addcontexts,
75
            false, 0, '', -1, true);
76
        $displaydata ['moveurl'] = $moveurl;
77
        $displaydata['returnurl'] = $returnurl;
78
        return $displaydata;
79
    }
80
 
81
    /**
82
     * Process the question came from the form post.
83
     *
84
     * @param array $rawquestions raw questions came as a part of post.
85
     * @return array question ids got from the post are processed and structured in an array.
86
     */
87
    public static function process_question_ids(array $rawquestions): array {
88
        $questionids = [];
89
        $questionlist = '';
90
        foreach ($rawquestions as $key => $notused) {
91
            // Parse input for question ids.
92
            if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
93
                $key = $matches[1];
94
                $questionids[] = $key;
95
            }
96
        }
97
        if (!empty($questionids)) {
98
            $questionlist = implode(',', $questionids);
99
        }
100
        return [$questionids, $questionlist];
101
    }
102
}