Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions classes/form/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ public function definition() {
$mform->addHelpButton('shared', 'availableincourses', 'tool_certificate');
$mform->setDefault('shared', 1);

$mform->addElement('text', 'maxissuances', get_string('maxissuances', 'tool_certificate'), array('size' => '3'));
$mform->setType('maxissuances', PARAM_INT);
$mform->setDefault('maxissuances', 0);
$mform->addHelpButton('maxissuances', 'maxissuances', 'tool_certificate');

$mform->addElement('advcheckbox', 'notify', get_string('notify', 'tool_certificate'));
$mform->addHelpButton('notify', 'notify', 'tool_certificate');
$mform->setDefault('notify', 1);

if (!$this->get_template()->get_id()) {
page::add_page_elements($mform);
} else {
Expand Down Expand Up @@ -156,7 +165,7 @@ public function check_access_for_dynamic_submission(): void {
*/
public function process_dynamic_submission() {
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot . '/course/lib.php');
$data = $this->get_data();

if (isset($data->categoryid)) {
Expand Down Expand Up @@ -187,7 +196,10 @@ public function set_data_for_dynamic_submission(): void {
'id' => $this->template->get_id(),
'name' => $this->template->get_name(),
'shared' => $this->template->get_shared(),
'categoryid' => $this->template->get_category_id(), ]);
'categoryid' => $this->template->get_category_id(),
'maxissuances' => $this->template->get_maxissuances(),
'notify' => $this->template->get_notify(),
]);
} else {
$data = template::instance()->new_page()->to_record();
unset($data->id, $data->templateid);
Expand Down
6 changes: 6 additions & 0 deletions classes/persistent/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ protected static function define_properties() {
'shared' => [
'type' => PARAM_BOOL,
],
'maxissuances' => [
'type' => PARAM_INT,
],
'notify' => [
'type' => PARAM_BOOL
]
];
}

Expand Down
40 changes: 39 additions & 1 deletion classes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use core_user;
use moodle_url;
use tool_certificate\customfield\issue_handler;
use moodle_exception;

/**
* Class represents a certificate template.
Expand Down Expand Up @@ -84,6 +85,12 @@ public function save($data) {
if (isset($data->shared)) {
$this->persistent->set('shared', $data->shared);
}
if (isset($data->maxissuances)) {
$this->persistent->set('maxissuances', $data->maxissuances);
}
if (isset($data->notify)) {
$this->persistent->set('notify', $data->notify);
}
$this->persistent->save();
\tool_certificate\event\template_updated::create_from_template($this)->trigger();
}
Expand Down Expand Up @@ -443,6 +450,24 @@ public function get_shared() {
return $this->persistent->get('shared');
}

/**
* Returns the maxissuances setting of the template.
*
* @return string the shared setting of the template
*/
public function get_maxissuances() {
return $this->persistent->get('maxissuances');
}

/**
* Returns the notify setting of the template.
*
* @return string the shared setting of the template
*/
public function get_notify() {
return $this->persistent->get('notify');
}

/**
* Returns the formatted name of the template.
*
Expand Down Expand Up @@ -647,6 +672,8 @@ public static function create($formdata) {
$template = new \stdClass();
$template->name = $formdata->name;
$template->shared = $formdata->shared ?? 0;
$template->maxissuances = $formdata->maxissuances ?? 0;
$template->notify = $formdata->notify ?? 1;
if (!isset($formdata->contextid)) {
debugging('Context is missing', DEBUG_DEVELOPER);
$template->contextid = \context_system::instance()->id;
Expand Down Expand Up @@ -697,6 +724,15 @@ public function issue_certificate($userid, $expires = null, array $data = [], $c

component_class_callback(\tool_tenant\config::class, 'push_for_user', [$userid]);

$maxissuances = $DB->get_field('tool_certificate_templates', 'maxissuances', array('id' => $this->get_id()));
$notify = (bool) $DB->get_field('tool_certificate_templates', 'notify', array('id' => $this->get_id()));
$usercertificates = $DB->count_records('tool_certificate_issues', array('userid' => $userid, 'templateid' => $this->get_id()));

if ($maxissuances > 0 && $usercertificates >= $maxissuances) {
// Do not issue the certificate and possibly show an error message.
throw new moodle_exception('maxissuancesreached', 'tool_certificate');
}

$issue = new \stdClass();
$issue->userid = $userid;
$issue->templateid = $this->get_id();
Expand Down Expand Up @@ -727,7 +763,9 @@ public function issue_certificate($userid, $expires = null, array $data = [], $c

// Create the issue file and send notification.
$issuefile = $this->create_issue_file($issue);
self::send_issue_notification($issue, $issuefile);
if ($notify) {
self::send_issue_notification($issue, $issuefile);
}

component_class_callback(\tool_tenant\config::class, 'pop', []);

Expand Down
4 changes: 3 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/certificate/db" VERSION="20220518" COMMENT="XMLDB file for Moodle tool_certificate"
<XMLDB PATH="admin/tool/certificate/db" VERSION="20241004" COMMENT="XMLDB file for Moodle tool_certificate"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -12,6 +12,8 @@
<FIELD NAME="shared" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="maxissuances" TYPE="int" LENGTH="3" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Maximum issuances per user. If 0 there is no limit."/>
<FIELD NAME="notify" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Notify user of new issued certificate."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for certificate_template"/>
Expand Down
73 changes: 60 additions & 13 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
function xmldb_tool_certificate_upgrade($oldversion) {
global $DB, $CFG;
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/certificate/db/upgradelib.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/certificate/db/upgradelib.php');

$dbman = $DB->get_manager();

Expand All @@ -53,33 +53,41 @@ function xmldb_tool_certificate_upgrade($oldversion) {
foreach ($elements as $element) {
$data = @json_decode($element->data, true);
$data['isbackground'] = 1;
$DB->update_record('tool_certificate_elements',
['id' => $element->id, 'element' => 'image', 'data' => json_encode($data)]);
$DB->update_record(
'tool_certificate_elements',
['id' => $element->id, 'element' => 'image', 'data' => json_encode($data)]
);
}

upgrade_plugin_savepoint(true, 2019030707, 'tool', 'certificate');
}

if ($oldversion < 2019030708) {
// Change instances of studentname to userfield.
$DB->execute("UPDATE {tool_certificate_elements} SET element = ?, data = ? WHERE element = ?",
['userfield', 'fullname', 'studentname']);
$DB->execute(
"UPDATE {tool_certificate_elements} SET element = ?, data = ? WHERE element = ?",
['userfield', 'fullname', 'studentname']
);

upgrade_plugin_savepoint(true, 2019030708, 'tool', 'certificate');
}

if ($oldversion < 2019030710) {
// Change refpoint of all images.
$DB->execute("UPDATE {tool_certificate_elements} SET refpoint = null WHERE element IN (?, ?, ?)",
['image', 'userpicture', 'digitalsignature']);
$DB->execute(
"UPDATE {tool_certificate_elements} SET refpoint = null WHERE element IN (?, ?, ?)",
['image', 'userpicture', 'digitalsignature']
);

upgrade_plugin_savepoint(true, 2019030710, 'tool', 'certificate');
}

if ($oldversion < 2019030711) {
// Change refpoint of all images.
$DB->execute("DELETE FROM {config_plugins} WHERE name = ? AND plugin IN (?, ?)",
['version', 'certificateelement_bgimage', 'certificateelement_studentname']);
$DB->execute(
"DELETE FROM {config_plugins} WHERE name = ? AND plugin IN (?, ?)",
['version', 'certificateelement_bgimage', 'certificateelement_studentname']
);

upgrade_plugin_savepoint(true, 2019030711, 'tool', 'certificate');
}
Expand Down Expand Up @@ -205,12 +213,21 @@ function xmldb_tool_certificate_upgrade($oldversion) {
$duplicatecounter = 1;

// For each duplicate code, retrieve all subsequent duplicates after the initial one and append counter.
$records = $DB->get_records_select('tool_certificate_issues', 'id <> :id AND code = :code',
['id' => $duplicatecode->minid, 'code' => $duplicatecode->code], 'id', 'id');
$records = $DB->get_records_select(
'tool_certificate_issues',
'id <> :id AND code = :code',
['id' => $duplicatecode->minid, 'code' => $duplicatecode->code],
'id',
'id'
);

foreach ($records as $record) {
$DB->set_field('tool_certificate_issues', 'code', $duplicatecode->code . $duplicatecounter++,
['id' => $record->id]);
$DB->set_field(
'tool_certificate_issues',
'code',
$duplicatecode->code . $duplicatecounter++,
['id' => $record->id]
);
}
}

Expand Down Expand Up @@ -247,5 +264,35 @@ function xmldb_tool_certificate_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2023071300, 'tool', 'certificate');
}

if ($oldversion < 2024081902) {

// Define field maxissuances to be added to tool_certificate_templates.
$table = new xmldb_table('tool_certificate_templates');
$field = new xmldb_field('maxissuances', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'timemodified');

// Conditionally launch add field maxissuances.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Certificate savepoint reached.
upgrade_plugin_savepoint(true, 2024081902, 'tool', 'certificate');
}

if ($oldversion < 2024100401) {

// Define field notify to be added to tool_certificate_templates.
$table = new xmldb_table('tool_certificate_templates');
$field = new xmldb_field('notify', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'maxissuances');

// Conditionally launch add field notify.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Certificate savepoint reached.
upgrade_plugin_savepoint(true, 2024100401, 'tool', 'certificate');
}

return true;
}
5 changes: 5 additions & 0 deletions lang/en/tool_certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
$string['verifycertificates'] = 'Verify certificates';
$string['verifynotallowed'] = 'You are not allowed to verify certificates.';
$string['viewcertificate'] = 'View certificate';
$string['maxissuances'] = 'Maximum issuances per user';
$string['maxissuances_help'] = 'The maximum number of times this certificate can be issued to a single user. If 0 there is no limit.';
$string['maxissuancesreached'] = 'You have reached the maximum number of times this certificate can be issued to you.';
$string['notify'] = 'Notify users';
$string['notify_help'] = 'Notify users that they received a new certificate.';

// Deprecated since 4.2.
$string['editcertificate'] = 'Edit certificate template \'{$a}\'';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$plugin->component = 'tool_certificate';
$plugin->release = '4.4.3';
$plugin->version = 2024090300;
$plugin->version = 2024100401;
$plugin->requires = 2022041900.00;
$plugin->maturity = MATURITY_STABLE;
$plugin->supported = [400, 404];