Proyectos de Subversion Moodle

Rev

| 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
use tool_courserating\api;
18
use tool_courserating\helper;
19
use tool_courserating\local\models\rating;
20
 
21
/**
22
 * Generator
23
 *
24
 * @package     tool_courserating
25
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
26
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class tool_courserating_generator extends testing_module_generator {
29
 
30
    /**
31
     * Set course rating mode
32
     *
33
     * @param int $courseid
34
     * @param int $mode
35
     */
36
    public function set_course_rating_mode(int $courseid, int $mode) {
37
        if ($data = helper::get_course_rating_enabled_data_in_cfield($courseid)) {
38
            $data->instance_form_save((object)[
39
                'id' => $courseid,
40
                $data->get_form_element_name() => $mode,
41
            ]);
42
            api::reindex($courseid);
43
        }
44
    }
45
 
46
    /**
47
     * Set courserating config and reindex
48
     *
49
     * @param string $name
50
     * @param mixed $value
51
     */
52
    public function set_config(string $name, $value) {
53
        set_config($name, $value, 'tool_courserating');
54
        api::reindex();
55
    }
56
 
57
    /**
58
     * Clear custom field cache
59
     *
60
     * Unfortunately we can not call the proper method from behat:
61
     * \core_course\customfield\course_handler::reset_caches()
62
     *
63
     * @return void
64
     */
65
    public function clear_course_custom_field_cache() {
66
        $reflection = new \ReflectionProperty(\core_course\customfield\course_handler::class, 'singleton');
67
        $reflection->setAccessible(true);
68
        $reflection->setValue(null, null);
69
    }
70
 
71
    /**
72
     * Create rating
73
     *
74
     * @param int $userid
75
     * @param int $courseid
76
     * @param int $rating
77
     * @param string $review
78
     * @return rating
79
     */
80
    public function create_rating(int $userid, int $courseid, int $rating, string $review = '') {
81
        $this->clear_course_custom_field_cache();
82
        $formdata = (object)[
83
            'review_editor' => ['text' => $review ?? '', 'format' => FORMAT_HTML],
84
            'review' => $review ?? '',
85
            'rating' => $rating,
86
        ];
87
        return api::set_rating($courseid, $formdata, $userid);
88
    }
89
}