Skip to content

Commit

Permalink
WR422914: Additional bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonThornett committed Sep 2, 2024
1 parent 890f308 commit 151e3d6
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 148 deletions.
93 changes: 68 additions & 25 deletions classes/frequency.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,14 @@ public function get_user_events_all(int $courseid, string $module = 'all', int $
* @param bool $cache Fetch events from cache.
* @return array $events The events.
*/
public function get_events_due_by_month(int $year, bool $cache = true): array {
public function get_events_due_by_month(int $year, int $month = 0, bool $cache = true): array {
global $DB, $PAGE;

// Adjust the cache key based on course.
if ($PAGE->course->id != SITEID) {
$cachekey = $PAGE->course->id . '_' . $year;
$cachekey = $PAGE->course->id . '_' . $year . '_' . $month;
} else {
$cachekey = (string) $year;
$cachekey = $year . '_' . $month;
}

// Try to get value from cache.
Expand All @@ -845,12 +845,10 @@ public function get_events_due_by_month(int $year, bool $cache = true): array {
} else { // Not valid cache data.
$modules = $this->get_process_modules();
[$insql, $params] = $DB->get_in_or_equal($modules);
$params[] = $year;
$sql = "SELECT s.endmonth, COUNT(s.id) as count
FROM {local_assessfreq_site} s
LEFT JOIN {course} c ON s.courseid = c.id
WHERE s.module $insql
AND s.endyear = ?";
WHERE s.module $insql ";

$includehiddencourses = get_config('local_assessfreq', 'hiddencourses');
if (!$includehiddencourses) {
Expand All @@ -861,10 +859,26 @@ public function get_events_due_by_month(int $year, bool $cache = true): array {
// Add the courseid restriction.
if ($PAGE->course->id != SITEID) {
$params[] = $PAGE->course->id;
$sql .= " AND c.id = ?";
$sql .= " AND c.id = ? ";
}

$sql .= 'GROUP BY s.endmonth
// Add month restrictions.
if ($month && $month > 1) {
$params[] = $month;
$params[] = $year;
$params[] = $month;
$params[] = $year + 1;
$sql .= " AND (s.endmonth >= ? AND s.endyear = ? OR s.endmonth < ? AND s.endyear = ?) ";
} else if ($month == 1) {
$params[] = $month;
$params[] = $year;
$sql .= " AND s.endmonth >= ? AND s.endyear = ? ";
} else {
$params[] = $year;
$sql .= " AND s.endyear = ? ";
}

$sql .= ' GROUP BY s.endmonth
ORDER BY s.endmonth ASC';

$events = $DB->get_records_sql($sql, $params);
Expand All @@ -889,14 +903,14 @@ public function get_events_due_by_month(int $year, bool $cache = true): array {
* @param bool $cache Fetch events from cache.
* @return array $events The events.
*/
public function get_events_due_monthly_by_user(int $year, bool $cache = true): array {
public function get_events_due_monthly_by_user(int $year, int $month = 0, bool $cache = true): array {
global $DB, $PAGE;

// Adjust the cache key based on course.
if ($PAGE->course->id != SITEID) {
$cachekey = $PAGE->course->id . '_' . $year;
$cachekey = $PAGE->course->id . '_' . $year . '_' . $month;
} else {
$cachekey = (string) $year;
$cachekey = $year . '_' . $month;
}

// Try to get value from cache.
Expand All @@ -908,13 +922,11 @@ public function get_events_due_monthly_by_user(int $year, bool $cache = true): a
} else { // Not valid cache data.
$modules = $this->get_process_modules();
[$insql, $params] = $DB->get_in_or_equal($modules);
$params[] = $year;
$sql = "SELECT s.endmonth, COUNT(u.id) as count
FROM {local_assessfreq_site} s
INNER JOIN {local_assessfreq_user} u ON s.id = u.eventid
INNER JOIN {course} c ON s.courseid = c.id
WHERE s.module $insql
AND s.endyear = ?";
WHERE s.module $insql ";

$includehiddencourses = get_config('local_assessfreq', 'hiddencourses');
if (!$includehiddencourses) {
Expand All @@ -925,10 +937,26 @@ public function get_events_due_monthly_by_user(int $year, bool $cache = true): a
// Add the courseid restriction.
if ($PAGE->course->id != SITEID) {
$params[] = $PAGE->course->id;
$sql .= " AND c.id = ?";
$sql .= " AND c.id = ? ";
}

// Add month restrictions.
if ($month && $month > 1) {
$params[] = $month;
$params[] = $year;
$params[] = $month;
$params[] = $year + 1;
$sql .= " AND (s.endmonth >= ? AND s.endyear = ? OR s.endmonth < ? AND s.endyear = ?) ";
} else if ($month == 1) {
$params[] = $month;
$params[] = $year;
$sql .= " AND s.endmonth >= ? AND s.endyear = ? ";
} else {
$params[] = $year;
$sql .= " AND s.endyear = ? ";
}

$sql .= 'GROUP BY s.endmonth
$sql .= ' GROUP BY s.endmonth
ORDER BY s.endmonth ASC';

$events = $DB->get_records_sql($sql, $params);
Expand All @@ -953,14 +981,14 @@ public function get_events_due_monthly_by_user(int $year, bool $cache = true): a
* @param bool $cache Fetch events from cache.
* @return array $events The events.
*/
public function get_events_due_by_activity(int $year, bool $cache = true): array {
public function get_events_due_by_activity(int $year, int $month = 0, bool $cache = true): array {
global $DB, $PAGE;

// Adjust the cache key based on course.
if ($PAGE->course->id != SITEID) {
$cachekey = $PAGE->course->id . '_' . $year;
$cachekey = $PAGE->course->id . '_' . $year . '_' . $month;
} else {
$cachekey = (string) $year;
$cachekey = $year . '_' . $month;
}

// Try to get value from cache.
Expand All @@ -970,11 +998,10 @@ public function get_events_due_by_activity(int $year, bool $cache = true): array
if ($data && (time() < $data->expiry) && $cache) { // Valid cache data.
$events = $data->events;
} else { // Not valid cache data.
$params = [$year];
$params = [];
$sql = 'SELECT s.module, COUNT(s.id) as count
FROM {local_assessfreq_site} s
LEFT JOIN {course} c ON s.courseid = c.id
WHERE s.endyear = ?';
LEFT JOIN {course} c ON s.courseid = c.id ';

$includehiddencourses = get_config('local_assessfreq', 'hiddencourses');
if (!$includehiddencourses) {
Expand All @@ -985,10 +1012,26 @@ public function get_events_due_by_activity(int $year, bool $cache = true): array
// Add the courseid restriction.
if ($PAGE->course->id != SITEID) {
$params[] = $PAGE->course->id;
$sql .= " AND c.id = ?";
$sql .= " AND c.id = ? ";
}

// Add month restrictions.
if ($month && $month > 1) {
$params[] = $month;
$params[] = $year;
$params[] = $month;
$params[] = $year + 1;
$sql .= " AND (s.endmonth >= ? AND s.endyear = ? OR s.endmonth < ? AND s.endyear = ?) ";
} else if ($month == 1) {
$params[] = $month;
$params[] = $year;
$sql .= " AND s.endmonth >= ? AND s.endyear = ? ";
} else {
$params[] = $year;
$sql .= " AND s.endyear = ? ";
}

$sql .= 'GROUP BY s.module
$sql .= ' GROUP BY s.module
ORDER BY s.module ASC';

$events = $DB->get_records_sql($sql, $params);
Expand Down Expand Up @@ -1121,7 +1164,7 @@ public function get_day_events(int $courseid, string $date, array $modules): arr
* @param array $modules List of modules to get events for.
* @return array $freqarray The array of even frequencies.
*/
public function get_frequency_array(int $year, int $month, string $metric, array $modules): array {
public function get_frequency_array(int $year = 0, int $month = 0, string $metric = 'assess', array $modules = []): array {
global $PAGE;

$freqarray = [];
Expand Down
8 changes: 4 additions & 4 deletions classes/output/all_participants_inprogress.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function get_all_participants_inprogress_chart(int $now, int $hoursahead
$quizzes = $quiz->get_quiz_summaries($now);

$inprogressquizzes = $quizzes['inprogress'];
$upcommingquizzes = $quizzes['upcomming'];
$upcomingquizzes = $quizzes['upcoming'];
$finishedquizzes = $quizzes['finished'];

foreach ($upcommingquizzes as $timestamp => $upcommingquiz) {
foreach ($upcommingquiz as $timestampupcomming => $upcomming) {
$inprogressquizzes[$timestampupcomming] = $upcomming;
foreach ($upcomingquizzes as $timestamp => $upcomingquiz) {
foreach ($upcomingquiz as $timestampupcoming => $upcoming) {
$inprogressquizzes[$timestampupcoming] = $upcoming;
}
}

Expand Down
10 changes: 5 additions & 5 deletions classes/output/student_search_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ public function query_db($pagesize, $useinitialsbar = false) {
$allquizzes = $quiz->get_quiz_summaries($this->now);

$inprogressquizzes = $allquizzes['inprogress'];
$upcommingquizzes = [];
$upcomingquizzes = [];
$finishedquizzes = [];

foreach ($allquizzes['upcomming'] as $upcomming) {
foreach ($upcomming as $quizobj) {
$upcommingquizzes[] = $quizobj;
foreach ($allquizzes['upcoming'] as $upcoming) {
foreach ($upcoming as $quizobj) {
$upcomingquizzes[] = $quizobj;
}
}

Expand All @@ -339,7 +339,7 @@ public function query_db($pagesize, $useinitialsbar = false) {
}
}

$quizzes = array_merge($inprogressquizzes, $upcommingquizzes, $finishedquizzes);
$quizzes = array_merge($inprogressquizzes, $upcomingquizzes, $finishedquizzes);

$allrecords = [];

Expand Down
16 changes: 8 additions & 8 deletions classes/output/upcomming_quizzes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Renderable for upcomming quizzes card.
* Renderable for upcoming quizzes card.
*
* @package local_assessfreq
* @copyright 2020 Matt Porritt <[email protected]>
Expand All @@ -27,21 +27,21 @@
use local_assessfreq\quiz;

/**
* Renderable for upcomming quizzes card.
* Renderable for upcoming quizzes card.
*
* @package local_assessfreq
* @copyright 2020 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class upcomming_quizzes {
class upcoming_quizzes {
/**
* Generate the markup for the upcomming quizzes chart,
* Generate the markup for the upcoming quizzes chart,
* used in the in progress quizzes dashboard.
*
* @param int $now Timestamp to get chart data for.
* @return array With Generated chart object and chart data status.
*/
public function get_upcomming_quizzes_chart(int $now): array {
public function get_upcoming_quizzes_chart(int $now): array {

// Get quizzes for the supplied timestamp.
$quiz = new quiz();
Expand All @@ -56,16 +56,16 @@ public function get_upcomming_quizzes_chart(int $now): array {
$quizseriesdata = [];
$participantseriesdata = [];

foreach ($quizzes['upcomming'] as $timestamp => $upcomming) {
foreach ($quizzes['upcoming'] as $timestamp => $upcoming) {
$quizcount = 0;
$participantcount = 0;

foreach ($upcomming as $quiz) {
foreach ($upcoming as $quiz) {
$quizcount++;
$participantcount += $quiz->participants;
}

// Check if inprogress quizzes are upcomming quizzes with overrides.
// Check if inprogress quizzes are upcoming quizzes with overrides.
foreach ($quizzes['inprogress'] as $inprogress) {
if ($inprogress->timestampopen >= $timestamp && $inprogress->timestampopen < $timestamp + HOURSECS) {
$quizcount++;
Expand Down
16 changes: 8 additions & 8 deletions classes/quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ public function get_inprogress_counts(int $now): array {
}

/**
* Get finished, in progress and upcomming quizzes and their associated data.
* Get finished, in progress and upcoming quizzes and their associated data.
*
* @param int $now Timestamp to use for reference for time.
* @return array $quizzes Array of finished, inprogress and upcomming quizzes with associated data.
* @return array $quizzes Array of finished, inprogress and upcoming quizzes with associated data.
*/
public function get_quiz_summaries(int $now): array {
// Get tracked quizzes.
Expand All @@ -446,20 +446,20 @@ public function get_quiz_summaries(int $now): array {
$quizzes = [
'finished' => [],
'inprogress' => [],
'upcomming' => [],
'upcoming' => [],
];

// Itterate through the hours, processing in progress and upcomming quizzes.
// Itterate through the hours, processing in progress and upcoming quizzes.
for ($hour = 0; $hour <= $this->hoursahead; $hour++) {
$time = $now + (HOURSECS * $hour);

if ($hour == 0) {
$quizzes['inprogress'] = [];
}

$quizzes['upcomming'][$time] = [];
$quizzes['upcoming'][$time] = [];

// Seperate out inprogress and upcomming quizzes, then get data for each quiz.
// Seperate out inprogress and upcoming quizzes, then get data for each quiz.
foreach ($trackedquizzes as $quiz) {
if ($quiz->timeopen < $time && $quiz->timeclose > $time && $hour === 0) { // Get inprogress quizzes.
$quizdata = $this->get_quiz_data($quiz->id);
Expand All @@ -478,7 +478,7 @@ public function get_quiz_summaries(int $now): array {

$quizzes['inprogress'][$quiz->id] = $quizdata;
unset($trackedquizzes[$quiz->id]); // Remove quiz from array to help with performance.
} else if (($quiz->timeopen >= $time) && ($quiz->timeopen < ($time + HOURSECS))) { // Get upcomming quizzes.
} else if (($quiz->timeopen >= $time) && ($quiz->timeopen < ($time + HOURSECS))) { // Get upcoming quizzes.
$quizdata = $this->get_quiz_data($quiz->id);
$quizdata->timestampopen = $quiz->timeopen;
$quizdata->timestampclose = $quiz->timeclose;
Expand All @@ -493,7 +493,7 @@ public function get_quiz_summaries(int $now): array {
$trackedrecords = $this->get_quiz_tracking($quiz->id);
$quizdata->tracking = array_pop($trackedrecords);

$quizzes['upcomming'][$time][$quiz->id] = $quizdata;
$quizzes['upcoming'][$time][$quiz->id] = $quizdata;
unset($trackedquizzes[$quiz->id]);
} else {
if (isset($quiz->overrides)) {
Expand Down
Loading

0 comments on commit 151e3d6

Please sign in to comment.