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 core_question\local\bank;
18
 
19
/**
20
 * Helper methods for question version options.
21
 *
22
 * @package    core_question
23
 * @copyright  2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @author     Conn Warwicker <conn.warwicker@catalyst-eu.net>
26
 */
27
class version_options {
28
 
29
    /**
30
     * Get the version options for display in dropdown menu
31
     * @param int $questionid
32
     * @return array
33
     * @throws \coding_exception
34
     */
35
    public static function get_version_menu_options(int $questionid): array {
36
 
37
        $versions = self::get_version_options($questionid);
38
        $latestversion = reset($versions);
39
 
40
        $return = [];
41
 
42
        // Add the "always latest" option to the beginning.
43
        $return[0] = get_string('alwayslatest', 'question');
44
 
45
        foreach ($versions as $version) {
46
            if ($version->version === $latestversion->version) {
47
                $value = get_string('versioninfolatestshort', 'question', $version->version);
48
            } else {
49
                $value = get_string('question_versionshort', 'question', $version->version);
50
            }
51
            $return[$version->version] = $value;
52
        }
53
 
54
        return $return;
55
 
56
    }
57
 
58
    /**
59
     * Get the available versions of a question where one of the version has the given question id.
60
     *
61
     * @param int $questionid id of a question.
62
     * @return stdClass[] other versions of this question. Each object has fields versionid,
63
     *       version and questionid. Array is returned most recent version first.
64
     */
65
    public static function get_version_options(int $questionid): array {
66
        global $DB;
67
 
68
        return $DB->get_records_sql("
69
                SELECT allversions.id AS versionid,
70
                       allversions.version,
71
                       allversions.questionid
72
 
73
                  FROM {question_versions} allversions
74
 
75
                 WHERE allversions.questionbankentryid = (
76
                            SELECT givenversion.questionbankentryid
77
                              FROM {question_versions} givenversion
78
                             WHERE givenversion.questionid = ?
79
                       )
80
                   AND allversions.status <> ?
81
 
82
              ORDER BY allversions.version DESC
83
              ", [$questionid, question_version_status::QUESTION_STATUS_DRAFT]);
84
    }
85
 
86
}