Annotation of loncom/interface/lonpdfupload.pm, revision 1.12

1.1       onken       1: # The LearningOnline Network with CAPA
1.12    ! bisitz      2: # PDF Form Upload Handler
1.1       onken       3: #
1.12    ! bisitz      4: # $Id: lonpdfupload.pm,v 1.11 2009/05/23 05:26:15 onken Exp $
1.1       onken       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: package Apache::lonpdfupload;
                     29: 
                     30: use lib '/home/httpd/lib/perl';
                     31: use Apache::Constants qw(:common :http);
                     32: use LONCAPA;
                     33: use LONCAPA::loncgi;
                     34: use File::Path;
                     35: use File::Basename;
                     36: use File::Copy;
                     37: use IO::File;
                     38: use Image::Magick;
                     39: use Apache::lonacc;
                     40: use Apache::lonxml;
                     41: use Apache::lonhtmlcommon();
                     42: use Apache::lonnet;
                     43: use Apache::loncommon();
                     44: use Apache::lonlocal;
                     45: use Apache::lonmsg();
                     46: use Apache::lonhomework;
                     47: use LONCAPA::Enrollment;
                     48: use LONCAPA::Configuration;
1.2       onken      49: use CAM::PDF;
1.1       onken      50: 
                     51: use strict;
                     52: 
                     53: sub handler() {
1.2       onken      54:     my $r = shift;
                     55: 
                     56:     # check user permissions 
                     57:     if(!&checkpermission($r)) {
                     58:         # stop processing 
                     59:         return OK;
                     60:     }
                     61: 
                     62:     $Apache::lonxml::request=$r;
                     63:     $Apache::lonxml::debug=$env{'user.debug'};
                     64: 
                     65:     $env{'request.uri'}=$r->uri;
                     66:     $r->content_type('text/html');
                     67:     $r->send_http_header();
1.9       raeburn    68:     $r->print(&Apache::loncommon::start_page('Upload PDF Form'));
1.2       onken      69: 
                     70:     #load post data into environment
                     71:     &Apache::lonacc::get_posted_cgi($r);
                     72: 
                     73:     # if a file was upload
                     74:     if($env{'form.Uploaded'} && $env{'form.file'}) {
                     75:         $r->print(&processPDF);
                     76:     } else { 
                     77:         # print upload form
                     78:         $r->print(&get_javascripts);
                     79:         $r->print(&get_uploadform);
                     80:     }
                     81: 
                     82:     #link to course-content
1.6       bisitz     83:     $r->print('<hr />'
                     84:              .'<p>'."\n"
1.5       bisitz     85:              .'<a href="/adm/navmaps">'."\n"
                     86:              .&mt("Navigate Contents")."\n"
                     87:              .'</a>'."\n"
                     88:              .'</p>'."\n"
                     89:     );
1.1       onken      90: 
1.2       onken      91:     #&dumpenv($r); #debug -> prints the environment
1.7       onken      92:     $r->print(&Apache::loncommon::end_page());
1.1       onken      93:     return OK;
1.2       onken      94: }
1.1       onken      95: 
                     96: 
                     97: sub checkpermission() {
                     98:     my $r = shift;
                     99:     if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                    100:         my $result  = <<END
                    101: Content-type: text/html
                    102: 
                    103: <html>
1.2       onken     104:   <head>
                    105:     <title>
                    106:       Bad Cookie
                    107:     </title>
                    108:   </head>
                    109:   <body>
                    110:     Your cookie information is incorrect.
                    111:   </body>
1.1       onken     112: </html>
                    113: END
                    114: ;
                    115:         $r->print($result);
                    116:         return 0;
                    117:     } else {
                    118:         return 1;
                    119:     }
                    120: }
                    121: 
                    122: 
                    123: sub get_javascripts() {
1.2       onken     124:     
1.6       bisitz    125:     my $message = &mt('Please choose a PDF-File.');
1.1       onken     126: 
1.2       onken     127:     # simple test if the upload ends with ".pdf"
                    128:     # it's only for giving a message to the user
                    129:     my $result .= <<END
                    130:   <script type="text/javascript">
1.1       onken     131:     function checkFilename(form) {
                    132:         var fileExt = form.file.value;
                    133:         fileExt = fileExt.match(/[.]pdf\$/g);
                    134:         if(fileExt) {
                    135:             return true;
                    136:         }
1.2       onken     137:         alert("$message");
1.1       onken     138:         return false;
                    139:     }
1.2       onken     140:   </script>
1.1       onken     141: END
                    142: ;
                    143:     return $result; 
                    144: }
                    145: 
1.2       onken     146: 
1.1       onken     147: sub get_uploadform() {
1.4       onken     148:     
                    149:     my %lt = &Apache::lonlocal::texthash(
1.6       bisitz    150:                  'title'  => 'Upload a PDF Form with filled Form Fields', 
                    151:                  'chFile' => 'File',
                    152:                  'submit' => 'Upload',
1.4       onken     153:              );
                    154: 
1.5       bisitz    155:     my $result = 
                    156:         '<br />'
                    157:        .'<form method="post" enctype="multipart/form-data" onsubmit="return checkFilename(this);">'
                    158:        .'<input type="hidden" name="type" value="upload" />'
                    159:        .&Apache::lonhtmlcommon::start_pick_box()
                    160:        .&Apache::lonhtmlcommon::row_headline()
                    161:        .'<h2>'.$lt{'title'}.'</h2>'
                    162:        .&Apache::lonhtmlcommon::row_closure()
                    163:        .&Apache::lonhtmlcommon::row_title($lt{'chFile'})
                    164:        .'<input type="file" name="file" id="filename" />'
                    165:        .&Apache::lonhtmlcommon::row_closure(1)
                    166:        .&Apache::lonhtmlcommon::end_pick_box()
                    167:        .'<input type="submit" name="Uploaded" value="'.$lt{'submit'}.'" />'
                    168:        .'</form>'
1.6       bisitz    169:        .'<br />';
1.5       bisitz    170: 
1.1       onken     171:   return $result;
                    172: }
                    173: 
                    174: sub processPDF {
1.2       onken     175:     my $result = ();  # message for Browser
                    176:     my @pdfdata = (); # answers from PDF-Forms
1.1       onken     177:     
1.2       onken     178:     @pdfdata = &get_pdf_data(); # get answers from PDF-Form
1.1       onken     179:     
                    180:     if (scalar @pdfdata) {    
1.2       onken     181:         &grade_pdf(@pdfdata);
1.1       onken     182:     } else {
1.6       bisitz    183:         $result .= '<p class="LC_error">'
                    184:                   .&mt("Can't find any valid PDF formfields.")
                    185:                   .'</p>';
1.1       onken     186:     }
                    187: }
                    188: 
                    189: sub get_pdf_data() {
                    190:     my @data = ();
1.2       onken     191:     my $pdf = CAM::PDF->new($env{'form.file'});
                    192: 
                    193:     my @formFields = $pdf->getFormFieldList(); #get names of formfields
1.1       onken     194:     
1.2       onken     195:     foreach my $field (@formFields) {
                    196: 	my $dict = $pdf->getFormFieldDict($pdf->getFormField($field)); # get formfield dictonary
1.1       onken     197: 
1.2       onken     198:         #
                    199:         # this is nessesary 'cause CAM::PDF has a problem with formfieldnames which include a
                    200:         # dot in fieldnames. So a fieldname like "i.am.aFormfield" will offer three fieldnames "i", "i.am" 
                    201:         # and "i.am.aFormfield". The fragmentary names keep no values and will be ignored.
                    202:         if($dict->{'V'}) {
                    203:             push(@data, $field."?". $dict->{'V'}{'value'}); #binding fieldname with value
                    204:         }
                    205:     } 
1.1       onken     206:     return @data;
                    207: }
                    208: 
                    209: sub grade_pdf {
                    210:     my $result = ();
                    211:     my @pdfdata = @_;
                    212:    
                    213:     my $meta = ();
                    214:     my %grades = ();
                    215:     my %problems = ();
                    216:         
                    217:     my $debug = ();
                    218: 
                    219:     $debug  .= "Found: ". scalar @pdfdata." Entries \n";
1.4       onken     220: 
1.1       onken     221:     foreach my $entry (sort(@pdfdata)) {
                    222:         if ($entry =~ /^meta.*/) {
1.2       onken     223:             $debug .= 'found: metadata -> '.$entry . "<br />";
                    224:             my ($label, $value) = ($entry =~ /^([^?]*)\?(.*)/);
1.1       onken     225:             my ($domain, $user) = split('&', $value);
1.4       onken     226:             $user =~ s/(.*)\n/$1/; #TODO is that equals to chomp?
1.1       onken     227:             
                    228:             if($user ne $env{'user.name'} or  $domain ne $env{'user.domain'}) {
1.12    ! bisitz    229:                 return '<p class="LC_error">'
        !           230:                       .&mt('Wrong username ([_1]) found in PDF file. Expected username: [_2]'
        !           231:                           ,$user.':'.$domain
        !           232:                           ,$env{'user.domain'}.':'.$env{'user.name'})
        !           233:                       .'</p>';
1.1       onken     234:             }
                    235: 
                    236:         } elsif($entry =~ /^upload.*/)  {
                    237:             $debug .= 'found: a problem -> '.$entry;
1.2       onken     238:             my ($label, $value) = ($entry =~ /^([^?]*)\?(.*)/);
1.1       onken     239:             my ($symb, $part, $type, $HWVAL) = split('&', $label);
                    240:             my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symb);  
                    241:             $value =~ s/(.*)\n/$1/; 
                    242: 
1.6       bisitz    243:             #filter incorrect radiobuttons (Bug in CABAReT Stage)
1.1       onken     244:             if($type eq 'radiobuttonresponse' && $value eq 'Off' ) {
                    245:                 next;
                    246:             }
                    247:  
                    248:             my $submit = $part;
                    249:             $submit =~ s/part_(.*)/submit_$1/;
                    250:             if($problems{$symb.$part}) {
                    251:                  $problems{$symb.$part}{$HWVAL} = $value;
                    252:             } else {
                    253:                  $problems{$symb.$part} =  { 'resource' => $resource,
1.11      onken     254:                                         'symb' => $symb,
1.1       onken     255:                                         'submitted' => $part,
                    256:                                         $submit => 'Answer',
                    257:                                         $HWVAL => $value};
                    258:             }
                    259:         } else {
                    260:             $debug .= 'found: -> '.$entry;
                    261:             next;
                    262:         }
                    263:     }
1.4       onken     264:     #$result .= $debug;
1.1       onken     265: 
1.12    ! bisitz    266:     $result .= '<h2>'.&mt('Results of PDF Form problems').'</h2>';
        !           267:     $result .= &Apache::loncommon::start_data_table()
        !           268:               .&Apache::loncommon::start_data_table_header_row()
        !           269:               .'<th>'.&mt('Problem Name').'</th>'
        !           270:               .'<th>'.&mt('Grading').'</th>'
        !           271:               .&Apache::loncommon::start_data_table_header_row()
        !           272:               .&Apache::loncommon::end_data_table_header_row();
        !           273: 
1.1       onken     274:     foreach my $key (sort (keys %problems)) {
                    275:         my %problem = %{$problems{$key}};
                    276:         my ($problemname, $grade) = &grade_problem(%problem);
1.4       onken     277: 
                    278:         $result .= &Apache::loncommon::start_data_table_row();
                    279:         $result .= "<td>$problemname</td><td class='";
1.8       onken     280:         if($grade eq "EXACT_ANS" || $grade eq "APPROX_ANS") {
1.4       onken     281:             $result .= "LC_answer_correct";
1.1       onken     282:         } else { 
1.4       onken     283:             $result .= "LC_answer_charged_try";
1.1       onken     284:         }
1.8       onken     285:         $grade = &parse_grade_answer($grade);
1.4       onken     286:         $result .= "'>$grade</span></td>";
                    287:         $result .= &Apache::loncommon::end_data_table_row();
                    288:     }
                    289:     $result .= &Apache::loncommon::end_data_table();
1.1       onken     290: 
                    291: 
                    292:     return $result;        
                    293: }
                    294: 
                    295: sub grade_problem {
                    296:     my %problem = @_;
1.7       onken     297:     my ($title, $part) = ();
1.1       onken     298: 
1.7       onken     299:     &Apache::loncommon::ssi_with_retries('/res/'.$problem{'resource'}, 5, %problem);
1.1       onken     300: 
1.7       onken     301:     $title = &Apache::lonnet::gettitle($problem{'symb'});    
                    302:     $part = $problem{submitted};
1.1       onken     303:     $part =~ s/part_(.*)/$1/;
1.7       onken     304:     unless($part eq '0') {
                    305:         #add information about part number
                    306:         $title .= " - Part $part";
                    307:     }
1.1       onken     308:  
                    309:     my %problemhash = &Apache::lonnet::restore($problem{'symb'});
                    310:     my $grade = $problemhash{"resource.$part.award"};
                    311: 
1.7       onken     312:     return ($title, $grade);    
1.1       onken     313: }
                    314: 
1.8       onken     315: sub parse_grade_answer {
                    316:     my ($shortcut) = @_;
                    317:      my %answerhash = ('EXACT_ANS' => &mt('You are correct.'),
1.10      onken     318:                        'APPROX_ANS' => &mt('You are correct.'),
1.8       onken     319:                        'INCORRECT' => &mt('You are incorrect'),
                    320:      );
                    321: 
                    322:     foreach my $key (keys %answerhash) {
                    323:         if($shortcut eq $key) {
                    324:             return $answerhash{$shortcut};
                    325:         }  
                    326:     }
                    327:     return &mt('See course contents for further information.');
                    328: 
                    329: }
                    330: 
                    331: 
1.1       onken     332: sub dumpenv  {
                    333:     my $r = shift;
                    334: 
                    335:     $r->print ("<br />-------------------<br />");
                    336:     foreach my $key (sort (keys %env)) {
                    337:         $r->print ("<br />$key -> $env{$key}");
                    338:     }
                    339:     $r->print ("<br />-------------------<br />");
                    340:     $r->print ("<br />-------------------<br />");
                    341:     foreach my $key (sort (keys %ENV)) {
                    342:         $r->print ("<br />$key -> $ENV{$key}");
                    343:     }
                    344:     $r->print ("<br />-------------------<br />");
                    345:     
                    346: }	
                    347: 
                    348: 1;
                    349: __END__
                    350: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>