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 moodle_exception;
20
use stdClass;
21
 
22
/**
23
 * Record the sharing of content to MoodleNet.
24
 *
25
 * @package   core
26
 * @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class share_recorder {
30
 
31
    /**
32
     * @var int The content being shared is a course.
33
     */
34
    public const TYPE_COURSE = 1;
35
 
36
    /**
37
     * @var int The content being shared is an activity.
38
     */
39
    public const TYPE_ACTIVITY = 2;
40
 
41
    /**
42
     * @var int The status of the share is 'sent'.
43
     */
44
    public const STATUS_SENT = 1;
45
 
46
    /**
47
     * @var int The status of the share is 'in progress'.
48
     */
49
    public const STATUS_IN_PROGRESS = 2;
50
 
51
    /**
52
     * @var int The status of the share is 'error'.
53
     */
54
    public const STATUS_ERROR = 3;
55
 
56
    /**
57
     * Get all allowed share types.
58
     *
59
     * @return array
60
     */
61
    protected static function get_allowed_share_types(): array {
62
 
63
        return [
64
            self::TYPE_ACTIVITY,
65
            self::TYPE_COURSE
66
        ];
67
    }
68
 
69
    /**
70
     * Get all allowed share statuses.
71
     * Note that the particular status values aid in sorting.
72
     *
73
     * @return array
74
     */
75
    protected static function get_allowed_share_statuses(): array {
76
 
77
        return [
78
            self::STATUS_SENT,
79
            self::STATUS_IN_PROGRESS,
80
            self::STATUS_ERROR,
81
        ];
82
    }
83
 
84
    /**
85
     * Create a new share progress record in the DB.
86
     *
87
     * @param int $sharetype The type of share (e.g. TYPE_COURSE).
88
     * @param int $userid The ID of the user performing the share.
89
     * @param int $courseid The associated course id.
90
     * @param int|null $cmid The associated course module id (when sharing activity).
91
     * @return int Returns the inserted record id.
92
     */
93
    public static function insert_share_progress(int $sharetype, int $userid, int $courseid, ?int $cmid = null): int {
94
        global $DB, $USER;
95
 
96
        if (!in_array($sharetype, self::get_allowed_share_types())) {
97
            throw new moodle_exception('moodlenet:invalidsharetype');
98
        }
99
 
100
        $data = new stdClass();
101
        $data->type = $sharetype;
102
        $data->courseid = $courseid;
103
        $data->cmid = $cmid;
104
        $data->userid = $userid;
105
        $data->timecreated = time();
106
        $data->status = self::STATUS_IN_PROGRESS;
107
 
108
        return $DB->insert_record('moodlenet_share_progress', $data);
109
    }
110
 
111
    /**
112
     * Update the share progress record in the DB.
113
     *
114
     * @param int $shareid The id of the share progress row being updated.
115
     * @param int $status The status of the share progress (e.g. STATUS_SENT).
116
     * @param string|null $resourceurl The resource url returned from MoodleNet.
117
     */
118
    public static function update_share_progress(int $shareid, int $status, ?string $resourceurl = null): void {
119
        global $DB;
120
 
121
        if (!in_array($status, self::get_allowed_share_statuses())) {
122
            throw new moodle_exception('moodlenet:invalidsharestatus');
123
        }
124
 
125
        $data = new stdClass();
126
        $data->id = $shareid;
127
        $data->resourceurl = $resourceurl;
128
        $data->status = $status;
129
 
130
        $DB->update_record('moodlenet_share_progress', $data);
131
    }
132
}