Skip to content

Commit

Permalink
Dash widgets support implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithyanandan-lms committed Feb 9, 2024
1 parent 3f701ae commit c971004
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 34 deletions.
2 changes: 1 addition & 1 deletion classes/events/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
15 changes: 7 additions & 8 deletions classes/form/skills_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function data_preprocessing(&$defaultvalues) {
$defaultvalues = (object) $defaultvalues;

$filemanagers = [
'image' => 'image',
'image' => 'levelimage',
];

// Levels count.
Expand All @@ -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.
Expand All @@ -263,6 +261,7 @@ public function data_preprocessing(&$defaultvalues) {
$defaultvalues->levels[$i][$configname] = $draftitemid;
}
}

}

/**
Expand All @@ -282,24 +281,24 @@ public function data_postprocessing(&$data) {
$data = (object) $data;

$filemanagers = [
'image' => 'image',
'image' => 'levelimage',
];

$levelscount = $data->levelscount;

// 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)
Expand Down
35 changes: 32 additions & 3 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
48 changes: 31 additions & 17 deletions classes/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions classes/skills.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
37 changes: 37 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c971004

Please sign in to comment.