-
Notifications
You must be signed in to change notification settings - Fork 10
/
review.pl
533 lines (453 loc) · 14 KB
/
review.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2013-2024, VU University Amsterdam
SWI-Prolog Solutions b.v.
This program 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 2
of the License, or (at your option) any later version.
This program 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 this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(pack_review,
[ pack_rating_votes/3, % +Pack, -Rating, -Votes
pack_comment_count/2, % +Pack, -CommentCount
pack_reviews//1, % +Pack
show_pack_rating//1, % +Pack
show_pack_rating//5, % +Pack, +Rating, +Votes, +Comment, +Opts
profile_reviews//1, % +UUID
user_review_count/2 % +UUID, -Count
]).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/html_write)).
:- use_module(library(http/html_head)).
:- use_module(library(persistency)).
:- use_module(library(aggregate)).
:- use_module(library(record)).
:- use_module(library(debug)).
:- use_module(markitup).
:- use_module(rating).
:- use_module(openid).
:- use_module(wiki).
:- http_handler(root(pack/review), pack_review, []).
:- http_handler(root(pack/review/submit), pack_submit_review, []).
:- http_handler(root(pack/review/rating), pack_rating, []).
/** <module> Handle rating and reviewing of packages
*/
/*******************************
* DATA *
*******************************/
:- persistent
review(pack:atom,
user:atom, % UUID of the user
time:number,
rating:integer,
comment:atom).
:- initialization
absolute_file_name(data('reviews.db'), File,
[ access(write) ]),
db_attach(File, []).
/*******************************
* INTERFACE *
*******************************/
%% pack_review(+Request)
%
% HTTP handler to review a pack.
pack_review(Request) :-
site_user(Request, UUID),
http_parameters(Request,
[ p(Pack, [])
]),
http_link_to_id(pack_submit_review, [], Action),
reply_html_page(
wiki(review(Pack)),
title('Review pack ~w'-[Pack]),
[ h1('Review pack ~w'-[Pack]),
\explain(Pack, UUID),
\html_requires(css('pack.css')),
form([ class(review), action(Action), method('POST') ],
[ input([type(hidden), name(p), value(Pack)]),
table([ \reviewer(Request, UUID),
\rating(Pack, UUID),
\comment(Pack, UUID),
tr(td([colspan(2), align(right)],
input([ type(submit),
value('Submit review')
])))
])
])
]).
explain(Pack, UUID) -->
{ site_user_property(UUID, name(Name))
},
html([ p([ 'Dear ', Name, ', you requested to review pack ', b(Pack), '. ',
'The text field uses PlDoc wiki format, which is a ',
'superset of Markdown. You can use the two ',
'left-most icons to open and close a preview window.'
]),
p([ 'Any user can have at most one review per pack. Trying ',
'to submit a new review will return the old review and ',
'allow you to update your opinion.'
])
]).
%% reviewer(+Request, +UUID)// is det.
%
% Present details about the reviewer
reviewer(Request, UUID) -->
{ site_user_property(UUID, name(Name)),
option(request_uri(RequestURI), Request),
http_link_to_id(create_profile, [return(RequestURI)], UpdateURL),
Update = a([class(update), href(UpdateURL)], 'Update profile')
}, !,
html([ tr([th('User:'), td([ input([ name(name),
value(Name),
disabled(disabled)
]),
Update
])])
]).
rating(Pack, UUID) -->
{ http_link_to_id(pack_rating, [], HREF),
( review(Pack, UUID, _, Rating0, _),
Rating0 > 0
-> Extra = [data_average(Rating0)]
; Extra = [data_average(0)],
Rating0 = 0
)
},
html(tr([ th('Your rating for ~w:'-[Pack]),
td( [ input([type(hidden), name(rating), value(Rating0)]),
\rate([ on_rating(HREF),
data_id(Pack),
set_field(rating),
rate_max(5),
step(true),
type(big),
can_rate_again(true)
| Extra
])
])
])).
%% pack_rating(+Request)
%
% Handle the actual rating
pack_rating(Request) :-
http_parameters(Request,
[ idBox(IdBox, []),
rate(Rate, [number])
], []),
debug(rating, 'Got idBox = ~q, Rate = ~q', [IdBox,Rate]),
format('Content-type: text/plain\n\n'),
format('true\n').
comment(Pack, UUID) -->
{ ( review(Pack, UUID, _, _, Comment)
-> Extra = [value(Comment)]
; Extra = []
)
},
html(tr(td(colspan(2),
\markitup([ id(comment),
markup(pldoc),
cold(60),
rows(10)
| Extra
])))).
%% pack_submit_review(+Request)
%
% Handle a pack review submission
pack_submit_review(Request) :-
site_user(Request, UUID),
http_parameters(Request,
[ p(Pack, []),
rating(Rating, [number]),
comment(Comment, [optional(true), default('')])
]),
reply_html_page(
wiki(review(Pack)),
title('Thanks for your review of ~w'-[Pack]),
[ \update_review(Pack, UUID, Rating, Comment)
]).
%% update_review(+Pack, +UUID, +Rating, +Comment)// is det.
%
% Assert/update a review about a pack.
update_review(Pack, UUID, Rating, Comment) -->
{ review(Pack, UUID, _Time, Rating, Comment) }, !,
html(h4(class(wiki), 'No changes, showing your existing comment')),
show_review(Pack, UUID),
refresh(Pack).
update_review(Pack, UUID, Rating, Comment) -->
{ review(Pack, UUID, _Time, _Rating, _Comment), !,
retractall_review(Pack, UUID, _, _, _),
get_time(TimeF),
Time is round(TimeF),
assert_review(Pack, UUID, Time, Rating, Comment)
},
html(h4(class(wiki), 'Updated your comments for pack ~w'-[Pack])),
show_review(Pack, UUID),
refresh(Pack).
update_review(Pack, UUID, Rating, Comment) -->
{ get_time(Time),
assert_review(Pack, UUID, Time, Rating, Comment)
},
html(h4(class(wiki), 'Added comment for pack ~w'-[Pack])),
show_review(Pack, UUID),
refresh(Pack).
refresh(Pack) -->
{ http_link_to_id(pack_list, [p(Pack)], ListPack),
Delay = 3
},
html([ 'Redirecting to pack ', a(href(ListPack), Pack),
' in ~w seconds'-[Delay]
]),
html_post(head,
meta([ 'http-equiv'(refresh),
content(Delay+';'+ListPack)
])).
/*******************************
* SHOW RESULTS *
*******************************/
%% pack_reviews(Pack)// is det.
%
% Show reviews for Pack
pack_reviews(Pack) -->
html(h2(class(wiki), 'Reviews')),
show_reviews(Pack).
show_reviews(Pack) -->
{ \+ review(Pack, _, _, _, _), !,
http_link_to_id(pack_review, [p(Pack)], HREF)
},
html([ p([ 'No reviews. ',
a(href(HREF), 'Create'), ' the first review!.'
])
]).
show_reviews(Pack) -->
{ findall(review(Pack, UUID, Time, Rating, Comment),
( review(Pack, UUID, Time, Rating, Comment),
Comment \== ''
),
Reviews),
length(Reviews, Count),
sort_reviews(time, Reviews, Sorted)
},
html([ div(\review_action(Pack)),
div(class(smallprint), \showing_reviews(Count))
]),
list_reviews(Sorted, []).
review_action(Pack) -->
{ site_user_logged_in(User),
review(Pack, User, Time, _Rating, _Comment),
http_link_to_id(pack_review, [p(Pack)], HREF)
}, !,
html([ a(href(HREF), 'Update'), ' your rating or review from ',
\show_time(Time), '.'
]).
review_action(Pack) -->
{ http_link_to_id(pack_review, [p(Pack)], HREF)
},
html([ a(href(HREF), 'Write'), ' a review or add a rating.' ]).
showing_reviews(Count) -->
{ Count >= 2 },
html([ 'Showing ~D reviews, '-[Count],
'sorted by date entered, last review first. '
]).
showing_reviews(_) --> [].
list_reviews([], _) --> [].
list_reviews([H|T], Options) --> list_review(H, Options), list_reviews(T, Options).
list_review(Review, Options) -->
{ review_name(Review, Pack),
review_user(Review, UUID),
review_time(Review, Time),
review_rating(Review, Rating),
review_comment(Review, Comment)
},
html([ div(class(review),
[ div(class(rating),
[ \show_pack(Pack, Options),
\show_rating_value(Pack, Rating, [])
]),
div(class(comment), \show_comment(Comment)),
div(class(reviewer), \show_reviewer(UUID, Time))
])
]).
:- record
review(name:atom,
user:atom,
time:number,
rating:integer,
comment:atom).
sort_reviews(By, Reviews, Sorted) :-
map_list_to_pairs(review_data(By), Reviews, Keyed),
keysort(Keyed, KeySorted),
pairs_values(KeySorted, Sorted0),
reverse(Sorted0, Sorted).
%% show_review(+Pack, +UUID)// is det.
%
% Show an individual review about Pack
show_review(Pack, UUID) -->
{ review(Pack, UUID, _Time, Rating, Comment),
http_link_to_id(pack_review, [p(Pack)], Update),
http_link_to_id(pack_list, [p(Pack)], ListPack)
},
html_requires(css('pack.css')),
html([ div(class(review),
[ b('Reviewer: '), \show_reviewer(UUID), ', ',
b('Your rating: '), \show_rating_value(Pack, Rating, []),
b('Average rating: '), \show_rating(Pack),
div(class(comment), \show_comment(Comment)),
ul([ li([a(href(Update), 'Update'), ' my review']),
li([a(href(ListPack), 'View'), ' pack ', Pack])
])
])
]).
%% show_pack(Pack, +Options)// is det.
show_pack(Pack, Options) -->
{ option(show_pack(true), Options), !,
http_link_to_id(pack_list, [p(Pack)], HREF)
},
html(span(['Pack: ', a(href(HREF), Pack)])).
show_pack(_, _) --> [].
%% show_reviewer(+UUID)
show_reviewer(UUID) -->
{ site_user_property(UUID, name(Name)),
http_link_to_id(view_profile, [user(UUID)], HREF),
Name \== '',
aggregate_all(count,
( review(_, UUID, _, _, Comment), Comment \== '' ),
Comments),
aggregate_all(count-sum(Rating),
( review(_, UUID, _, Rating, _), Rating > 0 ),
Ratings-Sum),
( Ratings > 0
-> Avg is Sum/Ratings,
format(atom(Title), '~D comments, ~D ratings (avg ~1f)',
[Comments, Ratings, Avg])
; format(atom(Title), '~D comments', [Comments])
)
}, !,
html(a([class(user), href(HREF), title(Title)], Name)).
show_reviewer(_UUID) -->
html(i(anonymous)).
%% show_reviewer(+UUID, +Time)
show_reviewer(UUID, Time) -->
show_time(Time),
html(', '),
show_reviewer(UUID).
show_time(Time) -->
{ format_time(atom(Date), '%A %d %B %Y', Time)
},
html(Date).
show_rating(Pack) -->
{ pack_rating_votes(Pack, Rating, Votes),
pack_comment_count(Pack, Count)
},
show_pack_rating(Pack, Rating, Votes, Count, []).
%% show_pack_rating(+Pack)// is det.
%
% Show overall rating. If there is no rating, offer to create one.
show_pack_rating(Pack) -->
{ pack_rating_votes(Pack, Rating, Votes) },
( { Votes =:= 0 }
-> { http_link_to_id(pack_review, [p(Pack)], HREF) },
html(span(class(not_rated),
[ 'Not rated. ', a(href(HREF), 'Create'),
' the first rating!'
]))
; { pack_comment_count(Pack, Count) },
show_pack_rating(Pack, Rating, Votes, Count, [])
).
%% pack_rating_votes(+Pack, -Rating, -Votes) is det.
%
% Rating is the current rating for Pack, based on Votes.
pack_rating_votes(Pack, Rating, Votes) :-
aggregate_all(count-sum(R), pack_rating(Pack, R), Votes-Sum),
Votes > 0, !,
Rating is Sum/Votes.
pack_rating_votes(_Pack, 0, 0).
pack_rating(Pack, Rating) :-
review(Pack, _, _, Rating, _),
Rating > 0.
%% pack_comment_count(Pack, Count)
%
% True when Count is the number of comments for Pack.
pack_comment_count(Pack, Count) :-
aggregate_all(count,
( review(Pack, _, _, _, Comment),
Comment \== ''
),
Count).
show_rating_value(Pack, Value, Options) -->
rate([ rate_max(5),
data_id(Pack),
type(small),
disabled(true),
class(rated),
post(script),
data_average(Value)
| Options
]).
%% show_pack_rating(+Pack, +Rating, +Votes, +CommentCount,
%% +Options)// is det.
%
% Show rating for Pack.
show_pack_rating(Pack, Rating, 0, 0, Options) --> !,
show_rating_value(Pack, Rating, Options).
show_pack_rating(Pack, Rating, Votes, Count, Options) -->
html(span(class(rating),
[ \show_rating_value(Pack, Rating, Options),
span(class(votes), ' (~D/~D)'-[Votes, Count])
])).
%% show_comment(+Comment)// is det.
%
% Display Comment. Comment is an atom holding Wiki text.
show_comment('') --> !,
html(i('No comment')).
show_comment(Text) -->
{ atom_codes(Text, Codes),
wiki_file_codes_to_dom(Codes, /, DOM0),
clean_dom(DOM0, DOM)
},
html(DOM).
clean_dom([p(X)], X) :- !.
clean_dom(X, X).
/*******************************
* PROFILE COMPONENTS *
*******************************/
%% profile_reviews(+UUID)// is det.
%
% Create a =h2= section with all reviews by a given OpenID.
profile_reviews(UUID) -->
{ findall(review(Pack, UUID, Time, Rating, Comment),
review(Pack, UUID, Time, Rating, Comment),
Reviews),
Reviews \== [], !,
length(Reviews, Count),
sort_reviews(time, Reviews, Sorted),
site_user_property(UUID, name(Name))
},
html_requires(css('pack.css')),
html([ h2(class(wiki), 'Reviews by ~w'-[Name]),
p([ \showing_reviews(Count)
])
]),
list_reviews(Sorted, [show_pack(true)]).
profile_reviews(_) -->
[].
%% user_review_count(+UUID, -Count) is det.
%
% True when Count is the number of reviews by UUID.
user_review_count(UUID, Count) :-
aggregate_all(count, review(_, UUID, _, _, _), Count).