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_ai\aiactions;
18
 
19
use core_ai\aiactions\responses\response_base;
20
 
21
/**
22
 * Base Action class.
23
 *
24
 * @package    core_ai
25
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
abstract class base {
29
    /** @var int Timestamp the action object was created. */
30
    protected readonly int $timecreated;
31
 
32
    /**
33
     * Constructor for the class.
34
     *
35
     * @param int $contextid The context ID where the action was created.
36
     */
37
    public function __construct(
38
        /** @var int The context ID the action was created in */
39
        protected int $contextid,
40
    ) {
41
        $this->timecreated = \core\di::get(\core\clock::class)->time();
42
    }
43
 
44
    /**
45
     * Responsible for storing any action specific data in the database.
46
     *
47
     * @param response_base $response The response object to store.
48
     * @return int The id of the stored action.
49
     */
50
    abstract public function store(response_base $response): int;
51
 
52
    /**
53
     * Get the basename of the class.
54
     *
55
     * This is used to generate the action name and description.
56
     *
57
     * @return string The basename of the class.
58
     */
59
    public static function get_basename(): string {
60
        return basename(str_replace('\\', '/', static::class));
61
    }
62
 
63
    /**
64
     * Get the action name.
65
     *
66
     * Defaults to the action name string.
67
     *
68
     * @return string
69
     */
70
    public static function get_name(): string {
71
        $stringid = 'action_' . self::get_basename();
72
        return get_string($stringid, 'core_ai');
73
    }
74
 
75
    /**
76
     * Get the action description.
77
     *
78
     * Defaults to the action description string.
79
     *
80
     * @return string
81
     */
82
    public static function get_description(): string {
83
        $stringid = 'action_' . self::get_basename() . '_desc';
84
        return get_string($stringid, 'core_ai');
85
    }
86
 
87
    /**
88
     * Get the system instruction for the action.
89
     *
90
     * @return string The system instruction for the action.
91
     */
92
    public static function get_system_instruction(): string {
93
        $stringid = 'action_' . self::get_basename() . '_instruction';
94
 
95
        // If the string doesn't exist, return an empty string.
96
        if (!get_string_manager()->string_exists($stringid, 'core_ai')) {
97
            return '';
98
        }
99
 
100
        return get_string($stringid, 'core_ai');
101
    }
102
 
103
    /**
104
     * Get a configuration option.
105
     *
106
     * @param string $name The name of the configuration option to get.
107
     * @return mixed The value of the configuration option.
108
     */
109
    public function get_configuration(string $name): mixed {
110
        return $this->$name;
111
    }
112
 
113
    /**
114
     * Return the correct table name for the action.
115
     *
116
     * @return string The correct table name for the action.
117
     */
118
    protected function get_tablename(): string {
119
        // Table name should always be in this format.
120
        return 'ai_action_' . $this->get_basename();
121
    }
122
 
123
    /**
124
     * Get the class name of the response object.
125
     *
126
     * @return string The class name of the response object.
127
     */
128
    public static function get_response_classname(): string {
129
        return responses::class . '\\response_' . self::get_basename();
130
    }
131
}