1: # The LearningOnline Network
2: # Feedback
3: #
4: # $Id: lonfeedback.pm,v 1.44 2003/06/04 22:05:22 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # (Internal Server Error Handler
29: #
30: # (Login Screen
31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
33: #
34: # 3/1/1 Gerd Kortemeyer)
35: #
36: # 3/1,2/3,2/5,2/6,2/8 Gerd Kortemeyer
37: # 2/9 Guy Albertelli
38: # 2/10 Gerd Kortemeyer
39: # 2/13 Guy Albertelli
40: # 7/25 Gerd Kortemeyer
41: # 7/26 Guy Albertelli
42: # 7/26,8/10,10/1,11/5,11/6,12/27,12/29 Gerd Kortemeyer
43: # YEAR=2002
44: # 1/1,1/16 Gerd Kortemeyer
45: #
46:
47: package Apache::lonfeedback;
48:
49: use strict;
50: use Apache::Constants qw(:common);
51: use Apache::lonmsg();
52: use Apache::loncommon();
53: use Apache::lontexconvert();
54:
55: sub mail_screen {
56: my ($r,$feedurl,$options) = @_;
57: my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion');
58: $r->print(<<ENDDOCUMENT);
59: <html>
60: <head>
61: <title>The LearningOnline Network with CAPA</title>
62: <meta http-equiv="pragma" content="no-cache"></meta>
63: <script>
64: function gosubmit() {
65: var rec=0;
66: if (typeof(document.mailform.elements.author)!="undefined") {
67: if (document.mailform.elements.author.checked) {
68: rec=1;
69: }
70: }
71: if (typeof(document.mailform.elements.question)!="undefined") {
72: if (document.mailform.elements.question.checked) {
73: rec=1;
74: }
75: }
76: if (typeof(document.mailform.elements.course)!="undefined") {
77: if (document.mailform.elements.course.checked) {
78: rec=1;
79: }
80: }
81: if (typeof(document.mailform.elements.policy)!="undefined") {
82: if (document.mailform.elements.policy.checked) {
83: rec=1;
84: }
85: }
86: if (typeof(document.mailform.elements.discuss)!="undefined") {
87: if (document.mailform.elements.discuss.checked) {
88: rec=1;
89: }
90: }
91: if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
92: if (document.mailform.elements.anondiscuss.checked) {
93: rec=1;
94: }
95: }
96:
97: if (rec) {
98: document.mailform.submit();
99: } else {
100: alert('Please check a feedback type.');
101: }
102: }
103: </script>
104: </head>
105: $bodytag
106: <h2><tt>$feedurl</tt></h2>
107: <form action="/adm/feedback" method="post" name="mailform"
108: enctype="multipart/form-data">
109: <input type=hidden name=postdata value="$feedurl">
110: Please check at least one of the following feedback types:
111: $options<hr>
112: My question/comment/feedback:<p>
113: <textarea name=comment cols=60 rows=10 wrap=hard>
114: </textarea><p>
115: Attachment (128 KB max size): <input type="file" name="attachment" />
116: </p>
117: <p>
118: <input type="hidden" name="sendit" value="1" />
119: <input type=button value="Send Feedback" onClick='gosubmit();' />
120: </p>
121: </form>
122: ENDDOCUMENT
123: $r->print(&generate_preview_button().'</body></html>');
124: }
125:
126: sub fail_redirect {
127: my ($r,$feedurl) = @_;
128: $r->print (<<ENDFAILREDIR);
129: <head><title>Feedback not sent</title>
130: <meta http-equiv="pragma" content="no-cache"></meta>
131: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
132: </head>
133: <html>
134: <body bgcolor="#FFFFFF">
135: <img align=right src=/adm/lonIcons/lonlogos.gif>
136: <b>Sorry, no recipients ...</b>
137: </body>
138: </html>
139: ENDFAILREDIR
140: }
141:
142: sub redirect_back {
143: my ($r,$feedurl,$typestyle,$sendsomething,$sendposts,$status) = @_;
144: $r->print (<<ENDREDIR);
145: <head>
146: <title>Feedback sent</title>
147: <meta http-equiv="pragma" content="no-cache"></meta>
148: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
149: </head>
150: <html>
151: <body bgcolor="#FFFFFF">
152: <img align=right src=/adm/lonIcons/lonlogos.gif>
153: $typestyle
154: <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
155: <font color=red>$status</font>
156: </body>
157: </html>
158: ENDREDIR
159: }
160:
161: sub no_redirect_back {
162: my ($r,$feedurl) = @_;
163: $r->print (<<ENDNOREDIR);
164: <head><title>Feedback not sent</title>
165: <meta http-equiv="pragma" content="no-cache"></meta>
166: ENDNOREDIR
167:
168: if ($feedurl!~/^\/adm\/feedback/) {
169: $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
170: }
171:
172: $r->print (<<ENDNOREDIRTWO);
173: </head>
174: <html>
175: <body bgcolor="#FFFFFF">
176: <img align=right src=/adm/lonIcons/lonlogos.gif>
177: <b>Sorry, no feedback possible on this resource ...</b>
178: </body>
179: </html>
180: ENDNOREDIRTWO
181: }
182:
183: sub screen_header {
184: my ($feedurl) = @_;
185: my $options='';
186: if (($feedurl=~/^\/res\//) && ($feedurl!~/^\/res\/adm/)) {
187: $options=
188: '<p><input type=checkbox name=author> Feedback to resource author';
189: }
190: if (&feedback_available(1)) {
191: $options.=
192: '<br><input type=checkbox name=question> Question about resource content';
193: }
194: if (&feedback_available(0,1)) {
195: $options.=
196: '<br><input type=checkbox name=course> '.
197: 'Question/Comment/Feedback about course content';
198: }
199: if (&feedback_available(0,0,1)) {
200: $options.=
201: '<br><input type=checkbox name=policy> '.
202: 'Question/Comment/Feedback about course policy';
203: }
204:
205: if ($ENV{'request.course.id'}) {
206: if (&Apache::lonnet::allowed('pch',
207: $ENV{'request.course.id'}.
208: ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
209: $options.='<br><input type=checkbox name=discuss> '.
210: '<b>Contribution to course discussion of resource</b>';
211: $options.='<br><input type=checkbox name=anondiscuss> '.
212: '<b>Anonymous contribution to course discussion of resource</b>'.
213: ' (name only visible to course faculty)';
214: }
215: }
216: return $options;
217: }
218:
219: sub resource_output {
220: my ($feedurl) = @_;
221: my $usersaw=&Apache::lonnet::ssi($feedurl);
222: $usersaw=~s/\<body[^\>]*\>//gi;
223: $usersaw=~s/\<\/body\>//gi;
224: $usersaw=~s/\<html\>//gi;
225: $usersaw=~s/\<\/html\>//gi;
226: $usersaw=~s/\<head\>//gi;
227: $usersaw=~s/\<\/head\>//gi;
228: $usersaw=~s/action\s*\=/would_be_action\=/gi;
229: return $usersaw;
230: }
231:
232: sub clear_out_html {
233: my ($message,$override)=@_;
234: my $cid=$ENV{'request.course.id'};
235: if (($ENV{"course.$cid.allow_limited_html_in_feedback"} =~ m/yes/i) ||
236: ($override)) {
237: # allows <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG>
238: # <BLOCKQUOTE> <DIV .*> <DIV> <IMG>
239: my %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1,
240: BR=>1, TT=>1, STRONG=>1, BLOCKQUOTE=>1, DIV=>1, IMG=>1);
241:
242: $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
243: {($html{uc($2)}&(length($1)<1000))?"\<$1":"\<$1"}/ge;
244: $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
245: {($html{uc($2)}&(length($1)<1000))?"$1\>":"$1\>"}/ge;
246: } else {
247: $message=~s/\<\/*m\s*\>//g;
248: $message=~s/\</\<\;/g;
249: $message=~s/\>/\>\;/g;
250: }
251: return $message;
252: }
253:
254: sub assemble_email {
255: my ($feedurl,$message,$prevattempts,$usersaw,$useranswer)=@_;
256: my $email=<<"ENDEMAIL";
257: Refers to <a href="$feedurl">$feedurl</a>
258:
259: $message
260: ENDEMAIL
261: my $citations=<<"ENDCITE";
262: <h2>Previous attempts of student (if applicable)</h2>
263: $prevattempts
264: <p><hr>
265: <h2>Original screen output (if applicable)</h2>
266: $usersaw
267: <h2>Correct Answer(s) (if applicable)</h2>
268: $useranswer
269: ENDCITE
270: return ($email,$citations);
271: }
272:
273: sub secapply {
274: my $rec=shift;
275: my $defaultflag=shift;
276: $rec=~s/\s+//g;
277: $rec=~s/\@/\:/g;
278: my ($adr,$sections)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
279: if ($sections) {
280: foreach (split(/\;/,$sections)) {
281: if (($_ eq $ENV{'request.course.sec'}) ||
282: ($defaultflag && ($_ eq '*'))) {
283: return $adr;
284: }
285: }
286: } else {
287: return $rec;
288: }
289: return '';
290: }
291:
292: sub decide_receiver {
293: my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
294: my $typestyle='';
295: my %to=();
296: if ($ENV{'form.author'}||$author) {
297: $typestyle.='Submitting as Author Feedback<br>';
298: $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
299: $to{$2.':'.$1}=1;
300: }
301: if ($ENV{'form.question'}||$question) {
302: $typestyle.='Submitting as Question<br>';
303: foreach (split(/\,/,
304: $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'})
305: ) {
306: my $rec=&secapply($_,$defaultflag);
307: if ($rec) { $to{$rec}=1; }
308: }
309: }
310: if ($ENV{'form.course'}||$course) {
311: $typestyle.='Submitting as Comment<br>';
312: foreach (split(/\,/,
313: $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'})
314: ) {
315: my $rec=&secapply($_,$defaultflag);
316: if ($rec) { $to{$rec}=1; }
317: }
318: }
319: if ($ENV{'form.policy'}||$policy) {
320: $typestyle.='Submitting as Policy Feedback<br>';
321: foreach (split(/\,/,
322: $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'})
323: ) {
324: my $rec=&secapply($_,$defaultflag);
325: if ($rec) { $to{$rec}=1; }
326: }
327: }
328: if ((scalar(%to) eq '0') && (!$defaultflag)) {
329: ($typestyle,%to)=
330: &decide_receiver($feedurl,$author,$question,$course,$policy,1);
331: }
332: return ($typestyle,%to);
333: }
334:
335: sub feedback_available {
336: my ($question,$course,$policy)=@_;
337: my ($typestyle,%to)=&decide_receiver('',0,$question,$course,$policy);
338: return scalar(%to);
339: }
340:
341: sub send_msg {
342: my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
343: my $status='';
344: my $sendsomething=0;
345: foreach (keys %to) {
346: if ($_) {
347: my $declutter=&Apache::lonnet::declutter($feedurl);
348: unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
349: 'Feedback ['.$declutter.']',$email,$citations,$feedurl,
350: $attachmenturl)=~/ok/) {
351: $status.='<br>Error sending message to '.$_.'<br>';
352: } else {
353: $sendsomething++;
354: }
355: }
356: }
357:
358: my %record=&Apache::lonnet::restore('_feedback');
359: my ($temp)=keys %record;
360: unless ($temp=~/^error\:/) {
361: my %newrecord=();
362: $newrecord{'resource'}=$feedurl;
363: $newrecord{'subnumber'}=$record{'subnumber'}+1;
364: unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
365: $status.='<br>Not registered<br>';
366: }
367: }
368:
369: return ($status,$sendsomething);
370: }
371:
372: sub adddiscuss {
373: my ($symb,$email,$anon,$attachmenturl)=@_;
374: my $status='';
375: if (&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
376: ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
377:
378: my %contrib=('message' => $email,
379: 'sendername' => $ENV{'user.name'},
380: 'senderdomain' => $ENV{'user.domain'},
381: 'screenname' => $ENV{'environment.screenname'},
382: 'plainname' => $ENV{'environment.firstname'}.' '.
383: $ENV{'environment.middlename'}.' '.
384: $ENV{'environment.lastname'}.' '.
385: $ENV{'enrironment.generation'},
386: 'attachmenturl'=> $attachmenturl);
387: if ($anon) {
388: $contrib{'anonymous'}='true';
389: }
390: if (($symb) && ($email)) {
391: $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
392: &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
393: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
394: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
395: my %storenewentry=($symb => time);
396: $status.='<br>Updating discussion time: '.
397: &Apache::lonnet::put('discussiontimes',\%storenewentry,
398: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
399: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
400: }
401: my %record=&Apache::lonnet::restore('_discussion');
402: my ($temp)=keys %record;
403: unless ($temp=~/^error\:/) {
404: my %newrecord=();
405: $newrecord{'resource'}=$symb;
406: $newrecord{'subnumber'}=$record{'subnumber'}+1;
407: $status.='<br>Registering: '.
408: &Apache::lonnet::cstore(\%newrecord,'_discussion');
409: }
410: } else {
411: $status.='Failed.';
412: }
413: return $status.'<br>';
414: }
415:
416: # ----------------------------------------------------------- Preview function
417:
418: sub show_preview {
419: my $r=shift;
420: my $message=&clear_out_html($ENV{'form.comment'});
421: $message=~s/\n/\<br \/\>/g;
422: $message=&Apache::lontexconvert::msgtexconverted($message);
423: $r->print('<table border="2"><tr><td>'.
424: $message.'</td></tr></table>');
425: }
426:
427: sub generate_preview_button {
428: return(<<ENDPREVIEW);
429: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
430: <input type="hidden" name="comment" />
431: <input type="button" value="Show Preview"
432: onClick="this.form.comment.value=document.mailform.comment.value;this.form.submit();" />
433: </form>
434: ENDPREVIEW
435: }
436: sub handler {
437: my $r = shift;
438: if ($r->header_only) {
439: $r->content_type('text/html');
440: $r->send_http_header;
441: return OK;
442: }
443:
444: # --------------------------- Get query string for limited number of parameters
445:
446: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
447: ['hide','unhide','deldisc','postdata','preview']);
448:
449: if (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
450: # ----------------------------------------------------------------- Hide/unhide
451: $r->content_type('text/html');
452: $r->send_http_header;
453:
454: my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
455:
456: my ($symb,$idx)=split(/\:\:\:/,$entry);
457: my ($map,$ind,$url)=split(/\_\_\_/,$symb);
458:
459: my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
460: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
461: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
462:
463:
464: my $currenthidden=$contrib{'hidden'};
465:
466: if ($ENV{'form.hide'}) {
467: $currenthidden.='.'.$idx.'.';
468: } else {
469: $currenthidden=~s/\.$idx\.//g;
470: }
471: my %newhash=('hidden' => $currenthidden);
472:
473: &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
474: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
475: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
476:
477: &redirect_back($r,&Apache::lonnet::clutter($url),
478: 'Changed discussion status<p>','0','0');
479: } elsif ($ENV{'form.deldisc'}) {
480: # --------------------------------------------------------------- Hide for good
481: $r->content_type('text/html');
482: $r->send_http_header;
483:
484: my $entry=$ENV{'form.deldisc'};
485:
486: my ($symb,$idx)=split(/\:\:\:/,$entry);
487: my ($map,$ind,$url)=split(/\_\_\_/,$symb);
488:
489: my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
490: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
491: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
492:
493:
494: my $currentdeleted=$contrib{'deleted'};
495:
496: $currentdeleted.='.'.$idx.'.';
497:
498: my %newhash=('deleted' => $currentdeleted);
499:
500: &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
501: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
502: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
503:
504: &redirect_back($r,&Apache::lonnet::clutter($url),
505: 'Changed discussion status<p>','0','0');
506: } elsif ($ENV{'form.preview'}) {
507: # -------------------------------------------------------- User wants a preview
508: &show_preview($r);
509: } else {
510: # ------------------------------------------------------------- Normal feedback
511: my $feedurl=$ENV{'form.postdata'};
512: $feedurl=~s/^http\:\/\///;
513: $feedurl=~s/^$ENV{'SERVER_NAME'}//;
514: $feedurl=~s/^$ENV{'HTTP_HOST'}//;
515:
516: my $symb=&Apache::lonnet::symbread($feedurl);
517: unless ($symb) {
518: $symb=$ENV{'form.symb'};
519: if ($symb) {
520: my ($map,$id,$url)=split(/\_\_\_/,$symb);
521: $feedurl=&Apache::lonnet::clutter($url);
522: }
523: }
524: my $goahead=1;
525: if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
526: unless ($symb) { $goahead=0; }
527: }
528:
529: if ($goahead) {
530: # Go ahead with feedback, no ambiguous reference
531: $r->content_type('text/html');
532: $r->send_http_header;
533:
534: if (
535: (
536: ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
537: )
538: ||
539: ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
540: ||
541: ($ENV{'request.course.id'} && ($symb=~/^bulletin\_\_\_/))
542: ) {
543: # --------------------------------------------------- Print login screen header
544: unless ($ENV{'form.sendit'}) {
545: my $options=&screen_header($feedurl);
546: if ($options) {
547: &mail_screen($r,$feedurl,$options);
548: } else {
549: &fail_redirect($r,$feedurl);
550: }
551: } else {
552:
553: # Get previous user input
554: my $prevattempts=&Apache::loncommon::get_previous_attempt(
555: $symb,$ENV{'user.name'},$ENV{'user.domain'},
556: $ENV{'request.course.id'});
557:
558: # Get output from resource
559: my $usersaw=&resource_output($feedurl);
560:
561: # Get resource answer
562: my $useranswer=&Apache::loncommon::get_student_answers(
563: $symb,$ENV{'user.name'},$ENV{'user.domain'},
564: $ENV{'request.course.id'});
565: # Get attachments, if any, and not too large
566: my $attachmenturl='';
567: if ($ENV{'form.attachment.filename'}) {
568: unless (length($ENV{'form.attachment'})>131072) {
569: $attachmenturl=&Apache::lonnet::userfileupload('attachment');
570: }
571: }
572: # Filter HTML out of message (could be nasty)
573: my $message=&clear_out_html($ENV{'form.comment'});
574:
575: # Assemble email
576: my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
577: $usersaw,$useranswer);
578:
579: # Who gets this?
580: my ($typestyle,%to) = &decide_receiver($feedurl);
581:
582: # Actually send mail
583: my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
584: $attachmenturl,%to);
585:
586: # Discussion? Store that.
587:
588: my $numpost=0;
589: if ($ENV{'form.discuss'}) {
590: $typestyle.=&adddiscuss($symb,$message,0,$attachmenturl);
591: $numpost++;
592: }
593:
594: if ($ENV{'form.anondiscuss'}) {
595: $typestyle.=&adddiscuss($symb,$message,1,$attachmenturl);
596: $numpost++;
597: }
598:
599:
600: # Receipt screen and redirect back to where came from
601: &redirect_back($r,$feedurl,$typestyle,$numsent,$numpost,$status);
602:
603: }
604: } else {
605: # Unable to give feedback
606: &no_redirect_back($r,$feedurl);
607: }
608: } else {
609: # Ambiguous Problem Resource
610: $r->internal_redirect('/adm/ambiguous');
611: }
612: }
613: return OK;
614: }
615:
616: 1;
617: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>