Skip to content

Commit

Permalink
Add doc reading status page
Browse files Browse the repository at this point in the history
  • Loading branch information
abeverley committed Aug 12, 2024
1 parent d52d8ee commit 953f2b1
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/Brass.pm
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,22 @@ any ['get', 'post'] => '/docread' => require_role doc => sub {
};
};

any ['get', 'post'] => '/docreadstatus' => require_role user_admin => sub {

my $schema = schema;
my $docschema = schema('doc');

my @doc_ids = map $_->doc_id, $schema->resultset('DocDocreadtype')->all;
my $docs = $docschema->resultset('Doc')->active->search({
id => \@doc_ids,
});
template 'docreadstatus' => {
docs => [$docs->all],
users => [$schema->resultset('User')->active->docread->all],
page => 'docread',
};
};

get '/doc' => require_role doc => sub {

my $schema = schema('doc');
Expand Down
11 changes: 10 additions & 1 deletion lib/Brass/DocSchema/Result/Doc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ __PACKAGE__->has_many(
{ cascade_copy => 0, cascade_delete => 0 },
);

# Last read date for a particular user
sub last_read
{ my ($self, $user) = @_;
my $lr = $user->docreads->search({
Expand All @@ -197,7 +198,15 @@ sub last_read
rows => 1,
})->next
or return;
$lr->datetime;
my $dt = $lr->datetime;
my $expiry = DateTime->now->subtract(years => 1);
my $status = $dt > $expiry->clone->add(months => 1) ? 'success'
: $dt > $expiry ? 'warning'
: 'danger';
{
date => $dt,
status => $status,
}
}

1;
26 changes: 26 additions & 0 deletions lib/Brass/DocSchema/ResultSet/Doc.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Brass::DocSchema::ResultSet::Doc;

use strict;
use warnings;

use Log::Report;

use base qw(DBIx::Class::ResultSet);

sub active
{ my $self = shift;

$self->search_rs({
retired => undef,
},{
order_by => 'me.title',
});
}

sub required_reading
{ my $self = shift;
$self->active->search_rs({
});
}

1;
2 changes: 1 addition & 1 deletion lib/Brass/Schema/Result/Docreadtype.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ __PACKAGE__->has_many(

__PACKAGE__->has_many(
"doc_docreadtypes",
"Brass::Schema::Result::UserDocreadtype",
"Brass::Schema::Result::DocDocreadtype",
{ "foreign.docreadtype_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
Expand Down
5 changes: 5 additions & 0 deletions lib/Brass/Schema/Result/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ sub has_docreadtype
(grep { $_->docreadtype_id == $docreadtype_id } $self->user_docreadtypes) ? 1 : 0;
}

sub must_read_doc
{ my ($self, $doc_id) = @_;
(grep { $_->docreadtype->doc_docreadtypes->search({doc_id => $doc_id})->count } $self->user_docreadtypes) ? 1 : 0;
}

sub servertypes_as_string
{ my $self = shift;
join ', ', map $_->servertype->name, $self->user_servertypes->all;
Expand Down
10 changes: 10 additions & 0 deletions lib/Brass/Schema/ResultSet/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ sub active
});
}

sub docread
{ my $self = shift;
$self->search_rs({
'user_docreadtypes.id' => { '!=' => undef },
},{
join => 'user_docreadtypes',
collapse => 1,
});
}

sub keys
{ my $self = shift;
$self->search_rs({
Expand Down
28 changes: 28 additions & 0 deletions views/docreadstatus.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Document reading status</h1>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
[% FOREACH user IN users %]
<th>[% user.name | html %]</th>
[% END %]
</tr>
[% FOREACH doc IN docs %]
<tr>
<td>[% doc.id %]</td>
<td><a href="/doc/latest/[% doc.id %]">[% doc.title | html %]</a></td>
[% FOREACH user IN users %]
[% IF user.must_read_doc(doc.id) %]
[% lr = doc.last_read(user) %]
[% IF lr %]
<td class="text-[% lr.status %]">[% lr.date.ymd | html %]</td>
[% ELSE %]
<td class="text-danger">Not yet read</td>
[% END %]
[% ELSE %]
<td>Nope</td>
[% END %]
[% END %]
</tr>
[% END %]
</table>
3 changes: 3 additions & 0 deletions views/layouts/main.tt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
<ul class="dropdown-menu">
<li [% IF page=="doc" %]class="active"[% END %]><a href="/doc">Documents</a></li>
<li [% IF page=="docread" %]class="active"[% END %]><a href="/docread">Required reading</a></li>
[% IF user.has_permission('user_admin') %]
<li [% IF page=="docreadstatus" %]class="active"[% END %]><a href="/docreadstatus">Required reading status</a></li>
[% END %]
<li [% IF page=="doc/image" %]class="active"[% END %]><a href="/doc/image/">Images</a></li>
</ul>
</li>
Expand Down

0 comments on commit 953f2b1

Please sign in to comment.