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
 * Groups 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\groups;
26
 
27
use block_dash\local\widget\abstract_widget;
28
use context_block;
29
use moodle_url;
30
use html_writer;
31
 
32
/**
33
 * Groups widget contains list of available contacts.
34
 */
35
class groups_widget extends abstract_widget {
36
 
37
    /**
38
     * Max Memebers count in group will displayed as images.
39
     */
40
    public const MEMBERSCOUNT = 10;
41
 
42
    /**
43
     * Get the name of widget.
44
     *
45
     * @return void
46
     */
47
    public function get_name() {
48
        return get_string('widget:mygroups', 'block_dash');
49
    }
50
 
51
    /**
52
     * Check the widget support uses the query method to build the widget.
53
     *
54
     * @return bool
55
     */
56
    public function supports_query() {
57
        return false;
58
    }
59
 
60
    /**
61
     * Layout class widget will use to render the widget content.
62
     *
63
     * @return \abstract_layout
64
     */
65
    public function layout() {
66
        return new groups_layout($this);
67
    }
68
 
69
    /**
70
     * Pre defined preferences that widget uses.
71
     *
72
     * @return array
73
     */
74
    public function widget_preferences() {
75
        $preferences = [
76
            'datasource' => 'groups',
77
            'layout' => 'groups',
78
        ];
79
        return $preferences;
80
    }
81
 
82
    /**
83
     * Add the create groups option next to header.
84
     *
85
     * @param array $data
86
     * @return void
87
     */
88
    public function update_data_before_render(&$data) {
89
        global $OUTPUT;
90
 
91
        $context = $this->get_block_instance()->context;
92
        $option = [
93
            'headermenu' => 'true',
94
            'creategroup' => has_capability('block/dash:mygroups_creategroup', $context),
95
        ];
96
        $data['blockmenu'] = $OUTPUT->render_from_template('block_dash/widget_groups', $option);
97
    }
98
 
99
    /**
100
     * Build widget data and send to layout thene the layout will render the widget.
101
     *
102
     * @return void
103
     */
104
    public function build_widget() {
105
        global $USER, $CFG, $PAGE;
106
 
107
        static $jsincluded = false;
108
 
109
        $userid = $USER->id;
110
        require_once($CFG->dirroot.'/lib/grouplib.php');
111
        require_once($CFG->dirroot . '/user/selector/lib.php');
112
 
113
        $context = $this->get_block_instance()->context;
114
        $creategroup = has_capability('block/dash:mygroups_creategroup', $context);
115
 
116
        $mygroups = groups_get_my_groups();
117
 
118
        array_walk($mygroups, function($group) use ($context) {
119
            $newgroup = groups_get_group($group->id);
120
            global $USER;
121
 
122
            $coursecontext = \context_course::instance($group->courseid, IGNORE_MISSING);
123
            if (empty($coursecontext)) {
124
                return null;
125
            }
126
            $conversation = (method_exists('\core_message\api', 'get_conversation_by_area'))
127
                ? \core_message\api::get_conversation_by_area(
128
                    'core_group', 'groups', $group->id, $coursecontext->id
129
                ) : '';
130
 
131
            $group->name = format_string($group->name);
132
            $group->chaturl = ($conversation && $conversation->enabled)
133
                ? new \moodle_url('/message/index.php', ['convid' => $conversation->id]) : '';
134
 
135
            $group->course = format_string(get_course($group->courseid)->fullname);
136
            $members = groups_get_members($group->id);
137
            unset($members[$USER->id]);
138
            if (count($members) > self::MEMBERSCOUNT) {
139
                $group->membercount = "+".(count($members) - self::MEMBERSCOUNT);
140
                $members = array_slice($members, 0, self::MEMBERSCOUNT);
141
            }
142
            $group->members = array_values($members);
143
 
144
            array_walk($group->members, function($member) {
145
                global $PAGE;
146
                // Set the user picture data.
147
                $userpicture = new \user_picture($member);
148
                $userpicture->size = 100; // Size f2.
149
                $member->profileimage = $userpicture->get_url($PAGE)->out(false);
150
                $member->fullname = fullname($member);
151
                $member->profileurl = new \moodle_url('/user/profile.php', ['id' => $member->id]);
152
            });
153
 
154
        });
155
 
156
        $this->data = (!empty($mygroups)) ? [
157
            'groups' => array_values($mygroups),
158
            'contextid' => $context->id,
159
            'viewgroups' => has_capability('block/dash:mygroups_view', $context),
160
            'adduser' => has_capability('block/dash:mygroups_addusers', $context),
161
            'leavegroup' => has_capability('block/dash:mygroups_leavegroup', $context),
162
            'viewmembers' => has_capability('block/dash:mygroups_viewmembers', $context),
163
            'creategroup' => has_capability('block/dash:mygroups_creategroup', $context),
164
        ] : [];
165
 
166
        if (!$jsincluded) {
167
            $PAGE->requires->js_call_amd('block_dash/groups', 'init', ['contextid' => $context->id]);
168
            $jsincluded = true;
169
        }
170
 
171
        return $this->data;
172
    }
173
 
174
    /**
175
     * Load groups that the contact user and the loggedin user in same group.
176
     *
177
     * @param context_block $context
178
     * @param stdclass $args
179
     * @return string $table html
180
     */
181
    public function viewmembers($context, $args) {
182
        global $CFG;
183
        require_once($CFG->dirroot.'/lib/grouplib.php');
184
        $groupid = (int) $args->group;
185
 
186
        if (block_dash_is_totara()) {
187
            $table = new \block_dash\table\members_totara($context->instanceid);
188
            $table->set_filterset($groupid);
189
        } else {
190
            $filterset = new \block_dash\table\members_filterset('dash-groups-'.$context->id);
191
            $group = new \core_table\local\filter\integer_filter('group');
192
            $group->add_filter_value($groupid);
193
            $filterset->add_filter($group);
194
 
195
            $table = new \block_dash\table\members($context->instanceid);
196
            $table->set_filterset($filterset);
197
        }
198
 
199
        ob_start();
200
        echo html_writer::start_div('dash-widget-table');
201
        $table->out(10, true);
202
        echo html_writer::end_div();
203
        $tablehtml = ob_get_contents();
204
        ob_end_clean();
205
 
206
        return $tablehtml;
207
    }
208
 
209
    /**
210
     * Returns the moodle form that helps to add memebers in the group.
211
     *
212
     * @param \context_block $context
213
     * @param \stdclass $args
214
     * @return \moodleform
215
     */
216
    public function addmembers($context, $args) {
217
        $group = groups_get_group($args->group);
218
        $memberform = new \block_dash\local\widget\groups\add_members(null, [
219
            'groupid' => $args->group, 'courseid' => $group->courseid,
220
        ]);
221
        return $memberform->render();
222
    }
223
 
224
    /**
225
     * Moodle form to create groups in any of the selected course.
226
     *
227
     * @param \context_block $context
228
     * @param \stdclass $args
229
     * @return void
230
     */
231
    public function creategroup($context, $args) {
232
        global $CFG;
233
 
234
        require_once($CFG->dirroot.'/lib/enrollib.php');
235
        require_once($CFG->dirroot.'/blocks/dash/locallib.php');
236
 
237
        $group = new \create_group();
238
        return $group->render();
239
    }
240
}