Skip to content

Commit

Permalink
Fix monthly report data aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Sep 6, 2024
1 parent 70fd39a commit 619e371
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 84 deletions.
2 changes: 2 additions & 0 deletions filter/filter_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class datalynx_filter {
public $contentfields;
public $eids;
public $users;
public $groups;
public $page;
public $authorsearch;

protected $_filteredtables = null;

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'mod_datalynx';
$plugin->version = 2024090500;
$plugin->release = 'v4.4-DataTreasure'; // Data words like data science, data mining.
$plugin->version = 2024090600;
$plugin->release = 'v4.5-DataTreasure'; // Data words like data science, data mining.
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = array(
Expand Down
1 change: 1 addition & 0 deletions view/pdf/view_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public function display(array $options = []): string {

echo $displaycontent;
}
return '';
}

/**
Expand Down
183 changes: 101 additions & 82 deletions view/report/view_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,123 +159,142 @@ public function display_entries(?array $options = null): string {
$output = html_writer::start_tag('div', ['class' => 'table-responsive']);

if ($this->view->param2 === 'month') {
// Iterate over each month and create a table
// Collect and aggregate data by month
$monthlydatabymonth = [];

foreach ($report as $userid => $data) {
$user = $DB->get_record('user', ['id' => $userid], 'id, firstname, lastname, email');
$user_info = "{$user->firstname} {$user->lastname} ({$user->id})<br>{$user->email}";
$userinfo = "{$user->firstname} {$user->lastname} ({$user->id})<br>{$user->email}";

foreach ($data['monthly'] as $month => $monthly_data) {
// Initialize sum variables for this month's table
$total_entries_sum = 0;
$matching_contents_sums = array_fill_keys(array_values($desiredfieldoptions), 0);
$not_yet_edited_sum = 0;
foreach ($data['monthly'] as $month => $monthlydata) {
if (!isset($monthlydatabymonth[$month])) {
$monthlydatabymonth[$month] = [];
}

// Create a new table for the month
$output .= html_writer::tag('h3', get_string('month') . ": " . $month);
$output .= html_writer::start_tag('table', ['class' => 'table table-striped table-bordered']);
$monthlydatabymonth[$month][$userid] = [
'userinfo' => $userinfo,
'totalentries' => $monthlydata['totalentries'],
'matchingcontents' => $monthlydata['matchingcontents'],
];
}
}

// Table header
$output .= html_writer::start_tag('thead');
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('th', get_string('user'));
$output .= html_writer::tag('th', get_string('month'));
$output .= html_writer::tag('th', get_string('aggregationsum', 'reportbuilder'));
foreach ($desiredfieldoptions as $label) {
$output .= html_writer::tag('th', $label);
}
$output .= html_writer::tag('th', get_string('notyetanswered', 'question'));
$output .= html_writer::end_tag('tr');
$output .= html_writer::end_tag('thead');
// Sort months in descending order
krsort($monthlydatabymonth);

foreach ($monthlydatabymonth as $month => $usersdata) {
// Initialize sum variables for this month's table
$totalentriessum = 0;
$matchingcontentssums = array_fill_keys($desiredfieldoptions, 0);
$notyeteditedsum = 0;

// Create a new table for the month
$output .= html_writer::tag('h3', get_string('month') . ": " . $month);
$output .= html_writer::start_tag('table', ['class' => 'table table-striped table-bordered']);

// Table header
$output .= html_writer::start_tag('thead');
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('th', get_string('user'));
$output .= html_writer::tag('th', get_string('aggregationsum', 'reportbuilder'));
foreach ($desiredfieldoptions as $label) {
$output .= html_writer::tag('th', $label);
}
$output .= html_writer::tag('th', get_string('notyetanswered', 'question'));
$output .= html_writer::end_tag('tr');
$output .= html_writer::end_tag('thead');

$output .= html_writer::start_tag('tbody');
$output .= html_writer::start_tag('tbody');

// Data rows
foreach ($usersdata as $userid => $user_month_data) {
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('td', $user_info);
$output .= html_writer::tag('td', $month);
$output .= html_writer::tag('td', $user_month_data['userinfo']);

// Total entries for this user in this month
$output .= html_writer::tag('td', $monthly_data['total_entries']);
$total_entries_sum += $monthly_data['total_entries'];
$usertotalentries = $user_month_data['totalentries'];
$output .= html_writer::tag('td', $usertotalentries);
$totalentriessum += $usertotalentries;

// Matching contents counts and "Not Yet Edited"
$sum_matching_contents = 0;
// Matching contents counts and "Not Yet Edited" for each user
$summatchingcontents = 0;
foreach ($desiredfieldoptions as $label) {
$count = isset($monthly_data['matching_contents'][$label]) ? $monthly_data['matching_contents'][$label] : 0;
$sum_matching_contents += $count;
$matching_contents_sums[$label] += $count;
$count = $user_month_data['matchingcontents'][$label] ?? 0;
if ($count !== 0) {
$summatchingcontents += $count;
$matchingcontentssums[$label] += $count;
}
$output .= html_writer::tag('td', $count);
}

$not_yet_edited = $monthly_data['total_entries'] - $sum_matching_contents;
$output .= html_writer::tag('td', $not_yet_edited);
$not_yet_edited_sum += $not_yet_edited;
$notyetedited = $usertotalentries - $summatchingcontents;
$output .= html_writer::tag('td', $notyetedited);
$notyeteditedsum += $notyetedited;

$output .= html_writer::end_tag('tr');
}

// Totals row for this month
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('td', get_string('total'), ['colspan' => 2]);
$output .= html_writer::tag('td', $total_entries_sum);
// Totals row for this month
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('td', get_string('total'));
$output .= html_writer::tag('td', $totalentriessum);

foreach ($matching_contents_sums as $sum) {
$output .= html_writer::tag('td', $sum);
}
foreach ($matchingcontentssums as $sum) {
$output .= html_writer::tag('td', $sum);
}

$output .= html_writer::tag('td', $not_yet_edited_sum);
$output .= html_writer::end_tag('tr');
$output .= html_writer::tag('td', $notyeteditedsum);
$output .= html_writer::end_tag('tr');

$output .= html_writer::end_tag('tbody');
$output .= html_writer::end_tag('table');
}
$output .= html_writer::end_tag('tbody');
$output .= html_writer::end_tag('table');
}
} else {
$output .= html_writer::start_tag('table', ['class' => 'table table-striped table-bordered']);

$output .= html_writer::start_tag('thead');
$output .= html_writer::start_tag('tr');

// Header row
$output .= html_writer::tag('th', get_string('user')); // Assuming you have a lang string for this
// Header row.
$output .= html_writer::tag('th', get_string('user')); // Assuming you have a lang string for this.
$output .= html_writer::tag('th', get_string('month'));
$output .= html_writer::tag('th', get_string('total'));

foreach ($desiredfieldoptions as $label) {
$output .= html_writer::tag('th', $label);
}
// Additional column for "Not Yet Edited"
// Additional column for "Not Yet Edited".
$output .= html_writer::tag('th', get_string('notyetanswered', 'question'));

$output .= html_writer::end_tag('tr');
$output .= html_writer::end_tag('thead');

$output .= html_writer::start_tag('tbody');

// Data rows
// Data rows.
foreach ($report as $userid => $data) {
// Get user info
// Get user info.
$user = $DB->get_record('user', ['id' => $userid], 'id, firstname, lastname, email, department');

// Prepare user column content
$user_info = "{$user->firstname} {$user->lastname} ({$user->id})<br>{$user->email} ({$user->department})";
// Prepare user column content.
$userinfo = "{$user->firstname} {$user->lastname} ({$user->id})<br>{$user->email} ({$user->department})";

foreach ($data['monthly'] as $month => $monthly_data) {
foreach ($data['monthly'] as $month => $monthlydata) {
$output .= html_writer::start_tag('tr');
$output .= html_writer::tag('td', $user_info);
$output .= html_writer::tag('td', $userinfo);
$output .= html_writer::tag('td', $month);
$output .= html_writer::tag('td', $monthly_data['total_entries']);
$output .= html_writer::tag('td', $monthlydata['totalentries']);

// Calculate the sum of matching contents
$sum_matching_contents = 0;
// Calculate the sum of matching contents.
$summatchingcontents = 0;
foreach ($desiredfieldoptions as $label) {
$count = isset($monthly_data['matching_contents'][$label]) ? $monthly_data['matching_contents'][$label] : 0;
$sum_matching_contents += $count;
$count = isset($monthlydata['matchingcontents'][$label]) ? $monthlydata['matchingcontents'][$label] : 0;
$summatchingcontents += $count;
$output .= html_writer::tag('td', $count);
}

// Calculate and display "Not Yet Edited"
$not_yet_edited = $monthly_data['total_entries'] - $sum_matching_contents;
$output .= html_writer::tag('td', $not_yet_edited);
// Calculate and display "Not Yet Edited".
$notyetedited = $monthlydata['totalentries'] - $summatchingcontents;
$output .= html_writer::tag('td', $notyetedited);

$output .= html_writer::end_tag('tr');
}
Expand Down Expand Up @@ -339,11 +358,11 @@ public function create_report_sql(): array {
$report = [];

foreach ($userarray as $userid => $entryids) {
$total_entries = count($entryids);
$totalentries = count($entryids);

// Initialize the report array for this user
// Initialize the report array for this user.
$report[$userid] = [
'total_entries' => 0,
'totalentries' => 0,
'monthly' => []
];

Expand All @@ -356,15 +375,15 @@ public function create_report_sql(): array {
// Initialize the month data if not set.
if (!isset($report[$userid]['monthly'][$month])) {
$report[$userid]['monthly'][$month] = [
'total_entries' => 0,
'matching_contents' => []
'totalentries' => 0,
'matchingcontents' => []
];
}

// Increment total entries for this month.
$report[$userid]['monthly'][$month]['total_entries'] += 1;
$report[$userid]['monthly'][$month]['totalentries'] += 1;

// Check for each desired field option in datalynx_contents using SQL
// Check for each desired field option in datalynx_contents using SQL.
foreach ($desiredfieldoptions as $value => $label) {
$sql = "SELECT COUNT(1)
FROM {datalynx_contents}
Expand All @@ -377,25 +396,25 @@ public function create_report_sql(): array {
'value' => (string)$value // Cast the value to string to ensure proper comparison.
];

$matching_contents_count = $DB->count_records_sql($sql, $params);
$matchingcontents_count = $DB->count_records_sql($sql, $params);

// Initialize the matching content count for this value if not set
if (!isset($report[$userid]['monthly'][$month]['matching_contents'][$label])) {
$report[$userid]['monthly'][$month]['matching_contents'][$label] = 0;
// Initialize the matching content count for this value if not set.
if (!isset($report[$userid]['monthly'][$month]['matchingcontents'][$label])) {
$report[$userid]['monthly'][$month]['matchingcontents'][$label] = 0;
}

// Increment matching content count for this value
$report[$userid]['monthly'][$month]['matching_contents'][$label] += $matching_contents_count;
// Increment matching content count for this value.
$report[$userid]['monthly'][$month]['matchingcontents'][$label] += $matchingcontents_count;
}
}
}

// Set total entries for the user.
$report[$userid]['total_entries'] = $total_entries;
// Set total entries for the user..
$report[$userid]['totalentries'] = $totalentries;
}
// Sort the report array by month in descending order
// Sort the report array by month in descending order.
foreach ($report as &$data) {
krsort($data['monthly']); // Sorts the 'monthly' array in descending order by month
krsort($data['monthly']); // Sorts the 'monthly' array in descending order by month.
}
return $report;
}
Expand Down

0 comments on commit 619e371

Please sign in to comment.