From c9710047e6b5a20187d952c5e22a00c9c9bc419e Mon Sep 17 00:00:00 2001 From: Nithy1anand Date: Fri, 9 Feb 2024 21:14:33 +0530 Subject: [PATCH] Dash widgets support implemented --- classes/events/observer.php | 2 +- classes/form/skills_form.php | 15 ++++++----- classes/helper.php | 35 +++++++++++++++++++++++--- classes/logs.php | 48 +++++++++++++++++++++++------------- classes/skills.php | 8 +++--- db/upgrade.php | 21 ++++++++++++++++ lib.php | 37 +++++++++++++++++++++++++++ version.php | 2 +- 8 files changed, 134 insertions(+), 34 deletions(-) diff --git a/classes/events/observer.php b/classes/events/observer.php index 2dbe2ef..e9cfd17 100644 --- a/classes/events/observer.php +++ b/classes/events/observer.php @@ -59,7 +59,7 @@ public static function course_completed(\core\event\course_completed $event) { public static function user_deleted(\core\event\user_deleted $event) { // Fetch the event data. $data = $event->get_data(); - $relateduserid = $data['userid']; // Completed user id. + $relateduserid = $data['objectid']; // Completed user id. // Remove the user skill points. user::get($relateduserid)->remove_user_skillpoints(); } diff --git a/classes/form/skills_form.php b/classes/form/skills_form.php index 8dd97d6..288fd8a 100644 --- a/classes/form/skills_form.php +++ b/classes/form/skills_form.php @@ -233,7 +233,7 @@ public function data_preprocessing(&$defaultvalues) { $defaultvalues = (object) $defaultvalues; $filemanagers = [ - 'image' => 'image', + 'image' => 'levelimage', ]; // Levels count. @@ -242,13 +242,11 @@ public function data_preprocessing(&$defaultvalues) { // Prepare the file manager fields to store images. foreach ($filemanagers as $configname => $filearea) { // For all levels in this skills. - for ($i = 1; $i <= $levelscount; $i++) { + for ($i = 0; $i <= $levelscount; $i++) { if (empty($defaultvalues->levels[$i])) { continue; } - // Fileare for this level. - $filearea .= '_' . $i; // Draft item id. $draftitemid = file_get_submitted_draft_itemid($filearea); // Use the level id as item id. @@ -263,6 +261,7 @@ public function data_preprocessing(&$defaultvalues) { $defaultvalues->levels[$i][$configname] = $draftitemid; } } + } /** @@ -282,7 +281,7 @@ public function data_postprocessing(&$data) { $data = (object) $data; $filemanagers = [ - 'image' => 'image', + 'image' => 'levelimage', ]; $levelscount = $data->levelscount; @@ -290,16 +289,16 @@ public function data_postprocessing(&$data) { // Prepare the file manager fields to store images. foreach ($filemanagers as $configname => $filearea) { - for ($i = 1; $i <= $levelscount; $i++) { + for ($i = 0; $i <= $levelscount; $i++) { if (empty($data->levels[$i])) { continue; } + // Level id used as item id. $levelid = $data->levels[$i]['id'] ?: 0; - // Now save the files in correct part of the File API. - $filearea .= '_' . $i; + // Now save the files in correct part of the File API. file_save_draft_area_files( $data->levels[$i][$configname], $context->id, 'tool_skills', $filearea, $levelid, $this->get_editor_options($context) diff --git a/classes/helper.php b/classes/helper.php index 19d346e..dbb2d9e 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -97,17 +97,46 @@ public static function get_user_completedskills(int $userid) { // List of skills available. $skills = \tool_skills\user::get($userid)->get_user_skills(); + $completed = []; foreach ($skills as $skill) { $skillpoint = $skill->skillobj->get_points_to_earnskill(); - $points = $skill->userpoints->points; + if ($skillpoint <= 0) { + continue; + } + + $points = $skill->userpoints->points ?? 0; $percentage = ($points / $skillpoint) * 100; if ($percentage >= 100) { - $completed[] = $skill->id; + $completed[] = $skill->skill; } } - return array_unique($completed); + return !empty($completed) ? array_unique($completed) : []; + } + + /** + * Calculate the skills total points assigned for the given courses. + * + * @param array $courseids + * @return int + */ + public static function get_courses_skill_points(array $courseids) { + global $DB; + + list($insql, $inparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'skp'); + + $sql = "SELECT tsl.skill, MAX(tsl.points) AS skillpoints + FROM {tool_skills_levels} tsl + JOIN {tool_skills_courses} tsc ON tsc.skill = tsl.skill + WHERE tsc.status = 1 AND tsc.courseid $insql + GROUP BY tsl.skill"; + + $skills = $DB->get_records_sql($sql, $inparams); + + $skillpoints = array_sum(array_column($skills, 'skillpoints')); + + return $skillpoints; } /** diff --git a/classes/logs.php b/classes/logs.php index 10464fd..83aaf0f 100644 --- a/classes/logs.php +++ b/classes/logs.php @@ -22,6 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace tool_skills; +use moodle_exception; /** * Maintain the user log of points allocations. @@ -49,6 +50,24 @@ public static function get() { return self::$instance; } + /** + * Get function. + * + * @return void + */ + public function get_log(int $skillid, int $userid, int $methodid, string $method, int $status=1) { + global $DB; + + if ($log = $DB->get_record('tool_skills_awardlogs', ['skill' => $skillid, 'userid' => $userid, + 'method' => $method, 'methodid' => $methodid, ])) { + return $log; + } else { + throw new moodle_exception('skillawardnotfound', 'tool_skills'); + } + + return false; + } + /** * Add the allocated user points and method of allocation to the logs. * @@ -63,23 +82,18 @@ public static function get() { public function add(int $skillid, int $userid, int $points, int $methodid, string $method, int $status=1) { global $DB; - if ($record = $DB->get_record('tool_skills_awardlogs', ['userid' => $userid, - 'method' => $method, - 'methodid' => $methodid, - ])) { - $record->points = $points; - $DB->update_record('tool_skills_awardlogs', $record); - } else { - $record = [ - 'skill' => $skillid, - 'userid' => $userid, - 'points' => $points, - 'methodid' => $methodid, - 'method' => $method, - 'status' => $status, - 'timecreated' => time(), - ]; - return $DB->insert_record('tool_skills_awardlogs', $record); + if (!$DB->record_exists('tool_skills_awardlogs', ['skill' => $skillid, 'userid' => $userid, + 'method' => $method, 'methodid' => $methodid, ])) { + $record = [ + 'skill' => $skillid, + 'userid' => $userid, + 'points' => $points, + 'methodid' => $methodid, + 'method' => $method, + 'status' => $status, + 'timecreated' => time(), + ]; + return $DB->insert_record('tool_skills_awardlogs', $record); } } diff --git a/classes/skills.php b/classes/skills.php index 077ba23..80ced6a 100644 --- a/classes/skills.php +++ b/classes/skills.php @@ -304,12 +304,12 @@ public function get_points_to_earnskill() { $levels = $this->get_levels(); // List of levels, available in the skill. - $pointstocomplete = 0; - foreach ($levels as $levelid => $level) { - $pointstocomplete += $level->points; // Increase the level points to find the max skill point. + if (!empty($levels)) { + $levelpoints = array_column($levels, 'points'); + $pointstocomplete = max($levelpoints); } - return $pointstocomplete; + return $pointstocomplete ?? 0; } /** diff --git a/db/upgrade.php b/db/upgrade.php index 106bb2e..66237a2 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -38,9 +38,30 @@ function xmldb_tool_skills_upgrade($oldversion) { // Conditionally launch add field timecreated. if ($dbman->table_exists($table) && !$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); + } upgrade_plugin_savepoint(true, 2023102505, 'tool', 'skills'); } + if ($oldversion < 2024019004) { + + $table = new xmldb_table('tool_skills_awardlogs'); + $field = new xmldb_field('skill', XMLDB_TYPE_INTEGER, 18, null, null, null, null, 'id'); + // Conditionally launch add field timecreated. + if ($dbman->table_exists($table) && $dbman->field_exists($table, $field)) { + + $logs = $DB->get_records('tool_skills_awardlogs', ['method' => 'course']); + $skills = $DB->get_records('tool_skills_courses', []); + + foreach ($logs as $log) { + if (isset($skills[$log->methodid])) { + $skill = $skills[$log->methodid]->skill; + $DB->update_record('tool_skills_awardlogs', ['id' => $log->id, 'skill' => $skill]); + } + } + } + upgrade_plugin_savepoint(true, 2024019004, 'tool', 'skills'); + } + return true; } diff --git a/lib.php b/lib.php index 794eaab..aad4f12 100644 --- a/lib.php +++ b/lib.php @@ -165,3 +165,40 @@ function tool_skills_get_fontawesome_icon_map() { 'tool_skills:f/active' => 'fa-undo', ]; } + + +/** + * File serving callback + * + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + * @param string $filearea file area + * @param array $args extra arguments + * @param bool $forcedownload whether or not force download + * @param array $options additional options affecting the file serving + * @return bool false if the file was not found, just send the file otherwise and do not return anything + */ +function tool_skills_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) { + + if ($context->contextlevel != CONTEXT_SYSTEM) { + return false; + } + + require_login(); + + if ($filearea == 'levelimage') { + + $relativepath = implode('/', $args); + + $fullpath = "/$context->id/tool_skills/$filearea/$relativepath"; + + $fs = get_file_storage(); + $file = $fs->get_file_by_hash(sha1($fullpath)); + if (!$file || $file->is_directory()) { + return false; + } + + send_stored_file($file, null, 0, $forcedownload, $options); + } +} diff --git a/version.php b/version.php index 77691a6..0e0e833 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2024019003; +$plugin->version = 2024019004; $plugin->requires = 2021051700; // Requires this Moodle version. $plugin->component = 'tool_skills'; // Full name of the plugin (used for diagnostics). $plugin->maturity = MATURITY_STABLE;