Skip to content

Commit

Permalink
Refactor duplicate output code
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed Feb 12, 2024
1 parent 137da30 commit ba75842
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 366 deletions.
41 changes: 3 additions & 38 deletions classes/output/assess_by_month.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class assess_by_month {
use generate_assess_by_month_chart;

/**
* Generate the markup for the process summary chart,
* used in the smart media dashboard.
Expand All @@ -46,44 +48,7 @@ public function get_assess_by_month_chart(int $year): array {
// Get events for the supplied year.
$frequency = new frequency();
$yeardata = $frequency->get_events_due_by_month($year);
$seriesdata = [];
$charttitle = get_string('assessbymonth', 'local_assessfreq');
$result = [];

// There is always 12 months in a year,
// even if we don't have data for them all.
for ($i = 1; $i <= 12; $i++) {
if (!empty($yeardata[$i])) {
$seriesdata[] = $yeardata[$i]->count;
} else {
$seriesdata[] = 0;
}
}

// Create chart object.
$events = new \core\chart_series($charttitle, $seriesdata);
$labels = [
get_string('jan', 'local_assessfreq'),
get_string('feb', 'local_assessfreq'),
get_string('mar', 'local_assessfreq'),
get_string('apr', 'local_assessfreq'),
get_string('may', 'local_assessfreq'),
get_string('jun', 'local_assessfreq'),
get_string('jul', 'local_assessfreq'),
get_string('aug', 'local_assessfreq'),
get_string('sep', 'local_assessfreq'),
get_string('oct', 'local_assessfreq'),
get_string('nov', 'local_assessfreq'),
get_string('dec', 'local_assessfreq'),
];

$chart = new \core\chart_bar();
$chart->add_series($events);
$chart->set_labels($labels);

$result['hasdata'] = true;
$result['chart'] = $chart;

return $result;
return $this->generate_chart($yeardata, $charttitle);
}
}
40 changes: 3 additions & 37 deletions classes/output/assess_by_month_student.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class assess_by_month_student {
use generate_assess_by_month_chart;

/**
* Generate the markup for the process summary chart,
* used in the dashboard.
Expand All @@ -46,44 +48,8 @@ public function get_assess_by_month_student_chart(int $year): array {
// Get events for the supplied year.
$frequency = new frequency();
$yeardata = $frequency->get_events_due_monthly_by_user($year);
$seriesdata = [];
$charttitle = get_string('assessbymonthstudents', 'local_assessfreq');
$result = [];

// There is always 12 months in a year,
// even if we don't have data for them all.
for ($i = 1; $i <= 12; $i++) {
if (!empty($yeardata[$i])) {
$seriesdata[] = $yeardata[$i]->count;
} else {
$seriesdata[] = 0;
}
}

// Create chart object.
$events = new \core\chart_series($charttitle, $seriesdata);
$labels = [
get_string('jan', 'local_assessfreq'),
get_string('feb', 'local_assessfreq'),
get_string('mar', 'local_assessfreq'),
get_string('apr', 'local_assessfreq'),
get_string('may', 'local_assessfreq'),
get_string('jun', 'local_assessfreq'),
get_string('jul', 'local_assessfreq'),
get_string('aug', 'local_assessfreq'),
get_string('sep', 'local_assessfreq'),
get_string('oct', 'local_assessfreq'),
get_string('nov', 'local_assessfreq'),
get_string('dec', 'local_assessfreq'),
];

$chart = new \core\chart_bar();
$chart->add_series($events);
$chart->set_labels($labels);

$result['hasdata'] = true;
$result['chart'] = $chart;

return $result;
return $this->generate_chart($yeardata, $charttitle);
}
}
201 changes: 201 additions & 0 deletions classes/output/dashboard_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace local_assessfreq\output;

/**
* Common code for outputting dashboard tables
*
* @package local_assessfreq
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait dashboard_table {
/**
* Get content for title column.
*
* @param \stdClass $row
* @return string html used to display the video field.
* @throws \moodle_exception
*/
public function col_fullname($row): string {
global $OUTPUT;

return $OUTPUT->user_picture($row, ['size' => 35, 'includefullname' => true]);
}

/**
* Get content for time start column.
* Displays the user attempt start time.
*
* @param \stdClass $row
* @return string html used to display the field.
*/
public function col_timestart($row) {
if ($row->timestart == 0) {
$content = \html_writer::span(get_string('na', 'local_assessfreq'));
} else {
$datetime = userdate($row->timestart, get_string('trenddatetime', 'local_assessfreq'));
$content = \html_writer::span($datetime);
}

return $content;
}

/**
* Get content for time finish column.
* Displays the user attempt finish time.
*
* @param \stdClass $row
* @return string html used to display the field.
*/
public function col_timefinish($row) {
if ($row->timefinish == 0 && $row->timestart == 0) {
$content = \html_writer::span(get_string('na', 'local_assessfreq'));
} else if ($row->timefinish == 0 && $row->timestart > 0) {
$time = $row->timestart + $row->timelimit;
$datetime = userdate($time, get_string('trenddatetime', 'local_assessfreq'));
$content = \html_writer::span($datetime, 'local-assessfreq-disabled');
} else {
$datetime = userdate($row->timefinish, get_string('trenddatetime', 'local_assessfreq'));
$content = \html_writer::span($datetime);
}

return $content;
}

/**
* Get content for state column.
* Displays the users state in the quiz.
*
* @param \stdClass $row
* @return string html used to display the field.
*/
public function col_state($row) {
if ($row->state == 'notloggedin') {
$color = 'background: ' . get_config('local_assessfreq', 'notloggedincolor');
} else if ($row->state == 'loggedin') {
$color = 'background: ' . get_config('local_assessfreq', 'loggedincolor');
} else if ($row->state == 'inprogress') {
$color = 'background: ' . get_config('local_assessfreq', 'inprogresscolor');
} else if ($row->state == 'uploadpending') {
$color = 'background: ' . get_config('local_assessfreq', 'inprogresscolor');
} else if ($row->state == 'finished') {
$color = 'background: ' . get_config('local_assessfreq', 'finishedcolor');
} else if ($row->state == 'abandoned') {
$color = 'background: ' . get_config('local_assessfreq', 'finishedcolor');
} else if ($row->state == 'overdue') {
$color = 'background: ' . get_config('local_assessfreq', 'finishedcolor');
}

$content = \html_writer::span('', 'local-assessfreq-status-icon', ['style' => $color]);
$content .= get_string($row->state, 'local_assessfreq');

return $content;
}

/**
* Return an array of headers common across dashboard tables.
*
* @return array
*/
protected function get_common_headers(): array {
return [
get_string('quiztimeopen', 'local_assessfreq'),
get_string('quiztimeclose', 'local_assessfreq'),
get_string('quiztimelimit', 'local_assessfreq'),
get_string('quiztimestart', 'local_assessfreq'),
get_string('quiztimefinish', 'local_assessfreq'),
get_string('status', 'local_assessfreq'),
get_string('actions', 'local_assessfreq'),
];
}

/**
* Return an array of columns common across dashboard tables.
*
* @return array
*/
protected function get_common_columns(): array {
return [
'timeopen',
'timeclose',
'timelimit',
'timestart',
'timefinish',
'state',
'actions',
];
}

/**
* Return HTML for common column actions.
*
* @param \stdClass $row
* @return string
*/
protected function get_common_column_actions(\stdClass $row): string {
global $OUTPUT;
$actions = '';
if (
$row->state == 'finished'
|| $row->state == 'inprogress'
|| $row->state == 'uploadpending'
|| $row->state == 'abandoned'
|| $row->state == 'overdue'
) {
$classes = 'action-icon';
$attempturl = new \moodle_url('/mod/quiz/review.php', ['attempt' => $row->attemptid]);
$attributes = [
'class' => $classes,
'id' => 'tool-assessfreq-attempt-' . $row->id,
'data-toggle' => 'tooltip',
'data-placement' => 'top',
'title' => get_string('userattempt', 'local_assessfreq'),
];
} else {
$classes = 'action-icon disabled';
$attempturl = '#';
$attributes = [
'class' => $classes,
'id' => 'tool-assessfreq-attempt-' . $row->id,
];
}
$icon = $OUTPUT->render(new \pix_icon('i/search', ''));
$actions .= \html_writer::link($attempturl, $icon, $attributes);

$profileurl = new \moodle_url('/user/profile.php', ['id' => $row->id]);
$icon = $OUTPUT->render(new \pix_icon('i/completion_self', ''));
$actions .= \html_writer::link($profileurl, $icon, [
'class' => 'action-icon',
'id' => 'tool-assessfreq-profile-' . $row->id,
'data-toggle' => 'tooltip',
'data-placement' => 'top',
'title' => get_string('userprofile', 'local_assessfreq'),
]);

$logurl = new \moodle_url('/report/log/user.php', ['id' => $row->id, 'course' => 1, 'mode' => 'all']);
$icon = $OUTPUT->render(new \pix_icon('i/report', ''));
$actions .= \html_writer::link($logurl, $icon, [
'class' => 'action-icon',
'id' => 'tool-assessfreq-log-' . $row->id,
'data-toggle' => 'tooltip',
'data-placement' => 'top',
'title' => get_string('userlogs', 'local_assessfreq'),
]);
return $actions;
}
}
75 changes: 75 additions & 0 deletions classes/output/generate_assess_by_month_chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace local_assessfreq\output;

/**
* Common code for generating charts of assessments by month
*
* @package local_assessfreq
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait generate_assess_by_month_chart {
/**
* Generate chart markup from monthly events for a year.
*
* @param array $yeardata
* @param string $charttitle
* @return array
*/
protected function generate_chart($yeardata, $charttitle): array {
$result = [];
$seriesdata = [];

// There is always 12 months in a year,
// even if we don't have data for them all.
for ($i = 1; $i <= 12; $i++) {
if (!empty($yeardata[$i])) {
$seriesdata[] = $yeardata[$i]->count;
} else {
$seriesdata[] = 0;
}
}

// Create chart object.
$events = new \core\chart_series($charttitle, $seriesdata);
$labels = [
get_string('jan', 'local_assessfreq'),
get_string('feb', 'local_assessfreq'),
get_string('mar', 'local_assessfreq'),
get_string('apr', 'local_assessfreq'),
get_string('may', 'local_assessfreq'),
get_string('jun', 'local_assessfreq'),
get_string('jul', 'local_assessfreq'),
get_string('aug', 'local_assessfreq'),
get_string('sep', 'local_assessfreq'),
get_string('oct', 'local_assessfreq'),
get_string('nov', 'local_assessfreq'),
get_string('dec', 'local_assessfreq'),
];

$chart = new \core\chart_bar();
$chart->add_series($events);
$chart->set_labels($labels);

$result['hasdata'] = true;
$result['chart'] = $chart;

return $result;
}

}
Loading

0 comments on commit ba75842

Please sign in to comment.