File:  [LON-CAPA] / loncom / interface / lonnotify.pm
Revision 1.7: download - view: text, annotated - select for diffs
Fri Oct 28 19:24:25 2005 UTC (18 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Typo.

    1: #
    2: # Copyright Michigan State University Board of Trustees
    3: #
    4: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    5: #
    6: # LON-CAPA is free software; you can redistribute it and/or modify
    7: # it under the terms of the GNU General Public License as published by
    8: # the Free Software Foundation; either version 2 of the License, or
    9: # (at your option) any later version.
   10: #
   11: # LON-CAPA is distributed in the hope that it will be useful,
   12: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14: # GNU General Public License for more details.
   15: #
   16: # You should have received a copy of the GNU General Public License
   17: # along with LON-CAPA; if not, write to the Free Software
   18: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19: #
   20: # /home/httpd/html/adm/gpl.txt
   21: #
   22: # http://www.lon-capa.org/
   23: #
   24:                                                                                 
   25: package Apache::lonnotify;
   26:                                                                                 
   27: use strict;
   28: use Apache::lonnet;
   29: use Apache::loncommon;
   30: use Apache::lonsupportreq;
   31: use LONCAPA::Enrollment;
   32: use Apache::Constants qw(:common :http);
   33: use Apache::lonlocal;
   34: use Mail::Send;
   35: use HTML::TokeParser;
   36: use HTML::Entities;
   37: 
   38: sub handler {
   39:     my ($r) = @_;
   40:     &Apache::loncommon::content_type($r,'text/html');
   41:     $r->send_http_header;
   42: 
   43:     if ($r->header_only) {
   44:         return OK;
   45:     }
   46:     my $cdom = $env{'request.role.domain'};
   47:     unless (&Apache::lonnet::allowed('psa',$cdom)) {
   48:         # Not allowed to broadcast e-mail system-wide 
   49:         $env{'user.error.msg'}="/adm/notify:psa:0:0:Cannot broadcast e-mail systemwide";
   50:         return HTTP_NOT_ACCEPTABLE;
   51:     }
   52: 
   53:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   54:                                             ['command']);
   55:     my $command = $env{'form.command'};
   56:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   57:     my %ltext=&Apache::lonlocal::texthash(
   58:                'note' => 'Notification E-mail',
   59:     );
   60:     my $function = &Apache::loncommon::get_users_function();
   61:     my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
   62:     my $bodytag = &Apache::loncommon::bodytag('Broadcast e-mail to users');
   63:     my $html=&Apache::lonxml::xmlbegin();
   64:     &Apache::lonhtmlcommon::add_breadcrumb
   65:         ({href=>'/adm/notify',
   66:           text=>"Broadcast E-mail"});
   67:     if ($command eq 'process') {
   68:         &print_request_receipt($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   69:     } elsif ($command eq 'compose') {
   70:         &print_composition_form($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   71:     } elsif ($command eq 'pick_target') {
   72:         &print_selection_form($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   73:     } elsif ($command eq 'pick_display') {
   74:         &print_display_option_form($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   75:     } elsif ($command eq 'display') {
   76:         &print_display($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   77:     } else {
   78:         &print_front_page($r,$cdom,$tablecolor,$bodytag,$html,\%ltext);
   79:     }
   80:     return OK;
   81: }
   82: 
   83: sub print_front_page {
   84:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
   85:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
   86:             (undef,'Broadcast e-mail to Domain','Broadcast_system_email');
   87:     my $jscript = qq|
   88: function next_page(caller) {
   89:     if (caller == 'view') {
   90:         document.front.command.value="pick_display"
   91:     }
   92:     else {
   93:         document.front.command.value="pick_target"
   94:     }
   95:     document.front.submit()
   96: }
   97:     |; 
   98:     my %lt=&Apache::lonlocal::texthash(
   99:                'note' => 'Notification E-mail',
  100:     );
  101:     my $output = <<"ENDONE";
  102: $html
  103: <head>
  104:  <title>LON-CAPA $lt{'note'}</title>
  105: <script type"text/javascript">
  106: $jscript
  107: </script>
  108: </head>
  109: $bodytag
  110: $breadcrumbs
  111: <br />
  112: ENDONE
  113:     $output .= '<form name="front" method="post">'.
  114:               '<input type="hidden" name="command" />';
  115:     $output .= &Apache::lonhtmlcommon::start_pick_box();
  116:     $output .= '<table cellspacing="8" cellpadding="8">'.
  117:               '<tr><td><a href="javascript:next_page('."'new'".')">'.
  118:               'Send a new e-mail message to selected users from this domain</a></td></tr><tr>'.
  119:               '<td><a href="javascript:next_page('."'view'".')">'.
  120:               'Display e-mail sent by Domain Coordinators in this domain'.
  121:               '</a></td></tr></table>';
  122:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  123:     $output .= qq(
  124: </form>
  125: </body>
  126: </html>);
  127:     $r->print($output);
  128:     return;
  129: }
  130: 
  131: sub print_display_option_form {
  132:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  133:     &Apache::lonhtmlcommon::add_breadcrumb
  134:          ({text=>"Display options"});
  135:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  136:             (undef,'Broadcast e-mail display options','Broadcast_system_email');
  137:     my $table_width = '';
  138:     my $col_width = '200';
  139:     my $formname = 'display_options';
  140:     my $cmd = 'display';
  141:     my $submit_text = 'Display e-mail';
  142:     my @roles = ('dc');
  143:     my $now = time;
  144:     my %lt=&Apache::lonlocal::texthash(
  145:                'note' => 'Notification E-mail',
  146:     );
  147:     my $startdateform = &Apache::lonhtmlcommon::date_setter($formname,
  148:                                                             'startdate',
  149:                                                             $now);
  150:     my $enddateform = &Apache::lonhtmlcommon::date_setter($formname,
  151:                                                           'enddate',
  152:                                                           $now);
  153:     my $jscript;
  154:     my $output = <<"ENDONE";
  155: $html
  156: <head>
  157:  <title>LON-CAPA $lt{'note'}</title>
  158: <script type"text/javascript">
  159: $jscript
  160: </script>
  161: </head>
  162: $bodytag
  163: $breadcrumbs
  164: <br />
  165: <form method="post" name="$formname">
  166: ENDONE
  167:     $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
  168:     $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Date range'));
  169:     $output .= '<td><table><tr><td>Earliest to display: </td><td>'.
  170:                 $startdateform.'</td></tr>';
  171:     $output .= '<tr><td>Latest to display: </td><td>'.$enddateform.
  172:                '</td></tr></table></td>';
  173:     $output .= &Apache::lonhtmlcommon::row_closure();
  174:     $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Choose sender(s)'));
  175:     my %personnel = &Apache::lonnet::get_domain_roles($cdom,\@roles);
  176:     $output .= '<td>';
  177:     my @domcc = ();
  178:     foreach my $server (keys %personnel) {
  179:         foreach my $user (sort(keys %{$personnel{$server}})) {
  180:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
  181:             unless (grep/^$uname:$udom$/,@domcc) {
  182:                 my %userinfo = &Apache::lonnet::get('environment',['lastname','firstname'],$udom,$uname);
  183:                 $output .= '<input type="checkbox" name="sender" value="'.$uname.':'.$udom.'" />&nbsp;'.$userinfo{firstname}.' '.$userinfo{lastname}.'&nbsp;&nbsp;('.$uname.':'.$udom.')';
  184:                 push (@domcc,$uname.':'.$udom);
  185:             }
  186:         }
  187:     }
  188:     $output .= '</td>';
  189:     $output .= &Apache::lonhtmlcommon::row_closure();
  190:     $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
  191:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  192:     $output .= qq(
  193: <input type="hidden" name="sortby" value="date" />
  194: </form>
  195: </body>
  196: </html>);
  197:     $r->print($output);
  198:     return;
  199: }
  200: 
  201: sub print_display {
  202:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  203:     &Apache::lonhtmlcommon::add_breadcrumb
  204:          ({href=>'/adm/notify?command=pick_display',
  205:           text=>"Display options"},
  206:          {text=>"E-mail display"});
  207:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  208:             (undef,'Display Broadcast e-mail','Broadcast_system_email');
  209:     my $table_width = '';
  210:     my $col_width = '200';
  211:     my $rowColor1 = "#ffffff";
  212:     my $rowColor2 = "#eeeeee";
  213:     my $rowColor;
  214:     my $msgcount = 0;
  215:     my $formname = 'display_sent';
  216:     my $start = &Apache::lonhtmlcommon::get_date_from_form('startdate');
  217:     my $end = &Apache::lonhtmlcommon::get_date_from_form('enddate');
  218:     my @senders = &Apache::loncommon::get_env_multiple('form.sender');
  219:     my %sentmail = &Apache::lonnet::dcmaildump($cdom,$start,$end,\@senders);
  220:     my %dcmail = ();
  221:     my %Sortby = ();
  222:     my $jscript = <<"ENDSCRIPT";
  223: function changeSort(caller) {
  224:     document.$formname.sortby.value = caller;
  225:     document.$formname.submit();   
  226: }
  227: ENDSCRIPT
  228:     my $output = <<"ENDONE";
  229: $html
  230: <head>
  231:  <title>LON-CAPA $$ltext{'note'}</title>
  232:  <script type"text/javascript">
  233: $jscript
  234:  </script>
  235: </head>
  236: $bodytag
  237: $breadcrumbs
  238: <br />
  239: <form method="post" name="$formname">
  240: ENDONE
  241: 
  242:     foreach my $server (keys(%sentmail)) {
  243:         foreach my $msgid (keys(%{$sentmail{$server}})) {
  244:             my %content = &unpackagemail($sentmail{$server}{$msgid});
  245:             if (defined($dcmail{$msgid})) {
  246:                 foreach my $user (keys(%{$content{'recipients'}})) {
  247:                     $dcmail{$msgid}{recipients}{$user} = $content{recipients}{$user};
  248:                 }
  249:             } else {
  250:                 $msgcount ++;
  251:                 %{$dcmail{$msgid}} = ();
  252:                 foreach my $item (keys(%content)) {
  253:                     if ($item eq 'recipients') {
  254:                         foreach my $user (keys(%{$content{recipients}})) {
  255:                             $dcmail{$msgid}{recipients}{$user} = $content{recipients}{$user};
  256:                         }
  257:                     } else {
  258:                         $dcmail{$msgid}{$item} = $content{$item};
  259:                     }
  260:                 }
  261:             }
  262:         }
  263:     }
  264:     $output .= &Apache::lonhtmlcommon::start_pick_box();
  265:     if ($msgcount > 0) {
  266:         my $rowNum = 0;
  267:         $output .= '<tr><td><table cellpadding="4" cellspacing="2" width="100%">
  268:                    <tr bgcolor="'.$tablecolor.'" align="center">
  269:                    <td><b><a href="javascript:changeSort('."'date'".')">Date</a></b></td>
  270:                    <td><b><a href="javascript:changeSort('."'subject'".')">Subject</a></b></td>
  271:                    <td><b><a href="javascript:changeSort('."'sender'".')">Sender</a></b></td>
  272:                    <td><b><a href="javascript:changeSort('."'message'".')">Message</a></b></td>
  273:                    <td><b><a href="javascript:changeSort('."'recipients'".')">Recipients</a></b></td>
  274:                    </tr>';
  275:         if (($env{'form.sortby'} eq 'date') || ($env{'form.sortby'} eq '') || (!defined($env{'form.sortby'})) || (($env{'form.sortby'} eq 'sender') && (@senders <= 1))) {
  276:             foreach my $msgid (sort(keys(%dcmail))) {
  277:                 if ($rowNum %2 == 1) {
  278:                     $rowColor = $rowColor1;
  279:                 } else {
  280:                     $rowColor = $rowColor2;
  281:                 }
  282:                 my $recipients = '';
  283:                 my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  284:                 $date = &Apache::lonlocal::locallocaltime($date);
  285:                 foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  286:                     $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  287:                 }
  288:                 $recipients =~ s/,\s$//;
  289:                 $output .= '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{subject}).'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{message}).'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
  290:                 $rowNum ++;
  291:             }
  292:         } else {
  293:             foreach my $msgid (sort(keys(%dcmail))) {
  294:                 my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  295:                 if ($env{'form.sortby'} eq 'subject') {
  296:                     push @{$Sortby{$dcmail{$msgid}{subject}}},$msgid;
  297:                 } elsif ($env{'form.sortby'} eq 'message') {
  298:                     push @{$Sortby{$dcmail{$msgid}{message}}},$msgid;
  299:                 } elsif ($env{'form.sortby'} eq 'recipients') {
  300:                     my $recipients ='';
  301:                     foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  302:                         $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  303:                     }
  304:                     $recipients =~ s/,\s$//;
  305:                     push @{$Sortby{$recipients}},$msgid;
  306:                 } elsif ($env{'form.sortby'} eq 'sender') {
  307:                     if (@senders > 1) {
  308:                        push @{$Sortby{$sname.':'.$sdom}},$msgid;
  309:                     }
  310:                 }
  311:             }
  312:             foreach my $key (sort(keys(%Sortby))) {
  313:                 foreach my $msgid (@{$Sortby{$key}}) {
  314:                     if ($rowNum %2 == 1) {
  315:                         $rowColor = $rowColor1;
  316:                     } else {
  317:                         $rowColor = $rowColor2;
  318:                     }
  319:                     my $recipients = '';
  320:                     if ($env{'form.sortby'} eq 'recipients') {
  321:                         $recipients = $key;
  322:                     } else {
  323:                         foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  324:                             $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  325:                         }
  326:                         $recipients =~ s/,\s$//;
  327:                     }
  328: 
  329:                     my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  330:                     $date = &Apache::lonlocal::locallocaltime($date);
  331:                     $output .=  '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{subject}).'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{message}).'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
  332:                     $rowNum ++;
  333:                 }
  334:             }
  335:         }
  336:         $output .= '</table></td></tr>';
  337:     } else {
  338:         $output .= '<tr bgcolor="#ffffff"><td>&nbsp;</td><td><br><center><i><b><small>&nbsp;&nbsp;No mail sent matching supplied criteria&nbsp;&nbsp;</small><br><br></b></i></td><td>&nbsp;</td></tr>';
  339:     }
  340:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  341:     $output .= &echo_form_input();
  342:     unless (defined($env{'form.sortby'})) {
  343:         $output .= qq(<input type="hidden" name="sortby" value="date" />\n);
  344:     } 
  345:     $output .= qq(
  346: </form>
  347: </body>
  348: </html>);
  349:     $r->print($output);
  350:     return;
  351: }
  352: 
  353: sub print_selection_form {
  354:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  355:     my %coursecodes = ();
  356:     my %codes = ();
  357:     my @codetitles = ();
  358:     my %cat_titles = ();
  359:     my %cat_order = ();
  360:     my %idlist = ();
  361:     my %idnums = ();
  362:     my %idlist_titles = ();
  363:     my $caller = 'global';
  364:     my $totcodes = 0;
  365:     my $format_reply;
  366:     my $jscript = '';
  367:     my $formname = 'rolefilter';
  368:     my $table_width = '100%';
  369:     my $col_width = '200';
  370:     my %lt=&Apache::lonlocal::texthash(
  371:                'note' => 'Notification E-mail', 
  372:                'buil' => 'Building valid e-mail address from username, if missing from preferences:',
  373:                'kerb' => 'Kerberos: enter default for each realm used in the domain, with comma separation of entries',
  374:                'infs' => 'Internal, Filesystem and Local authentication: enter single default.',
  375:                'comp' => 'Compose Message'
  376:            );
  377:     my $loaditems = qq|
  378: function initialize_codes() {
  379:     return;
  380: }
  381:     |;
  382:     &Apache::lonhtmlcommon::add_breadcrumb
  383:           ({text=>"Select Audience"});
  384: 
  385:     $totcodes = &Apache::lonsupportreq::retrieve_instcodes(\%coursecodes,$cdom,$totcodes);
  386:     if ($totcodes > 0) {
  387:         $format_reply = &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
  388:         if ($format_reply eq 'ok') {
  389:             my $numtypes = @codetitles;
  390:             &Apache::lonsupportreq::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
  391:             &Apache::lonsupportreq::javascript_code_selections($formname,$numtypes,\%cat_titles,\$jscript,\%idlist,\%idnums,\%idlist_titles,\@codetitles);
  392:             $loaditems = '';
  393:         }
  394:     }
  395: 
  396:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  397:             (undef,'Choose e-mail audience','Broadcast_system_email');
  398:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
  399:     my $output = <<"ENDONE";
  400: $html
  401: <head>
  402:  <title>LON-CAPA $lt{'note'}</title>
  403: <script type"text/javascript">
  404: $jscript
  405: </script>
  406: $cb_jscript
  407: </head>
  408: $bodytag
  409: $breadcrumbs
  410: <br />
  411: <form method="post" name="$formname">
  412: ENDONE
  413:    $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
  414:    my @roles = ('ow','cc','in','ta','ep','ad','st','cr');
  415:    my %longtypes = ();
  416:    my %authtypes = ();
  417:    &form_elements(\%longtypes,\%authtypes);
  418:    my $descrip = $lt{'buil'}.' 
  419: <ul>
  420: <li>'.$lt{'kerb'}.'<br />(e.g., MSU.EDU=msu.edu, MSUE.EDU=msue.msu.edu).</li>
  421: <li>'.$lt{'infs'}.'</li>
  422: </ul>'."\n";
  423:    my $submit_text = $lt{'comp'};
  424:    my $cmd = 'compose';
  425:    $output .= &Apache::lonhtmlcommon::role_select_row(\@roles,$col_width,$tablecolor,'Roles');
  426:    $output .= &Apache::lonhtmlcommon::course_select_row($col_width,$tablecolor,'Courses',$formname,$totcodes,\@codetitles,\%idlist,\%idlist_titles);
  427:    $output .= &Apache::lonhtmlcommon::status_select_row(\%longtypes,$col_width,$tablecolor,&mt('Access status'));
  428:    $output .= &Apache::lonhtmlcommon::email_default_row(\%authtypes,$col_width,$tablecolor,&mt('Username -> Email conversion'),$descrip);
  429:    $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
  430:    $output .= &Apache::lonhtmlcommon::end_pick_box();
  431:    $output .= qq(
  432: </form>
  433: </body>
  434: </html>);
  435:     $r->print($output);
  436:     return;
  437: }
  438: 
  439: sub print_composition_form {
  440:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  441:     &Apache::lonhtmlcommon::add_breadcrumb
  442:         ({href=>'/adm/notify?command=pick_target',
  443:           text=>"Select Audience"},
  444:          {text=>"Compose Message"});
  445:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
  446:     my $breadcrumbs = (&Apache::lonhtmlcommon::breadcrumbs
  447:         (undef,'Broadcast e-mail to users','Broadcast_system_email'));
  448: 
  449:     my %lt=&Apache::lonlocal::texthash(
  450:                       'note' => 'Notification E-mail',
  451:                       'nore' => 'No recipients identified',
  452:                       'emad' => 'e-mail address',
  453:                    );
  454:     $r->print(<<ENDONE);
  455: $html
  456: <head>
  457:  <title>LON-CAPA $lt{'note'}</title>
  458: <script type="text/javascript">
  459: $jscript
  460: </script>
  461: </head>
  462: $bodytag $breadcrumbs
  463: <br /> 
  464: ENDONE
  465:     my $coursefilter = $env{'form.coursepick'};
  466:     my %courses = ();
  467:     if ($coursefilter eq 'all') {
  468:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.');
  469:     } elsif ($coursefilter eq 'category') {
  470:         my $instcode = '';
  471:         my @cats = ('Semester','Year','Department','Number');
  472:         foreach my $category (@cats) {
  473:             if (defined($env{'form.'.$category})) {
  474:                 unless ($env{'form.'.$category} eq '-1') {
  475:                     $instcode .= $env{'form.'.$category};
  476:                 }
  477:             }
  478:         }
  479:         if ($instcode eq '') {
  480:             $instcode = '.';
  481:         }
  482:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.');
  483:     } elsif ($coursefilter eq 'specific') {
  484:         if ($env{'form.coursetotal'} > 1) {
  485:             my @course_ids = split(/&&/,$env{'form.courselist'});
  486:             foreach (@course_ids) {
  487:                 $courses{$_} = '';
  488:             }
  489:         } else {
  490:             $courses{$env{'form.courselist'}} = '';
  491:         }
  492:     }
  493: 
  494:     my @types = &Apache::loncommon::get_env_multiple('form.types');
  495:     my @roles = &Apache::loncommon::get_env_multiple('form.roles');
  496: 
  497:     my %longtypes = ();
  498:     my %authtypes = ();
  499:     my %email_defaults = ();
  500:     my $table_width = '100%';
  501:     my $col_width = '200';
  502: 
  503:     &form_elements(\%longtypes,\%authtypes);
  504:     foreach my $auth (keys(%authtypes)) {
  505:         if (exists($env{'form.'.$auth})) {
  506:              my $default = $env{'form.'.$auth};
  507:              $default =~ s/^,+//;
  508:              $default =~ s/,+$//;
  509:              if ($auth =~ /^krb/) {
  510:                  %{$email_defaults{$auth}} = ();
  511:                  if ($default =~ /,/) {
  512:                      my @items = split(/,/,$default);
  513:                      foreach my $item (@items) {
  514:                          my ($realm,$value) = split(/=/,$item);
  515:                          $email_defaults{$auth}{$realm} = $value;
  516:                      }
  517:                  } else {
  518:                      my ($realm,$value) = split(/=/,$default);
  519:                      $email_defaults{$auth}{$realm} = $value;
  520:                  }
  521:              } else {
  522:                  $email_defaults{$auth} = $default;
  523:              }
  524:          }
  525:     }
  526: 
  527:     my $sender = &get_user_info($env{'user.name'},%email_defaults);
  528: 
  529:     my %recipients = ();
  530:     my %users = ();
  531:     my %access = ();
  532:     my $totalrecip = 0;
  533:     my @unmatched = ();
  534:     foreach my $role (@roles) {
  535:         %{$users{$role}} = ();
  536:     }
  537:     foreach my $type (@types) {
  538:         $access{$type} = $type;
  539:     }
  540:     foreach my $course_id (keys(%courses)) {
  541:         my ($cdom,$cnum) = split(/_/,$course_id);
  542:         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\%users);
  543:     }
  544:     foreach my $role (keys(%users)) {
  545:         foreach my $user (keys(%{$users{$role}})) {
  546:             unless (defined($recipients{$user})) {
  547:                 $recipients{$user} = &get_user_info($user,%email_defaults);
  548:                 if ($recipients{$user} eq '') {
  549:                     push @unmatched, $user;
  550:                 } else {
  551:                     $totalrecip ++;
  552:                 } 
  553:             }
  554:         }
  555:     }
  556:     my $output  = '<form name="compose" method="post">'."\n";
  557:   
  558:     if ($totalrecip > 0) {
  559:         $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
  560:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Subject'));
  561:         $output .= ' <td><input type="text" name="subject" size="30" /></td>';
  562:         $output .= &Apache::lonhtmlcommon::row_closure();
  563:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Message'));
  564:         $output .= '  <td><textarea name="message" id="message"
  565:                       cols="60" rows="10" wrap="hard"></textarea></td>';
  566:         $output .= &Apache::lonhtmlcommon::row_closure();
  567:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Recipients'));
  568:         $output .= '<td><input type="button" value="check all" 
  569:                     onclick="javascript:checkAll(document.compose.recipient)" />
  570:                     &nbsp;&nbsp;<input type="button" value="uncheck all"
  571:                     onclick="javascript:uncheckAll(document.compose.recipient)" />
  572:                     <br /><table border="0">';
  573:         if (keys(%recipients) > 0) {
  574:             $output .= '<tr><td>&nbsp;</td><td><small><b>username:domain</b></small></td><td>&nbsp;&nbsp;</td><td><small><b>'.$lt{'emad'}.'</b></small></td></tr>';
  575:         }
  576:         foreach my $username (sort(keys(%recipients))) {
  577:             if ($recipients{$username} =~ /\@/) {
  578:                 my $value=&Apache::lonnet::escape($username).':'.&Apache::lonnet::escape($recipients{$username});
  579:                 $output .= '<tr><td><input type="checkbox" name="recipient" value="'.$value.'" checked="checked" /></td><td>'.$username.'</td><td>&nbsp;&nbsp;</td><td>'.$recipients{$username}.'</td></tr>';
  580:             }
  581:         }
  582:         $output .= '</table>';
  583:         if (@unmatched) {
  584:             $output .= '<br /><br />'.&mt('Could not determine e-mail addresses for the following users:').'<ul>';
  585:             foreach my $username (sort @unmatched) {
  586:                 $output .= '<li>'.$username.'</li>';
  587:             }
  588:             $output .= '</ul>';
  589:         }
  590:         $output .= '</td>';
  591:         $output .= &Apache::lonhtmlcommon::row_closure();
  592:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Sender e-mail address'));
  593:         $output .= '<td><input type="text" name="sender" value="'.$sender.'" /></td>';
  594:         $output .= &Apache::lonhtmlcommon::row_closure();
  595:         $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),'process',&mt('Send Message'));
  596:         $output .= &Apache::lonhtmlcommon::end_pick_box();
  597:     } else {
  598:         $output .= $lt{'nore'};
  599:     }
  600:     $output .= &echo_form_input('command');
  601:     $output .= '</form></body></html>';
  602:     $r->print($output);
  603:     return;
  604: }
  605: 
  606: 
  607: sub print_request_receipt {
  608:     my ($r,$dom,$tablecolor,$bodytag,$html,$ltext) =@_;
  609:     my @recipients = &Apache::loncommon::get_env_multiple('form.recipient');
  610:     my $subject = $env{'form.subject'};
  611:     my $message = $env{'form.message'};
  612:     my $from = $env{'form.sender'};
  613:     my $jscript = <<ENDSCRIPT;
  614: function showCompose() {
  615:     document.goback.command.value = 'compose';
  616:     document.goback.submit();
  617: }
  618: ENDSCRIPT
  619:     &Apache::lonhtmlcommon::add_breadcrumb
  620:         ({href=>'/adm/notify?command=pick_target',
  621:           text=>"Select audience"});
  622:     &Apache::lonhtmlcommon::add_breadcrumb
  623:         ({href=>'javascript:showCompose()',
  624:           text=>"Compose Message"});
  625:     &Apache::lonhtmlcommon::add_breadcrumb
  626:         ({href=>'/adm/notify?command=process',
  627:           text=>"Outcome"});
  628:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  629:             (undef,'E-mail Delivery','Broadcast_system_email');
  630:     my $output = <<ENDONE;
  631: $html
  632: <head>
  633:  <title>LON-CAPA Notification E-mail</title>
  634: <script type"text/javascript">
  635: $jscript
  636: </script>
  637: </head>
  638: $bodytag
  639: $breadcrumbs
  640: <br />
  641: <form name="goback" method="post">
  642: ENDONE
  643:     $output .= &Apache::lonhtmlcommon::start_pick_box();
  644:     my @deliveries = ();
  645:     &broadcast_email(\@recipients,$subject,$from,$message,\@deliveries);
  646:     if (@deliveries > 0) {
  647:         &store_mail($subject,$message,$dom,\@deliveries);
  648:         $output .= '<tr>
  649:                      <td>
  650:                       <table cellpadding="4" cellspacing="2" width="100%">
  651:                        <tr bgcolor="'.$tablecolor.'" align="center">
  652:                         <td><b>Status</b></td>
  653:                         <td><b>Subject</b></td>
  654:                         <td><b>Message</b></td>
  655:                         <td><b>Recipients</b></td>
  656:                        </tr>
  657:                        <tr bgcolor="#eeeeee">
  658:                         <td valign="middle">Sent</td>
  659:                         <td valign="middle">'.&cr_to_br($subject).'</td>
  660:                         <td valign="middle">'.&cr_to_br($message).'</td>
  661:                         <td>';
  662:         foreach my $person (@deliveries) {
  663:             my ($username,$email) = split(/:/,$person);
  664:             $output .= &Apache::lonnet::unescape($email).'&nbsp;('.&Apache::lonnet::unescape($username).')<br />'."\n";
  665:         }
  666:         $output .= '</td>
  667:                    </tr>
  668:                   </table>
  669:                  </td>
  670:                 </tr>';
  671:         &store_mail($subject,$message,$dom,\@deliveries);
  672:     } else {
  673:         $output .= 'No mail sent - no recipients identified'; 
  674:     }
  675:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  676:     $output .= '<br /><a href="/adm/notify">Send another message?</a>'."\n";
  677:     $output .= &echo_form_input();
  678:     $output .= '
  679: </form>
  680: </body>
  681: </html>';
  682:     $r->print($output);
  683:     return;
  684: }
  685: 
  686: sub broadcast_email {
  687:     my ($recipients,$subject,$from,$message,$deliveries,$ltext)=@_;
  688: # find some way to spread out delivery for large numbers of recipients.
  689:     foreach my $user (@{$recipients}) {
  690:         my $msg = new Mail::Send;
  691:         my ($username,$to) = split(/:/,$user);
  692:         $username = &Apache::lonnet::unescape($username);
  693:         $to = &Apache::lonnet::unescape($to);
  694:         $msg->to($to);
  695:         $msg->subject($subject);
  696:         $msg->add('From',"$from");
  697:         if (my $fh = $msg->open()) {
  698:             print $fh $message;
  699:             $fh->close;
  700:             push(@{$deliveries},$user); 
  701:         }
  702:     }
  703: }
  704: 
  705: sub get_user_info {
  706:     my ($user,%email_defaults) = @_;
  707:     my ($uname,$udom) = split(/:/,$user);
  708:     my @emailtypes = ('permanentemail','critnotification','notification');
  709:     my %userinfo = &Apache::lonnet::get('environment',\@emailtypes,$udom,$uname);
  710:     my $email = '';
  711:     foreach my $type (@emailtypes) {
  712:         $email = $userinfo{$type};
  713:         if ($email =~ /\@/) {
  714:             last;
  715:         }
  716:     }
  717:     if ($email eq '') {
  718:         my $authinfo  = &Apache::lonnet::queryauthenticate($uname,$udom);
  719:         my ($authtype,$autharg) = split(/:/,$authinfo);
  720:         if ($authtype =~ /^krb/) {
  721:             if (defined($email_defaults{$authtype}{$autharg})) {
  722:                 $email = $uname.'@'.$email_defaults{$authtype}{$autharg};
  723:             }
  724:         } else {
  725:             if ((defined($email_defaults{$authtype})) && ($email_defaults{$authtype} ne '')) {
  726:                 $email = $uname.'@'.$email_defaults{$authtype};
  727:             }
  728:         }
  729:     }
  730:     return $email;
  731: }
  732: 
  733: sub form_elements {
  734:    my ($longtypes,$authtypes,$ltext) = @_;
  735:    %{$longtypes} = (
  736:                    active => 'Currently has access',
  737:                    previous => 'Previously had access',
  738:                    future => 'Will have future access',
  739:                    );
  740:    %{$authtypes} = (
  741:                    krb4 => 'Kerberos 4',
  742:                    krb5 => 'Kerberos 5',
  743:                    internal => 'Internal (LON-CAPA)',
  744:                    unix => 'Filesystem (UNIX)',
  745:                    local => 'Local/Customized',
  746:                    );
  747:    return;
  748: }
  749: 
  750: sub store_mail {
  751:     my ($subject,$message,$domain,$recipients,$attachmenturl,$ltext) = @_;
  752:     my %servers = ();
  753:     my $msgid=&packagemail($subject,$message,$domain,
  754:                            $recipients,\%servers,$attachmenturl);
  755: # Store in dc email db files on appropriate servers.
  756:     foreach my $server (keys(%servers)) {
  757:         unless (&Apache::lonnet::dcmailput($domain,$msgid,\%servers,$server) eq 'ok') {
  758:             &Apache::lonnet::logthis('Storage of dc mail failed for domain'.$domain.' for server: '.
  759:                       $server.'.  Message ID was '.$msgid);
  760:         }
  761:     }
  762: }
  763: 
  764: sub packagemail {
  765:     my ($subject,$message,$dom,$recipients,$servers,$attachmenturl,$ltext) = @_;
  766:     my %record = ();
  767:     my $partsubj=$subject;
  768:     $partsubj=&Apache::lonnet::escape($partsubj);
  769:     $message =&HTML::Entities::encode($message,'<>&"');
  770:     $subject =&HTML::Entities::encode($subject,'<>&"');
  771:     #remove machine specification
  772:     $attachmenturl =~ s|^http://[^/]+/|/|;
  773:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
  774:     my $now=time;
  775:     my $msgid= &Apache::lonnet::escape($now).':'.$partsubj.':'.
  776:            &Apache::lonnet::escape($env{'user.name'}).':'.
  777:            &Apache::lonnet::escape($env{'user.domain'}).':'.
  778:            &Apache::lonnet::escape($dom).':'.$$;
  779:     my $result='<sendername>'.$env{'user.name'}.'</sendername>'.
  780:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
  781:            '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  782:            '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  783:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  784:            '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  785:            '<msgid>'.$msgid.'</msgid>'.
  786:            '<dcdomain>'.$dom.'</dcdomain>'.
  787:            '<subject>'.$subject.'</subject>'.
  788:            '<message>'.$message.'</message>'."\n";
  789:     if (defined($attachmenturl)) {
  790:         $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  791:     }
  792:     foreach my $recip (@{$recipients}) {
  793:         my ($username,$email) = split(/:/,$recip);
  794:         $username = &Apache::lonnet::unescape($username);
  795:         $email = &Apache::lonnet::unescape($email);
  796:         my ($uname,$udom) = split(/:/,$username);
  797:         my $uhom=&Apache::lonnet::homeserver($uname,$udom);
  798:         if ($uhom ne 'no_host') {
  799:             $username = &HTML::Entities::encode($username,'<>&"');
  800:             $email = &HTML::Entities::encode($email,'<>&"');
  801:             $record{$uhom} .= '<recipient username="'.$username.'">'.
  802:                               $email.'</recipient>';
  803:         }
  804:     }
  805:     foreach my $server (keys(%record)) {
  806:         $$servers{$server} = $result.$record{$server};
  807:     }
  808:     return $msgid;
  809: }
  810: 
  811: sub unpackagemail {
  812:     my ($message,$notoken,$ltext)=@_;
  813:     my $parser=HTML::TokeParser->new(\$message);
  814:     my $token;
  815:     my %content=();
  816:     %{$content{recipients}} = ();
  817:     while ($token=$parser->get_token()) {
  818:         if ($token->[0] eq 'S') {
  819:             my $entry=$token->[1];
  820:             my $value=$parser->get_text('/'.$entry);
  821:             my ($username,$email);
  822:             if ($entry eq 'recipient') {
  823:                 $username = $token->[2]{'username'};
  824:                 $username = &HTML::Entities::decode($username,'<>&"');
  825:                 $content{recipients}{$username} = 
  826:                       &HTML::Entities::decode($value,'<>&"');
  827:             } elsif ($entry eq 'subject' || $entry eq 'message') {
  828:                 $content{$entry}=&HTML::Entities::decode($value,'<>&"');
  829:             } else {
  830:                 $content{$entry}=$value;
  831:             }
  832:         }
  833:     }
  834:     if ($content{'attachmenturl'}) {
  835:         my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
  836:         if ($notoken) {
  837:             $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';       } else {
  838:             &Apache::lonnet::allowuploaded('/adm/notify',
  839:                                           $content{'attachmenturl'});
  840:             $content{'message'}.='<p>'.&mt('Attachment').
  841:                ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
  842:                $fname.'</tt></a>';
  843:         }
  844:     }
  845:     return %content;
  846: }
  847: 
  848: sub echo_form_input {
  849:     my (@excluded) = @_;
  850:     my $output = '';
  851:     foreach my $key (keys(%env)) {
  852:         if ($key =~ /^form\.(.+)$/) {
  853:             if ((!@excluded) || (!grep/^$1$/,@excluded)) {
  854:                 if (ref($env{$key})) {
  855:                     foreach my $value (@{$env{$key}}) {
  856:                         $output .= qq(<input type="hidden" name="$1" value="$value" />\n);
  857:                     }
  858:                 } else {
  859:                     $output .= qq(<input type="hidden" name="$1" value="$env{$key}" />\n);
  860:                 }
  861:             }
  862:         }
  863:     }
  864:     return $output;
  865: }
  866: 
  867: sub cr_to_br {
  868:     my $incoming = shift;
  869:     $incoming =~ s/\n/\<br \/\>/g;
  870:     return $incoming;
  871: }
  872: 
  873: 
  874: 1;

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