moodle-mod_htmlcert/lib.php

366 lines
11 KiB
PHP
Raw Normal View History

<?php
2021-11-24 07:29:43 +00:00
// This file is part of the htmlcert module for 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/>.
/**
2021-11-24 07:29:43 +00:00
* htmlcert module core interaction API
*
2021-11-24 07:29:43 +00:00
* @package mod_htmlcert
* @copyright 2013 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
/**
2021-11-24 07:29:43 +00:00
* Add htmlcert instance.
*
2013-05-28 08:33:46 +00:00
* @param stdClass $data
2021-11-24 07:29:43 +00:00
* @param mod_htmlcert_mod_form $mform
* @return int new htmlcert instance id
*/
2021-11-24 07:29:43 +00:00
function htmlcert_add_instance($data, $mform) {
global $DB;
2021-11-24 07:29:43 +00:00
// Create a template for this htmlcert to use.
$context = context_module::instance($data->coursemodule);
2021-11-24 07:29:43 +00:00
$template = \mod_htmlcert\template::create($data->name, $context->id);
// Add the data to the DB.
$data->templateid = $template->get_id();
2021-11-24 07:29:43 +00:00
$data->protection = \mod_htmlcert\certificate::set_protection($data);
$data->timecreated = time();
$data->timemodified = $data->timecreated;
2021-11-24 07:29:43 +00:00
$data->id = $DB->insert_record('htmlcert', $data);
return $data->id;
}
/**
2021-11-24 07:29:43 +00:00
* Update htmlcert instance.
*
* @param stdClass $data
2021-11-24 07:29:43 +00:00
* @param mod_htmlcert_mod_form $mform
* @return bool true
*/
2021-11-24 07:29:43 +00:00
function htmlcert_update_instance($data, $mform) {
global $DB;
2021-11-24 07:29:43 +00:00
$data->protection = \mod_htmlcert\certificate::set_protection($data);
$data->timemodified = time();
$data->id = $data->instance;
2021-11-24 07:29:43 +00:00
return $DB->update_record('htmlcert', $data);
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id
* @return bool true if successful
*/
2021-11-24 07:29:43 +00:00
function htmlcert_delete_instance($id) {
2015-03-14 04:09:56 +00:00
global $CFG, $DB;
2021-11-24 07:29:43 +00:00
// Ensure the htmlcert exists.
if (!$htmlcert = $DB->get_record('htmlcert', array('id' => $id))) {
return false;
}
// Get the course module as it is used when deleting files.
2021-11-24 07:29:43 +00:00
if (!$cm = get_coursemodule_from_instance('htmlcert', $id)) {
return false;
}
2021-11-24 07:29:43 +00:00
// Delete the htmlcert instance.
if (!$DB->delete_records('htmlcert', array('id' => $id))) {
return false;
}
// Now, delete the template associated with this certificate.
2021-11-24 07:29:43 +00:00
if ($template = $DB->get_record('htmlcert_templates', array('id' => $htmlcert->templateid))) {
$template = new \mod_htmlcert\template($template);
$template->delete();
}
2021-11-24 07:29:43 +00:00
// Delete the htmlcert issues.
if (!$DB->delete_records('htmlcert_issues', array('htmlcertid' => $id))) {
return false;
}
2021-11-24 07:29:43 +00:00
// Delete any files associated with the htmlcert.
2013-05-28 09:00:07 +00:00
$context = context_module::instance($cm->id);
$fs = get_file_storage();
$fs->delete_area_files($context->id);
return true;
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
2021-11-24 07:29:43 +00:00
* This function will remove all posts from the specified htmlcert
* and clean up any related data.
*
* @param stdClass $data the data submitted from the reset course.
* @return array status array
*/
2021-11-24 07:29:43 +00:00
function htmlcert_reset_userdata($data) {
global $DB;
2021-11-24 07:29:43 +00:00
$componentstr = get_string('modulenameplural', 'htmlcert');
$status = array();
2021-11-24 07:29:43 +00:00
if (!empty($data->reset_htmlcert)) {
$sql = "SELECT cert.id
2021-11-24 07:29:43 +00:00
FROM {htmlcert} cert
WHERE cert.course = :courseid";
2021-11-24 07:29:43 +00:00
$DB->delete_records_select('htmlcert_issues', "htmlcertid IN ($sql)", array('courseid' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('deleteissuedcertificates', 'htmlcert'),
'error' => false);
}
return $status;
}
/**
* Implementation of the function for printing the form elements that control
2021-11-24 07:29:43 +00:00
* whether the course reset functionality affects the htmlcert.
*
2021-11-24 07:29:43 +00:00
* @param mod_htmlcert_mod_form $mform form passed by reference
*/
2021-11-24 07:29:43 +00:00
function htmlcert_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'htmlcertheader', get_string('modulenameplural', 'htmlcert'));
$mform->addElement('advcheckbox', 'reset_htmlcert', get_string('deleteissuedcertificates', 'htmlcert'));
}
/**
* Course reset form defaults.
*
* @param stdClass $course
* @return array
*/
2021-11-24 07:29:43 +00:00
function htmlcert_reset_course_form_defaults($course) {
return array('reset_htmlcert' => 1);
}
/**
2021-11-24 07:29:43 +00:00
* Returns information about received htmlcert.
* Used for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
2021-11-24 07:29:43 +00:00
* @param stdClass $htmlcert
* @return stdClass the user outline object
*/
2021-11-24 07:29:43 +00:00
function htmlcert_user_outline($course, $user, $mod, $htmlcert) {
global $DB;
$result = new stdClass();
2021-11-24 07:29:43 +00:00
if ($issue = $DB->get_record('htmlcert_issues', array('htmlcertid' => $htmlcert->id, 'userid' => $user->id))) {
$result->info = get_string('receiveddate', 'htmlcert');
$result->time = $issue->timecreated;
} else {
2021-11-24 07:29:43 +00:00
$result->info = get_string('notissued', 'htmlcert');
}
return $result;
}
/**
2021-11-24 07:29:43 +00:00
* Returns information about received htmlcert.
* Used for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
2021-11-24 07:29:43 +00:00
* @param stdClass $htmlcert
* @return string the user complete information
*/
2021-11-24 07:29:43 +00:00
function htmlcert_user_complete($course, $user, $mod, $htmlcert) {
global $DB, $OUTPUT;
2021-11-24 07:29:43 +00:00
if ($issue = $DB->get_record('htmlcert_issues', array('htmlcertid' => $htmlcert->id, 'userid' => $user->id))) {
echo $OUTPUT->box_start();
2021-11-24 07:29:43 +00:00
echo get_string('receiveddate', 'htmlcert') . ": ";
echo userdate($issue->timecreated);
echo $OUTPUT->box_end();
} else {
2021-11-24 07:29:43 +00:00
print_string('notissued', 'htmlcert');
}
}
/**
* Serves certificate issues and other files.
*
* @param stdClass $course
* @param stdClass $cm
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool|null false if file not found, does not return anything if found - just send the file
*/
2021-11-24 07:29:43 +00:00
function htmlcert_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
// We are positioning the elements.
if ($filearea === 'image') {
if ($context->contextlevel == CONTEXT_MODULE) {
require_login($course, false, $cm);
2021-11-24 07:29:43 +00:00
} else if ($context->contextlevel == CONTEXT_SYSTEM && !has_capability('mod/htmlcert:manage', $context)) {
return false;
}
$relativepath = implode('/', $args);
2021-11-24 07:29:43 +00:00
$fullpath = '/' . $context->id . '/mod_htmlcert/image/' . $relativepath;
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, $forcedownload);
}
}
/**
2017-02-16 12:12:19 +00:00
* The features this activity supports.
*
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
2021-11-24 07:29:43 +00:00
function htmlcert_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
default:
return null;
}
}
/**
2021-11-24 07:29:43 +00:00
* Used for course participation report (in case htmlcert is added).
*
* @return array
*/
2021-11-24 07:29:43 +00:00
function htmlcert_get_view_actions() {
return array('view', 'view all', 'view report');
}
/**
2021-11-24 07:29:43 +00:00
* Used for course participation report (in case htmlcert is added).
*
* @return array
*/
2021-11-24 07:29:43 +00:00
function htmlcert_get_post_actions() {
return array('received');
}
/**
* Function to be run periodically according to the moodle cron.
*/
2021-11-24 07:29:43 +00:00
function htmlcert_cron() {
return true;
}
/**
* This function extends the settings navigation block for the site.
*
* It is safe to rely on PAGE here as we will only ever be within the module
* context when this is called.
*
* @param settings_navigation $settings
2021-11-24 07:29:43 +00:00
* @param navigation_node $htmlcertnode
*/
2021-11-24 07:29:43 +00:00
function htmlcert_extend_settings_navigation(settings_navigation $settings, navigation_node $htmlcertnode) {
global $DB, $PAGE;
2021-11-24 07:29:43 +00:00
$keys = $htmlcertnode->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if ($i === false and array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
2021-11-24 07:29:43 +00:00
if (has_capability('mod/htmlcert:manage', $PAGE->cm->context)) {
// Get the template id.
2021-11-24 07:29:43 +00:00
$templateid = $DB->get_field('htmlcert', 'templateid', array('id' => $PAGE->cm->instance));
$node = navigation_node::create(get_string('edithtmlcert', 'htmlcert'),
new moodle_url('/mod/htmlcert/edit.php', array('tid' => $templateid)),
navigation_node::TYPE_SETTING, null, 'mod_htmlcert_edit',
new pix_icon('t/edit', ''));
2021-11-24 07:29:43 +00:00
$htmlcertnode->add_node($node, $beforekey);
}
2021-11-24 07:29:43 +00:00
if (has_capability('mod/htmlcert:verifycertificate', $PAGE->cm->context)) {
$node = navigation_node::create(get_string('verifycertificate', 'htmlcert'),
new moodle_url('/mod/htmlcert/verify_certificate.php', array('contextid' => $PAGE->cm->context->id)),
navigation_node::TYPE_SETTING, null, 'mod_htmlcert_verify_certificate',
new pix_icon('t/check', ''));
2021-11-24 07:29:43 +00:00
$htmlcertnode->add_node($node, $beforekey);
}
2021-11-24 07:29:43 +00:00
return $htmlcertnode->trim_if_empty();
}
/**
* Add nodes to myprofile page.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser
* @param stdClass $course Course object
* @return bool
*/
2021-11-24 07:29:43 +00:00
function mod_htmlcert_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
$url = new moodle_url('/mod/htmlcert/my_certificates.php', array('userid' => $user->id));
$node = new core_user\output\myprofile\node('miscellaneous', 'myhtmlcerts',
get_string('mycertificates', 'htmlcert'), null, $url);
$tree->add_node($node);
}
/**
* Get icon mapping for font-awesome.
*/
2021-11-24 07:29:43 +00:00
function mod_htmlcert_get_fontawesome_icon_map() {
return [
2021-11-24 07:29:43 +00:00
'mod_htmlcert:download' => 'fa-download'
];
}