From 01e2ad37e3f66bb36ce1f2b6176540feb10382e1 Mon Sep 17 00:00:00 2001 From: Sarathlal Sarangadharan Date: Wed, 6 Aug 2025 16:13:07 +0100 Subject: [PATCH 1/2] TD-5858: Endpoint for certificate for MyLearning page redesign --- local/mylearningservice/db/services.php | 12 +++- local/mylearningservice/externallib.php | 92 ++++++++++++++++++++++++- local/mylearningservice/version.php | 2 +- 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/local/mylearningservice/db/services.php b/local/mylearningservice/db/services.php index b4e9735b43d..a8d460e67cf 100644 --- a/local/mylearningservice/db/services.php +++ b/local/mylearningservice/db/services.php @@ -9,12 +9,22 @@ 'ajax' => true, 'capabilities' => 'moodle/course:view', ), + 'mylearningservice_get_user_certificates' => array( + 'classname' => 'mylearningservice_external', + 'methodname' => 'get_user_certificates', + 'classpath' => 'local/mylearningservice/externallib.php', + 'description' => 'Get certificates awarded to a user', + 'type' => 'read', + 'ajax' => true, + 'capabilities' => 'moodle/course:view', + ), ); $services = array( 'Get Recent Courses' => array( 'functions' => array( - 'mylearningservice_get_recent_courses' + 'mylearningservice_get_recent_courses', + 'mylearningservice_get_user_certificates' ), 'restrictedusers' => 0, 'enabled' => 1, diff --git a/local/mylearningservice/externallib.php b/local/mylearningservice/externallib.php index 04af6b8e8e4..d919f01670a 100644 --- a/local/mylearningservice/externallib.php +++ b/local/mylearningservice/externallib.php @@ -167,7 +167,7 @@ function($favourite) { if (!$courseimage) { $courseimage = $OUTPUT->get_generated_url_for_course($context); } - $hascertificate = $DB->record_exists('customcert', ['course' => $course->id]); + $hascertificate =self:: course_has_certificate_enabled($course->id); $courseresult = [ 'id' => $course->id, 'shortname' => $course->shortname, @@ -210,6 +210,21 @@ function($favourite) { return $result; } + public static function course_has_certificate_enabled($courseid) { + global $DB; + + $sql = "SELECT cm.id + FROM {course_modules} cm + JOIN {modules} m ON m.id = cm.module + WHERE m.name = :modname + AND cm.course = :courseid + AND cm.visible = 1"; // only visible activities + + return $DB->record_exists_sql($sql, [ + 'modname' => 'coursecertificate', + 'courseid' => $courseid + ]); +} public static function get_recent_courses_returns() { return new external_multiple_structure( @@ -254,4 +269,79 @@ public static function get_recent_courses_returns() { ) ); } + + public static function get_user_certificates_parameters() { + return new external_function_parameters( + [ + 'userid' => new external_value(PARAM_INT, 'User ID') + ] + ); +} +public static function get_user_certificates($userid) { + global $DB, $CFG; + + require_once($CFG->libdir . '/filelib.php'); + + // Validate params + $params = self::validate_parameters(self::get_user_certificates_parameters(), array('userid' => $userid)); + + // SQL to fetch certificates linked to a course + $sql = "SELECT ci.id AS issueid, + ci.timecreated, + ct.name AS certificatename, + c.id AS courseid, + c.fullname AS coursename, + cm.id AS cmid + FROM {tool_certificate_issues} ci + JOIN {tool_certificate_templates} ct + ON ci.templateid = ct.id + JOIN {course} c + ON ci.courseid = c.id + JOIN {course_modules} cm + ON cm.course = c.id + JOIN {modules} m + ON m.id = cm.module AND m.name = 'coursecertificate' + WHERE ci.userid = :userid"; + + + $records = $DB->get_records_sql($sql, ['userid' => $userid]); + + $results = []; + foreach ($records as $rec) { + // Build download link + $downloadurl = new moodle_url('/mod/coursecertificate/view.php', [ + 'id' => $rec->cmid, + 'download' => 1 + ]); + + // Build preview link + $previewurl = new moodle_url('/mod/coursecertificate/view.php', [ + 'id' => $rec->cmid + ]); + + $results[] = [ + 'resourcetype' => 'Course', + 'resourcetitle' => $rec->certificatename, + 'coursename' => $rec->coursename, + 'awardeddate' => $rec->timecreated, + 'downloadlink' => $downloadurl->out(false), + 'previewlink' => $previewurl->out(false) + ]; + } + + return $results; +} +public static function get_user_certificates_returns() { + return new external_multiple_structure( + new external_single_structure( + [ + 'resourcetype' => new external_value(PARAM_TEXT, 'Type of resource'), + 'resourcetitle' => new external_value(PARAM_TEXT, 'Certificate title'), + 'awardeddate' => new external_value(PARAM_TEXT, 'Award date'), + 'downloadlink' => new external_value(PARAM_URL, 'Download link'), + 'previewlink' => new external_value(PARAM_URL, 'Preview link') + ] + ) + ); +} } diff --git a/local/mylearningservice/version.php b/local/mylearningservice/version.php index 8e2114f30e8..8c4184f5604 100644 --- a/local/mylearningservice/version.php +++ b/local/mylearningservice/version.php @@ -2,7 +2,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'local_mylearningservice'; -$plugin->version = 2025073100; +$plugin->version = 2025073102; $plugin->requires = 2024100290; $plugin->maturity = MATURITY_STABLE; $plugin->release = '1.0'; From d213657e26a67fe04fe9462d6e484c7bef640d17 Mon Sep 17 00:00:00 2001 From: Sarathlal Sarangadharan Date: Wed, 6 Aug 2025 16:25:28 +0100 Subject: [PATCH 2/2] TD-5858: Added missed resource name --- local/mylearningservice/externallib.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/local/mylearningservice/externallib.php b/local/mylearningservice/externallib.php index d919f01670a..ca5baf2b85c 100644 --- a/local/mylearningservice/externallib.php +++ b/local/mylearningservice/externallib.php @@ -322,7 +322,7 @@ public static function get_user_certificates($userid) { $results[] = [ 'resourcetype' => 'Course', 'resourcetitle' => $rec->certificatename, - 'coursename' => $rec->coursename, + 'resourcename' => $rec->coursename, 'awardeddate' => $rec->timecreated, 'downloadlink' => $downloadurl->out(false), 'previewlink' => $previewurl->out(false) @@ -337,6 +337,7 @@ public static function get_user_certificates_returns() { [ 'resourcetype' => new external_value(PARAM_TEXT, 'Type of resource'), 'resourcetitle' => new external_value(PARAM_TEXT, 'Certificate title'), + 'resourcename' => new external_value(PARAM_TEXT, 'Course name'), 'awardeddate' => new external_value(PARAM_TEXT, 'Award date'), 'downloadlink' => new external_value(PARAM_URL, 'Download link'), 'previewlink' => new external_value(PARAM_URL, 'Preview link')