Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 tool_mobile;
18
 
19
use core\session\utility\cookie_helper;
20
use html_writer;
21
 
22
/**
23
 * Allows plugins to add any elements to the footer.
24
 *
25
 * @package    tool_mobile
26
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class hook_callbacks {
30
    /**
31
     * Callback to add head elements.
32
     *
33
     * @param \core\hook\output\before_standard_head_html_generation $hook
34
     */
35
    public static function before_standard_head_html_generation(
36
        \core\hook\output\before_standard_head_html_generation $hook,
37
    ): void {
38
        global $CFG, $PAGE;
39
        // Smart App Banners meta tag is only displayed if mobile services are enabled and configured.
40
        if (!empty($CFG->enablemobilewebservice)) {
41
            $mobilesettings = get_config('tool_mobile');
42
            if (!empty($mobilesettings->enablesmartappbanners)) {
43
                if (!empty($mobilesettings->iosappid)) {
44
                    $hook->add_html(
45
                        '<meta name="apple-itunes-app" content="app-id=' . s($mobilesettings->iosappid) . ', ' .
46
                            'app-argument=' . $PAGE->url->out() . '"/>'
47
                    );
48
                }
49
 
50
                if (!empty($mobilesettings->androidappid)) {
51
                    $mobilemanifesturl = "$CFG->wwwroot/$CFG->admin/tool/mobile/mobile.webmanifest.php";
52
                    $hook->add_html('<link rel="manifest" href="' . $mobilemanifesturl . '" />');
53
                }
54
            }
55
        }
56
    }
57
 
58
    /**
59
     * Callback to add head elements.
60
     *
61
     * @param \core\hook\output\before_standard_footer_html_generation $hook
62
     */
63
    public static function before_standard_footer_html_generation(
64
        \core\hook\output\before_standard_footer_html_generation $hook,
65
    ): void {
66
        global $CFG;
67
 
68
        require_once(__DIR__ . '/../lib.php');
69
 
70
        if (empty($CFG->enablemobilewebservice)) {
71
            return;
72
        }
73
 
74
        $url = tool_mobile_create_app_download_url();
75
        if (empty($url)) {
76
            return;
77
        }
78
        $hook->add_html(
79
            html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile'), ['class' => 'mobilelink']),
80
        );
81
    }
82
 
83
    /**
84
     * Callback to recover $SESSION->wantsurl.
85
     *
86
     * @param \core_user\hook\after_login_completed $hook
87
     */
88
    public static function after_login_completed(
89
        \core_user\hook\after_login_completed $hook,
90
    ): void {
91
        global $SESSION, $CFG;
92
 
93
        // Check if the user is doing a mobile app launch, if that's the case, ensure $SESSION->wantsurl is correctly set.
94
        if (!NO_MOODLE_COOKIES && !empty($_COOKIE['tool_mobile_launch'])) {
95
            if (empty($SESSION->wantsurl) || strpos($SESSION->wantsurl, '/tool/mobile/launch.php') === false) {
96
                $params = json_decode($_COOKIE['tool_mobile_launch'], true);
97
                $SESSION->wantsurl = (new \moodle_url("/$CFG->admin/tool/mobile/launch.php", $params))->out(false);
98
            }
99
        }
100
 
101
        // Set Partitioned and Secure attributes to the MoodleSession cookie if the user is using the Moodle app.
102
        if (\core_useragent::is_moodle_app()) {
103
            cookie_helper::add_attributes_to_cookie_response_header(
104
                'MoodleSession' . $CFG->sessioncookie,
105
                ['Secure', 'Partitioned'],
106
            );
107
        }
108
    }
109
}