2012-12-07 09:34:46 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of the customcert 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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-03-11 07:24:51 +00:00
|
|
|
* Customcert module core interaction API
|
2012-12-07 09:34:46 +00:00
|
|
|
*
|
2013-04-26 07:30:33 +00:00
|
|
|
* @package mod_customcert
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2012-12-07 09:34:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2013-04-10 09:17:19 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
/**
|
|
|
|
* Add customcert instance.
|
|
|
|
*
|
2013-05-28 08:33:46 +00:00
|
|
|
* @param stdClass $data
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param mod_customcert_mod_form $mform
|
2012-12-07 09:34:46 +00:00
|
|
|
* @return int new customcert instance id
|
|
|
|
*/
|
|
|
|
function customcert_add_instance($data, $mform) {
|
|
|
|
global $DB;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Create a template for this customcert to use.
|
|
|
|
$context = context_module::instance($data->coursemodule);
|
|
|
|
$template = \mod_customcert\template::create($data->name, $context->id);
|
|
|
|
|
|
|
|
// Add the data to the DB.
|
|
|
|
$data->templateid = $template->get_id();
|
|
|
|
$data->protection = \mod_customcert\certificate::set_protection($data);
|
2013-02-20 12:21:57 +00:00
|
|
|
$data->timecreated = time();
|
2012-12-07 09:34:46 +00:00
|
|
|
$data->timemodified = $data->timecreated;
|
2013-05-16 09:12:51 +00:00
|
|
|
$data->id = $DB->insert_record('customcert', $data);
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2013-05-22 11:37:39 +00:00
|
|
|
// Add a page to this customcert.
|
2016-02-16 09:03:34 +00:00
|
|
|
$template->add_page();
|
2013-05-16 09:12:51 +00:00
|
|
|
|
|
|
|
return $data->id;
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update customcert instance.
|
|
|
|
*
|
|
|
|
* @param stdClass $data
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param mod_customcert_mod_form $mform
|
2012-12-07 09:34:46 +00:00
|
|
|
* @return bool true
|
|
|
|
*/
|
|
|
|
function customcert_update_instance($data, $mform) {
|
|
|
|
global $DB;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
$data->protection = \mod_customcert\certificate::set_protection($data);
|
2012-12-07 09:34:46 +00:00
|
|
|
$data->timemodified = time();
|
|
|
|
$data->id = $data->instance;
|
|
|
|
|
|
|
|
return $DB->update_record('customcert', $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
|
|
|
|
*/
|
|
|
|
function customcert_delete_instance($id) {
|
2015-03-14 04:09:56 +00:00
|
|
|
global $CFG, $DB;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
// Ensure the customcert exists.
|
2016-02-16 09:03:34 +00:00
|
|
|
if (!$customcert = $DB->get_record('customcert', array('id' => $id))) {
|
2012-12-07 09:34:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
// Get the course module as it is used when deleting files.
|
2012-12-07 09:34:46 +00:00
|
|
|
if (!$cm = get_coursemodule_from_instance('customcert', $id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the customcert instance.
|
|
|
|
if (!$DB->delete_records('customcert', array('id' => $id))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Now, delete the template associated with this certificate.
|
|
|
|
if ($template = $DB->get_record('customcert_templates', array('id' => $customcert->templateid))) {
|
|
|
|
$template = new \mod_customcert\template($template);
|
|
|
|
$template->delete();
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the customcert issues.
|
|
|
|
if (!$DB->delete_records('customcert_issues', array('customcertid' => $id))) {
|
|
|
|
return false;
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any files associated with the customcert.
|
2013-05-28 09:00:07 +00:00
|
|
|
$context = context_module::instance($cm->id);
|
2012-12-07 09:34:46 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$fs->delete_area_files($context->id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used by the reset_course_userdata function in moodlelib.
|
|
|
|
* This function will remove all posts from the specified customcert
|
|
|
|
* and clean up any related data.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param stdClass $data the data submitted from the reset course.
|
2012-12-07 09:34:46 +00:00
|
|
|
* @return array status array
|
|
|
|
*/
|
|
|
|
function customcert_reset_userdata($data) {
|
2013-05-30 09:53:19 +00:00
|
|
|
global $DB;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$componentstr = get_string('modulenameplural', 'customcert');
|
|
|
|
$status = array();
|
|
|
|
|
|
|
|
if (!empty($data->reset_customcert)) {
|
|
|
|
$sql = "SELECT cert.id
|
2015-03-11 08:22:44 +00:00
|
|
|
FROM {customcert} cert
|
|
|
|
WHERE cert.course = :courseid";
|
2012-12-07 09:34:46 +00:00
|
|
|
$DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", array('courseid' => $data->courseid));
|
|
|
|
$status[] = array('component' => $componentstr, 'item' => get_string('customcertremoved', 'customcert'), 'error' => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating dates - shift may be negative too.
|
|
|
|
if ($data->timeshift) {
|
|
|
|
shift_course_mod_dates('customcert', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
|
|
|
|
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the function for printing the form elements that control
|
|
|
|
* whether the course reset functionality affects the customcert.
|
|
|
|
*
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param mod_customcert_mod_form $mform form passed by reference
|
2012-12-07 09:34:46 +00:00
|
|
|
*/
|
|
|
|
function customcert_reset_course_form_definition(&$mform) {
|
|
|
|
$mform->addElement('header', 'customcertheader', get_string('modulenameplural', 'customcert'));
|
|
|
|
$mform->addElement('advcheckbox', 'reset_customcert', get_string('deletissuedcustomcerts', 'customcert'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Course reset form defaults.
|
|
|
|
*
|
|
|
|
* @param stdClass $course
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function customcert_reset_course_form_defaults($course) {
|
|
|
|
return array('reset_customcert' => 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about received customcert.
|
|
|
|
* Used for user activity reports.
|
|
|
|
*
|
|
|
|
* @param stdClass $course
|
|
|
|
* @param stdClass $user
|
|
|
|
* @param stdClass $mod
|
|
|
|
* @param stdClass $customcert
|
|
|
|
* @return stdClass the user outline object
|
|
|
|
*/
|
|
|
|
function customcert_user_outline($course, $user, $mod, $customcert) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$result = new stdClass();
|
|
|
|
if ($issue = $DB->get_record('customcert_issues', array('customcertid' => $customcert->id, 'userid' => $user->id))) {
|
|
|
|
$result->info = get_string('issued', 'customcert');
|
|
|
|
$result->time = $issue->timecreated;
|
|
|
|
} else {
|
|
|
|
$result->info = get_string('notissued', 'customcert');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about received customcert.
|
|
|
|
* Used for user activity reports.
|
|
|
|
*
|
|
|
|
* @param stdClass $course
|
|
|
|
* @param stdClass $user
|
|
|
|
* @param stdClass $mod
|
2013-05-28 08:33:46 +00:00
|
|
|
* @param stdClass $customcert
|
2012-12-07 09:34:46 +00:00
|
|
|
* @return string the user complete information
|
|
|
|
*/
|
|
|
|
function customcert_user_complete($course, $user, $mod, $customcert) {
|
2013-07-25 04:32:39 +00:00
|
|
|
global $DB, $OUTPUT;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2013-07-25 04:32:39 +00:00
|
|
|
if ($issue = $DB->get_record('customcert_issues', array('customcertid' => $customcert->id, 'userid' => $user->id))) {
|
2012-12-07 09:34:46 +00:00
|
|
|
echo $OUTPUT->box_start();
|
|
|
|
echo get_string('issued', 'customcert') . ": ";
|
|
|
|
echo userdate($issue->timecreated);
|
|
|
|
customcert_print_user_files($customcert->id, $user->id);
|
|
|
|
echo '<br />';
|
|
|
|
echo $OUTPUT->box_end();
|
|
|
|
} else {
|
|
|
|
print_string('notissuedyet', 'customcert');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* Serves certificate issues and other files.
|
|
|
|
*
|
|
|
|
* @param stdClass $course
|
|
|
|
* @param stdClass $cm
|
|
|
|
* @param context $context
|
|
|
|
* @param string $filearea
|
|
|
|
* @param array $args
|
|
|
|
* @param bool $forcedownload
|
2016-02-16 09:03:34 +00:00
|
|
|
* @return bool|null false if file not found, does not return anything if found - just send the file
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
|
|
|
function customcert_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);
|
|
|
|
} else if ($context->contextlevel == CONTEXT_SYSTEM && !has_capability('mod/certificate:manage', $context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$relativepath = implode('/', $args);
|
|
|
|
$fullpath = '/' . $context->id . '/mod_customcert/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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
function customcert_supports($feature) {
|
|
|
|
switch ($feature) {
|
2013-07-25 04:32:39 +00:00
|
|
|
case FEATURE_GROUPS:
|
|
|
|
return true;
|
|
|
|
case FEATURE_GROUPINGS:
|
|
|
|
return true;
|
|
|
|
case FEATURE_MOD_INTRO:
|
|
|
|
return true;
|
|
|
|
case FEATURE_COMPLETION_TRACKS_VIEWS:
|
|
|
|
return true;
|
|
|
|
case FEATURE_BACKUP_MOODLE2:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return null;
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for course participation report (in case customcert is added).
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function customcert_get_view_actions() {
|
|
|
|
return array('view', 'view all', 'view report');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for course participation report (in case customcert is added).
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function customcert_get_post_actions() {
|
|
|
|
return array('received');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to be run periodically according to the moodle cron.
|
|
|
|
*/
|
2015-03-11 07:24:51 +00:00
|
|
|
function customcert_cron() {
|
2012-12-07 09:34:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:39:57 +00:00
|
|
|
/**
|
|
|
|
* Serve the edit element as a fragment.
|
|
|
|
*
|
|
|
|
* @param array $args List of named arguments for the fragment loader.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function mod_customcert_output_fragment_editelement($args) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
// Get the element.
|
|
|
|
$element = $DB->get_record('customcert_elements', array('id' => $args['elementid']), '*', MUST_EXIST);
|
|
|
|
|
|
|
|
$pageurl = new moodle_url('/mod/customcert/rearrange.php', array('pid' => $element->pageid));
|
|
|
|
$form = new \mod_customcert\edit_element_form($pageurl, array('element' => $element, 'rearrange' => true));
|
|
|
|
|
|
|
|
return $form->render();
|
|
|
|
}
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param navigation_node $customcertnode
|
|
|
|
*/
|
|
|
|
function customcert_extend_settings_navigation(settings_navigation $settings, navigation_node $customcertnode) {
|
2016-02-16 09:03:34 +00:00
|
|
|
global $DB, $PAGE;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$keys = $customcertnode->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];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_capability('mod/customcert:manage', $PAGE->cm->context)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
// Get the template id.
|
|
|
|
$templateid = $DB->get_field('customcert', 'templateid', array('id' => $PAGE->cm->instance));
|
2012-12-07 09:34:46 +00:00
|
|
|
$node = navigation_node::create(get_string('editcustomcert', 'customcert'),
|
2016-02-16 09:03:34 +00:00
|
|
|
new moodle_url('/mod/customcert/edit.php', array('tid' => $templateid)),
|
2012-12-07 09:34:46 +00:00
|
|
|
navigation_node::TYPE_SETTING, null, 'mod_customcert_edit',
|
|
|
|
new pix_icon('t/edit', ''));
|
|
|
|
$customcertnode->add_node($node, $beforekey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $customcertnode->trim_if_empty();
|
|
|
|
}
|