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
/**
18
 * Lib functions, mostly callbacks.
19
 *
20
 * @package    tool_mobile
21
 * @copyright  2017 Juan Leyva
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Generate the app download url to promote moodle mobile.
29
 *
30
 * @return moodle_url|void App download moodle_url object or return if setuplink is not set.
31
 */
32
function tool_mobile_create_app_download_url() {
33
    global $CFG;
34
 
35
    $mobilesettings = get_config('tool_mobile');
36
 
37
    if (empty($mobilesettings->setuplink)) {
38
        return;
39
    }
40
 
41
    $downloadurl = new moodle_url($mobilesettings->setuplink);
42
 
43
    // Do not update the URL if it is a custom one (we may break it completely).
44
    if ($mobilesettings->setuplink != 'https://download.moodle.org/mobile') {
45
        return $downloadurl;
46
    }
47
 
48
    $downloadurl->param('version', $CFG->version);
49
    $downloadurl->param('lang', current_language());
50
 
51
    if (!empty($mobilesettings->iosappid)) {
52
        $downloadurl->param('iosappid', $mobilesettings->iosappid);
53
    }
54
 
55
    if (!empty($mobilesettings->androidappid)) {
56
        $downloadurl->param('androidappid', $mobilesettings->androidappid);
57
    }
58
 
59
    // For privacy reasons, add siteurl param only if the site is registered.
60
    // This is to implement Google Play Referrer (so the site url is automatically populated in the app after installation).
61
    if (\core\hub\registration::is_registered()) {
62
        $downloadurl->param('siteurl', $CFG->wwwroot);
63
    }
64
 
65
    return $downloadurl;
66
}
67
 
68
/**
69
 * Return the user mobile app WebService access token.
70
 *
71
 * @param  int $userid the user to return the token from
72
 * @return stdClass|false the token or false if the token doesn't exists
73
 * @since  3.10
74
 */
75
function tool_mobile_get_token($userid) {
76
    global $DB;
77
 
78
    $sql = "SELECT t.*
79
              FROM {external_tokens} t, {external_services} s
80
             WHERE t.externalserviceid = s.id
81
               AND s.enabled = 1
82
               AND s.shortname IN ('moodle_mobile_app', 'local_mobile')
83
               AND t.userid = ?";
84
 
85
    return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE);
86
}
87
 
88
/**
89
 * Checks if the given user has a mobile token (has used recently the app).
90
 *
91
 * @param  int $userid the user to check
92
 * @return bool true if the user has a token, false otherwise.
93
 */
94
function tool_mobile_user_has_token($userid) {
95
 
96
    return !empty(tool_mobile_get_token($userid));
97
}
98
 
99
/**
100
 * User profile page callback.
101
 *
102
 * Used add a section about the moodle mobile app.
103
 *
104
 * @param \core_user\output\myprofile\tree $tree My profile tree where the setting will be added.
105
 * @param stdClass $user The user object.
106
 * @param bool $iscurrentuser Is this the current user viewing
107
 * @return void Return if the mobile web services setting is disabled or if not the current user.
108
 */
109
function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) {
110
    global $CFG;
111
 
112
    if (empty($CFG->enablemobilewebservice)) {
113
        return;
114
    }
115
 
116
    $newnodes = [];
117
    $mobilesettings = get_config('tool_mobile');
118
 
119
    // Check if we should display a QR code.
120
    if ($iscurrentuser && !empty($mobilesettings->qrcodetype)) {
121
        $mobileqr = null;
122
        $qrcodeforappstr = get_string('qrcodeformobileappaccess', 'tool_mobile');
123
 
124
        if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_LOGIN && is_https()) {
125
 
126
            if (is_siteadmin() || \core\session\manager::is_loggedinas()) {
127
                $mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile');
128
            } else {
129
                $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
130
 
131
                $qrkeyttl = !empty($mobilesettings->qrkeyttl) ? $mobilesettings->qrkeyttl : tool_mobile\api::LOGIN_QR_KEY_TTL;
132
                $mobileqr = html_writer::tag('p', get_string('qrcodeformobileapploginabout', 'tool_mobile',
133
                    format_time($qrkeyttl)));
134
                $mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'),
135
                    ['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse',
136
                    'role' => 'button', 'aria-expanded' => 'false']);
137
                $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']);
138
            }
139
 
140
        } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) {
141
            $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
142
 
143
            $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile');
144
            $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr));
145
        }
146
 
147
        if ($mobileqr) {
148
            $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr);
149
        }
150
    }
151
 
152
    // Check if the user is using the app, encouraging him to use it otherwise.
153
    if ($iscurrentuser || is_siteadmin()) {
154
        $usertoken = tool_mobile_get_token($user->id);
155
        $mobilestrconnected = null;
156
        $mobilelastaccess = null;
157
 
158
        if ($usertoken) {
159
            $mobilestrconnected = get_string('lastsiteaccess');
160
            if ($usertoken->lastaccess) {
161
                $mobilelastaccess = userdate($usertoken->lastaccess) . "&nbsp; (" . format_time(time() - $usertoken->lastaccess) . ")";
162
                // Logout link.
163
                $validtoken = empty($usertoken->validuntil) || time() < $usertoken->validuntil;
164
                if ($iscurrentuser && $validtoken) {
165
                    $url = new moodle_url('/'.$CFG->admin.'/tool/mobile/logout.php', ['sesskey' => sesskey()]);
166
                    $logoutlink = html_writer::link($url, get_string('logout'));
167
                    $mobilelastaccess .= "&nbsp; ($logoutlink)";
168
                }
169
            } else {
170
                // We should not reach this point.
171
                $mobilelastaccess = get_string("never");
172
            }
173
        } else if ($url = tool_mobile_create_app_download_url()) {
174
             $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out());
175
        }
176
 
177
        if ($mobilestrconnected) {
178
            $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null,
179
                $mobilelastaccess);
180
        }
181
    }
182
 
183
    // Add nodes, if any.
184
    if (!empty($newnodes)) {
185
        $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity');
186
        $tree->add_category($mobilecat);
187
 
188
        foreach ($newnodes as $node) {
189
            $tree->add_node($node);
190
        }
191
    }
192
}
193
 
194
/**
195
 * Callback to be able to change a message/notification data per processor.
196
 *
197
 * @param  str $procname    processor name
198
 * @param  stdClass $data   message or notification data
199
 */
200
function tool_mobile_pre_processor_message_send($procname, $data) {
201
    global $CFG;
202
 
203
    if (empty($CFG->enablemobilewebservice)) {
204
        return;
205
    }
206
 
207
    if (empty($data->userto)) {
208
        return;
209
    }
210
 
211
    // Only hack email.
212
    if ($procname == 'email') {
213
 
214
        // Send a message only when there is an HTML version of the email, mobile services are enabled,
215
        // the user receiving the message has not used the app and there is an app download URL set.
216
        if (empty($data->fullmessagehtml)) {
217
            return;
218
        }
219
 
220
        if (!$url = tool_mobile_create_app_download_url()) {
221
            return;
222
        }
223
 
224
        $userto = is_object($data->userto) ? $data->userto->id : $data->userto;
225
        if (tool_mobile_user_has_token($userto)) {
226
            return;
227
        }
228
 
229
        $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out()));
230
    }
231
}