Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Update #86byt93eq fetch note logic moved to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nihaalshaikh committed May 28, 2024
1 parent 8680608 commit 63187a8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
13 changes: 4 additions & 9 deletions block_coursenotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function init(): void {
*
*/
public function get_content(): object {
global $USER, $DB, $COURSE, $OUTPUT;
global $COURSE, $OUTPUT;

if ($this->content !== null) {
return $this->content;
Expand All @@ -57,15 +57,10 @@ public function get_content(): object {
'courseid' => $COURSE->id,
];

// Fetch notes from the database.
$conditions = [
'userid' => $USER->id,
'courseid' => $COURSE->id,
];
$notes = $DB->get_records('block_coursenotes', $conditions, 'timecreated DESC', '*', 0, 1);
// Fetch the latest coursenote.
$latestnote = helper::fetch_latest_coursenote_for_user();

if ($notes) {
$latestnote = reset($notes); // Get the first (latest) record.
if ($latestnote) {
$data['coursenote'] = $latestnote->coursenote;
}

Expand Down
34 changes: 34 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,38 @@ public static function isduplicate($params, $notes): bool {
}
return false;
}

/**
* Fetches the latest course note for a user and course.
*
* @return mixed The latest course note or false if none found.
*/
public static function fetch_latest_coursenote_for_user(): mixed {
global $DB, $COURSE, $USER;

$conditions = [
'userid' => $USER->id,
'courseid' => $COURSE->id,
];

$notes = $DB->get_records('block_coursenotes', $conditions, 'timecreated DESC', '*', 0, 1);

return $notes ? reset($notes) : false;
}

/**
* Fetches all notes for a user and course.
*
* @return array An array of course notes.
*/
public static function fetch_all_coursenotes_for_user(): array {
global $DB, $COURSE, $USER;

$conditions = [
'userid' => $USER->id,
'courseid' => $COURSE->id,
];

return $DB->get_records('block_coursenotes', $conditions, 'timecreated ASC');
}
}

0 comments on commit 63187a8

Please sign in to comment.