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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Library of interface functions and constants.
19
 *
20
 * @package     mod_qbank
21
 * @copyright   2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
22
 * @author      Simon Adams <simon.adams@catalyst-eu.net>
23
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
/**
27
 * Return if the plugin supports $feature.
28
 *
29
 * @param string $feature Constant representing the feature.
30
 * @return bool|string|null True if module supports feature, false if not, null if it doesn't know or string for the module purpose.
31
 */
32
function qbank_supports(string $feature) {
33
    switch ($feature) {
34
        case FEATURE_BACKUP_MOODLE2:
35
        case FEATURE_PUBLISHES_QUESTIONS:
36
        case FEATURE_SHOW_DESCRIPTION:
37
        case FEATURE_USES_QUESTIONS:
38
            return true;
39
        case FEATURE_CAN_DISPLAY:
40
        case FEATURE_CAN_UNINSTALL:
41
        case FEATURE_COMMENT:
42
        case FEATURE_COMPLETION:
43
        case FEATURE_COMPLETION_HAS_RULES:
44
        case FEATURE_COMPLETION_TRACKS_VIEWS:
45
        case FEATURE_CONTROLS_GRADE_VISIBILITY:
46
        case FEATURE_GRADE_OUTCOMES:
47
        case FEATURE_MODEDIT_DEFAULT_COMPLETION:
48
            return false;
49
        case FEATURE_MOD_PURPOSE:
50
            return MOD_PURPOSE_CONTENT;
51
        default:
52
            return null;
53
    }
54
}
55
 
56
/**
57
 * Saves a new instance of the mod_qbank into the database.
58
 *
59
 * Given an object containing all the necessary data,
60
 * this function will create a new instance and return the id number of the instance.
61
 *
62
 * @param stdClass $moduleinstance An object from the form.
63
 * @param mod_qbank_mod_form|null $mform The form. Not used in this function.
64
 * @return int The id of the newly inserted record.
65
 */
66
function qbank_add_instance(stdClass $moduleinstance, ?mod_qbank_mod_form $mform): int {
67
    global $DB;
68
 
69
    $moduleinstance->timecreated = time();
70
 
71
    return $DB->insert_record('qbank', $moduleinstance);
72
}
73
 
74
/**
75
 * Updates an instance of the mod_qbank in the database.
76
 * Given an object containing all the necessary data,
77
 * this function will update an existing instance with new data.
78
 *
79
 * @param stdClass $moduleinstance An object from the form in mod_form.php.
80
 * @param mod_qbank_mod_form|null $mform The form. Not used in this function.
81
 * @return bool True if successful, false otherwise.
82
 */
83
function qbank_update_instance(stdClass $moduleinstance, ?mod_qbank_mod_form $mform): bool {
84
    global $DB;
85
 
86
    $moduleinstance->timemodified = time();
87
    $moduleinstance->id = $moduleinstance->instance;
88
 
89
    return $DB->update_record('qbank', $moduleinstance);
90
}
91
 
92
/**
93
 * Removes an instance of the mod_qbank from the database.
94
 * We don't need to do anything for questions, question_categories, or user records here
95
 * as the module deletion API cleans that up for us.
96
 *
97
 * @param int $id id of the module instance.
98
 * @return bool True if successful, false on failure.
99
 */
100
function qbank_delete_instance(int $id): bool {
101
    global $DB;
102
 
103
    if (!$DB->record_exists('qbank', ['id' => $id])) {
104
        return false;
105
    }
106
 
107
    $DB->delete_records('qbank', ['id' => $id]);
108
 
109
    return true;
110
}
111
 
112
/**
113
 * Callback for tool_generator so we can add qbanks to generated courses.
114
 *
115
 * @param tool_generator_course_backend $backend
116
 * @param testing_data_generator $generator
117
 * @param int $courseid
118
 * @param int $number
119
 * @return void
120
 * @throws coding_exception
121
 */
122
function qbank_course_backend_generator_create_activity(
123
    tool_generator_course_backend $backend,
124
    testing_data_generator $generator,
125
    int $courseid,
126
    int $number,
127
) {
128
    // Set up generator.
129
    $generator = $generator->get_plugin_generator('mod_qbank');
130
 
131
    // Create assignments.
132
    $backend->log('createqbank', $number, true, 'mod_qbank');
133
    for ($i = 1; $i <= $number; $i++) {
134
        $qbank = $generator->create_instance(
135
            [
136
                'course' => $courseid,
137
                'name' => "Question bank course {$courseid} bank {$i}",
138
            ],
139
            [
140
                'section' => 0,
141
            ],
142
        );
143
        question_get_default_category(\core\context\module::instance($qbank->cmid)->id, true);
144
        $backend->dot($i, $number);
145
    }
146
    $backend->end_log();
147
}