Skip to content

Commit

Permalink
Refactor template
Browse files Browse the repository at this point in the history
Reduce duplicate code and reuse the same template for feedback and marking.
  • Loading branch information
stuartlamour committed Nov 28, 2024
1 parent da6e014 commit 95e0d9b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 68 deletions.
66 changes: 33 additions & 33 deletions block_my_feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,38 @@ public function get_content(): stdClass {

if (self::is_teacher()) {
// Teacher content.
$template->marking = $this->fetch_marking($USER);
$template->mods = $this->fetch_marking($USER);
} else {
// Student content.
$template->feedback = $this->fetch_feedback($USER);
$template->mods = $this->fetch_feedback($USER);
$template->showfeedbacktrackerlink = true;
}

if (isset($template->feedback) || isset($template->marking)) {
if (isset($template->mods)) {
$this->content->text = $OUTPUT->render_from_template('block_my_feedback/content', $template);
}

return $this->content;
}

/**
* Return if user has archetype editingteacher.
*
*/
public static function is_teacher(): bool {
global $DB, $USER;
// Get id's from role where archetype is editingteacher.
$roles = $DB->get_fieldset('role', 'id', ['archetype' => 'editingteacher']);

// Check if user has editingteacher role on any courses.
list($roles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED);
$params['userid'] = $USER->id;
$sql = "SELECT id
FROM {role_assignments}
WHERE userid = :userid
AND roleid $roles";
return $DB->record_exists_sql($sql, $params);
}

/**
* Return marking for a user.
Expand Down Expand Up @@ -106,6 +124,7 @@ public function fetch_marking(stdClass $user): ?array {

$modinfo = get_fast_modinfo($course->id);

// Loop through assessments for this course.
foreach ($summatives as $summative) {

// Check this is a course mod.
Expand Down Expand Up @@ -212,8 +231,9 @@ public function is_course_current(stdClass $course): bool {
if ($course->enddate == 0) {
return true; // Enddate is set to 0 when no end date, show course.
}
$future = strtotime('+3 month'); // Allow for Late summer assessments.
if ($future > $course->enddate) {
// Show course 3 months after end date to allow for UCL late summer assessments.
$cutoffdate = strtotime('+3 month');
if ($course->enddate > $cutoffdate) {
return false; // After the end date.
}
}
Expand All @@ -226,15 +246,15 @@ public function is_course_current(stdClass $course): bool {
* @param int $duedate
*/
public function duedate_in_range(int $duedate): ?int {
// Only show dates within a month.
$past = strtotime('-2 month');
$future = strtotime('+1 month');
// If due date is too far in the future.
if ($duedate > $future) {
// Only show dates within UCL limits for marking.
$startdate = strtotime('-2 month');
$cutoffdate = strtotime('+1 month');
// If due date is beyond cutoff.
if ($duedate > $cutoffdate) {
return false;
}
// If due date is too far in the past.
if ($duedate < $past) {
if ($duedate < $startdate) {
return false;
}
return $duedate;
Expand Down Expand Up @@ -276,8 +296,8 @@ public function fetch_feedback($user): ?array {
$feedback = new stdClass();
$feedback->id = $f->gradeid;
$feedback->date = date('jS F', $f->lastmodified);
$feedback->activityname = $f->name;
$feedback->link = new moodle_url('/mod/'.$f->modname.'/view.php', ['id' => $f->cmid]);
$feedback->name = $f->name;
$feedback->url = new moodle_url('/mod/'.$f->modname.'/view.php', ['id' => $f->cmid]);

// Course.
$course = $DB->get_record('course', ['id' => $f->course]);
Expand Down Expand Up @@ -396,26 +416,6 @@ public function show_quiz_submission(stdClass $submission): bool {
return false;
}

/**
* Return if user has archetype editingteacher.
*
*/
public static function is_teacher(): bool {
global $DB, $USER;

// Get id's from role where archetype is editingteacher.
$roles = $DB->get_fieldset('role', 'id', ['archetype' => 'editingteacher']);

// Check if user has editingteacher role on any courses.
list($roles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED);
$params['userid'] = $USER->id;
$sql = "SELECT id
FROM {role_assignments}
WHERE userid = :userid
AND roleid $roles";
return $DB->record_exists_sql($sql, $params);
}

/**
* Defines in which pages this block can be added.
*
Expand Down
57 changes: 22 additions & 35 deletions templates/content.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,55 @@
@template blocks_my_feedback/content
Example context (json):
{
"allfeedbackurl": "cgf/intervalurl/",
"nofeedback": 0,
"feedback": [
"showfeedbacktrackerlink": "true",
"mods": [
{
"icon": "moodleurl/icon.png",
"coursename": "A course",
"name": "A mod",
"url": "/foo/",
"duedate": "10th Jan 2023",
"tutorname": "Bob Smith",
"date": "24th March 2023"
"requiremarking": "3",
},
{
"icon": "moodleurl/icon.png",
"coursename": "A course",
"tutorname": "Rob Smith",
"date": "24th April 2023"
}
]

}
}}

{{! TODO - Lots of duplication here, can we use the same template for marking and feedback? }}
{{#marking}}
{{! Mods for feedback and marking. }}
{{#mods}}
<div class="d-flex">

<div class="mr-3 bg-cover bg-light rounded-circle flex-shrink-0" style="background-image: url('{{{icon}}}'); height: 4rem; width: 4rem;"></div>

<div>
<div class="line-clamp-1 text-muted">
<small>{{{coursename}}}</small>
<small>{{coursename}}</small>
</div>
<a href="{{url}}" class="font-weight-bold">{{{name}}}</a>
<a href="{{{url}}}" class="font-weight-bold">{{name}}</a>
{{#duedate}}
<div>
<small>{{#str}} t:due, block_my_feedback {{/str}} {{duedate}}</small>
</div>
{{/duedate}}
</div>
</div>

<div class='text-right'>
<small><a href="{{{markingurl}}}">{{requiremarking}} {{#str}} t:needmarking, block_my_feedback {{/str}}</a></small>
</div>
<hr>
{{/marking}}


{{#feedback}}
<div class="d-flex">

<div class="mr-3 bg-cover rounded-circle flex-shrink-0" style="background-image: url('{{{icon}}}'); height: 4rem; width: 4rem;"></div>

<div>
<div class="line-clamp-1 text-muted">
<small>{{coursename}}</small>
</div>
<a href="{{{link}}}" class="font-weight-bold">{{activityname}}</a>
</div>
</div>
<div class='text-right small'>
{{! Student. }}
{{#date}}
{{#tutorname}}{{.}} · {{/tutorname}} {{date}}
{{/date}}

<div class='text-right'>
<small>{{#tutorname}}{{.}} · {{/tutorname}} {{date}}</small>
{{! Admin. }}
{{#requiremarking}}
<a href="{{{markingurl}}}">{{requiremarking}} {{#str}} t:needmarking, block_my_feedback {{/str}}</a>
{{/requiremarking}}
</div>
<hr>
{{/feedback}}
{{/mods}}

{{#showfeedbacktrackerlink}}
<div class="bg-light pt-2 pb-3 px-3">
Expand Down

0 comments on commit 95e0d9b

Please sign in to comment.