• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ewallah / moodle-report_comments / 3991272918

pending completion
3991272918

push

github

Renaat Debleu
coveralls

90 of 270 relevant lines covered (33.33%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.49
/classes/usertable.php
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
 * Comments table per user.
19
 *
20
 * @package    report_comments
21
 * @copyright  2017 iplusacademy.org
22
 * @author     Renaat Debleu <info@eWallah.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace report_comments;
26

27
defined('MOODLE_INTERNAL') || die;
×
28
require_once($CFG->libdir . '/tablelib.php');
×
29

30
use context;
31
use core_user;
32
use core_component;
33
use html_writer;
34
use moodle_url;
35
use moodle_exception;
36
use stdClass;
37

38
/**
39
 * Extends table_sql to provide a table of user comments
40
 *
41
 * @package    report_comments
42
 * @copyright  2017 iplusacademy.org
43
 * @author     Renaat Debleu <info@eWallah.net>
44
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
46
class usertable extends \table_sql {
47

48
    /** @var string Time format. */
49
    protected $timeformat;
50
    /** @var int Counter. */
51
    protected $counter;
52
    /** @var bool Download. */
53
    public $download;
54

55
    /**
56
     * Overridden constructor.
57
     *
58
     * @param int $userid The affected user
59
     * @param bool $download quickgrading Is this table wrapped in a quickgrading form?
60
     */
61
    public function __construct($userid, $download = false) {
62
        parent::__construct('comments');
2✔
63
        $this->download = $download;
2✔
64
        $arr = ['course' => 1, 'id' => $userid];
2✔
65
        $this->define_baseurl(new moodle_url('/report/comments/index.php', $arr));
2✔
66
        $this->timeformat = get_string('strftimerecentfull', 'langconfig');
2✔
67
        $this->counter = 1;
2✔
68
        $this->set_sql('id, timecreated, userid, content, format, contextid, component, commentarea, itemid',
2✔
69
           '{comments}', 'userid = :userid', ['userid' => $userid]);
2✔
70
        $this->set_count_sql('SELECT COUNT(id) FROM {comments} WHERE userid = :userid', ['userid' => $userid]);
2✔
71
        if ($download) {
2✔
72
            $this->define_columns(['id', 'timecreated', 'userid', 'content']);
1✔
73
            $this->define_headers(['', get_string('date'), get_string('user'), get_string('content')]);
1✔
74
        } else {
75
            $this->define_columns(['id', 'timecreated', 'userid', 'content', 'action']);
2✔
76
            $this->define_headers(['', get_string('date'), get_string('user'), get_string('content'), get_string('action')]);
2✔
77
        }
78
        $this->column_nosort[] = 'action';
2✔
79
        $this->collapsible(false);
2✔
80
        $this->showdownloadbuttonsat = [TABLE_P_BOTTOM];
2✔
81
    }
82

83
    /**
84
     * Id columnn.
85
     * Used to check capabilities.
86
     *
87
     * @param stdClass $row
88
     * @return string
89
     */
90
    public function col_id(stdClass $row) {
91
        global $CFG;
2✔
92
        $context = context::instance_by_id($row->contextid, IGNORE_MISSING);
2✔
93
        if ($context) {
2✔
94
            $row->contexturl = false;
2✔
95
            if (has_capability('report/comments:view', $context)) {
2✔
96
                switch ($context->contextlevel) {
2✔
97
                    case CONTEXT_MODULE:
2✔
98
                        $cm = get_coursemodule_from_id('', $context->instanceid);
1✔
99
                        $base = core_component::get_component_directory('mod_' . $cm->modname);
1✔
100
                        if (file_exists("$base/view.php")) {
1✔
101
                            $base = substr($base, strlen($CFG->dirroot));
1✔
102
                            $row->contexturl = new moodle_url("$base/view.php", ['id' => $cm->id]);
1✔
103
                        }
104
                        break;
1✔
105
                    case CONTEXT_COURSE:
2✔
106
                        $row->contexturl = course_get_url(get_course($context->instanceid));
1✔
107
                        break;
1✔
108
                    default:
109
                        throw new moodle_exception('invalid context');
1✔
110
                }
111
            }
112
        }
113
        if (!$row->contexturl) {
1✔
114
            $row->content = '***';
1✔
115
            $row->context = 0;
1✔
116
        }
117
        return $this->counter++;
1✔
118
    }
119

120
    /**
121
     * Time comment created columnn.
122
     *
123
     * @param stdClass $row
124
     * @return string
125
     */
126
    public function col_timecreated(stdClass $row) {
127
        if ($row->contexturl) {
1✔
128
            return html_writer::link($row->contexturl, userdate($row->timecreated, $this->timeformat));
1✔
129
        }
130
        return userdate($row->timecreated, $this->timeformat);
1✔
131
    }
132

133
    /**
134
     * Comment text columnn.
135
     *
136
     * @param stdClass $row
137
     * @return string
138
     */
139
    public function col_content(stdClass $row) {
140
        return format_text($row->content, $row->format);
1✔
141
    }
142

143
    /**
144
     * User picture columnn.
145
     *
146
     * @param stdClass $row
147
     * @return string
148
     */
149
    public function col_userid(stdClass $row) {
150
        global $OUTPUT;
1✔
151
        $s = '';
1✔
152
        if ($row->contexturl && $user = core_user::get_user($row->userid)) {
1✔
153
            $s = ($this->download) ? fullname($user) : $OUTPUT->user_picture($user);
1✔
154
        }
155
        return $s;
1✔
156
    }
157

158
    /**
159
     * Columnn with buttons.
160
     *
161
     * @param stdClass $row
162
     * @return string
163
     */
164
    public function col_action(stdClass $row) {
165
        $s = '';
1✔
166
        if (!$this->is_downloading() && $row->contexturl) {
1✔
167
            $arr = $this->baseurl->params();
1✔
168
            $arr['action'] = 'delete';
1✔
169
            $arr['sesskey'] = sesskey();
1✔
170
            $url = new moodle_url($this->baseurl->out_omit_querystring(), $arr);
1✔
171
            $s = html_writer::empty_tag('input', ['type' => 'submit', 'formaction' => $url, 'value' => get_string('delete')]);
1✔
172
        }
173
        return $s;
1✔
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc