[LON-CAPA-cvs] cvs: loncom /interface lonnotify.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Tue, 02 Aug 2005 05:03:12 -0000


This is a MIME encoded message

--raeburn1122958992
Content-Type: text/plain

raeburn		Tue Aug  2 01:03:12 2005 EDT

  Added files:                 
    /loncom/interface	lonnotify.pm 
  Log:
  Saving my work. Untility for use by DCs to send e-mail to active users, classified by role, year, semester, department etc. Incomplete. 
  
  
--raeburn1122958992
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20050802010312.txt"


Index: loncom/interface/lonnotify.pm
+++ loncom/interface/lonnotify.pm
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
                                                                                
package Apache::lonnotify;
                                                                                
use strict;
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonsupportreq;
use LONCAPA::Enrollment;
use Apache::Constants qw(:common :http);
use Apache::lonlocal;

sub handler {
    my ($r) = @_;
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;

    if ($r->header_only) {
        return OK;
    }
#    my $codedom = $env{'request.role.domain'};
    my $cdom = 'northwood5';
    unless (&Apache::lonnet::allowed('psa',$env{'request.role.domain'})) {
        # Not allowed to broadcast e-mail system-wide 
        $env{'user.error.msg'}="/adm/notify:psa:0:0:Cannot broadcast e-mail systemwide";
        return HTTP_NOT_ACCEPTABLE;
    }

    my $command = $env{'form.command'};
    &Apache::lonhtmlcommon::clear_breadcrumbs();
    &Apache::lonhtmlcommon::add_breadcrumb
        ({href=>'/adm/notify',
          text=>"Broadcast e-mail"});
    if ($command eq 'process') {
        &print_request_receipt($r,$cdom);
    } elsif ($command eq 'compose') {
        &print_composition_form($r,$cdom);
    } else {
        &print_selection_form($r,$cdom);
    }
    return OK;
}

sub print_selection_form {
    my ($r,$cdom) = @_;
    my %coursecodes = ();
    my %codes = ();
    my @codetitles = ();
    my %cat_titles = ();
    my %cat_order = ();
    my %idlist = ();
    my %idnums = ();
    my %idlist_titles = ();
    my $caller = 'global';
    my $totcodes = 0;
    my $format_reply;
    my $jscript = '';
    my $loaditems = qq|
function initialize_codes() {
    return;
}
    |;
    $totcodes = &Apache::lonsupportreq::retrieve_instcodes(\%coursecodes,$cdom,$totcodes);
    if ($totcodes > 0) {
        $format_reply = &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
        if ($format_reply eq 'ok') {
            my $numtypes = @codetitles;
            &Apache::lonsupportreq::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
            &Apache::lonsupportreq::javascript_code_selections($numtypes,\%cat_titles,\$jscript,\%idlist,\%idnums,\%idlist_titles,\@codetitles);
            $loaditems = '';
        }
    }

    my $function = &Apache::loncommon::get_users_function();
    my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
    my $bodytag = &Apache::loncommon::bodytag('Broadcast e-mail to users');
    my $html=&Apache::lonxml::xmlbegin();
    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
            (undef,'Broadcast e-mail to users','Broadcast_system_email');
    $r->print(<<ENDONE);
$html
<head>
 <title>LON-CAPA Notification E-mail</title>
<script type"text/javascript">
$jscript
</script>
</head>
$bodytag
$breadcrumbs
<br />
<form method="post" name="logproblem">
 <table width="580" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
  <tr>
   <td>
    <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
     <tr>
      <td>
       <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
        <tr>
         <td>
          <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
           <tr>
            <td width="140" bgcolor="$tablecolor">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Roles:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top">
             <table width="100%" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td>
                <select name="roles" multiple>
ENDONE
    foreach  ('cc','in','ta','ep','ad') {
        my $plrole=&Apache::lonnet::plaintext($_);
        $r->print('  <option value="'.$_.'">'.$plrole.'</option>');
    }
    $r->print(<<ENDTWO);
                </select>
               </td>
              </tr>
             </table>
            </td>
           </tr>
           <tr>
            <td width="140" bgcolor="$tablecolor">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Courses:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top">
             <table width="100%" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td>
ENDTWO
    if ($totcodes > 0) {
        my $numtitles = @codetitles;
        if ($numtitles == 0) {
            $r->print('No institutional course code categories found.<br />Can not select courses to receive e-mail.');
        } else {
            $r->print('<table><tr><td>'.$codetitles[0].'<br />'."\n".
               '<select name="'.$codetitles[0].
               '" onChange="courseSet('."'$codetitles[0]'".')">'."\n".
               ' <option value="-1" />Select'."\n");
            my @items = ();
            my @longitems = ();
            if ($idlist{$codetitles[0]} =~ /","/) {
                @items = split/","/,$idlist{$codetitles[0]};
            } else {
                $items[0] = $idlist{$codetitles[0]};
            }
            if (defined($idlist_titles{$codetitles[0]})) {
                if ($idlist_titles{$codetitles[0]} =~ /","/) {
                    @longitems = split/","/,$idlist_titles{$codetitles[0]};
                } else {
                    $longitems[0] = $idlist_titles{$codetitles[0]};
                }
                for (my $i=0; $i<@longitems; $i++) {
                    if ($longitems[$i] eq '') {
                        $longitems[$i] = $items[$i];
                    }
                }
            } else {
                @longitems = @items;
            }
            for (my $i=0; $i<@items; $i++) {
                $r->print(' <option value="'.$items[$i].'">'.$longitems[$i].'</option>');
            }
            $r->print('</select></td>');
            for (my $i=1; $i<$numtitles; $i++) {
                $r->print('<td>'.$codetitles[$i].'<br />'."\n".
                          '<select name="'.$codetitles[$i].
                          '" onChange="courseSet('."'$codetitles[$i]'".')">'."\n".
                          '<option value="-1">&lt;-Pick '.$codetitles[$i-1].'</option>'."\n".
                          '</select>'."\n".
                          '</td>'
                );
            }
            $r->print('</tr></table>');
        }
    }
    $r->print(<<END);
               </td>
              </tr>
             </table>
            </td>
           </tr>
           <tr>
            <td width="140" bgcolor="$tablecolor">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Submit:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top" align="right">
             <br />
             <input type="hidden" name="command" value="compose" />
             <input type="submit" value="Compose Message"/> &nbsp;
             <br /><br />
            </td>
           </tr>
          </table>
         </td>
        </tr>
       </table>
      </td>
     </tr>
    </table>
   </td>
  </tr>
 </table>
</form>
</body>
</html>
END
    return;
}

sub print_composition_form {
    my ($r,$cdom) = @_;
    my $function = &Apache::loncommon::get_users_function();
    my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
    my $bodytag = &Apache::loncommon::bodytag('Broadcast e-mail to users');
    my $html=&Apache::lonxml::xmlbegin();
    &Apache::lonhtmlcommon::add_breadcrumb
        ({href=>'/adm/notify?command=compose',
          text=>"Compose Message"});
    my $jscript = <<"END";
function checkAll(field) {
    if (field.length > 0) {
        for (i = 0; i < field.length; i++) {
            field[i].checked = true ;
        }
    } else {
        field.checked = true
    }
}
                                                                                         
function uncheckAll(field) {
    if (field.length > 0) {
        for (i = 0; i < field.length; i++) {
            field[i].checked = false ;
        }     } else {
        field.checked = false ;
    }
}
END
    my $breadcrumbs = (&Apache::lonhtmlcommon::breadcrumbs
        (undef,'Broadcast e-mail to users','Broadcast_system_email'));

    $r->print(<<ENDONE);
$html
<head>
 <title>LON-CAPA Notification E-mail</title>
<script type"text/javascript">
$jscript
</script>
</head>
$bodytag $breadcrumbs
<br /> 
ENDONE
    my $instcode = '';
    my @cats = ('Semester','Year','Department','Number');
    foreach my $category (@cats) {
        if (defined($env{'form.'.$category})) {
            unless ($env{'form.'.$category} eq '-1') {
                $instcode .= $env{'form.'.$category};
            }
        }
    }
    if ($instcode eq '') {
        $instcode = '.';
    }
    my %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.');
    my @roles = &Apache::loncommon::get_env_multiple('form.roles');
    my %recipients = ();
    foreach my $course_id (keys %courses) {
        &get_active_users($course_id,\@roles,\%recipients);
    }
    if (%recipients) {
        $r->print('<form name="compose" method="post"> 
<input type="hidden" command="process" />
 <table width="580" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
  <tr>
   <td>
    <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
     <tr>
      <td>
       <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
        <tr>
         <td>
          <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
           <tr>
            <td width="140" bgcolor="'.$tablecolor.'">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Subject:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top">
             <table width="100%" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td><input type="text" name="subject" size="30" />
               </td>
              </tr>
             </table>
            </td>
           </tr>
           <tr>
            <td width="140" bgcolor="'.$tablecolor.'">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right">Message:</td>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top">
             <table width="100%" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td>
                  <textarea name="message" id="message"
                   cols="60" rows="10" wrap="hard"></textarea>
               </td>
              </tr>
             </table>
            </td>
           </tr>
           <tr>
            <td width="140" bgcolor="'.$tablecolor.'">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Recipients:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top">
             <table width="100%" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td>
                <input type="button" value="check all"
onclick="javascript:checkAll(document.compose.email)" />
                &nbsp;&nbsp;<input type="button" value="uncheck all"
onclick="javascript:uncheckAll(document.compose.email)" /><br />
                <table border="0">');
        foreach my $username (sort keys %recipients) {
            if ($recipients{$username} =~ /\@/) {
                my $value=&Apache::lonnet::escape($username).':'.&Apache::lonnet::escape($recipients{$username});
                $r->print('<tr><td><input type="checkbox" name="email" value="'.$value.'" checked="checked" /></td><td>'.$username.'</td><td>'.$recipients{$username}.'</td></tr>');
                $numrecep ++;
            }
        }
        $r->print('
                </table>
               </td>
              </tr>
             </table>
            </td>
           </tr>
           <tr>
            <td width="140" bgcolor="'.$tablecolor.'">
             <table width="140" border="0" cellpadding="8" cellspacing="0">
              <tr>
               <td align="right"><b>Submit:</b>
               </td>
              </tr>
             </table>
            </td>
            <td width="100%" valign="top" align="right">
             <br />
             <input type="hidden" name="command" value="compose" />
             <input type="submit" name="sendemail" value="Send Message"/> &nbsp;
             <br /><br />
            </td>
           </tr>
          </table>
         </td>
        </tr>
       </table>
      </td>
     </tr>
    </table>
   </td>
  </tr>
 </table>
</form>');
    } else {
        $r->print('No recipients identified');
    }
    $r->print('</body></html>');
    return;
}


sub print_request_receipt {
    my ($r,$dom) =@_;
    my @recipients = &Apache::loncommon::get_env_multiple('recipients');
    my $subject = $env{'form.subject'};
    my $message = $env{'form.message'};
    my $function = &Apache::loncommon::get_users_function();
    my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
    my $bodytag = &Apache::loncommon::bodytag('Broadcast e-mail to users');
    my $html=&Apache::lonxml::xmlbegin();
    my $jscript;
    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
            (undef,'Broadcast e-mail to users','Broadcast_system_email');
    $r->print(<<ENDONE);
$html
<head>
 <title>LON-CAPA Notification E-mail</title>
<script type"text/javascript">
$jscript
</script>
</head>
$bodytag
$breadcrumbs
ENDONE
    $r->print($subject.'<br /><br />');
    $r->print($message.'<br /><br />');
    foreach my $person (@recipients) {
        my ($username,$email) = split/:/,$person;
        $r->print('user: '.&Apache::lonnet::unescape($username).' email: '.&Apache::lonnet::unescape($email).'<br />');
    }
    return;
}

sub get_active_users {
    my ($course_id,$roles,$recipients) = @_;
    if (@{$roles} > 0) {
        my ($cdom,$cnum) = split/_/,$course_id;
        my @coursepersonnel = &Apache::lonnet::getkeys('nohist_userroles',$cdom,$cnum);
        foreach my $person (@coursepersonnel) {
            my ($role,$user) = ($person =~ /^([^:]*):(.+)$/);
            if (($role) && (grep/^$role$/,@{$roles})) {
                unless (exists($$recipients{$user})) {
                    my ($uname,$udom) = split/:/,$user;
                    if ($uname ne '' && $udom ne '') {
                        if (&LONCAPA::Enrollment::check_user_status($udom,$uname,$cdom,$cnum,$role) eq 'ok') {
                            my %userinfo = &Apache::lonnet::get('environment',['permanenetemail','notification','critnotification',],$udom,$uname);
                            my @emailtypes = ('permanentemail','critnotification','notification');
                            my $email = '';
                            foreach (@emailtypes) {
                                $email = $userinfo{$_};
                                if ($email =~ /\@/) {
                                    last;    
                                }
                            }
                            if ($email eq '') {
                               my $authinfo  = &Apache::lonnet::queryauthenticate($uname,$udom);
                               my ($authtype,$autharg) = split/:/,$authinfo;
                               if (($authtype =~ /^krb/) && ($autharg =~ /^MSU/)) {
                                   $email = $uname.'@msu.edu';
                               }  
                            }
                            $$recipients{$user} = $email;
                        }
                    }
                }
            }
        }
    }
}

1;

--raeburn1122958992--