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 aiplacement_courseassist;
18
 
19
use core_ai\aiactions\explain_text;
20
use core_ai\aiactions\summarise_text;
21
use core_ai\manager;
22
 
23
/**
24
 * AI Placement course assist utils.
25
 *
26
 * @package    aiplacement_courseassist
27
 * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class utils {
31
    /**
32
     * Check if AI Placement course assist is available for the context.
33
     *
34
     * @param \context $context The context.
35
     * @return bool True if AI Placement course assist is available, false otherwise.
36
     */
37
    public static function is_course_assist_available(\context $context): bool {
38
        [$plugintype, $pluginname] = explode('_', \core_component::normalize_componentname('aiplacement_courseassist'), 2);
39
        $pluginmanager = \core_plugin_manager::resolve_plugininfo_class($plugintype);
40
        if (!$pluginmanager::is_plugin_enabled($pluginname)) {
41
            return false;
42
        }
43
 
44
        if (empty(self::get_actions_available($context))) {
45
            return false;
46
        }
47
 
48
        return true;
49
    }
50
 
51
    /**
52
     * Get all the actions available and return action data for template.
53
     *
54
     * @param \context $context The context.
55
     * @return array Return the actions available with data.
56
     */
57
    public static function get_actions_available(\context $context): array {
58
        $actions = [];
59
        $actionclasses = [
60
            summarise_text::class,
61
            explain_text::class,
62
        ];
63
        $manager = \core\di::get(manager::class);
64
        $providers = $manager->get_providers_for_actions($actionclasses, true);
65
 
66
        // Summarise text.
67
        if (has_capability('aiplacement/courseassist:summarise_text', $context)
68
            && $manager->is_action_available(summarise_text::class)
69
            && $manager->is_action_enabled('aiplacement_courseassist', summarise_text::class)
70
            && !empty($providers[summarise_text::class])
71
        ) {
72
            $actions[] = [
73
                'action' => 'summarise',
74
                'buttontext' => get_string('summarise', 'aiplacement_courseassist'),
75
                'title' => get_string('summarise_tooltips', 'aiplacement_courseassist'),
76
            ];
77
        }
78
        // Explain text.
79
        if (has_capability('aiplacement/courseassist:explain_text', $context)
80
            && $manager->is_action_available(explain_text::class)
81
            && $manager->is_action_enabled('aiplacement_courseassist', explain_text::class)
82
            && !empty($providers[explain_text::class])
83
        ) {
84
            $actions[] = [
85
                'action' => 'explain',
86
                'buttontext' => get_string('explain', 'aiplacement_courseassist'),
87
                'title' => get_string('explain_tooltips', 'aiplacement_courseassist'),
88
            ];
89
        }
90
 
91
        return $actions;
92
    }
93
}