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_analytics\tests;
18
 
19
use phpunit_util;
20
 
21
/**
22
 * A trait to check machine learning configurations.
23
 *
24
 * @package    core_analytics
25
 * @category   test
26
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
trait mlbackend_helper_trait {
30
    /**
31
     * Check if mlbackend_python is configured.
32
     *
33
     * @return bool
34
     */
35
    public static function is_mlbackend_python_configured(): bool {
36
        if (defined('TEST_MLBACKEND_PYTHON_HOST') && defined('TEST_MLBACKEND_PYTHON_PORT')
37
                && defined('TEST_MLBACKEND_PYTHON_USERNAME') && defined('TEST_MLBACKEND_PYTHON_USERNAME')) {
38
            return true;
39
        }
40
        return false;
41
    }
42
 
43
    /**
44
     * Generate test courses.
45
     *
46
     * @param int $ncourses Number of courses to generate.
47
     * @param array $params Additional parameters for the courses.
48
     * @param bool $ismulticlass Whether to generate courses for multi-classification.
49
     */
50
    public function generate_courses(
51
        int $ncourses,
52
        array $params = [],
53
        bool $ismulticlass = false,
54
    ): void {
55
        $params = $params + [
56
            'startdate' => mktime(0, 0, 0, 10, 24, 2015),
57
            'enddate' => mktime(0, 0, 0, 2, 24, 2016),
58
        ];
59
        for ($i = 0; $i < $ncourses; $i++) {
60
            $name = 'a' . random_string(10);
61
            $courseparams = ['shortname' => $name, 'fullname' => $name] + $params;
62
            phpunit_util::get_data_generator()->create_course($courseparams);
63
        }
64
        for ($i = 0; $i < $ncourses; $i++) {
65
            $name = 'b' . random_string(10);
66
            $courseparams = ['shortname' => $name, 'fullname' => $name] + $params;
67
            phpunit_util::get_data_generator()->create_course($courseparams);
68
        }
69
        if ($ismulticlass) {
70
            for ($i = 0; $i < $ncourses; $i++) {
71
                $name = 'c' . random_string(10);
72
                $courseparams = ['shortname' => $name, 'fullname' => $name] + $params;
73
                phpunit_util::get_data_generator()->create_course($courseparams);
74
            }
75
        }
76
    }
77
}