Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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 core\moodlenet;
18
 
19
use cm_info;
20
use core\event\moodlenet_resource_exported;
21
use core\oauth2\client;
22
use moodle_exception;
23
use stored_file;
24
 
25
/**
26
 * API for sharing Moodle LMS activities to MoodleNet instances.
27
 *
28
 * @package   core
29
 * @copyright 2023 Michael Hawkins <michaelh@moodle.com>
30
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class activity_sender extends resource_sender {
33
 
34
    /**
35
     * @var cm_info The context module info object for the activity being shared.
36
     */
37
    protected cm_info $cminfo;
38
 
39
    /**
40
     * Class constructor.
41
     *
42
     * @param int $cmid The course module ID of the activity being shared
43
     * @param int $userid The user ID who is sharing the activity
44
     * @param moodlenet_client $moodlenetclient The moodlenet_client object used to perform the share
45
     * @param client $oauthclient The OAuth 2 client for the MoodleNet instance
46
     * @param int $shareformat The data format to share in. Defaults to a Moodle backup (SHARE_FORMAT_BACKUP)
47
     */
48
    public function __construct(
49
        int $cmid,
50
        protected int $userid,
51
        protected moodlenet_client $moodlenetclient,
52
        protected client $oauthclient,
53
        protected int $shareformat = self::SHARE_FORMAT_BACKUP,
54
    ) {
55
        parent::__construct($cmid, $userid, $moodlenetclient, $oauthclient, $shareformat);
56
        [$this->course, $this->cminfo] = get_course_and_cm_from_cmid($cmid);
57
        $this->packager = new activity_packager($this->cminfo, $this->userid);
58
    }
59
 
60
    /**
1441 ariadna 61
     * @deprecated since Moodle 4.3 MDL-75318
1 efrain 62
     */
1441 ariadna 63
    #[\core\attribute\deprecated('share_resource()', since: '4.3', mdl: 'MDL-75318', final: true)]
64
    public function share_activity() {
65
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 66
    }
67
 
68
    /**
69
     * Share an activity/resource to MoodleNet.
70
     *
71
     * @return array The HTTP response code from MoodleNet and the MoodleNet draft resource URL (URL empty string on fail).
72
     *               Format: ['responsecode' => 201, 'drafturl' => 'https://draft.mnurl/here']
73
     */
74
    public function share_resource(): array {
75
        $accesstoken = '';
76
        $resourceurl = '';
77
        $issuer = $this->oauthclient->get_issuer();
78
 
79
        // Check user can share to the requested MoodleNet instance.
80
        $coursecontext = \core\context\course::instance($this->course->id);
81
        $usercanshare = utilities::can_user_share($coursecontext, $this->userid);
82
 
83
        if ($usercanshare && utilities::is_valid_instance($issuer) && $this->oauthclient->is_logged_in()) {
84
            $accesstoken = $this->oauthclient->get_accesstoken()->token;
85
        }
86
 
87
        // Throw an exception if the user is not currently set up to be able to share to MoodleNet.
88
        if (!$accesstoken) {
89
            throw new moodle_exception('moodlenet:usernotconfigured');
90
        }
91
 
92
        // Attempt to prepare and send the resource if validation has passed and we have an OAuth 2 token.
93
 
94
        // Prepare file in requested format.
95
        $filedata = $this->prepare_share_contents();
96
 
97
        // If we have successfully prepared a file to share of permitted size, share it to MoodleNet.
98
        if (!empty($filedata)) {
99
            // Avoid sending a file larger than the defined limit.
100
            $filesize = $filedata->get_filesize();
101
            if ($filesize > self::MAX_FILESIZE) {
102
                $filedata->delete();
103
                throw new moodle_exception('moodlenet:sharefilesizelimitexceeded', 'core', '', [
104
                    'filesize' => $filesize,
105
                    'filesizelimit' => self::MAX_FILESIZE,
106
                ]);
107
            }
108
 
109
            // MoodleNet only accept plaintext descriptions.
110
            $resourcedescription = $this->get_resource_description($coursecontext);
111
 
112
            $response = $this->moodlenetclient->create_resource_from_stored_file(
113
                $filedata,
114
                $this->cminfo->name,
115
                $resourcedescription,
116
            );
117
            $responsecode = $response->getStatusCode();
118
 
119
            $responsebody = json_decode($response->getBody());
120
            $resourceurl = $responsebody->homepage ?? '';
121
 
122
            // Delete the generated file now it is no longer required.
123
            // (It has either been sent, or failed - retries not currently supported).
124
            $filedata->delete();
125
        }
126
 
127
        // Log every attempt to share (and whether or not it was successful).
128
        $this->log_event($coursecontext, $this->cminfo->id, $resourceurl, $responsecode);
129
 
130
        return [
131
            'responsecode' => $responsecode,
132
            'drafturl' => $resourceurl,
133
        ];
134
    }
135
 
136
    /**
137
     * Log an event to the admin logs for an outbound share attempt.
138
     *
139
     * @param \context $coursecontext The course context being shared from.
140
     * @param int $cmid The CMID of the activity being shared.
141
     * @param string $resourceurl The URL of the draft resource if it was created.
142
     * @param int $responsecode The HTTP response code describing the outcome of the attempt.
143
     * @return void
144
     */
145
    protected function log_event(
146
        \core\context $coursecontext,
147
        int $cmid,
148
        string $resourceurl,
149
        int $responsecode,
150
    ): void {
151
        $event = moodlenet_resource_exported::create([
152
            'context' => $coursecontext,
153
            'other' => [
154
                'cmids' => [$cmid],
155
                'resourceurl' => $resourceurl,
156
                'success' => ($responsecode == 201),
157
            ],
158
        ]);
159
        $event->trigger();
160
    }
161
 
162
    /**
163
     * Fetch the description for the resource being created, in a supported text format.
164
     *
165
     * @param \context $coursecontext The course context being shared from.
166
     * @return string Converted activity description.
167
     */
168
    protected function get_resource_description(
169
        \context $coursecontext,
170
    ): string {
171
        global $PAGE, $DB;
172
        // We need to set the page context here because content_to_text and format_text will need the page context to work.
173
        $PAGE->set_context($coursecontext);
174
 
175
        $intro = $DB->get_record($this->cminfo->modname, ['id' => $this->cminfo->instance], 'intro, introformat', MUST_EXIST);
176
        $processeddescription = strip_tags($intro->intro);
177
        $processeddescription = content_to_text(format_text(
178
            $processeddescription,
179
            $intro->introformat,
180
        ), $intro->introformat);
181
 
182
        return $processeddescription;
183
    }
184
}