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
 * Post read receipt collection class.
19
 *
20
 * @package    mod_forum
21
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_forum\local\entities;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use mod_forum\local\entities\post as post_entity;
30
use stdClass;
31
 
32
/**
33
 * Post read receipt collection class.
34
 *
35
 * Contains the list of read receipts for posts.
36
 *
37
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class post_read_receipt_collection {
41
    /** @var stdClass[] $receiptsbypostid Receipt records indexed by post id */
42
    private $receiptsbypostid = [];
43
 
44
    /**
45
     * Constructor.
46
     *
47
     * @param array $records The list of post read receipt records.
48
     */
49
    public function __construct(array $records) {
50
        foreach ($records as $record) {
51
            $postid = $record->postid;
52
 
53
            if (isset($this->receiptsbypostid[$postid])) {
54
                $this->receiptsbypostid[$postid][] = $record;
55
            } else {
56
                $this->receiptsbypostid[$postid] = [$record];
57
            }
58
        }
59
    }
60
 
61
    /**
62
     * Check whether a user has read a post.
63
     *
64
     * @param stdClass $user The user to check
65
     * @param post_entity $post The post to check
66
     * @return bool
67
     */
68
    public function has_user_read_post(stdClass $user, post_entity $post): bool {
69
        global $CFG;
70
        $isoldpost = ($post->get_time_modified() < (time() - ($CFG->forum_oldpostdays * 24 * 3600)));
71
 
72
        if ($isoldpost) {
73
            return true;
74
        }
75
 
76
        $receipts = isset($this->receiptsbypostid[$post->get_id()]) ? $this->receiptsbypostid[$post->get_id()] : [];
77
 
78
        foreach ($receipts as $receipt) {
79
            if ($receipt->userid == $user->id) {
80
                return true;
81
            }
82
        }
83
 
84
        return false;
85
    }
86
}