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