Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
 * Contains a helper class providing util methods for testing.
19
 *
20
 * @package    core_message
21
 * @copyright  2018 Jake Dallimore <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_message\tests;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
/**
30
 * The helper class providing util methods for testing.
31
 *
32
 * @copyright  2018 Jake Dallimore <markn@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class helper {
36
    /**
37
     * Sends a message to a conversation.
38
     *
39
     * @param \stdClass $userfrom user object of the one sending the message.
40
     * @param int $convid id of the conversation in which we'll send the message.
41
     * @param string $message message to send.
42
     * @param int $time the time the message was sent.
43
     * @return int the id of the message which was sent.
44
     * @throws \dml_exception if the conversation doesn't exist.
45
     */
46
    public static function send_fake_message_to_conversation(\stdClass $userfrom, int $convid, string $message = 'Hello world!',
47
            int $time = null): int {
48
        global $DB;
49
        $conversationrec = $DB->get_record('message_conversations', ['id' => $convid], 'id', MUST_EXIST);
50
        $conversationid = $conversationrec->id;
51
        $time = $time ?? time();
52
        $record = new \stdClass();
53
        $record->useridfrom = $userfrom->id;
54
        $record->conversationid = $conversationid;
55
        $record->subject = 'No subject';
56
        $record->fullmessage = $message;
57
        $record->smallmessage = $message;
58
        $record->timecreated = $time;
59
        return $DB->insert_record('messages', $record);
60
    }
61
 
62
    /**
63
     * Send a fake unread notification.
64
     *
65
     * message_send() does not support transaction, this function will simulate a message
66
     * sent from a user to another. We should stop using it once message_send() will support
67
     * transactions. This is not clean at all, this is just used to add rows to the table.
68
     *
69
     * @param stdClass $userfrom user object of the one sending the message.
70
     * @param stdClass $userto user object of the one receiving the message.
71
     * @param string $message message to send.
72
     * @param int $timecreated time the message was created.
73
     * @return int the id of the message
74
     */
75
    public static function send_fake_unread_notification(\stdClass $userfrom, \stdClass $userto, string $message = 'Hello world!',
76
            int $timecreated = 0): int {
77
        global $DB;
78
 
79
        $record = new \stdClass();
80
        $record->useridfrom = $userfrom->id;
81
        $record->useridto = $userto->id;
82
        $record->notification = 1;
83
        $record->subject = 'No subject';
84
        $record->fullmessage = $message;
85
        $record->smallmessage = $message;
86
        $record->timecreated = $timecreated ? $timecreated : time();
87
        $record->customdata  = json_encode(['datakey' => 'data']);
88
 
89
        return $DB->insert_record('notifications', $record);
90
    }
91
 
92
    /**
93
     * Send a fake read notification.
94
     *
95
     * message_send() does not support transaction, this function will simulate a message
96
     * sent from a user to another. We should stop using it once message_send() will support
97
     * transactions. This is not clean at all, this is just used to add rows to the table.
98
     *
99
     * @param stdClass $userfrom user object of the one sending the message.
100
     * @param stdClass $userto user object of the one receiving the message.
101
     * @param string $message message to send.
102
     * @param int $timecreated time the message was created.
103
     * @param int $timeread the the message was read
104
     * @return int the id of the message
105
     */
106
    public static function send_fake_read_notification(\stdClass $userfrom, \stdClass $userto, string $message = 'Hello world!',
107
                                                       int $timecreated = 0, int $timeread = 0): int {
108
        global $DB;
109
 
110
        $record = new \stdClass();
111
        $record->useridfrom = $userfrom->id;
112
        $record->useridto = $userto->id;
113
        $record->notification = 1;
114
        $record->subject = 'No subject';
115
        $record->fullmessage = $message;
116
        $record->smallmessage = $message;
117
        $record->timecreated = $timecreated ? $timecreated : time();
118
        $record->timeread = $timeread ? $timeread : time();
119
 
120
        $record->id = $DB->insert_record('notifications', $record);
121
 
122
        // Mark it as read.
123
        \core_message\api::mark_notification_as_read($record);
124
 
125
        return $record->id;
126
    }
127
}