2018-05-23 06:35:23 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the mobile output class for the custom certificate.
|
|
|
|
*
|
2021-11-24 07:29:43 +00:00
|
|
|
* @package mod_htmlcert
|
|
|
|
* @copyright 2018 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
2018-05-23 06:35:23 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
namespace mod_htmlcert\output;
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mobile output class for the custom certificate.
|
|
|
|
*
|
|
|
|
* @copyright 2018 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class mobile {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the initial page when viewing the activity for the mobile app.
|
|
|
|
*
|
|
|
|
* @param array $args Arguments from tool_mobile_get_content WS
|
|
|
|
* @return array HTML, javascript and other data
|
|
|
|
*/
|
|
|
|
public static function mobile_view_activity($args) {
|
|
|
|
global $OUTPUT, $DB, $USER;
|
|
|
|
|
|
|
|
$args = (object) $args;
|
2021-06-18 07:13:56 +00:00
|
|
|
$versionname = $args->appversioncode >= 3950 ? 'latest' : 'ionic3';
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
$cmid = $args->cmid;
|
2021-06-18 07:13:56 +00:00
|
|
|
$groupid = empty($args->group) ? 0 : (int) $args->group; // By default, group 0.
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
// Capabilities check.
|
2021-11-24 07:29:43 +00:00
|
|
|
$cm = get_coursemodule_from_id('htmlcert', $cmid);
|
2018-05-23 06:35:23 +00:00
|
|
|
$context = \context_module::instance($cm->id);
|
2021-11-24 07:29:43 +00:00
|
|
|
self::require_capability($cm, $context, 'mod/htmlcert:view');
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
// Set some variables we are going to be using.
|
2021-11-24 07:29:43 +00:00
|
|
|
$certificate = $DB->get_record('htmlcert', ['id' => $cm->instance], '*', MUST_EXIST);
|
2018-05-23 06:35:23 +00:00
|
|
|
$certificate->name = format_string($certificate->name);
|
|
|
|
list($certificate->intro, $certificate->introformat) = external_format_text($certificate->intro,
|
2021-11-24 07:29:43 +00:00
|
|
|
$certificate->introformat, $context->id, 'mod_htmlcert', 'intro');
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
// Get any issues this person may have.
|
2018-07-09 15:45:19 +00:00
|
|
|
$issue = false;
|
2021-11-24 07:29:43 +00:00
|
|
|
if ($issues = $DB->get_records('htmlcert_issues', ['userid' => $USER->id, 'htmlcertid' => $certificate->id],
|
2018-07-09 15:45:19 +00:00
|
|
|
'timecreated DESC')) {
|
|
|
|
$issue = reset($issues);
|
|
|
|
}
|
2018-05-23 06:35:23 +00:00
|
|
|
|
2018-07-10 06:23:27 +00:00
|
|
|
$requiredtimemet = true;
|
2021-11-24 07:29:43 +00:00
|
|
|
$canmanage = has_capability('mod/htmlcert:manage', $context);
|
2018-06-19 11:10:08 +00:00
|
|
|
if ($certificate->requiredtime && !$canmanage) {
|
2021-11-24 07:29:43 +00:00
|
|
|
if (\mod_htmlcert\certificate::get_course_time($certificate->course) < ($certificate->requiredtime * 60)) {
|
2018-07-10 06:23:27 +00:00
|
|
|
$requiredtimemet = false;
|
2018-05-23 06:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileurl = "";
|
2018-07-10 06:23:27 +00:00
|
|
|
if ($requiredtimemet) {
|
2021-11-24 07:29:43 +00:00
|
|
|
$fileurl = new \moodle_url('/mod/htmlcert/mobile/pluginfile.php', ['certificateid' => $certificate->id,
|
2018-05-23 06:35:23 +00:00
|
|
|
'userid' => $USER->id]);
|
|
|
|
$fileurl = $fileurl->out(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$showreport = false;
|
2018-07-01 12:18:09 +00:00
|
|
|
$groups = [];
|
|
|
|
$recipients = [];
|
2021-11-24 07:29:43 +00:00
|
|
|
if (has_capability('mod/htmlcert:viewreport', $context)) {
|
2018-05-23 06:35:23 +00:00
|
|
|
$showreport = true;
|
2018-07-01 12:18:09 +00:00
|
|
|
|
|
|
|
// Get the groups (if any) to display - also sets active group.
|
|
|
|
$groups = self::get_groups($cm, $groupid, $USER->id);
|
2018-05-23 06:35:23 +00:00
|
|
|
$groupmode = groups_get_activity_groupmode($cm);
|
|
|
|
if (has_capability('moodle/site:accessallgroups', $context)) {
|
|
|
|
$groupmode = 'aag';
|
|
|
|
}
|
2018-07-01 12:18:09 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$recipients = \mod_htmlcert\certificate::get_issues($certificate->id, $groupmode, $cm, 0, 0);
|
2018-07-01 12:18:09 +00:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
$recipient->displayname = fullname($recipient);
|
2021-11-24 07:29:43 +00:00
|
|
|
$recipient->fileurl = new \moodle_url('/mod/htmlcert/mobile/pluginfile.php', ['certificateid' => $certificate->id,
|
2018-07-01 12:18:09 +00:00
|
|
|
'userid' => $recipient->id]);
|
|
|
|
}
|
2018-05-23 06:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'certificate' => $certificate,
|
|
|
|
'cmid' => $cm->id,
|
2018-07-09 15:45:19 +00:00
|
|
|
'issue' => $issue,
|
2018-07-01 12:18:09 +00:00
|
|
|
'showgroups' => !empty($groups),
|
|
|
|
'groups' => array_values($groups),
|
2018-06-19 11:10:08 +00:00
|
|
|
'canmanage' => $canmanage,
|
2018-07-10 06:23:27 +00:00
|
|
|
'requiredtimemet' => $requiredtimemet,
|
2018-05-23 06:35:23 +00:00
|
|
|
'fileurl' => $fileurl,
|
|
|
|
'showreport' => $showreport,
|
2018-07-01 12:18:09 +00:00
|
|
|
'hasrecipients' => !empty($recipients),
|
|
|
|
'recipients' => array_values($recipients),
|
2021-06-18 07:13:56 +00:00
|
|
|
'numrecipients' => count($recipients),
|
2018-05-23 06:35:23 +00:00
|
|
|
'currenttimestamp' => time()
|
|
|
|
];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'templates' => [
|
|
|
|
[
|
|
|
|
'id' => 'main',
|
2021-11-24 07:29:43 +00:00
|
|
|
'html' => $OUTPUT->render_from_template("mod_htmlcert/mobile_view_activity_page_$versionname", $data),
|
2018-05-23 06:35:23 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'javascript' => '',
|
2021-06-18 07:13:56 +00:00
|
|
|
'otherdata' => [
|
|
|
|
'group' => $groupid,
|
|
|
|
]
|
2018-05-23 06:35:23 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of groups to be displayed (if applicable) for the activity.
|
|
|
|
*
|
|
|
|
* The groups API is a mess hence the hackiness.
|
|
|
|
*
|
|
|
|
* @param \stdClass $cm The course module
|
|
|
|
* @param int $groupid The group id
|
|
|
|
* @param int $userid The user id
|
|
|
|
* @return array The array of groups, may be empty.
|
|
|
|
*/
|
|
|
|
protected static function get_groups($cm, $groupid, $userid) {
|
|
|
|
$arrgroups = [];
|
|
|
|
if ($groupmode = groups_get_activity_groupmode($cm)) {
|
|
|
|
if ($groups = groups_get_activity_allowed_groups($cm, $userid)) {
|
|
|
|
$context = \context_module::instance($cm->id);
|
|
|
|
if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
|
|
|
|
$allparticipants = new \stdClass();
|
|
|
|
$allparticipants->id = 0;
|
|
|
|
$allparticipants->name = get_string('allparticipants');
|
|
|
|
$allparticipants->selected = $groupid === 0;
|
|
|
|
$arrgroups[0] = $allparticipants;
|
|
|
|
}
|
|
|
|
self::update_active_group($groupmode, $groupid, $groups, $cm);
|
|
|
|
// Detect which group is selected.
|
|
|
|
foreach ($groups as $gid => $group) {
|
|
|
|
$group->selected = $gid == $groupid;
|
|
|
|
$arrgroups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arrgroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the active group in the session.
|
|
|
|
*
|
|
|
|
* This is a hack. We can't call groups_get_activity_group to update the active group as it relies
|
|
|
|
* on optional_param('group' .. which we won't have when using the mobile app.
|
|
|
|
*
|
|
|
|
* @param int $groupmode The group mode we are in, eg. NOGROUPS, VISIBLEGROUPS
|
|
|
|
* @param int $groupid The id of the group that has been selected
|
|
|
|
* @param array $allowedgroups The allowed groups this user can access
|
|
|
|
* @param \stdClass $cm The course module
|
|
|
|
*/
|
|
|
|
private static function update_active_group($groupmode, $groupid, $allowedgroups, $cm) {
|
|
|
|
global $SESSION;
|
|
|
|
|
|
|
|
$context = \context_module::instance($cm->id);
|
|
|
|
|
|
|
|
if (has_capability('moodle/site:accessallgroups', $context)) {
|
|
|
|
$groupmode = 'aag';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($groupid == 0) {
|
|
|
|
// The groups are only all visible in VISIBLEGROUPS mode or if the user can access all groups.
|
|
|
|
if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
|
|
|
|
$SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($allowedgroups && array_key_exists($groupid, $allowedgroups)) {
|
|
|
|
$SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $groupid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirms the user is logged in and has the specified capability.
|
|
|
|
*
|
|
|
|
* @param \stdClass $cm
|
|
|
|
* @param \context $context
|
|
|
|
* @param string $cap
|
|
|
|
*/
|
|
|
|
protected static function require_capability(\stdClass $cm, \context $context, string $cap) {
|
|
|
|
require_login($cm->course, false, $cm, true, true);
|
|
|
|
require_capability($cap, $context);
|
|
|
|
}
|
|
|
|
}
|