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 mod_quiz\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once($CFG->libdir . '/externallib.php');
22
require_once($CFG->dirroot . '/question/editlib.php');
23
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
24
 
25
use external_function_parameters;
26
use external_single_structure;
27
use external_value;
28
use external_api;
1441 ariadna 29
use core_question\category_manager;
1 efrain 30
use mod_quiz\question\bank\filter\custom_category_condition;
31
use mod_quiz\quiz_settings;
32
use mod_quiz\structure;
33
 
34
/**
35
 * Add random questions to a quiz.
36
 *
37
 * @package    mod_quiz
38
 * @copyright  2022 Catalyst IT Australia Pty Ltd
39
 * @author     Nathan Nguyen <nathannguyen@catalyst-au.net>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class add_random_questions extends external_api {
43
 
44
    /**
45
     * Parameters for the web service function
46
     *
47
     * @return external_function_parameters
48
     */
49
    public static function execute_parameters(): external_function_parameters {
50
        return new external_function_parameters (
51
            [
52
                'cmid' => new external_value(PARAM_INT, 'The cmid of the quiz'),
53
                'addonpage' => new external_value(PARAM_INT, 'The page where random questions will be added to'),
54
                'randomcount' => new external_value(PARAM_INT, 'Number of random questions'),
55
                'filtercondition' => new external_value(
56
                    PARAM_TEXT,
57
                    '(Optional) The filter condition used when adding random questions from an existing category.
58
                    Not required if adding random questions from a new category.',
59
                    VALUE_DEFAULT,
60
                    '',
61
                ),
62
                'newcategory' => new external_value(
63
                    PARAM_TEXT,
64
                    '(Optional) The name of a new question category to create and use for the random questions.',
65
                    VALUE_DEFAULT,
66
                    '',
67
                ),
68
                'parentcategory' => new external_value(
69
                    PARAM_TEXT,
70
                    '(Optional) The parent of the new question category, if creating one.',
71
                    VALUE_DEFAULT,
72
                    0,
73
                ),
74
            ]
75
        );
76
    }
77
 
78
    /**
79
     * Add random questions.
80
     *
81
     * @param int $cmid The cmid of the quiz
82
     * @param int $addonpage The page where random questions will be added to
83
     * @param int $randomcount Number of random questions
84
     * @param string $filtercondition Filter condition
85
     * @param string $newcategory add new category
86
     * @param string $parentcategory parent category of new category
87
     * @return array result
88
     */
89
    public static function execute(
90
        int $cmid,
91
        int $addonpage,
92
        int $randomcount,
93
        string $filtercondition = '',
94
        string $newcategory = '',
95
        string $parentcategory = '',
96
    ): array {
97
        [
98
            'cmid' => $cmid,
99
            'addonpage' => $addonpage,
100
            'randomcount' => $randomcount,
101
            'filtercondition' => $filtercondition,
102
            'newcategory' => $newcategory,
103
            'parentcategory' => $parentcategory,
104
        ] = self::validate_parameters(self::execute_parameters(), [
105
            'cmid' => $cmid,
106
            'addonpage' => $addonpage,
107
            'randomcount' => $randomcount,
108
            'filtercondition' => $filtercondition,
109
            'newcategory' => $newcategory,
110
            'parentcategory' => $parentcategory,
111
        ]);
112
 
113
        // Validate context.
114
        $thiscontext = \context_module::instance($cmid);
115
        self::validate_context($thiscontext);
116
        require_capability('mod/quiz:manage', $thiscontext);
117
 
118
        // If filtercondition is not empty, decode it. Otherwise, set it to empty array.
119
        $filtercondition = !empty($filtercondition) ? json_decode($filtercondition, true) : [];
120
 
121
        // Create new category.
122
        if (!empty($newcategory)) {
1441 ariadna 123
            $categorymanager = new category_manager();
124
            $categoryid = $categorymanager->add_category($parentcategory, $newcategory, '');
1 efrain 125
            $filter = [
126
                'category' => [
127
                    'jointype' => custom_category_condition::JOINTYPE_DEFAULT,
128
                    'values' => [$categoryid],
129
                    'filteroptions' => ['includesubcategories' => false],
130
                ]
131
            ];
132
            // Generate default filter condition for the random question to be added in the new category.
133
            $filtercondition = [
134
                'qpage' => 0,
135
                'cat' => "{$categoryid},{$thiscontext->id}",
136
                'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
137
                'tabname' => 'questions',
138
                'sortdata' => [],
139
                'filter' => $filter,
140
            ];
141
        }
142
 
143
        // Add random question to the quiz.
144
        [$quiz, ] = get_module_from_cmid($cmid);
145
        $settings = quiz_settings::create_for_cmid($cmid);
146
        $structure = structure::create_for_quiz($settings);
147
        $structure->add_random_questions($addonpage, $randomcount, $filtercondition);
148
        quiz_delete_previews($quiz);
149
        quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_quiz_sumgrades();
150
 
151
        return ['message' => get_string('addarandomquestion_success', 'mod_quiz')];
152
    }
153
 
154
    /**
155
     * Returns description of method result value.
156
     *
157
     * @return external_value
158
     */
159
    public static function execute_returns() {
160
        return new external_single_structure([
161
            'message' => new external_value(PARAM_TEXT, 'Message', VALUE_OPTIONAL)
162
        ]);
163
    }
164
}