File:  [LON-CAPA] / loncom / interface / lonnotify.pm
Revision 1.27: download - view: text, annotated - select for diffs
Tue Nov 13 03:53:30 2007 UTC (16 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
Style

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

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