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 core\moodlenet;
18
 
19
use core\event\moodlenet_resource_exported;
20
use core\oauth2\client;
21
use moodle_exception;
22
 
23
/**
24
 * API for sharing a number of Moodle LMS activities as a course backup to MoodleNet instances.
25
 *
26
 * @package    core
27
 * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class course_partial_sender extends course_sender {
31
 
32
    /**
33
     * Constructor for course sender.
34
     *
35
     * @param int $courseid The course ID of the course being shared
36
     * @param int $userid The user ID who is sharing the activity
37
     * @param moodlenet_client $moodlenetclient The moodlenet_client object used to perform the share
38
     * @param client $oauthclient The OAuth 2 client for the MoodleNet instance
39
     * @param int $shareformat The data format to share in. Defaults to a Moodle backup (SHARE_FORMAT_BACKUP)
40
     */
41
    public function __construct(
42
        int $courseid,
43
        protected int $userid,
44
        protected moodlenet_client $moodlenetclient,
45
        protected client $oauthclient,
46
        protected array $cmids,
47
        protected int $shareformat = self::SHARE_FORMAT_BACKUP,
48
    ) {
49
        parent::__construct($courseid, $userid, $moodlenetclient, $oauthclient, $shareformat);
50
        $this->validate_course_module_ids($this->course, $this->cmids);
51
        $this->packager = new course_partial_packager($this->course, $this->cmids, $this->userid);
52
    }
53
 
54
    /**
55
     * Log an event to the admin logs for an outbound share attempt.
56
     *
57
     * @param string $resourceurl The URL of the draft resource if it was created
58
     * @param int $responsecode The HTTP response code describing the outcome of the attempt
59
     * @return void
60
     */
61
    protected function log_event(
62
        string $resourceurl,
63
        int $responsecode,
64
    ): void {
65
        $event = moodlenet_resource_exported::create([
66
            'context' => $this->coursecontext,
67
            'other' => [
68
                'cmids' => $this->cmids,
69
                'courseid' => [$this->course->id],
70
                'resourceurl' => $resourceurl,
71
                'success' => ($responsecode === 201),
72
            ],
73
        ]);
74
        $event->trigger();
75
    }
76
 
77
    /**
78
     * Validate the course module ids.
79
     *
80
     * @param \stdClass $course Course object
81
     * @param array $cmids List of course module ids to check
82
     * @return void
83
     */
84
    protected function validate_course_module_ids(
85
        \stdClass $course,
86
        array $cmids,
87
    ): void {
88
        if (empty($cmids)) {
89
            throw new moodle_exception('invalidcoursemodule');
90
        }
91
        $modinfo = get_fast_modinfo($course);
92
        $cms = $modinfo->get_cms();
93
        foreach ($cmids as $cmid) {
94
            if (!array_key_exists($cmid, $cms)) {
95
                throw new moodle_exception('invalidcoursemodule');
96
            }
97
        }
98
    }
99
 
100
}