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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ that matches your Moodle version (eg. MOODLE_22 is for Moodle 2.2, MOODLE_23 is
and download the zip, uncompress this zip and extract the folder. The folder will have a name similar to moodle-mod_simplecertificate-c9fbadb, you MUST rename this to simplecertificate.
Place this folder in your mod folder in your Moodle directory.

> The reason this is not the recommended method is due to the fact you have to over-write the contents of this folder to apply any future updates to the simplecertificate module. In the above method there is a simple command to update the files.
> The reason this is not the recommended method is due to the fact you have to over-write the contents of this folder to apply any future updates to the simplecertificate module. In the above method there is a simple command to update the files.s
166 changes: 166 additions & 0 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace mod_simplecertificate;

use completion_info;

/**
* Event observers
*
Expand Down Expand Up @@ -48,4 +50,168 @@ public static function sendemails(\core\event\course_completed $event) {
}
}

/**
* Callback function triggered when an activity is completed.
*
* @param \core\event\course_module_completion_updated $event
*/
public static function activity_completed(\core\event\course_module_completion_updated $event) {
global $DB;

$userid = $event->relateduserid;
$courseid = $event->courseid;

$autoGenerateCertificate = $DB->get_record('simplecertificate', ['course' => $courseid], 'autogeneratecertificate');
if(!$autoGenerateCertificate || $autoGenerateCertificate->autogeneratecertificate === 0)
{
return;
}
// Check if all activities are completed.
if (self::are_all_activities_completed($courseid, $userid)) {
// Generate the certificate.
self::generate_certificate($courseid, $userid);

// Optionally mark the course as complete.
self::mark_course_complete($courseid, $userid);
}
}

/**
* Check if all activities in the course are completed.
*
* @param int $courseid The ID of the course.
* @param int $userid The ID of the user.
* @return bool True if all activities are completed, false otherwise.
*/
private static function are_all_activities_completed($courseid, $userid) {
global $DB;
$completion = new completion_info(get_course($courseid));

$activities = $completion->get_activities();

foreach ($activities as $activity) {
// Get the module name (e.g., 'quiz', 'forum', 'simplecertificate')
$modname = $DB->get_field('modules', 'name', ['id' => $activity->module]);

// Skip the Simple Certificate module
if ($modname === 'simplecertificate') {
continue;
}

// Check completion status of the activity
$completiondata = $completion->get_data($activity, true, $userid);

// Check if the activity is not complete (0 or 3)
// or if completionstate is not 1 or 2
if (!in_array($completiondata->completionstate, [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS])) {
return false;
}
}

return true;
}

/**
* Generate the certificate for the user.
*
* @param int $courseid The ID of the course.
* @param int $userid The ID of the user.
*/
private static function generate_certificate($courseid, $userid) {
global $DB, $CFG;
require_once ($CFG->dirroot . '/mod/simplecertificate/locallib.php');

$instanceid = $DB->get_field('simplecertificate', 'id', ['course' => $courseid]);
// Get the course module for the Simple Certificate.
$cm = get_coursemodule_from_instance('simplecertificate', $instanceid, $courseid);
if (!$cm) {
return;
}

$context = \context_module::instance($cm->id);

// Check if the certificate already exists for the user.
if (!$DB->record_exists('simplecertificate_issues', ['userid' => $userid, 'certificateid' => $cm->instance])) {

$course = $DB->get_record('course', ['id' => $cm->course]);
$user = $DB->get_record('user', ['id' => $userid]);
$simplecertificate = new \simplecertificate($context, $cm, $course);
$issuecert = $simplecertificate->get_issue($user);
$simplecertificate->get_issue_file($issuecert);
// Generate the certificate.
//simplecertificate_generate_certificate($userid, $context);

// Optionally notify the user or perform other actions.
}
}

/**
* Mark the course as complete for the user.
*
* @param int $courseid The ID of the course.
* @param int $userid The ID of the user.
*/
private static function mark_course_complete($courseid, $userid) {
global $DB;

$completion = new \completion_info(get_course($courseid));

// Ensure the course has completion enabled
if (!$completion->is_enabled()) {
return;
}

// Check if the course is already marked as complete
$coursecompletion = $DB->get_record('course_completions', [
'course' => $courseid,
'userid' => $userid,
]);

if ($coursecompletion && $coursecompletion->timecompleted) {
return; // Course already completed, no need to proceed.
}

// Check if all criteria are complete
$criteria = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY);
$allcompleted = true;

foreach ($criteria as $criterion) {
$completiondata = $criterion->get_completion_state($userid);
if ($completiondata != COMPLETION_COMPLETE && $completiondata != COMPLETION_COMPLETE_PASS) {
$allcompleted = false;
break;
}
}

// If all criteria are completed, mark the course as complete
if ($allcompleted) {
if (!$coursecompletion) {
// Create a new course completion record if it doesn't exist
$coursecompletion = (object)[
'course' => $courseid,
'userid' => $userid,
'timecompleted' => time(),
'status' => COMPLETION_COMPLETE,
];
$DB->insert_record('course_completions', $coursecompletion);
} else {
// Update the existing course completion record
$coursecompletion->timecompleted = time();
$coursecompletion->status = COMPLETION_COMPLETE;
$DB->update_record('course_completions', $coursecompletion);
}

// Trigger course completion event, ensure 'relateduserid' is passed in 'other'
$event = \core\event\course_completed::create([
'objectid' => $courseid,
'context' => \context_course::instance($courseid),
'relateduserid' => $userid, // This is crucial to set
'other' => [
'relateduserid' => $userid // Add 'relateduserid' to 'other'
]
]);
$event->trigger();
}
}

}
6 changes: 5 additions & 1 deletion db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
$observers = array(
array(
'eventname' => '\core\event\course_completed',
'callback' => '\mod_simplecertificate\observer::sendemails'
'callback' => '\mod_simplecertificate\observer::sendemails',
),
array(
'eventname' => '\core\event\course_module_completion_updated',
'callback' => '\mod_simplecertificate\observer::activity_completed',
)
);
3 changes: 2 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<FIELD NAME="secondpagetext" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="secondpagetextformat" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="secondimage" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="autogeneratecertificate" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0"/>
<FIELD NAME="timestartdatefmt" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
Expand Down Expand Up @@ -72,4 +73,4 @@
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
15 changes: 15 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,20 @@ function xmldb_simplecertificate_upgrade($oldversion = 0) {
// Simplecertificate savepoint reached.
upgrade_mod_savepoint(true, 2020091500, 'simplecertificate');
}

if ($oldversion < 2024051103) {
// Define field autogeneratecertificate to be added to simplecertificate.
$table = new xmldb_table('simplecertificate');
$field = new xmldb_field('autogeneratecertificate', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timemodified');

// Add field if it doesn't already exist.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Save upgrade step.
upgrade_mod_savepoint(true, 2024051103, 'simplecertificate');
}

return true;
}
3 changes: 3 additions & 0 deletions lang/en/simplecertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,6 @@
$string['signimage_help'] = '';
$string['signinfo'] = '';
$string['signinfo_help'] = 'A property by line. Use the structure: key=value';
$string['autogeneratecertificate'] = 'Autogenerate Certificate';
$string['autogeneratecertificate_desc'] = 'Automatically generate the certificate when the user completes all required activities.';
$string['autogeneratecertificate_help'] = 'If enabled, the certificate will be automatically generated when the user completes all required activities.';
10 changes: 10 additions & 0 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ public function definition() {
$mform->setDefault('delivery', 0);
$mform->addHelpButton('delivery', 'delivery', 'simplecertificate');

// Auto Generate Certificate Option
$mform->addElement(
'select',
'autogeneratecertificate', // Name of the field
get_string('autogeneratecertificate', 'simplecertificate'), // Label
array(0 => get_string('no'), 1 => get_string('yes')) // Options
);
$mform->setDefault('autogeneratecertificate', 0); // Default value: No
$mform->addHelpButton('autogeneratecertificate', 'autogeneratecertificate', 'simplecertificate'); // Add help button if required

// Report Cert.
// TODO acredito que seja para verificar o certificado pelo código, se for isto pode remover.
$reportfile = "$CFG->dirroot/simplecertificates/index.php";
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 @@


defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024051102; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2024051103; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2022112808; // Requires this Moodle version (moodle 3.9.x).
$plugin->cron = 4 * 3600; // Period for cron to check this module (secs).
$plugin->component = 'mod_simplecertificate';
Expand Down