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
 * Contains the mobile output class for the custom certificate.
19
 *
20
 * @package   mod_customcert
21
 * @copyright 2018 Mark Nelson <markn@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_customcert\output;
26
 
27
/**
28
 * Mobile output class for the custom certificate.
29
 *
30
 * @copyright  2018 Mark Nelson <markn@moodle.com>
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class mobile {
34
 
35
    /**
36
     * Returns the initial page when viewing the activity for the mobile app.
37
     *
38
     * @param  array $args Arguments from tool_mobile_get_content WS
39
     * @return array HTML, javascript and other data
40
     */
41
    public static function mobile_view_activity($args) {
42
        global $OUTPUT, $DB, $USER;
43
 
44
        $args = (object) $args;
45
        $versionname = $args->appversioncode >= 3950 ? 'latest' : 'ionic3';
46
 
47
        $cmid = $args->cmid;
48
        $groupid = empty($args->group) ? 0 : (int) $args->group; // By default, group 0.
49
 
50
        // Capabilities check.
51
        $cm = get_coursemodule_from_id('customcert', $cmid);
52
        $context = \context_module::instance($cm->id);
53
        self::require_capability($cm, $context, 'mod/customcert:view');
54
 
55
        // Set some variables we are going to be using.
56
        $certificate = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
57
        $certificate->name = format_string($certificate->name);
58
        list($certificate->intro, $certificate->introformat) = \core_external\util::format_text($certificate->intro,
59
            $certificate->introformat, $context, 'mod_customcert', 'intro');
60
 
61
        // Get any issues this person may have.
62
        $issue = false;
63
        if ($issues = $DB->get_records('customcert_issues', ['userid' => $USER->id, 'customcertid' => $certificate->id],
64
                'timecreated DESC')) {
65
            $issue = reset($issues);
66
        }
67
 
68
        $requiredtimemet = true;
69
        $canmanage = has_capability('mod/customcert:manage', $context);
70
        if ($certificate->requiredtime && !$canmanage) {
71
            if (\mod_customcert\certificate::get_course_time($certificate->course) < ($certificate->requiredtime * 60)) {
72
                $requiredtimemet = false;
73
            }
74
        }
75
 
76
        $fileurl = "";
77
        if ($requiredtimemet) {
78
            $fileurl = new \moodle_url('/mod/customcert/mobile/pluginfile.php', ['certificateid' => $certificate->id,
79
                'userid' => $USER->id]);
80
            $fileurl = $fileurl->out(true);
81
        }
82
 
83
        $showreport = false;
84
        $groups = [];
85
        $recipients = [];
86
        if (has_capability('mod/customcert:viewreport', $context)) {
87
            $showreport = true;
88
 
89
            // Get the groups (if any) to display - also sets active group.
90
            $groups = self::get_groups($cm, $groupid, $USER->id);
91
            $groupmode = groups_get_activity_groupmode($cm);
92
            if (has_capability('moodle/site:accessallgroups', $context)) {
93
                $groupmode = 'aag';
94
            }
95
 
96
            $recipients = \mod_customcert\certificate::get_issues($certificate->id, $groupmode, $cm, 0, 0);
97
            foreach ($recipients as $recipient) {
98
                $recipient->displayname = fullname($recipient);
99
                $recipient->fileurl = new \moodle_url('/mod/customcert/mobile/pluginfile.php', ['certificateid' => $certificate->id,
100
                    'userid' => $recipient->id]);
101
            }
102
        }
103
 
104
        $data = [
105
            'certificate' => $certificate,
106
            'cmid' => $cm->id,
107
            'issue' => $issue,
108
            'showgroups' => !empty($groups),
109
            'groups' => array_values($groups),
110
            'canmanage' => $canmanage,
111
            'requiredtimemet' => $requiredtimemet,
112
            'fileurl' => $fileurl,
113
            'showreport' => $showreport,
114
            'hasrecipients' => !empty($recipients),
115
            'recipients' => array_values($recipients),
116
            'numrecipients' => count($recipients),
117
            'currenttimestamp' => time(),
118
        ];
119
 
120
        return [
121
            'templates' => [
122
                [
123
                    'id' => 'main',
124
                    'html' => $OUTPUT->render_from_template("mod_customcert/mobile_view_activity_page_$versionname", $data),
125
                ],
126
            ],
127
            'javascript' => '',
128
            'otherdata' => [
129
                'group' => $groupid,
130
            ],
131
        ];
132
    }
133
 
134
    /**
135
     * Returns an array of groups to be displayed (if applicable) for the activity.
136
     *
137
     * The groups API is a mess hence the hackiness.
138
     *
139
     * @param \stdClass $cm The course module
140
     * @param int $groupid The group id
141
     * @param int $userid The user id
142
     * @return array The array of groups, may be empty.
143
     */
144
    protected static function get_groups($cm, $groupid, $userid) {
145
        $arrgroups = [];
146
        if ($groupmode = groups_get_activity_groupmode($cm)) {
147
            if ($groups = groups_get_activity_allowed_groups($cm, $userid)) {
148
                $context = \context_module::instance($cm->id);
149
                if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
150
                    $allparticipants = new \stdClass();
151
                    $allparticipants->id = 0;
152
                    $allparticipants->name = get_string('allparticipants');
153
                    $allparticipants->selected = $groupid === 0;
154
                    $arrgroups[0] = $allparticipants;
155
                }
156
                self::update_active_group($groupmode, $groupid, $groups, $cm);
157
                // Detect which group is selected.
158
                foreach ($groups as $gid => $group) {
159
                    $group->selected = $gid == $groupid;
160
                    $arrgroups[] = $group;
161
                }
162
            }
163
        }
164
 
165
        return $arrgroups;
166
    }
167
 
168
    /**
169
     * Update the active group in the session.
170
     *
171
     * This is a hack. We can't call groups_get_activity_group to update the active group as it relies
172
     * on optional_param('group' .. which we won't have when using the mobile app.
173
     *
174
     * @param int $groupmode The group mode we are in, eg. NOGROUPS, VISIBLEGROUPS
175
     * @param int $groupid The id of the group that has been selected
176
     * @param array $allowedgroups The allowed groups this user can access
177
     * @param \stdClass $cm The course module
178
     */
179
    private static function update_active_group($groupmode, $groupid, $allowedgroups, $cm) {
180
        global $SESSION;
181
 
182
        $context = \context_module::instance($cm->id);
183
 
184
        if (has_capability('moodle/site:accessallgroups', $context)) {
185
            $groupmode = 'aag';
186
        }
187
 
188
        if ($groupid == 0) {
189
            // The groups are only all visible in VISIBLEGROUPS mode or if the user can access all groups.
190
            if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
191
                $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
192
            }
193
        } else {
194
            if ($allowedgroups && array_key_exists($groupid, $allowedgroups)) {
195
                $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $groupid;
196
            }
197
        }
198
    }
199
 
200
    /**
201
     * Confirms the user is logged in and has the specified capability.
202
     *
203
     * @param \stdClass $cm
204
     * @param \context $context
205
     * @param string $cap
206
     */
207
    protected static function require_capability(\stdClass $cm, \context $context, string $cap) {
208
        require_login($cm->course, false, $cm, true, true);
209
        require_capability($cap, $context);
210
    }
211
}