File:  [LON-CAPA] / loncom / interface / lonnotify.pm
Revision 1.4: download - view: text, annotated - select for diffs
Tue Oct 11 21:29:39 2005 UTC (18 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Domain roles now stored in nohist_domainroles.db file in /home/httpd/lonUsers/$dom on library servers in domain.  To be switched to storage on a single "primary" library server.

lonnotify::print_display_option_form() uses contents of nohist_domainroles.db to present choice of Domain Coordinators when selecting options for display of sent mail.

lond routines added for storage of broadcase dcmail.

Future consolidation of put/dump lond routines for domain-level db files is desirable.

    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:         print STDERR "key level 1 is $server\n"; 
  180:         foreach my $user (sort(keys %{$personnel{$server}})) {
  181:             print STDERR "key level 2 is $user\n";
  182:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
  183:             unless (grep/^$uname:$udom$/,@domcc) {
  184:                 my %userinfo = &Apache::lonnet::get('environment',['lastname','firstname'],$udom,$uname);
  185:                 $output .= '<input type="checkbox" name="sender" value="'.$uname.':'.$udom.'" />&nbsp;'.$userinfo{firstname}.' '.$userinfo{lastname}.'&nbsp;&nbsp;('.$uname.':'.$udom.')';
  186:                 push (@domcc,$uname.':'.$udom);
  187:             }
  188:         }
  189:     }
  190:     $output .= '</td>';
  191:     $output .= &Apache::lonhtmlcommon::row_closure();
  192:     $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
  193:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  194:     $output .= qq(
  195: <input type="hidden" name="sortby" value="date" />
  196: </form>
  197: </body>
  198: </html>);
  199:     $r->print($output);
  200:     return;
  201: }
  202: 
  203: sub print_display {
  204:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  205:     &Apache::lonhtmlcommon::add_breadcrumb
  206:          ({href=>'/adm/notify?command=pick_display',
  207:           text=>"Display options"},
  208:          {text=>"E-mail display"});
  209:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  210:             (undef,'Display Broadcast e-mail','Broadcast_system_email');
  211:     my $table_width = '';
  212:     my $col_width = '200';
  213:     my $rowColor1 = "#ffffff";
  214:     my $rowColor2 = "#eeeeee";
  215:     my $rowColor;
  216:     my $msgcount = 0;
  217:     my $formname = 'display_sent';
  218:     my $start = &Apache::lonhtmlcommon::get_date_from_form('startdate');
  219:     my $end = &Apache::lonhtmlcommon::get_date_from_form('enddate');
  220:     my @senders = &Apache::loncommon::get_env_multiple('form.sender');
  221:     my $senderlist = join('&',@senders); 
  222:     my %sentmail = &Apache::lonnet::dcmaildump($cdom,$start,$end,$senderlist);
  223:     my %dcmail = ();
  224:     my %Sortby = ();
  225:     my $jscript = <<"ENDSCRIPT";
  226: function changeSort(caller) {
  227:     document.$formname.sortby.value = caller;
  228:     document.$formname.submit();   
  229: }
  230: ENDSCRIPT
  231:     my $output = <<"ENDONE";
  232: $html
  233: <head>
  234:  <title>LON-CAPA $$ltext{'note'}</title>
  235:  <script type"text/javascript">
  236: $jscript
  237:  </script>
  238: </head>
  239: $bodytag
  240: $breadcrumbs
  241: <br />
  242: <form method="post" name="$formname">
  243: ENDONE
  244: 
  245:     foreach my $server (keys(%sentmail)) {
  246:         foreach my $msgid (keys(%{$sentmail{$server}})) {
  247:             my %content = &unpackagemail($sentmail{$server}{$msgid});
  248:             if (defined($dcmail{$msgid})) {
  249:                 foreach my $user (keys(%{$content{'recipients'}})) {
  250:                     $dcmail{$msgid}{recipients}{$user} = $content{recipients}{$user};
  251:                 }
  252:             } else {
  253:                 $msgcount ++;
  254:                 %{$dcmail{$msgid}} = ();
  255:                 foreach my $item (keys(%content)) {
  256:                     if ($item eq 'recipients') {
  257:                         foreach my $user (keys(%{$content{recipients}})) {
  258:                             $dcmail{$msgid}{recipients}{$user} = $content{recipients}{$user};
  259:                         }
  260:                     } else {
  261:                         $dcmail{$msgid}{$item} = $content{$item};
  262:                     }
  263:                 }
  264:             }
  265:         }
  266:     }
  267:     $output .= &Apache::lonhtmlcommon::start_pick_box();
  268:     if ($msgcount > 0) {
  269:         my $rowNum = 0;
  270:         $output .= '<tr><td><table cellpadding="4" cellspacing="2" width="100%">
  271:                    <tr bgcolor="'.$tablecolor.'" align="center">
  272:                    <td><b><a href="javascript:changeSort('."'date'".')">Date</a></b></td>
  273:                    <td><b><a href="javascript:changeSort('."'subject'".')">Subject</a></b></td>
  274:                    <td><b><a href="javascript:changeSort('."'sender'".')">Sender</a></b></td>
  275:                    <td><b><a href="javascript:changeSort('."'message'".')">Message</a></b></td>
  276:                    <td><b><a href="javascript:changeSort('."'recipients'".')">Recipients</a></b></td>
  277:                    </tr>';
  278:         if (($env{'form.sortby'} eq 'date') || ($env{'form.sortby'} eq '') || (!defined($env{'form.sortby'})) || (($env{'form.sortby'} eq 'sender') && (@senders <= 1))) {
  279:             foreach my $msgid (sort(keys(%dcmail))) {
  280:                 if ($rowNum %2 == 1) {
  281:                     $rowColor = $rowColor1;
  282:                 } else {
  283:                     $rowColor = $rowColor2;
  284:                 }
  285:                 my $recipients = '';
  286:                 my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  287:                 $date = &Apache::lonlocal::locallocaltime($date);
  288:                 foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  289:                     $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  290:                 }
  291:                 $recipients =~ s/,\s$//;
  292:                 $output .= '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.$dcmail{$msgid}{subject}.'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.$dcmail{$msgid}{message}.'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
  293:                 $rowNum ++;
  294:             }
  295:         } else {
  296:             foreach my $msgid (sort(keys(%dcmail))) {
  297:                 my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  298:                 if ($env{'form.sortby'} eq 'subject') {
  299:                     push @{$Sortby{$dcmail{$msgid}{subject}}},$msgid;
  300:                 } elsif ($env{'form.sortby'} eq 'message') {
  301:                     push @{$Sortby{$dcmail{$msgid}{message}}},$msgid;
  302:                 } elsif ($env{'form.sortby'} eq 'recipients') {
  303:                     my $recipients ='';
  304:                     foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  305:                         $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  306:                     }
  307:                     $recipients =~ s/,\s$//;
  308:                     push @{$Sortby{$recipients}},$msgid;
  309:                 } elsif ($env{'form.sortby'} eq 'sender') {
  310:                     if (@senders > 1) {
  311:                        push @{$Sortby{$sname.':'.$sdom}},$msgid;
  312:                     }
  313:                 }
  314:             }
  315:             foreach my $key (sort(keys(%Sortby))) {
  316:                 foreach my $msgid (@{$Sortby{$key}}) {
  317:                     if ($rowNum %2 == 1) {
  318:                         $rowColor = $rowColor1;
  319:                     } else {
  320:                         $rowColor = $rowColor2;
  321:                     }
  322:                     my $recipients = '';
  323:                     if ($env{'form.sortby'} eq 'recipients') {
  324:                         $recipients = $key;
  325:                     } else {
  326:                         foreach my $user (sort(keys(%{$dcmail{$msgid}{recipients}}))) {
  327:                             $recipients .= $dcmail{$msgid}{recipients}{$user}.', ';
  328:                         }
  329:                         $recipients =~ s/,\s$//;
  330:                     }
  331:                     my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
  332:                     $date = &Apache::lonlocal::locallocaltime($date);
  333:                     $output .=  '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.$dcmail{$msgid}{subject}.'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.$dcmail{$msgid}{message}.'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
  334:                     $rowNum ++;
  335:                 }
  336:             }
  337:         }
  338:         $output .= '</table></td></tr>';
  339:     } else {
  340:         $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>';
  341:     }
  342:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  343:     $output .= &echo_form_input();
  344:     unless (defined($env{'form.sortby'})) {
  345:         $output .= qq(<input type="hidden" name="sortby" value="date" />\n);
  346:     } 
  347:     $output .= qq(
  348: </form>
  349: </body>
  350: </html>);
  351:     $r->print($output);
  352:     return;
  353: }
  354: 
  355: sub print_selection_form {
  356:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  357:     my %coursecodes = ();
  358:     my %codes = ();
  359:     my @codetitles = ();
  360:     my %cat_titles = ();
  361:     my %cat_order = ();
  362:     my %idlist = ();
  363:     my %idnums = ();
  364:     my %idlist_titles = ();
  365:     my $caller = 'global';
  366:     my $totcodes = 0;
  367:     my $format_reply;
  368:     my $jscript = '';
  369:     my $formname = 'rolefilter';
  370:     my $table_width = '100%';
  371:     my $col_width = '200';
  372:     my %lt=&Apache::lonlocal::texthash(
  373:                'note' => 'Notification E-mail', 
  374:                'buil' => 'Building valid e-mail address from username, if missing from preferences:',
  375:                'kerb' => 'Kerberos: enter default for each realm used in the domain, with comma separation of entries',
  376:                'infs' => 'Internal, Filesystem and Local authentication: enter single default.',
  377:                'comp' => 'Compose Message'
  378:            );
  379:     my $loaditems = qq|
  380: function initialize_codes() {
  381:     return;
  382: }
  383:     |;
  384:     &Apache::lonhtmlcommon::add_breadcrumb
  385:           ({text=>"Select Audience"});
  386: 
  387:     $totcodes = &Apache::lonsupportreq::retrieve_instcodes(\%coursecodes,$cdom,$totcodes);
  388:     if ($totcodes > 0) {
  389:         $format_reply = &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
  390:         if ($format_reply eq 'ok') {
  391:             my $numtypes = @codetitles;
  392:             &Apache::lonsupportreq::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
  393:             &Apache::lonsupportreq::javascript_code_selections($formname,$numtypes,\%cat_titles,\$jscript,\%idlist,\%idnums,\%idlist_titles,\@codetitles);
  394:             $loaditems = '';
  395:         }
  396:     }
  397: 
  398:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  399:             (undef,'Choose e-mail audience','Broadcast_system_email');
  400:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
  401:     my $output = <<"ENDONE";
  402: $html
  403: <head>
  404:  <title>LON-CAPA $lt{'note'}</title>
  405: <script type"text/javascript">
  406: $jscript
  407: </script>
  408: $cb_jscript
  409: </head>
  410: $bodytag
  411: $breadcrumbs
  412: <br />
  413: <form method="post" name="$formname">
  414: ENDONE
  415:    $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
  416:    my @roles = ('cc','in','ta','ep','ad','st','cr');
  417:    my %longtypes = ();
  418:    my %authtypes = ();
  419:    &form_elements(\%longtypes,\%authtypes);
  420:    my $descrip = $lt{'buil'}.' 
  421: <ul>
  422: <li>'.$lt{'kerb'}.'<br />(e.g., MSU.EDU=msu.edu, MSUE.EDU=msue.msu.edu).</li>
  423: <li>'.$lt{'infs'}.'</li>
  424: </ul>'."\n";
  425:    my $submit_text = $lt{'comp'};
  426:    my $cmd = 'compose';
  427:    $output .= &Apache::lonhtmlcommon::role_select_row(\@roles,$col_width,$tablecolor,'Roles');
  428:    $output .= &Apache::lonhtmlcommon::course_select_row($col_width,$tablecolor,'Courses',$formname,$totcodes,\@codetitles,\%idlist,\%idlist_titles);
  429:    $output .= &Apache::lonhtmlcommon::status_select_row(\%longtypes,$col_width,$tablecolor,&mt('Access status'));
  430:    $output .= &Apache::lonhtmlcommon::email_default_row(\%authtypes,$col_width,$tablecolor,&mt('Username -> Email conversion'),$descrip);
  431:    $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
  432:    $output .= &Apache::lonhtmlcommon::end_pick_box();
  433:    $output .= qq(
  434: </form>
  435: </body>
  436: </html>);
  437:     $r->print($output);
  438:     return;
  439: }
  440: 
  441: sub print_composition_form {
  442:     my ($r,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
  443:     &Apache::lonhtmlcommon::add_breadcrumb
  444:         ({href=>'/adm/notify?command=pick_target',
  445:           text=>"Select Audience"},
  446:          {text=>"Compose Message"});
  447:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
  448:     my $breadcrumbs = (&Apache::lonhtmlcommon::breadcrumbs
  449:         (undef,'Broadcast e-mail to users','Broadcast_system_email'));
  450: 
  451:     my %lt=&Apache::lonlocal::texthash(
  452:                       'note' => 'Notification E-mail',
  453:                       'nore' => 'No recipients identified',
  454:                       'emad' => 'e-mail address',
  455:                    );
  456:     $r->print(<<ENDONE);
  457: $html
  458: <head>
  459:  <title>LON-CAPA $lt{'note'}</title>
  460: <script type"text/javascript">
  461: $jscript
  462: </script>
  463: </head>
  464: $bodytag $breadcrumbs
  465: <br /> 
  466: ENDONE
  467:     my $coursefilter = $env{'form.coursepick'};
  468:     my %courses = ();
  469:     if ($coursefilter eq 'all') {
  470:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.');
  471:     } elsif ($coursefilter eq 'category') {
  472:         my $instcode = '';
  473:         my @cats = ('Semester','Year','Department','Number');
  474:         foreach my $category (@cats) {
  475:             if (defined($env{'form.'.$category})) {
  476:                 unless ($env{'form.'.$category} eq '-1') {
  477:                     $instcode .= $env{'form.'.$category};
  478:                 }
  479:             }
  480:         }
  481:         if ($instcode eq '') {
  482:             $instcode = '.';
  483:         }
  484:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.');
  485:     } elsif ($coursefilter eq 'specific') {
  486:         if ($env{'form.coursetotal'} > 1) {
  487:             my @course_ids = split(/&&/,$env{'form.courselist'});
  488:             foreach (@course_ids) {
  489:                 $courses{$_} = '';
  490:             }
  491:         } else {
  492:             $courses{$env{'form.courselist'}} = '';
  493:         }
  494:     }
  495: 
  496:     my @types = &Apache::loncommon::get_env_multiple('form.types');
  497:     my @roles = &Apache::loncommon::get_env_multiple('form.roles');
  498: 
  499:     my %longtypes = ();
  500:     my %authtypes = ();
  501:     my %email_defaults = ();
  502:     my $table_width = '100%';
  503:     my $col_width = '200';
  504: 
  505:     &form_elements(\%longtypes,\%authtypes);
  506:     foreach my $auth (keys(%authtypes)) {
  507:         if (exists($env{'form.'.$auth})) {
  508:              my $default = $env{'form.'.$auth};
  509:              $default =~ s/^,+//;
  510:              $default =~ s/,+$//;
  511:              if ($auth =~ /^krb/) {
  512:                  %{$email_defaults{$auth}} = ();
  513:                  if ($default =~ /,/) {
  514:                      my @items = split(/,/,$default);
  515:                      foreach my $item (@items) {
  516:                          my ($realm,$value) = split(/=/,$item);
  517:                          $email_defaults{$auth}{$realm} = $value;
  518:                      }
  519:                  } else {
  520:                      my ($realm,$value) = split(/=/,$default);
  521:                      $email_defaults{$auth}{$realm} = $value;
  522:                  }
  523:              } else {
  524:                  $email_defaults{$auth} = $default;
  525:              }
  526:          }
  527:     }
  528: 
  529:     my $sender = &get_user_info($env{'user.name'},%email_defaults);
  530: 
  531:     my %recipients = ();
  532:     my %users = ();
  533:     my %access = ();
  534:     my $totalrecip = 0;
  535:     my @unmatched = ();
  536:     foreach my $role (@roles) {
  537:         %{$users{$role}} = ();
  538:     }
  539:     foreach my $type (@types) {
  540:         $access{$type} = $type;
  541:     }
  542:     foreach my $course_id (keys(%courses)) {
  543:         my ($cdom,$cnum) = split(/_/,$course_id);
  544:         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\%users);
  545:     }
  546:     foreach my $role (keys(%users)) {
  547:         foreach my $user (keys(%{$users{$role}})) {
  548:             unless (defined($recipients{$user})) {
  549:                 $recipients{$user} = &get_user_info($user,%email_defaults);
  550:                 if ($recipients{$user} eq '') {
  551:                     push @unmatched, $user;
  552:                 } else {
  553:                     $totalrecip ++;
  554:                 } 
  555:             }
  556:         }
  557:     }
  558:     my $output  = '<form name="compose" method="post">'."\n";
  559:   
  560:     if ($totalrecip > 0) {
  561:         $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
  562:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Subject'));
  563:         $output .= ' <td><input type="text" name="subject" size="30" /></td>';
  564:         $output .= &Apache::lonhtmlcommon::row_closure();
  565:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Message'));
  566:         $output .= '  <td><textarea name="message" id="message"
  567:                       cols="60" rows="10" wrap="hard"></textarea></td>';
  568:         $output .= &Apache::lonhtmlcommon::row_closure();
  569:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Recipients'));
  570:         $output .= '<td><input type="button" value="check all" 
  571:                     onclick="javascript:checkAll(document.compose.recipient)" />
  572:                     &nbsp;&nbsp;<input type="button" value="uncheck all"
  573:                     onclick="javascript:uncheckAll(document.compose.recipient)" />
  574:                     <br /><table border="0">';
  575:         if (keys(%recipients) > 0) {
  576:             $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>';
  577:         }
  578:         foreach my $username (sort(keys(%recipients))) {
  579:             if ($recipients{$username} =~ /\@/) {
  580:                 my $value=&Apache::lonnet::escape($username).':'.&Apache::lonnet::escape($recipients{$username});
  581:                 $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>';
  582:             }
  583:         }
  584:         $output .= '</table></td>';
  585:         $output .= &Apache::lonhtmlcommon::row_closure();
  586:         $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Sender e-mail address'));
  587:         $output .= '<td><input type="text" name="sender" value="'.$sender.'" /></td>';
  588:         $output .= &Apache::lonhtmlcommon::row_closure();
  589:         $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),'process',&mt('Send Message'));
  590:         $output .= &Apache::lonhtmlcommon::end_pick_box();
  591:     } else {
  592:         $output .= $lt{'nore'};
  593:     }
  594:     $output .= &echo_form_input('command');
  595:     $output .= '</form></body></html>';
  596:     $r->print($output);
  597:     return;
  598: }
  599: 
  600: 
  601: sub print_request_receipt {
  602:     my ($r,$dom,$tablecolor,$bodytag,$html,$ltext) =@_;
  603:     my @recipients = &Apache::loncommon::get_env_multiple('form.recipient');
  604:     my $subject = $env{'form.subject'};
  605:     my $message = $env{'form.message'};
  606:     my $from = $env{'form.sender'};
  607:     my $jscript = <<ENDSCRIPT;
  608: function showCompose() {
  609:     document.goback.command.value = 'compose';
  610:     document.goback.submit();
  611: }
  612: ENDSCRIPT
  613:     &Apache::lonhtmlcommon::add_breadcrumb
  614:         ({href=>'/adm/notify?command=pick_target',
  615:           text=>"Select audience"});
  616:     &Apache::lonhtmlcommon::add_breadcrumb
  617:         ({href=>'javascript:showCompose()',
  618:           text=>"Compose Message"});
  619:     &Apache::lonhtmlcommon::add_breadcrumb
  620:         ({href=>'/adm/notify?command=process',
  621:           text=>"Outcome"});
  622:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
  623:             (undef,'E-mail Delivery','Broadcast_system_email');
  624:     my $output = <<ENDONE;
  625: $html
  626: <head>
  627:  <title>LON-CAPA Notification E-mail</title>
  628: <script type"text/javascript">
  629: $jscript
  630: </script>
  631: </head>
  632: $bodytag
  633: $breadcrumbs
  634: <br />
  635: <form name="goback" method="post">
  636: ENDONE
  637:     $output .= &Apache::lonhtmlcommon::start_pick_box();
  638:     my @deliveries = ();
  639:     &broadcast_email(\@recipients,$subject,$from,$message,\@deliveries);
  640:     if (@deliveries > 0) {
  641:         $output .= '<tr>
  642:                      <td>
  643:                       <table cellpadding="4" cellspacing="2" width="100%">
  644:                        <tr bgcolor="'.$tablecolor.'" align="center">
  645:                         <td><b>Status</b></td>
  646:                         <td><b>Subject</b></td>
  647:                         <td><b>Message</b></td>
  648:                         <td><b>Recipients</b></td>
  649:                        </tr>
  650:                        <tr bgcolor="#eeeeee">
  651:                         <td valign="middle">Sent</td>
  652:                         <td valign="middle">'.$subject.'</td>
  653:                         <td valign="middle">'.$message.'</td>
  654:                         <td>';
  655:         foreach my $person (@deliveries) {
  656:             my ($username,$email) = split(/:/,$person);
  657:             $output .= &Apache::lonnet::unescape($email).'&nbsp;('.&Apache::lonnet::unescape($username).')<br />'."\n";
  658:         }
  659:         $output .= '</td>
  660:                    </tr>
  661:                   </table>
  662:                  </td>
  663:                 </tr>';
  664:         &store_mail($subject,$message,$dom,\@deliveries);
  665:     } else {
  666:         $output .= 'No mail sent - no recipients identified'; 
  667:     }
  668:     $output .= &Apache::lonhtmlcommon::end_pick_box();
  669:     $output .= '<br /><a href="/adm/notify">Send another message?</a>'."\n";
  670:     $output .= &echo_form_input();
  671:     $output .= '
  672: </form>
  673: </body>
  674: </html>';
  675:     $r->print($output);
  676:     return;
  677: }
  678: 
  679: sub broadcast_email {
  680:     my ($recipients,$subject,$from,$message,$deliveries,$ltext)=@_;
  681: # find some way to spread out delivery for large numbers of recipients.
  682:     foreach my $user (@{$recipients}) {
  683:         my $msg = new Mail::Send;
  684:         my ($username,$to) = split(/:/,$user);
  685:         $username = &Apache::lonnet::unescape($username);
  686:         $to = &Apache::lonnet::unescape($to);
  687:         $msg->to($to);
  688:         $msg->subject($subject);
  689:         $msg->add('From',"$from");
  690:         if (my $fh = $msg->open()) {
  691:             print $fh $message;
  692:             $fh->close;
  693:             push(@{$deliveries},$user); 
  694:         }
  695:     }
  696: }
  697: 
  698: sub get_user_info {
  699:     my ($user,%email_defaults,$ltext) = @_;
  700:     my ($uname,$udom) = split(/:/,$user);
  701:     my @emailtypes = ('permanentemail','critnotification','notification');
  702:     my %userinfo = &Apache::lonnet::get('environment',\@emailtypes,$udom,$uname);
  703:     my $email = '';
  704:     foreach my $type (@emailtypes) {
  705:         $email = $userinfo{$type};
  706:         if ($email =~ /\@/) {
  707:             last;
  708:         }
  709:     }
  710:     if ($email eq '') {
  711:         my $authinfo  = &Apache::lonnet::queryauthenticate($uname,$udom);
  712:         my ($authtype,$autharg) = split(/:/,$authinfo);
  713:         if ($authtype =~ /^krb/) {
  714:             if (defined($email_defaults{$authtype}{$autharg})) {
  715:                 $email = $uname.'@'.$email_defaults{$authtype}{$autharg};
  716:             }
  717:         } else {
  718:             if (defined($email_defaults{$authtype})) {
  719:                 $email = $uname.'@'.$email_defaults{$authtype};
  720:             }
  721:         }
  722:     }
  723:     return $email;
  724: }
  725: 
  726: sub form_elements {
  727:    my ($longtypes,$authtypes,$ltext) = @_;
  728:    %{$longtypes} = (
  729:                    active => 'Currently has access',
  730:                    previous => 'Previously had access',
  731:                    future => 'Will have future access',
  732:                    );
  733:    %{$authtypes} = (
  734:                    krb4 => 'Kerberos 4',
  735:                    krb5 => 'Kerberos 5',
  736:                    int => 'Internal (LON-CAPA)',
  737:                    unix => 'Filesystem (UNIX)',
  738:                    local => 'Local/Customized',
  739:                    );
  740:    return;
  741: }
  742: 
  743: sub store_mail {
  744:     my ($subject,$message,$domain,$recipients,$attachmenturl,$ltext) = @_;
  745:     my %servers = ();
  746:     my $msgid=&packagemail($subject,$message,$domain,
  747:                            $recipients,\%servers,$attachmenturl);
  748: # Store in dc email db files on appropriate servers.
  749:     foreach my $server (keys(%servers)) {
  750:         unless (&Apache::lonnet::dcmailput($domain,$msgid,\%servers,$server) eq 'ok') {
  751:             &logthis('Storage of dc mail failed for domain'.$domain.' for server: '.
  752:                       $server.'.  Message ID was '.$msgid);
  753:         }
  754:     }
  755: }
  756: 
  757: sub packagemail {
  758:     my ($subject,$message,$dom,$recipients,$servers,$attachmenturl,$ltext) = @_;
  759:     my %record = ();
  760:     my $partsubj=$subject;
  761:     $partsubj=&Apache::lonnet::escape($partsubj);
  762:     $message =&HTML::Entities::encode($message,'<>&"');
  763:     $subject =&HTML::Entities::encode($subject,'<>&"');
  764:     #remove machine specification
  765:     $attachmenturl =~ s|^http://[^/]+/|/|;
  766:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
  767:     my $now=time;
  768:     my $msgid= &Apache::lonnet::escape($now).':'.$partsubj.':'.
  769:            &Apache::lonnet::escape($env{'user.name'}).':'.
  770:            &Apache::lonnet::escape($env{'user.domain'}).':'.
  771:            &Apache::lonnet::escape($dom).':'.$$;
  772:     my $result='<sendername>'.$env{'user.name'}.'</sendername>'.
  773:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
  774:            '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  775:            '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  776:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  777:            '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  778:            '<msgid>'.$msgid.'</msgid>'.
  779:            '<dcdomain>'.$dom.'</dcdomain>'.
  780:            '<subject>'.$subject.'</subject>'.
  781:            '<message>'.$message.'</message>'."\n";
  782:     if (defined($attachmenturl)) {
  783:         $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  784:     }
  785:     foreach my $recip (@{$recipients}) {
  786:         my ($username,$email) = split(/:/,$recip);
  787:         $username = &Apache::lonnet::unescape($username);
  788:         $email = &Apache::lonnet::unescape($email);
  789:         my ($uname,$udom) = split(/:/,$username);
  790:         my $uhom=&Apache::lonnet::homeserver($uname,$udom);
  791:         if ($uhom ne 'no_host') {
  792:             $username = &HTML::Entities::encode($username,'<>&"');
  793:             $email = &HTML::Entities::encode($email,'<>&"');
  794:             $record{$uhom} .= '<recipient username="'.$username.'">'.
  795:                               $email.'</recipient>';
  796:         }
  797:     }
  798:     foreach my $server (keys(%record)) {
  799:         $$servers{$server} = $result.$record{$server};
  800:     }
  801:     return $msgid;
  802: }
  803: 
  804: sub unpackagemail {
  805:     my ($message,$notoken,$ltext)=@_;
  806:     my $parser=HTML::TokeParser->new(\$message);
  807:     my $token;
  808:     my %content=();
  809:     %{$content{recipients}} = ();
  810:     while ($token=$parser->get_token()) {
  811:         if ($token->[0] eq 'S') {
  812:             my $entry=$token->[1];
  813:             my $value=$parser->get_text('/'.$entry);
  814:             my ($username,$email);
  815:             if ($entry eq 'recipient') {
  816:                 $username = $token->[2]{'username'};
  817:                 $username = &HTML::Entities::decode($username,'<>&"');
  818:                 $content{recipients}{$username} = 
  819:                       &HTML::Entities::decode($value,'<>&"');
  820:             } elsif ($entry eq 'subject' || $entry eq 'message') {
  821:                 $content{$entry}=&HTML::Entities::decode($value,'<>&"');
  822:             } else {
  823:                 $content{$entry}=$value;
  824:             }
  825:         }
  826:     }
  827:     if ($content{'attachmenturl'}) {
  828:         my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
  829:         if ($notoken) {
  830:             $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';       } else {
  831:             &Apache::lonnet::allowuploaded('/adm/notify',
  832:                                           $content{'attachmenturl'});
  833:             $content{'message'}.='<p>'.&mt('Attachment').
  834:                ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
  835:                $fname.'</tt></a>';
  836:         }
  837:     }
  838:     return %content;
  839: }
  840: 
  841: sub echo_form_input {
  842:     my (@excluded) = @_;
  843:     my $output = '';
  844:     foreach my $key (keys(%env)) {
  845:         if ($key =~ /^form\.(.+)$/) {
  846:             if ((!@excluded) || (!grep/^$1$/,@excluded)) {
  847:                 if (ref($env{$key})) {
  848:                     foreach my $value (@{$env{$key}}) {
  849:                         $output .= qq(<input type="hidden" name="$1" value="$value" />\n);
  850:                     }
  851:                 } else {
  852:                     $output .= qq(<input type="hidden" name="$1" value="$env{$key}" />\n);
  853:                 }
  854:             }
  855:         }
  856:     }
  857:     return $output;
  858: }
  859: 
  860: 1;

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