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
 * The comments block helper functions and callbacks
19
 *
20
 * @package   block_comments
21
 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Validate comment parameter before perform other comments actions
29
 *
30
 * @package  block_comments
31
 * @category comment
32
 *
33
 * @param stdClass $comment_param {
34
 *              context  => context the context object
35
 *              courseid => int course id
36
 *              cm       => stdClass course module object
37
 *              commentarea => string comment area
38
 *              itemid      => int itemid
39
 * }
40
 * @return boolean
41
 */
42
function block_comments_comment_validate($comment_param) {
43
    if ($comment_param->commentarea != 'page_comments') {
44
        throw new comment_exception('invalidcommentarea');
45
    }
46
    if ($comment_param->itemid != 0) {
47
        throw new comment_exception('invalidcommentitemid');
48
    }
49
    return true;
50
}
51
 
52
/**
53
 * Running addtional permission check on plugins
54
 *
55
 * @package  block_comments
56
 * @category comment
57
 *
58
 * @param stdClass $args
59
 * @return array
60
 */
61
function block_comments_comment_permissions($args) {
62
    global $DB, $USER;
63
    // By default, anyone can post and view comments.
64
    $canpost = $canview = true;
65
    // Check if it's the user context and not the owner's profile.
66
    if ($args->context->contextlevel == CONTEXT_USER && $USER->id != $args->context->instanceid) {
67
        // Check whether the context owner has a comment block in the user's profile.
68
        $sqlparam = [
69
            'blockname' => 'comments',
70
            'parentcontextid' => $args->context->id,
71
            'pagetypepattern' => 'user-profile',
72
        ];
73
        // If the comment block is not present at the target user's profile,
74
        // then the logged-in user cannot post or view comments.
75
        $canpost = $canview = $DB->record_exists_select(
76
            'block_instances',
77
            'blockname = :blockname AND parentcontextid = :parentcontextid AND pagetypepattern = :pagetypepattern',
78
            $sqlparam,
79
        );
80
    }
81
    return ['post' => $canpost, 'view' => $canview];
82
}
83
 
84
/**
85
 * Validate comment data before displaying comments
86
 *
87
 * @package  block_comments
88
 * @category comment
89
 *
90
 * @param stdClass $comment
91
 * @param stdClass $args
92
 * @return boolean
93
 */
94
function block_comments_comment_display($comments, $args) {
95
    if ($args->commentarea != 'page_comments') {
96
        throw new comment_exception('invalidcommentarea');
97
    }
98
    if ($args->itemid != 0) {
99
        throw new comment_exception('invalidcommentitemid');
100
    }
101
    return $comments;
102
}