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
 * Contacts widget class contains the layout information and generate the data for widget.
19
 *
20
 * @package    block_dash
21
 * @copyright  2022 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\widget\contacts;
26
 
27
use block_dash\local\widget\abstract_widget;
28
use context_block;
29
use moodle_url;
30
 
31
/**
32
 * Contacts widget contains list of available contacts.
33
 */
34
class contacts_widget extends abstract_widget {
35
 
36
    /**
37
     * Get the name of widget.
38
     *
39
     * @return void
40
     */
41
    public function get_name() {
42
        return get_string('widget:mycontacts', 'block_dash');
43
    }
44
 
45
    /**
46
     * Check the widget support uses the query method to build the widget.
47
     *
48
     * @return bool
49
     */
50
    public function supports_query() {
51
        return false;
52
    }
53
 
54
    /**
55
     * Layout class widget will use to render the widget content.
56
     *
57
     * @return \abstract_layout
58
     */
59
    public function layout() {
60
        return new contacts_layout($this);
61
    }
62
 
63
    /**
64
     * Pre defined preferences that widget uses.
65
     *
66
     * @return array
67
     */
68
    public function widget_preferences() {
69
        $preferences = [
70
            'datasource' => 'contacts',
71
            'layout' => 'contacts',
72
        ];
73
        return $preferences;
74
    }
75
 
76
    /**
77
     * Build widget data and send to layout thene the layout will render the widget.
78
     *
79
     * @return void
80
     */
81
    public function build_widget() {
82
        global $USER, $DB, $PAGE;
83
        static $jsincluded = false;
84
        $userid = $USER->id;
85
        $contactslist = (method_exists('\core_message\api', 'get_user_contacts'))
86
            ? \core_message\api::get_user_contacts($userid) : \core_message\api::get_contacts($userid);
87
 
88
        if (defined('\core_message\api::MESSAGE_ACTION_READ')) {
89
            $unreadcountssql = 'SELECT DISTINCT m.useridfrom, count(m.id) as unreadcount
90
                                FROM {messages} m
91
                            INNER JOIN {message_conversations} mc
92
                                    ON mc.id = m.conversationid
93
                            INNER JOIN {message_conversation_members} mcm
94
                                    ON m.conversationid = mcm.conversationid
95
                            LEFT JOIN {message_user_actions} mua
96
                                    ON (mua.messageid = m.id AND mua.userid = ? AND
97
                                    (mua.action = ? OR mua.action = ?))
98
                                WHERE mcm.userid = ?
99
                                AND m.useridfrom != ?
100
                                AND mua.id is NULL
101
                            GROUP BY m.useridfrom';
102
            $unreadcounts = $DB->get_records_sql($unreadcountssql,
103
                [
104
                    $userid,
105
                    \core_message\api::MESSAGE_ACTION_READ,
106
                    \core_message\api::MESSAGE_ACTION_DELETED,
107
                    $userid,
108
                    $userid,
109
                ]
110
            );
111
        } else {
112
            $unreadcountssql = 'SELECT useridfrom, count(*) as count
113
                                FROM {message}
114
                                WHERE useridto = ?
115
                                    AND timeusertodeleted = 0
116
                                    AND notification = 0
117
                                GROUP BY useridfrom';
118
            $unreadcounts = $DB->get_records_sql($unreadcountssql, [$userid]);
119
        }
120
 
121
        array_walk($contactslist, function($value) use ($unreadcounts, $PAGE) {
122
            $muserid = (isset($value->id)) ? $value->id : $value->userid; // Totara support.
123
            $value->contacturl = new \moodle_url('/message/index.php', ['id' => $muserid]);
124
            $user = \core_user::get_user($muserid);
125
            $userpicture = new \user_picture($user);
126
            $userpicture->size = 200; // Size f1.
127
            $value->profileimageurl = $userpicture->get_url($PAGE)->out(false);
128
            if ($user->picture == 0) {
129
                $value->profiletext = ucwords($value->fullname)[0];
130
            }
131
            $value->unreadcount = isset($unreadcounts[$user->id]) ?
132
                (isset($unreadcounts[$user->id]->unreadcount)
133
                    ? $unreadcounts[$user->id]->unreadcount : $unreadcounts[$user->id]->count) : false;
134
            $value->profileurl = new \moodle_url('/user/profile.php', ['id' => $muserid]);
135
            $value->id = $muserid; // Totara support.
136
        });
137
 
138
        $contextid = $this->get_block_instance()->context->id;
139
        $this->data = (!empty($contactslist)) ? [
140
            'contacts' => array_values($contactslist),
141
            'contextid' => $contextid,
142
 
143
        ] : [];
144
 
145
        $this->include_suggest_contacts();
146
 
147
        if (!$jsincluded) {
148
            $PAGE->requires->js_call_amd('block_dash/contacts', 'init', ['contextid' => $contextid]);
149
            $jsincluded = true;
150
        }
151
 
152
        return $this->data;
153
    }
154
 
155
    /**
156
     * Get user picture url for contact.
157
     *
158
     * @param stdclass $userid
159
     * @param string $suggestiontext
160
     * @return stdclass
161
     */
162
    public function get_user_data($userid, $suggestiontext) {
163
        global $PAGE, $OUTPUT;
164
        $user = \core_user::get_user($userid);
165
        $user->fullname = fullname($user);
166
        $user->suggestinfo[] = $suggestiontext;
167
        if (isset($user->picture) && $user->picture == 0) {
168
            $user->profiletext = ucwords($user->fullname)[0];
169
        }
170
        $userpicture = new \user_picture($user);
171
        $userpicture->size = 1; // Size f1.
172
        $user->profileimageurl = $userpicture->get_url($PAGE)->out(false);
173
        $user->addcontacticon = $icon = $OUTPUT->pix_icon('t/addcontact', get_string('addtocontacts', 'block_dash'), 'moodle',
174
        ['class' => 'drag-handle']);
175
        return $user;
176
    }
177
 
178
    /**
179
     * Include suggest contacts.
180
     *
181
     * @return array
182
     */
183
    public function include_suggest_contacts() {
184
        global $USER, $DB, $CFG;
185
        require_once($CFG->dirroot. '/cohort/lib.php');
186
        $userid = $USER->id;
187
        // User interests.
188
        $interests = \core_tag_tag::get_item_tags_array('core', 'user', $userid);
189
        $intereststatus = get_config('block_dash', 'suggestinterests');
190
 
191
        if (!empty($interests) && $intereststatus) {
192
            list($insql, $inparams) = $DB->get_in_or_equal($interests, SQL_PARAMS_NAMED, 'tg');
193
 
194
            $sql = "SELECT ti.*, tg.name, tg.rawname FROM {tag_instance} ti
195
            JOIN {tag} tg ON tg.id = ti.tagid
196
            WHERE ti.itemid <> :userid AND ti.itemtype = :itemtype AND ti.tagid IN ( SELECT id FROM {tag} t WHERE t.name $insql )";
197
            $lists = $DB->get_records_sql($sql, $inparams + ['userid' => $userid, 'itemtype' => 'user']);
198
        }
199
 
200
        $suggestions = [];
201
        if (isset($lists) && !empty($lists)) {
202
 
203
            $i = 0;
204
            foreach ($lists as $list) {
205
                $suggestiontext = get_string('suggestion:interest', 'block_dash', ['interest' => $list->name]);
206
                if (in_array($list->itemid, array_keys($suggestions))) {
207
                    $suggestions[$list->itemid]->suggestinfo[] = $suggestiontext;
208
                } else {
209
                    if ($intereststatus <= $i) {
210
                        continue;
211
                    }
212
                    $i++;
213
                    $user = $this->get_user_data($list->itemid, $suggestiontext);
214
                    $suggestions[$user->id]  = $user;
215
                }
216
            }
217
        }
218
 
219
        // Cohort suggestions.
220
        $sql = 'SELECT c.*
221
        FROM {cohort} c
222
        JOIN {cohort_members} cm ON c.id = cm.cohortid
223
        WHERE cm.userid = ? AND c.visible = 1';
224
        $usercohorts = $DB->get_records_sql($sql, [$userid]);
225
        $cohorts = array_column($usercohorts, 'id');
226
        $cohortstatus = get_config('block_dash', 'suggestcohort');
227
        if (!empty($cohorts) && $cohortstatus) {
228
            list($insql, $inparams) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED, 'ch');
229
            $sql = "SELECT cm.*, ch.name FROM {cohort_members} cm
230
                    JOIN {cohort} ch ON ch.id = cm.cohortid
231
                    WHERE cm.userid <> :userid AND cm.cohortid $insql";
232
            $members = $DB->get_records_sql($sql, $inparams + ['userid' => $userid]);
233
        }
234
        if (isset($members) && !empty($members)) {
235
            $i = 0;
236
            foreach ($members as $member) {
237
                $suggestiontext = get_string('suggestion:cohort', 'block_dash',
238
                    ['cohort' => $member->name]);
239
                if (in_array($member->userid, array_keys($suggestions))) {
240
                    $suggestions[$member->userid]->suggestinfo[] = $suggestiontext;
241
                } else {
242
                    if ($cohortstatus <= $i) {
243
                        continue;
244
                    }
245
                    $i++;
246
                    $user = $this->get_user_data($member->userid, $suggestiontext);
247
                    $suggestions[$user->id]  = $user;
248
                }
249
            }
250
        }
251
 
252
        // Groups suggestion.
253
        $sql = 'SELECT *, gm.id FROM {groups_members} gm
254
        JOIN {groups} g ON g.id = gm.groupid
255
        JOIN {user} u ON u.id = gm.userid
256
        WHERE gm.userid != :userid AND g.id IN (
257
            SELECT groupid FROM {groups_members} WHERE userid = :currentuserid
258
        )';
259
        $groups = $DB->get_records_sql($sql, ['userid' => $userid, 'currentuserid' => $userid]);
260
        $groupstatus = get_config('block_dash', 'suggestgroups');
261
        $i = 0;
262
        if (!empty($groups) && $groupstatus) {
263
            foreach ($groups as $group) {
264
                $suggestiontext = get_string('suggestion:groups', 'block_dash', ['group' => $group->name]);
265
                if (in_array($group->userid, array_keys($suggestions))) {
266
                    $suggestions[$group->userid]->suggestinfo[] = $suggestiontext;
267
                } else {
268
                    if ($groupstatus <= $i) {
269
                        continue;
270
                    }
271
                    $i++;
272
                    $user = $this->get_user_data($group->userid, $suggestiontext);
273
                    $suggestions[$user->id]  = $user;
274
                }
275
            }
276
        }
277
 
278
        $suggestusers = get_config('block_dash', 'suggestusers');
279
        $users = explode(',', $suggestusers);
280
        $suggestiontext = get_string('suggestion:users', 'block_dash');
281
        $users = array_filter($users, function($value) {
282
            return !is_null($value) && $value !== '';
283
        });
284
        if (!empty($users)) {
285
            foreach ($users as $suggestuser) {
286
                if (in_array($suggestuser, array_keys($suggestions))) {
287
                    $suggestions[$suggestuser]->suggestinfo[] = $suggestiontext;
288
                } else {
289
                    $user = $this->get_user_data($suggestuser, $suggestiontext);
290
                    $suggestions[$user->id]  = $user;
291
                }
292
            }
293
        }
294
 
295
        $contactslist = (method_exists('\core_message\api', 'get_user_contacts')) ?
296
            \core_message\api::get_user_contacts($userid) :
297
            array_flip(array_column(\core_message\api::get_contacts($userid), 'userid'));
298
 
299
        $contactusers = array_keys($contactslist);
300
        $suggestions = array_filter($suggestions, function($value) use ($contactusers) {
301
            if (!in_array($value->id, $contactusers)) {
302
                return $value;
303
            }
304
        });
305
        $this->data['suggestions'] = array_values($suggestions);
306
        $this->data['currentuser'] = $userid;
307
    }
308
 
309
    /**
310
     * Requires the JS libraries for the toggle contact button.
311
     *
312
     * @return void
313
     */
314
    public static function togglecontact_requirejs() {
315
        global $PAGE;
316
 
317
        static $done = false;
318
        if ($done) {
319
            return;
320
        }
321
 
322
        $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', ['.toggle-contact-button']);
323
        $done = true;
324
    }
325
 
326
    /**
327
     * Load groups that the contact user and the loggedin user in same group.
328
     *
329
     * @param context_block $context
330
     * @param stdclass $args
331
     * @return string $table html
332
     */
333
    public function load_groups($context, $args) {
334
        global $CFG;
335
        require_once($CFG->dirroot.'/lib/grouplib.php');
336
        $contactuserid = (int) $args->contactuser;
337
 
338
        if (block_dash_is_totara()) {
339
            $table = new \block_dash\table\groups_totara($context->instanceid);
340
            $table->set_filterset($contactuserid);
341
        } else {
342
            $filterset = new \block_dash\table\groups_filterset('dash-groups-'.$context->id);
343
            $contactuser = new \core_table\local\filter\integer_filter('contactuser');
344
            $contactuser->add_filter_value($contactuserid);
345
            $filterset->add_filter($contactuser);
346
 
347
            $table = new \block_dash\table\groups($context->instanceid);
348
            $table->set_filterset($filterset);
349
        }
350
 
351
        ob_start();
352
        echo \html_writer::start_div('dash-widget-table');
353
        $table->out(10, true);
354
        echo \html_writer::end_div();
355
        $tablehtml = ob_get_contents();
356
        ob_end_clean();
357
 
358
        return $tablehtml;
359
    }
360
}