File:  [LON-CAPA] / loncom / interface / lonsupportreq.pm
Revision 1.63: download - view: text, annotated - select for diffs
Wed Mar 2 22:43:42 2011 UTC (13 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Scope.

    1: #
    2: # $Id: lonsupportreq.pm,v 1.63 2011/03/02 22:43:42 raeburn Exp $
    3: #
    4: # Copyright Michigan State University Board of Trustees
    5: #
    6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    7: #
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: 
   27: package Apache::lonsupportreq;
   28: 
   29: use strict;
   30: use MIME::Types;
   31: use MIME::Lite;
   32: use CGI::Cookie();
   33: use Apache::Constants qw(:common);
   34: use Apache::loncommon();
   35: use Apache::lonhtmlcommon;
   36: use Apache::lonnet;
   37: use Apache::lonlocal;
   38: use Apache::lonacc();
   39: use Apache::courseclassifier;
   40: use LONCAPA qw(:DEFAULT :match);
   41:  
   42: 
   43: sub handler {
   44:     my ($r) = @_;
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     $r->send_http_header;
   47: 
   48:     if ($r->header_only) {
   49:         return OK;
   50:     }
   51:     if ($r->uri eq '/adm/helpdesk') {
   52: 	&Apache::lonlocal::get_language_handle($r);
   53:     }
   54: 
   55:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['origurl','function']);
   56:     if ($r->uri eq '/adm/helpdesk') {
   57:         &Apache::lonacc::get_posted_cgi($r);
   58:     }
   59:     my $function = $env{'form.function'};
   60:     my $origurl = $env{'form.origurl'};
   61:     my $command = $env{'form.command'};
   62: 
   63:     if ($command eq 'process') {
   64:         &print_request_receipt($r,$origurl,$function);
   65:     } else {
   66:         &print_request_form($r,$origurl,$function);
   67:     }
   68:     return OK;
   69: }
   70:     
   71: sub print_request_form {
   72:     my ($r,$origurl,$function) = @_;
   73:     my ($os,$browser,$bversion,$uhost,$uname,$udom,$uhome,$urole,$usec,$email,$cid,
   74:         $cdom,$cnum,$ctitle,$ccode,$sectionlist,$lastname,$firstname,$server,
   75:         $formname,$public);
   76:     $function = &Apache::loncommon::get_users_function() if (!$function);
   77:     $ccode = '';
   78:     $os = $env{'browser.os'};
   79:     $browser = $env{'browser.type'};
   80:     $bversion = $env{'browser.version'};
   81:     $uhost = $env{'request.host'};
   82:     if (($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public')) {
   83:         $public = 1;
   84:     } else {
   85:         $uname = $env{'user.name'};
   86:         $udom = $env{'user.domain'};
   87:     }
   88:     $uhome = $env{'user.home'};
   89:     $urole = $env{'request.role'};
   90:     $usec = $env{'request.course.sec'};
   91:     $cid = $env{'request.course.id'};
   92:     $formname = 'logproblem';
   93:     my $machine = &Apache::lonnet::absolute_url();
   94:     if ($origurl =~ m-^https?://-) {
   95:         $server = $origurl;
   96:     } else {
   97:         $server = $machine.$origurl;
   98:     }
   99:     my $showserver = $server;
  100:     $showserver =~ s/\?.*$//;
  101:     my %lt = &Apache::lonlocal::texthash (
  102:                   email => 'The e-mail address you entered',
  103:                   notv => 'is not a valid e-mail address',
  104:                   rsub => 'You must include a subject',
  105:                   rdes => 'You must include a description',
  106:                   name => 'Name',
  107:                   subm => 'Submit Request',
  108:                   emad => 'Your e-mail address',
  109:                   emac => 'Cc', 
  110:                   unme => 'username',
  111:                   doma => 'domain',
  112:                   entu => 'Enter the username you use to log-in to LON-CAPA',
  113:                   chdo => 'Choose your LON-CAPA domain',
  114:                   entr => 'Enter the username you use to log-in to LON-CAPA, and your domain.',
  115:                   urlp => 'URL of page',
  116:                   phon => 'Phone',
  117:                   crsd => 'Course Details',
  118:                   enin => 'Enter institutional course code',
  119:                   pick => 'Pick',
  120:                   enct => 'Enter course title',
  121:                   secn => 'Section Number',
  122:                   sele => 'Select',
  123:                   titl => 'Title',
  124:                   lsec => 'LON-CAPA sec',
  125:                   subj => 'Subject',
  126:                   detd => 'Detailed Description',
  127:                   opfi => 'Optional file upload',
  128:                   uplf => 'Upload a file (e.g., a screenshot) relevant to your help request (128 KB max.)',
  129:                   fini => 'Finish',
  130:                   clfm => 'Clear Form',
  131:     );
  132:     my $scripttag = (<<"END");
  133: function validate() {
  134:     if (validmail(document.logproblem.email) == false) {
  135:         alert("$lt{'email'}: "+document.logproblem.email.value+" $lt{'notv'}.");
  136:         return;
  137:     }
  138:     if (document.logproblem.subject.value == '') {
  139:         alert("$lt{'rsub'}.");
  140:         return;
  141:     }
  142:     if (document.logproblem.description.value == '') {
  143:         alert("$lt{'rdes'}.");
  144:         return;
  145:     }
  146:     document.logproblem.submit();
  147: }
  148: 
  149: END
  150:     $scripttag .= &Apache::lonhtmlcommon::javascript_valid_email();
  151:     if ($cid) {
  152:         $cdom = $env{'course.'.$cid.'.domain'};
  153:         $cnum = $env{'course.'.$cid.'.num'};
  154:     }
  155:     if ($cdom && $cnum) {
  156:         my %csettings = &Apache::lonnet::get('environment',['description','internal.coursecode','internal.sectionnums'],$cdom,$cnum);
  157:         $ctitle = $csettings{'description'};
  158:         $ccode = $csettings{'internal.coursecode'};
  159:         $sectionlist = $csettings{'internal.sectionnums'};
  160:     }
  161: 
  162:     if ($env{'environment.permanentemail'}) {
  163:         $email = $env{'environment.permanentemail'};
  164:     } elsif ($env{'environment.critnotification'}) {
  165:         $email = $env{'environment.critnotification'};
  166:     } elsif ($env{'environment.notification'}) {
  167:         $email = $env{'environment.notification'};
  168:     }
  169:     if ($env{'environment.lastname'}) {
  170:         $lastname = $env{'environment.lastname'};
  171:     }
  172:     if ($env{'environment.firstname'}) {
  173:         $firstname = $env{'environment.firstname'};
  174:     }
  175:     my @sections = split(/,/,$sectionlist);
  176:     my %groupid;
  177:     foreach my $section (@sections) {
  178:         my ($sec,$grp) = split(/:/,$section);
  179:         $groupid{$sec} = $grp;
  180:     }
  181:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['codedom',
  182:                                                  'useremail','useraccount']);
  183:     if ($env{'form.origurl'} eq '/adm/createaccount') {
  184:         if ($email eq '') {
  185:             if ($env{'form.useremail'} =~ /^[^\@]+\@[^\@]+$/) {
  186:                 $email = &HTML::Entities::encode($env{'form.useremail'},'"<>&');
  187:             }
  188:         }
  189:         if ($uname eq '') {
  190:             if ($env{'form.useraccount'} =~ /^$match_username$/) {
  191:                 $uname = &HTML::Entities::encode($env{'form.useraccount'},'"<>&');
  192:             }
  193:         }
  194:     }
  195:     my $codedom = &get_domain();
  196:     my $details_title;
  197:     if ($codedom) {
  198:         $details_title = '<br />('.$codedom.')';
  199:     }
  200:     my %coursecodes;
  201:     my %codes;
  202:     my @codetitles;
  203:     my %cat_titles;
  204:     my %cat_order;
  205:     my %idlist;
  206:     my %idnums;
  207:     my %idlist_titles;
  208:     my $caller = 'global';
  209:     my $totcodes = 0;
  210:     my $format_reply;
  211:     my $jscript = '';
  212:     my $loaditems = qq|
  213: function initialize_codes() {
  214:     return;
  215: }
  216:     |;
  217:     if ($cnum) {
  218:         $coursecodes{$cnum} = $ccode;
  219:         if ($ccode eq '') {
  220:             $totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom,$totcodes);
  221:         } else {
  222:             $coursecodes{$cnum} = $ccode;
  223:             $caller = $cnum;
  224:             $totcodes ++;
  225:         }
  226:     } else { 
  227:         $totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom,$totcodes);
  228:     }
  229:     if ($totcodes > 0) {
  230:         if ($ccode eq '') {
  231:             $format_reply = &Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
  232:             if ($format_reply eq 'ok') {
  233:                 my $numtypes = @codetitles;
  234:                 &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
  235:                 my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
  236:                 my $longtitles_str = join('","',@{$longtitles});
  237:                 my $allidlist = $idlist{$codetitles[0]};
  238:                 $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
  239:                 $jscript .= $scripttext;
  240:                 $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
  241:                 $loaditems = '';
  242:             }
  243:         }
  244:     }
  245: 	
  246:     my $js = '<script type="text/javascript">'."\n$scripttag\n$jscript\n".
  247: 	     $loaditems.'</script>';
  248:     my %add_entries = (topmargin    => "0",
  249: 		       marginheight => "0",
  250: 		       onLoad       =>"initialize_codes()",);
  251: 
  252:     
  253:     $r->print(&Apache::loncommon::start_page('Support Request',$js,
  254: 				       { 'function'    => $function,
  255: 					 'add_entries' => \%add_entries,
  256: 					 'only_body'   => 1,}));
  257:     if ($r->uri eq '/adm/helpdesk') {
  258:         &print_header($r,$origurl);
  259:     }
  260:     my @css = ('LC_evenrow_value','LC_oddrow_value');
  261:     my $num = 1;
  262:     my $i = $num%2;
  263:     $r->print('<form method="post" name="logproblem" enctype="multipart/form-data">'."\n");
  264:     my $output = &Apache::lonhtmlcommon::start_pick_box().
  265:                  &Apache::lonhtmlcommon::row_title($lt{'name'},undef,$css[$num])."\n";
  266:     my $fullname = '';
  267:     if ((defined($lastname) && $lastname ne '') && (defined($firstname) && $firstname ne '')) {
  268:         $fullname = "$firstname $lastname"; 
  269:         $output .= $fullname.'<input type="hidden" name="username" value="'.&HTML::Entities::encode($fullname,'"<>&').'" />';
  270:     } else {
  271:         if (defined($firstname) && $firstname ne '') {
  272:             $fullname = $firstname;
  273:         } elsif (defined($lastname) && $lastname ne '') {
  274:             $fullname = " $lastname";
  275:         }
  276:         $output .= '<input type="text" size="20" name="username" value="'.&HTML::Entities::encode($fullname,'"<>&').'" />';
  277:     }
  278:     $output .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="'.$lt{'subm'}.'" onclick="validate()" />&nbsp;'.
  279:                 &Apache::lonhtmlcommon::row_closure()."\n";
  280:     $num ++;
  281:     $i = $num%2;
  282:     $output .= &Apache::lonhtmlcommon::row_title($lt{'emad'},undef,$css[$i]).
  283:                '<input type="text" size="20" name="email" value="'.
  284:                &HTML::Entities::encode($email,'"<>&').'" /><br />'."\n".
  285:                &Apache::lonhtmlcommon::row_closure();
  286:     $num ++;
  287:     $i = $num%2;
  288:     if (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
  289:         $output .= &Apache::lonhtmlcommon::row_title($lt{'emac'},undef,$css[$i]).
  290:                    '<input type="text" size="50" name="cc" value="" /><br />'."\n".
  291:                    &Apache::lonhtmlcommon::row_closure();
  292:         $num ++;
  293:         $i = $num%2;
  294:     }
  295:     $output .= &Apache::lonhtmlcommon::row_title("$lt{'unme'}/$lt{'doma'}",undef,$css[$i]);
  296:     my $udom_input = '<input type="hidden" name="udom" value="'.
  297:                      &HTML::Entities::encode($udom,'"<>&').'" />';
  298:     my $uname_input = '<input type="hidden" name="uname" value="'.
  299:                       &HTML::Entities::encode($uname,'"<>&').'" />'; 
  300:     if (($env{'user.name'} =~ /^$match_username$/) && 
  301:         ($env{'user.domain'} =~ /^$match_domain$/) && (!$public)) {
  302:         $output .= '<i>'.$lt{'unme'}.'</i>:&nbsp;'.$uname.'&nbsp;&nbsp;<i>'.$lt{'doma'}.'</i>:&nbsp;'.$udom.$udom_input.$uname_input;
  303:     } else {
  304:         my $udomform = '';
  305:         my $unameform = '';
  306:         if (($env{'user.domain'} =~ /^$match_domain$/) && (!$public)) {
  307:             $output .= $lt{'entu'};
  308:         } elsif (($env{'user.name'} =~ /^$match_username$/) && (!$public)) { 
  309:             $output .= $lt{'chdo'};
  310:         } else {
  311:             $output .= $lt{'entr'};
  312:         }
  313:         $output .= '<br />';
  314:         if (!$public) {
  315:             if ($env{'user.domain'} =~ /^$match_domain$/) {
  316:                 $udomform = '<i>'.$lt{'doma'}.'</i>:&nbsp;'.$udom.$udom_input;
  317:             } elsif ($env{'user.name'} =~ /^$match_username$/) {
  318:                 $unameform = '<i>'.$lt{'unme'}.'</i>:&nbsp;'.$uname.'&nbsp;&nbsp;'.$uname_input;
  319:             }
  320:         }
  321:         if ($udomform eq '') {
  322:             $udomform = '<i>'.$lt{'doma'}.'</i>:&nbsp;';
  323:             $udomform .= &Apache::loncommon::select_dom_form($codedom,'udom');
  324:         }
  325:         if ($unameform eq '') {
  326:             $unameform= '<i>'.$lt{'unme'}.'</i>:&nbsp;<input type="text" size="20" name="uname" value="'.$uname.'" />&nbsp;&nbsp;';
  327:         }
  328:         $output .= $unameform.$udomform;
  329:     }
  330:     $output .= &Apache::lonhtmlcommon::row_closure();
  331:     $num ++;
  332:     $i = $num%2;
  333:     $output .= &Apache::lonhtmlcommon::row_title("$lt{'urlp'}",undef,$css[$i]).
  334:                $showserver.'<input type="hidden" name="sourceurl" value="'.
  335:                &HTML::Entities::encode($server,'"<>&').'" />'.
  336:                &Apache::lonhtmlcommon::row_closure().
  337:                &Apache::lonhtmlcommon::row_title("$lt{'phon'}",undef,'LC_evenrow_value').
  338:                '<input type="text" size="15" name="phone" /><br />'.
  339:                &Apache::lonhtmlcommon::row_closure();
  340:     $num ++;
  341:     $i = $num%2; 
  342:     $output .= &Apache::lonhtmlcommon::row_title("$lt{'crsd'}$details_title",undef,$css[$i]);
  343:     if ($cnum) {
  344:         if ($coursecodes{$cnum}) {
  345:             foreach my $item (@codetitles) {
  346:                 $output .= '<i>'.$item.'</i>:&nbsp;'.$codes{$cnum}{$item}.';&nbsp;';
  347:             }
  348:             $output .= '&nbsp;<input type="hidden" name="coursecode" value="'.&HTML::Entities::encode($coursecodes{$cnum},'"<>&').'" />';
  349:         } else {
  350:             $output .= $lt{'enin'}.':&nbsp;
  351:                   <input type="text" name="coursecode" size="15" value="" />';
  352:         }
  353:     } else {
  354:         if ($totcodes > 0) {
  355:             my $numtitles = @codetitles;
  356:             if ($numtitles == 0) {
  357:                 $output .= $lt{'enin'}.':&nbsp;
  358:                   <input type="text" name="coursecode" size="15" value="" />';
  359:             } else {
  360:                 my @standardnames = &Apache::loncommon::get_standard_codeitems();
  361:                 my $lasttitle = $numtitles;
  362:                 if ($numtitles > 4) {
  363:                     $lasttitle = 4;
  364:                 } 
  365:                 $output .= '<table><tr><td>'.$codetitles[0].'<br />'."\n".
  366:                       '<select name="'.$standardnames[0].'" onchange="courseSet('."'$codetitles[0]'".')">'."\n".
  367:                       ' <option value="-1" />'.$lt{'sele'}."\n";
  368:                 my @items = ();
  369:                 my @longitems = ();
  370:                 if ($idlist{$codetitles[0]} =~ /","/) {
  371:                     @items = split(/","/,$idlist{$codetitles[0]});
  372:                 } else {
  373:                     $items[0] = $idlist{$codetitles[0]};
  374:                 }
  375:                 if (defined($idlist_titles{$codetitles[0]})) {
  376:                     if ($idlist_titles{$codetitles[0]} =~ /","/) {
  377:                         @longitems = split(/","/,$idlist_titles{$codetitles[0]});
  378:                     } else {
  379:                         $longitems[0] = $idlist_titles{$codetitles[0]};
  380:                     }
  381:                     for (my $i=0; $i<@longitems; $i++) {
  382:                         if ($longitems[$i] eq '') {
  383:                             $longitems[$i] = $items[$i];
  384:                         }
  385:                     }
  386:                 } else {
  387:                     @longitems = @items;
  388:                 }
  389:                 for (my $i=0; $i<@items; $i++) {
  390:                     $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>'."\n";
  391:                 }
  392:                 $output .= '</select></td>';
  393:                 for (my $i=1; $i<$numtitles; $i++) {
  394:                     $output .= '<td>'.$codetitles[$i].'<br />'."\n".
  395:                      '<select name="'.$standardnames[$i].'" onchange="courseSet('."'$codetitles[$i]'".')">'."\n".
  396:                      '<option value="-1">&lt;-'.$lt{'pick'}.' '.$codetitles[$i-1].'</option>'."\n".
  397:                      '</select>'."\n".
  398:                      '</td>'."\n";
  399:                 }
  400:                 $output .= '</tr></table>';
  401:                 if ($numtitles > 4) {
  402:                     $output .= '<br /><br />'.$codetitles[$numtitles].'<br />'."\n".
  403:                           '<select name="'.$standardnames[$numtitles].'" onchange="courseSet('."'$codetitles[$numtitles]'".')">'."\n".
  404:                           '<option value="-1">&lt;-'.$lt{'pick'}.' '.$codetitles[$numtitles-1].'</option>'."\n".
  405:                           '</select>'."\n";
  406:                 }
  407:             }
  408:         } else {
  409:             $output .= $lt{'enin'}.':&nbsp;
  410:                   <input type="text" name="coursecode" size="15" value="" />';
  411:         }
  412:     }
  413:     if ($ctitle) {
  414:         $output .= '<br /><i>'.$lt{'titl'}.'</i>:&nbsp;'.$ctitle.
  415:                    '<input type="hidden" name="title" value="'.
  416:                    &HTML::Entities::encode($ctitle,'"<>&').'" />'."\n";
  417:     } else {
  418:         $output .= '<br />'.$lt{'enct'}.':&nbsp;
  419:                  <input type="text" name="title" size="25" value="" />'."\n";
  420:     }
  421:     $output .= &Apache::lonhtmlcommon::row_closure();
  422:     $num ++;
  423:     $i = $num%2;
  424:     $output .= &Apache::lonhtmlcommon::row_title($lt{'secn'},undef,$css[$i]);
  425:     if ($sectionlist) {
  426:         $output .= "<select name=\"section\"\n>".
  427:                    "  <option value=\"\" selected=\"selected\">$lt{'sele'}</option>\n";
  428:         foreach my $id (sort(keys(%groupid))) {
  429:             if ($id eq $groupid{$id} || $groupid{$id} eq '') {
  430:                 $output .= "  <option value=".
  431:                            &HTML::Entities::encode($id,'"<>&').
  432:                            " >$id</option>\n";
  433:             } else {
  434:                 $output .= "  <option value=".
  435:                            &HTML::Entities::encode($id,'"<>&').
  436:                            " >$id - ($lt{'lsec'}: $groupid{$id})</option>\n";
  437:             }
  438:         }
  439:         $output .= "</select>";
  440:     } else {
  441:         $output .= '<input type="text" name="section" size="10" />';
  442:     }
  443:     $output .= &Apache::lonhtmlcommon::row_closure();
  444:     $num ++;
  445:     $i = $num%2; 
  446:     $output .= &Apache::lonhtmlcommon::row_title($lt{'subj'},undef,'LC_oddrow_value').
  447:                '  <input type="text" size="40" name="subject" />'."\n".
  448:                &Apache::lonhtmlcommon::row_closure().
  449:                &Apache::lonhtmlcommon::row_title($lt{'detd'},undef,'LC_evenrow_value').
  450:                '  <textarea rows="10" cols="45" name="description" wrap="virtual"></textarea>'.
  451:                &Apache::lonhtmlcommon::row_closure();
  452:     $num ++;
  453:     $i = $num%2; 
  454:     if (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
  455:         $output .= &Apache::lonhtmlcommon::row_title($lt{'opfi'},undef,$css[$i]).
  456:                    ' <input type="file" name="screenshot" size="20" /><br />'.$lt{'uplf'}."\n".
  457:         &Apache::lonhtmlcommon::row_closure();
  458:         $num ++;
  459:         $i = $num%2; 
  460:     }
  461:     $output .= &Apache::lonhtmlcommon::row_title($lt{'fini'},undef,$css[$i]);
  462:     $output .= <<END;
  463:              <table border="0" cellpadding="8" cellspacing="0">
  464:               <tr>
  465:                <td>
  466:                 <input type="hidden" name="command" value="process" />
  467:                 <input type="button" value="$lt{'subm'}" onclick="validate()" /> &nbsp;
  468:                </td>
  469:                <td>&nbsp;</td>
  470:                <td>
  471:                 <input type="reset" value="$lt{'clfm'}" />
  472:                </td>
  473:               </tr>
  474:              </table>
  475: END
  476:     $output .= &Apache::lonhtmlcommon::row_closure(1);
  477:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  478:     $r->print(<<END);
  479: $output
  480: </form>
  481: <br />
  482: END
  483:     $r->print(&Apache::loncommon::end_page());
  484:     return;
  485: }
  486: 
  487: sub print_request_receipt {
  488:     my ($r,$url,$function) = @_;
  489:     my @ENVvars = ('HTTP_HOST','HTTP_USER_AGENT','REMOTE_ADDR','SERVER_ADDR','SERVER_NAME');
  490:     my @envvars = ('browser.os','browser.type','browser.version','user.home','request.role');
  491:     my @loncvars = ('user.name','user.domain','request.course.sec','request.course.id');
  492:     my @cookievars = ('lonID');
  493: 
  494:     my $admin = $Apache::lonnet::perlvar{'lonAdminMail'};
  495:     my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  496:     my $defdom = &get_domain();
  497:     my $to = &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
  498:                                                       $defdom,$origmail);
  499:     my $from = $admin;
  500:     my $bcc;
  501:     my %domconfig =
  502:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
  503:     if (ref($domconfig{'contacts'}) eq 'HASH') {
  504:         if (exists($domconfig{'contacts'}{'helpdeskmail'})) {
  505:             if (ref($domconfig{'contacts'}{'helpdeskmail'}) eq 'HASH') {
  506:                 my $bccmail = $domconfig{'contacts'}{'helpdeskmail'}{'bcc'};
  507:                 if ($bccmail ne '') {
  508:                     my @bccs = split(/,/,$bccmail);
  509:                     my @ok_bccs;
  510:                     foreach my $bcc (@bccs) {
  511:                         $bcc =~ s/^\s+//g;
  512:                         $bcc =~ s/\s+$//g;
  513:                         if ($bcc =~ m/^[^\@]+\@[^\@]+$/) {
  514:                             if (!(grep(/^\Q$bcc\E$/,@ok_bccs))) {
  515:                                 push(@ok_bccs,$bcc);
  516:                             }
  517:                         }
  518:                     }
  519:                     if (@ok_bccs > 0) {
  520:                         $bcc = join(', ',@ok_bccs);
  521:                     }
  522:                 }
  523:             }
  524:         }
  525:     }
  526:     my $reporttime = &Apache::lonlocal::locallocaltime(time);
  527:     my @formvars = ('username','email','uname','udom','sourceurl','phone','section','coursecode','title','subject','description','screenshot');
  528: 
  529:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},\@formvars);
  530:     my $coursecode = $env{'form.coursecode'};
  531:     if ($coursecode eq '') {
  532:         my $totcodes = 0;
  533:         my %coursecodes;
  534:         $totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$defdom,$totcodes);
  535:         my @standardnames = &Apache::loncommon::get_standard_codeitems();
  536:         if ($totcodes > 0) {
  537:             $coursecode = &Apache::courseclassifier::instcode_from_selectors($defdom); 
  538:         } 
  539:         if ($coursecode eq '') {
  540:             foreach my $item (@standardnames) {
  541:                 if ((defined($env{'form.'.$item})) && ($env{'form.'.$item} ne '-1')) {
  542:                     $coursecode .= $env{'form.'.$item};
  543:                 }
  544:             }
  545:         }
  546:     }
  547:     my %lt = &Apache::lonlocal::texthash (
  548:                  username    => 'Name',
  549:                  email       => 'E-mail',
  550:                  cc          => 'Cc',
  551:                  user        => 'Username/domain',
  552:                  phone       => 'Phone',
  553:                  crsi        => 'Course Information',
  554:                  subject     => 'Subject',
  555:                  description => 'Description',
  556:                  sourceurl   => 'URL',
  557:                  date        => 'Date/Time',
  558:                  secn        => 'Section',
  559:                  asup        => 'A support request has been sent to',
  560:                  warn        => 'Warning: Problem with support e-mail address',
  561:                  your        => 'Your support request contained the following information',
  562:                  sect        => 'section',
  563:                  info        => 'Information supplied',
  564:                  adin        => 'Additional information recorded',
  565:     );
  566: 
  567:     my (@ok_ccs,@bad_ccs,$badccmsg,$okcclist);
  568:     if ((defined($env{'user.name'})) && (defined($env{'form.cc'}))) {
  569:         my @ccs;
  570:         if ($env{'form.cc'} =~ /,/) {
  571:             @ccs = split(/,/,$env{'form.cc'});
  572:         } else {
  573:             $env{'form.cc'} =~ s/^\s+//;
  574:             $env{'form.cc'} =~ s/\s+$//;
  575:             @ccs = $env{'form.cc'};
  576:         }
  577:         foreach my $cc (@ccs) {
  578:             $cc =~ s/^\s+//g;
  579:             $cc =~ s/\s+$//g;
  580:             if ($cc =~ m/^[^\@]+\@[^\@]+$/) {
  581:                 if (!(grep(/^\Q$cc\E$/,@ok_ccs))) {
  582:                     push(@ok_ccs,$cc);
  583:                 }
  584:             } elsif ($cc ne '') {
  585:                 if (!(grep(/^\Q$cc\E$/,@bad_ccs))) {
  586:                     push(@bad_ccs,$cc);
  587:                 }
  588:             }
  589:         }
  590:         if (@ok_ccs > 0) {
  591:            $okcclist = join(', ',@ok_ccs); 
  592:         } 
  593:         if (@bad_ccs == 1) {
  594:             $badccmsg .= '<br />'.&mt('The following Cc e-mail address is invalid: ').$bad_ccs[0];
  595:         } elsif (@bad_ccs > 1) {
  596:             my $bad_cc_string = join(', ',@bad_ccs);
  597:             $badccmsg .= '<br />'.&mt('The following Cc e-mail addresses are invalid: ').$bad_cc_string;
  598:         }
  599:     }
  600:     $env{'form.user'} = "'".$env{'form.uname'}.':'.$env{'form.udom'}."'";
  601:     $env{'form.crsi'} = $env{'form.title'}.' - '.$coursecode.' - '.$lt{'sect'}.': '.$env{'form.section'};
  602:     my $supportmsg = <<END;
  603: $lt{'username'}: $env{'form.username'}
  604: $lt{'email'}: $env{'form.email'}
  605: $lt{'cc'}: $okcclist
  606: $lt{'user'}: $env{'form.user'}
  607: $lt{'phone'}: $env{'form.phone'}
  608: $lt{'crsi'}: $env{'form.crsi'}
  609: $lt{'subject'}: $env{'form.subject'}
  610: $lt{'description'}: $env{'form.description'}
  611: $lt{'sourceurl'}: $env{'form.sourceurl'}
  612: $lt{'date'}: $reporttime
  613: 
  614: END
  615:     my $displaymsg;
  616:     foreach my $item ('username','email','cc','user','phone','crsi','subject','description','sourceurl') {
  617:         if ($env{'form.'.$item} ne '') {
  618:             if ($item eq 'description') {
  619:                 my $descrip = $env{'form.description'};
  620:                 $descrip =  &cleanup_html($descrip);
  621:                 $descrip =~ s|[\n\r\f]|<br />|g;
  622:                 $displaymsg .= 
  623:                     '<span class="LC_helpform_receipt_cat">'.
  624:                     "$lt{$item}</span>: $descrip<br />\n";
  625:             } elsif ($item eq 'sourceurl') {
  626:                 my $showurl = $env{'form.sourceurl'};
  627:                 $showurl =~ s/\?.*$//;
  628:                 $showurl =  &cleanup_html($showurl);
  629:                 $displaymsg .= 
  630:                     '<span class="LC_helpform_receipt_cat">'.
  631:                     "$lt{$item}</span>: $showurl<br />\n";
  632:             } elsif ($item eq 'cc') {
  633:                 $displaymsg .=
  634:                     '<span class="LC_helpform_receipt_cat">'.
  635:                     "$lt{$item}</span>: $okcclist<br />\n";
  636:             } else {
  637:                 my $showitem = $env{'form.'.$item};
  638:                 $showitem = &cleanup_html($showitem);
  639:                 $displaymsg .= 
  640:                     '<span class="LC_helpform_receipt_cat">'.
  641:                     "$lt{$item}</span>: $showitem<br />\n";
  642:             }
  643:         }
  644:     }
  645:     $displaymsg .= '<span class="LC_helpform_receipt_cat">'.
  646:                    $lt{'date'}.'</span>: '.$reporttime.'<br />'."\n";
  647: 
  648:     my $start_page = 
  649: 	&Apache::loncommon::start_page('Support request recorded',undef,
  650: 				       {'function'    => $function,
  651: 					'add_entries' => {
  652: 					    topmargin    => "0",
  653: 					    marginheight => "0",
  654: 					},
  655: 					'only_body'   => 1,});
  656: 
  657:     $r->print(<<"END");
  658: $start_page
  659: <form name="logproblem">
  660: <input type="hidden" name="command" value="result" />
  661: </form>
  662: END
  663:     if ($r->uri eq '/adm/helpdesk') {
  664:         &print_header($r,$url,'process');
  665:     }
  666:     my $bad_email = 0;
  667:     if ($to =~ /,/) {
  668:         my @ok_email; 
  669:         foreach my $email (split(/,/,$to)) {
  670:             if ($email =~ m/^[^\@]+\@[^\@]+$/) {
  671:                 if (!grep(/^\Q$email\E$/,@ok_email)) {
  672:                     push(@ok_email,$email);
  673:                 }
  674:             }
  675:         }
  676:         if (@ok_email > 0) {
  677:             $to = join(',',@ok_email);
  678:         } elsif ($admin =~ m/^[^\@]+\@[^\@]+$/) {
  679:             $to = $admin;
  680:         } else {
  681:             $bad_email = 1;
  682:         }
  683:     } elsif ($to !~ m/^[^\@]+\@[^\@]+$/) {
  684:         if ($admin =~ m/^[^\@]+\@[^\@]+$/) {
  685:             $to = $admin;
  686:         } else {
  687:             $bad_email = 1;
  688:         }
  689:     }
  690:     if ($bad_email) {
  691:         $r->print('
  692:  <h3>'.$lt{'warn'}.'</h3>'.
  693: &mt('As the e-mail address provided for this LON-CAPA server ([_1]) does not appear to be a valid e-mail address, your support request has <b>not</b> been sent to the LON-CAPA support staff or administrator at your institution.',$to).' '.&mt('Instead a copy has been sent to the LON-CAPA support team at Michigan State University.')); 
  694:             $to = 'helpdesk@lon-capa.org';
  695:     } else {
  696:         $r->print('<h3>'.$lt{'asup'}.' '.$to.'</h3>');
  697:     }
  698:     if (defined($env{'form.email'})) {
  699:         $env{'form.email'} =~ s/^\s+//;
  700:         $env{'form.email'} =~ s/\s+$//;
  701:         if ($env{'form.email'} =~ m/^[^\@]+\@[^\@]+$/) {
  702:             $from = $env{'form.email'};
  703:         }
  704:     }
  705: 
  706:     if (defined($env{'form.cc'})) {
  707:         if ($badccmsg) {
  708:             $displaymsg .= $badccmsg;
  709:         }
  710:     }
  711: 
  712:     my $subject = $env{'form.subject'};
  713:     $subject =~ s/(`)/'/g;
  714:     $subject =~ s/\$/\(\$\)/g;
  715:     $supportmsg =~ s/(`)/'/g;
  716:     $supportmsg =~ s/\$/\(\$\)/g;
  717:     $displaymsg =~ s/(`)/'/g;
  718:     $displaymsg =~ s/\$/\(\$\)/g;
  719:     my $fname;
  720: 
  721:     my $attachmentpath = '';
  722:     my $attachmentsize = '';
  723:     if ((defined($env{'user.name'})) && ($env{'user.name'} ne 'public')
  724:         && ($env{'user.domain'} ne 'public')) {
  725:         if ($env{'form.screenshot.filename'}) {
  726:             $attachmentsize = length($env{'form.screenshot'});
  727:             if ($attachmentsize > 131072) {
  728:                 $displaymsg .= '<br />'.&mt('The uploaded screenshot file ([_1] bytes) included with your request exceeded the maximum allowed size - 128 KB, and has therefore been discarded.',$attachmentsize);
  729:             } else {
  730:                 $attachmentpath=&Apache::lonnet::userfileupload('screenshot',undef,'helprequests');
  731:             }
  732:         }
  733:     }
  734: 
  735:     my %cookies;
  736:     my $cookie=CGI::Cookie->parse($r->header_in('Cookie'));
  737:     if ($$cookie{'lonID'} =~ /lonID=($LONCAPA::handle_re);/) {
  738:         $cookies{'lonID'} = $1;
  739:     }
  740: 
  741:     if ($attachmentpath =~ m-/([^/]+)$-) {
  742:         $fname = $1;
  743:         $displaymsg .= '<br />'
  744:                       .&mt('An uploaded screenshot file [_1] ([_2] bytes) was included in the request sent by [_3].'
  745:                           ,'<span class="LC_filename">'.$fname.'<span>'
  746:                           ,$attachmentsize
  747:                           ,$env{'user.name'}.':'.$env{'user.domain'}
  748:                        );
  749:         $supportmsg .= "\n";
  750:         foreach my $var (@cookievars) {
  751:             $supportmsg .= "$var: $cookies{$var}\n";
  752:         }
  753:         foreach my $var(@ENVvars) {
  754:             $supportmsg .= "$var: $ENV{$var}\n";
  755:         }
  756:         foreach my $var (@envvars) {
  757:             $supportmsg .= "$var: $env{$var}\n";
  758:         }
  759:     }
  760:  
  761:     my $msg = MIME::Lite->new(
  762:                  From    => $from,
  763:                  To      => $to,
  764:                  Subject => $subject,
  765:                  Type    =>'TEXT',
  766:                  Data    => $supportmsg,
  767:                  );
  768:     if (@ok_ccs > 0) {
  769:         my $cc_string = join(', ',@ok_ccs);
  770:         $msg->add("Cc" => $cc_string);
  771:     }
  772:     if ($bcc ne '') {
  773:         $msg->add("Bcc" => $bcc);
  774:     }
  775:     $msg->add('Content-type','text/plain; charset=UTF-8');
  776: 
  777:     if ($attachmentpath) {
  778:         my ($type, $encoding) = MIME::Types::by_suffix($attachmentpath);
  779:         $msg->attach(Type     => $type,
  780:                      Path     => $attachmentpath,
  781:                      Filename => $fname
  782:                      );
  783: 
  784:     } else {
  785:         my $envdata = '';
  786:         foreach my $var (@cookievars) {
  787:             $envdata .= "$var: $cookies{$var}\n";
  788:         }
  789:         foreach my $var (@ENVvars) {
  790:             $envdata .= "$var: $ENV{$var}\n";
  791:         }
  792:         foreach my $var (@envvars) {
  793:             $envdata .= "$var: $env{$var}\n";
  794:         }
  795:         foreach my $var (@loncvars) {
  796:             $envdata .= "$var: $env{$var}\n";
  797:         }
  798:         $msg->attach(Type => 'TEXT',
  799:                      Data => $envdata);
  800:     }
  801: 
  802: ### Send it:
  803:     $msg->send('sendmail');
  804: 
  805:     if ($attachmentpath =~ m|$Apache::lonnet::perlvar{'lonDaemons'}/tmp/helprequests/(\d+)/[^/]+|) {
  806:         unlink($attachmentpath);
  807:     }
  808:     $r->print('<b>'.$lt{'your'}.'</b>:<br /><br />'."\n");
  809:     $r->print('<div style="width:620px;">'.
  810:               &Apache::lonhtmlcommon::start_pick_box().
  811:               &Apache::lonhtmlcommon::row_title($lt{'info'},undef,'LC_oddrow_value')."\n".$displaymsg."\n".
  812:               &Apache::lonhtmlcommon::row_closure().
  813:               &Apache::lonhtmlcommon::row_title($lt{'adin'},undef,'LC_evenrow_value'));
  814:     my $envmsg;
  815:     foreach my $var (@cookievars) {
  816:         if ($cookies{$var} ne '') {
  817:             $envmsg.= '<span class="LC_helpform_receipt_cat">'.
  818:                       $var.'</span>:&nbsp;'.$cookies{$var}.', ';
  819:         }
  820:     }
  821:     foreach my $var (@ENVvars) {
  822:         if ($ENV{$var} ne '') {
  823:             $envmsg .= '<span class="LC_helpform_receipt_cat">'.
  824:                        $var.'</span>:&nbsp;'.$ENV{$var}.', ';
  825:         }
  826:     }
  827:     foreach my $var (@envvars) {
  828:         if ($env{$var} ne '') { 
  829:             $envmsg .= '<span class="LC_helpform_receipt_cat">'.
  830:                        $var.'</span>:&nbsp;'.$env{$var}.', ';
  831:         }
  832:     }
  833:     $envmsg =~ s/, $//;
  834:     $r->print($envmsg."\n".
  835:               &Apache::lonhtmlcommon::row_closure(1)."\n".
  836:               &Apache::lonhtmlcommon::end_pick_box().
  837:               "</div>\n".
  838:               &Apache::loncommon::end_page());
  839: }
  840: 
  841: sub print_header {
  842:     my ($r,$origurl,$command) = @_;
  843:     my $location=&Apache::loncommon::lonhttpdurl("/adm");
  844:     my ($component_url);
  845:     my $helpdesk_link = '<a href="javascript:validate()">';
  846:     if ($command eq 'process') {
  847:         $helpdesk_link = '<a href="/adm/helpdesk">';
  848:     }
  849:     my %lt = &Apache::lonlocal::texthash (
  850:                                            login => 'Log-in help',
  851:                                            ask   => 'Ask helpdesk',
  852:                                            getst => 'Getting started guide',
  853:                                            back =>  'Back to last location',
  854:                                            headline => 'help/support',
  855:                                          );
  856:     my ($getstartlink,$reviewtext);
  857:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/gettingstarted.html') {
  858:         $getstartlink = qq|<td align="center">&nbsp;<b><a href="/adm/gettingstarted.html">$lt{'getst'}</a></td>|;
  859:         $reviewtext = &mt('Please review the information in "Log-in help" and the "Getting started" guide if you are unable to log-in.');
  860:     } else {
  861:         $reviewtext = &mt('Please review the information in "Log-in help" if you are unable to log-in.');
  862:     }
  863:     if ($origurl eq '') {
  864:         $origurl = 'javascript:history.go(-1)';
  865:     }
  866:     $r->print(<<END);
  867: <table width="620" border="0" cellspacing="0" cellpadding="0" height="55">   <tr height="50">    <td width='5'>&nbsp;</td>
  868:    <td>
  869:     <fieldset>
  870:       <legend>
  871:         <img src="$location/lonIcons/minilogo.gif" height="20" width="29" valign="bottom" />
  872:         LON-CAPA $lt{'headline'}
  873:       </legend>
  874:  <table id="LC_helpmenu_links">
  875:    <tr>
  876:     <td align="center"><span class="LC_nobreak"><img src="$location/help/help.png" border="0" alt="($lt{'login'})" valign="middle" />&nbsp;<b><a href="/adm/loginproblems.html">$lt{'login'}</a></b>&nbsp;</span></td>
  877:     <td align="center"><span class="LC_nobreak">&nbsp;<b>$helpdesk_link<img src="$location/lonIcons/helpdesk.gif" border="0" alt="($lt{'ask'})" valign="middle" />&nbsp;$lt{'ask'}</a></b>&nbsp;</span></td>$getstartlink
  878:     <td align="center"><span class="LC_nobreak">&nbsp;<b><a href="$origurl" target="_top"><img src="$location/lonIcons/move_up.gif" border="0" alt="($lt{'back'})" valign="middle" />&nbsp;$lt{'back'}</a></b>&nbsp;</span></td>
  879:    </tr>
  880:  </table>
  881: </fieldset>
  882:   </td>
  883:   <td width='5'>&nbsp;</td>
  884:  </tr>
  885:  <tr height='5'>
  886:   <td colspan='3' height='5'>&nbsp;</td>
  887:  </tr>
  888: END
  889:     if  ($command ne 'process') {
  890:         $r->print('
  891:  <tr>
  892:   <td colspan="3">'.$reviewtext.' '
  893: .&mt('If your problem is still unresolved, the form below can be used to send a question to the LON-CAPA helpdesk.').'<br />'
  894: .'<font size="-1"><b>'.&mt('Students').'</b>: '.&mt('Do <b>not</b> use this form to ask questions about course content.').' '
  895: .&mt('Contact your instructor instead.')
  896: .'</font><br /><br />
  897:   </td>
  898:  </tr>');
  899:     }
  900:     $r->print('
  901: </table>');
  902:     return;
  903: }
  904: 
  905: sub get_domain {
  906:     my $codedom;
  907:     if (exists($env{'form.codedom'})) {
  908:         $codedom = $env{'form.codedom'};
  909:     } elsif ($env{'request.course.id'}) {
  910:         $codedom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  911:     } elsif ($env{'request.role.domain'}) {
  912:         $codedom = $env{'request.role.domain'};
  913:     } else {
  914:         $codedom = &Apache::lonnet::default_login_domain();
  915:     }
  916:     return $codedom;
  917: }
  918: 
  919: sub cleanup_html {
  920:     my ($incoming) = @_;
  921:     my $outgoing;
  922:     if ($incoming ne '') {
  923:         $outgoing = $incoming;
  924:         $outgoing =~ s/\#/&#035;/g;
  925:         $outgoing =~ s/\&/&#038;/g;
  926:         $outgoing =~ s/</&#060;/g;
  927:         $outgoing =~ s/>/&#062;/g;
  928:         $outgoing =~ s/\(/&#040/g;
  929:         $outgoing =~ s/\)/&#041;/g;
  930:         $outgoing =~ s/"/&#034;/g;
  931:         $outgoing =~ s/'/&#039;/g;
  932:         $outgoing =~ s/\$/&#036;/g;
  933:     }
  934:     return $outgoing;
  935: }
  936: 
  937: 1;

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