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\output;
18
 
19
use aiplacement_courseassist\utils;
20
use core\hook\output\after_http_headers;
21
use core\hook\output\before_footer_html_generation;
22
 
23
/**
24
 * Output handler for the course assist AI Placement.
25
 *
26
 * @package    aiplacement_courseassist
27
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class assist_ui {
31
    /**
32
     * Bootstrap the course assist UI.
33
     *
34
     * @param before_footer_html_generation $hook
35
     */
36
    public static function load_assist_ui(before_footer_html_generation $hook): void {
37
        global $PAGE, $OUTPUT, $USER;
38
 
39
        // Preflight checks.
40
        if (!self::preflight_checks()) {
41
            return;
42
        }
43
 
44
        // Load the markup for the assist interface.
45
        $params = [
46
            'userid' => $USER->id,
47
            'contextid' => $PAGE->context->id,
48
        ];
49
        $html = $OUTPUT->render_from_template('aiplacement_courseassist/drawer', $params);
50
        $hook->add_html($html);
51
    }
52
 
53
    /**
54
     * Determine if we should be loading a single button or a dropdown.
55
     *
56
     * @param after_http_headers $hook
57
     */
58
    public static function action_buttons_handler(after_http_headers $hook): void {
59
        global $PAGE, $OUTPUT;
60
 
61
        // Preflight checks.
62
        if (!self::preflight_checks()) {
63
            return;
64
        }
65
 
66
        $actions['actions'] = utils::get_actions_available($PAGE->context);
67
 
68
        // No actions available.
69
        if (empty($actions['actions'])) {
70
            return;
71
        }
72
 
73
        if (count($actions['actions']) > 1) {
74
            $actions['isdropdown'] = true;
75
        }
76
 
77
        $html = $OUTPUT->render_from_template('aiplacement_courseassist/actions', $actions);
78
        $hook->add_html($html);
79
    }
80
 
81
    /**
82
     * Preflight checks to determine if the assist UI should be loaded.
83
     *
84
     * @return bool
85
     */
86
    private static function preflight_checks(): bool {
87
        global $PAGE;
88
        if (during_initial_install()) {
89
            return false;
90
        }
91
        if (!get_config('aiplacement_courseassist', 'version')) {
92
            return false;
93
        }
94
        if (in_array($PAGE->pagelayout, ['maintenance', 'print', 'redirect', 'embedded'])) {
95
            // Do not try to show assist UI inside iframe, in maintenance mode,
96
            // when printing, or during redirects.
97
            return false;
98
        }
99
        // Check we are in the right context, exit if not activity.
100
        if ($PAGE->context->contextlevel != CONTEXT_MODULE) {
101
            return false;
102
        }
103
 
104
        // Check if the user has permission to use the AI service.
105
        return utils::is_course_assist_available($PAGE->context);
106
    }
107
}