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

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

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