Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9 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
/**
18
 * Block class
19
 *
20
 * @package    block_openai_chat
21
 * @copyright  2023 Bryce Yoder <me@bryceyoder.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
class block_openai_chat extends block_base {
26
    public function init() {
27
        $this->title = get_string('openai_chat', 'block_openai_chat');
28
    }
29
 
30
    public function has_config() {
31
        return true;
32
    }
33
 
34
    function applicable_formats() {
35
        return array('all' => true);
36
    }
37
 
38
    public function specialization() {
39
        if (!empty($this->config->title)) {
40
            $this->title = $this->config->title;
41
        }
42
    }
43
 
44
    public function get_content() {
45
        global $OUTPUT;
46
        global $PAGE;
47
 
48
        if ($this->content !== null) {
49
            return $this->content;
50
        }
51
 
52
        // Send data to front end
53
        $persistconvo = get_config('block_openai_chat', 'persistconvo');
54
        if (!empty($this->config)) {
55
            $persistconvo = (property_exists($this->config, 'persistconvo') && get_config('block_openai_chat', 'allowinstancesettings')) ? $this->config->persistconvo : $persistconvo;
56
        }
57
        $this->page->requires->js_call_amd('block_openai_chat/lib', 'init', [[
58
            'blockId' => $this->instance->id,
59
            'api_type' => get_config('block_openai_chat', 'type') ? get_config('block_openai_chat', 'type') : 'chat',
60
            'persistConvo' => $persistconvo
61
        ]]);
62
 
63
        // Determine if name labels should be shown.
64
        $showlabelscss = '';
65
        if (!empty($this->config) && !$this->config->showlabels) {
66
            $showlabelscss = '
67
                .openai_message:before {
68
                    display: none;
69
                }
70
                .openai_message {
71
                    margin-bottom: 0.5rem;
72
                }
73
            ';
74
        }
75
 
76
        // First, fetch the global settings for these (and the defaults if not set)
77
        $assistantname = get_config('block_openai_chat', 'assistantname') ? get_config('block_openai_chat', 'assistantname') : get_string('defaultassistantname', 'block_openai_chat');
78
        $username = get_config('block_openai_chat', 'username') ? get_config('block_openai_chat', 'username') : get_string('defaultusername', 'block_openai_chat');
79
 
80
        // Then, override with local settings if available
81
        if (!empty($this->config)) {
82
            $assistantname = (property_exists($this->config, 'assistantname') && $this->config->assistantname) ? $this->config->assistantname : $assistantname;
83
            $username = (property_exists($this->config, 'username') && $this->config->username) ? $this->config->username : $username;
84
        }
85
        $assistantname = format_string($assistantname, true, ['context' => $this->context]);
86
        $username = format_string($username, true, ['context' => $this->context]);
87
 
88
        $this->content = new stdClass;
89
        $this->content->text = '
90
            <script>
91
                var assistantName = "' . $assistantname . '";
92
                var userName = "' . $username . '";
93
            </script>
94
 
95
            <style>
96
                ' . $showlabelscss . '
97
                .openai_message.user:before {
98
                    content: "' . $username . '";
99
                }
100
                .openai_message.bot:before {
101
                    content: "' . $assistantname . '";
102
                }
103
            </style>
104
 
105
            <div id="openai_chat_log" role="log"></div>
106
        ';
107
 
108
        if (
109
            empty(get_config('block_openai_chat', 'apikey')) &&
110
            (!get_config('block_openai_chat', 'allowinstancesettings') || empty($this->config->apikey))
111
        ) {
112
            $this->content->footer = get_string('apikeymissing', 'block_openai_chat');
113
        } else {
114
            $contextdata = [
115
                'logging_enabled' => get_config('block_openai_chat', 'logging'),
116
                'is_edit_mode' => $PAGE->user_is_editing(),
117
                'pix_popout' => '/blocks/openai_chat/pix/arrow-up-right-from-square.svg',
118
                'pix_arrow_right' => '/blocks/openai_chat/pix/arrow-right.svg',
119
                'pix_refresh' => '/blocks/openai_chat/pix/refresh.svg',
120
            ];
121
 
122
            $this->content->footer = $OUTPUT->render_from_template('block_openai_chat/control_bar', $contextdata);
123
        }
124
 
125
        return $this->content;
126
    }
127
}