Skip to content

Commit 4a6515f

Browse files
committed
Added an option to notify user when is getting a new certificate.
1 parent a487457 commit 4a6515f

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

classes/form/details.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public function definition() {
9999
$mform->setDefault('maxissuances', 0);
100100
$mform->addHelpButton('maxissuances', 'maxissuances', 'tool_certificate');
101101

102+
$mform->addElement('advcheckbox', 'notify', get_string('notify', 'tool_certificate'));
103+
$mform->addHelpButton('notify', 'notify', 'tool_certificate');
104+
$mform->setDefault('notify', 1);
105+
102106
if (!$this->get_template()->get_id()) {
103107
page::add_page_elements($mform);
104108
} else {
@@ -194,6 +198,7 @@ public function set_data_for_dynamic_submission(): void {
194198
'shared' => $this->template->get_shared(),
195199
'categoryid' => $this->template->get_category_id(),
196200
'maxissuances' => $this->template->get_maxissuances(),
201+
'notify' => $this->template->get_notify(),
197202
]);
198203
} else {
199204
$data = template::instance()->new_page()->to_record();

classes/persistent/template.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ protected static function define_properties() {
5757
],
5858
'maxissuances' => [
5959
'type' => PARAM_INT,
60+
],
61+
'notify' => [
62+
'type' => PARAM_BOOL
6063
]
6164
];
6265
}

classes/template.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function save($data) {
8888
if (isset($data->maxissuances)) {
8989
$this->persistent->set('maxissuances', $data->maxissuances);
9090
}
91+
if (isset($data->notify)) {
92+
$this->persistent->set('notify', $data->notify);
93+
}
9194
$this->persistent->save();
9295
\tool_certificate\event\template_updated::create_from_template($this)->trigger();
9396
}
@@ -456,6 +459,15 @@ public function get_maxissuances() {
456459
return $this->persistent->get('maxissuances');
457460
}
458461

462+
/**
463+
* Returns the notify setting of the template.
464+
*
465+
* @return string the shared setting of the template
466+
*/
467+
public function get_notify() {
468+
return $this->persistent->get('notify');
469+
}
470+
459471
/**
460472
* Returns the formatted name of the template.
461473
*
@@ -661,6 +673,7 @@ public static function create($formdata) {
661673
$template->name = $formdata->name;
662674
$template->shared = $formdata->shared ?? 0;
663675
$template->maxissuances = $formdata->maxissuances ?? 0;
676+
$template->notify = $formdata->notify ?? 1;
664677
if (!isset($formdata->contextid)) {
665678
debugging('Context is missing', DEBUG_DEVELOPER);
666679
$template->contextid = \context_system::instance()->id;
@@ -712,6 +725,7 @@ public function issue_certificate($userid, $expires = null, array $data = [], $c
712725
component_class_callback(\tool_tenant\config::class, 'push_for_user', [$userid]);
713726

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

717731
if ($maxissuances > 0 && $usercertificates >= $maxissuances) {
@@ -749,7 +763,9 @@ public function issue_certificate($userid, $expires = null, array $data = [], $c
749763

750764
// Create the issue file and send notification.
751765
$issuefile = $this->create_issue_file($issue);
752-
self::send_issue_notification($issue, $issuefile);
766+
if ($notify) {
767+
self::send_issue_notification($issue, $issuefile);
768+
}
753769

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

db/install.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<XMLDB PATH="admin/tool/certificate/db" VERSION="20240819" COMMENT="XMLDB file for Moodle tool_certificate"
2+
<XMLDB PATH="admin/tool/certificate/db" VERSION="20241004" COMMENT="XMLDB file for Moodle tool_certificate"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
55
>
@@ -13,6 +13,7 @@
1313
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
1414
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
1515
<FIELD NAME="maxissuances" TYPE="int" LENGTH="3" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Maximum issuances per user. If 0 there is no limit."/>
16+
<FIELD NAME="notify" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Notify user of new issued certificate."/>
1617
</FIELDS>
1718
<KEYS>
1819
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for certificate_template"/>

db/upgrade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,20 @@ function xmldb_tool_certificate_upgrade($oldversion) {
279279
upgrade_plugin_savepoint(true, 2024081902, 'tool', 'certificate');
280280
}
281281

282+
if ($oldversion < 2024100401) {
283+
284+
// Define field notify to be added to tool_certificate_templates.
285+
$table = new xmldb_table('tool_certificate_templates');
286+
$field = new xmldb_field('notify', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'maxissuances');
287+
288+
// Conditionally launch add field notify.
289+
if (!$dbman->field_exists($table, $field)) {
290+
$dbman->add_field($table, $field);
291+
}
292+
293+
// Certificate savepoint reached.
294+
upgrade_plugin_savepoint(true, 2024100401, 'tool', 'certificate');
295+
}
296+
282297
return true;
283298
}

lang/en/tool_certificate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@
202202
$string['maxissuances'] = 'Maximum issuances per user';
203203
$string['maxissuances_help'] = 'The maximum number of times this certificate can be issued to a single user. If 0 there is no limit.';
204204
$string['maxissuancesreached'] = 'You have reached the maximum number of times this certificate can be issued to you.';
205+
$string['notify'] = 'Notify users';
206+
$string['notify_help'] = 'Notify users that they received a new certificate.';
205207

206208
// Deprecated since 4.2.
207209
$string['editcertificate'] = 'Edit certificate template \'{$a}\'';

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$plugin->component = 'tool_certificate';
2828
$plugin->release = '4.4.3';
29-
$plugin->version = 2024090300;
29+
$plugin->version = 2024100401;
3030
$plugin->requires = 2022041900.00;
3131
$plugin->maturity = MATURITY_STABLE;
3232
$plugin->supported = [400, 404];

0 commit comments

Comments
 (0)