Annotation of loncom/interface/lonnotify.pm, revision 1.38

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

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