# # 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(< LON-CAPA Notification E-mail $bodytag $breadcrumbs
Roles:
Courses:
ENDTWO if ($totcodes > 0) { my $numtitles = @codetitles; if ($numtitles == 0) { $r->print('No institutional course code categories found.
Can not select courses to receive e-mail.'); } else { $r->print(''); for (my $i=1; $i<$numtitles; $i++) { $r->print('' ); } $r->print('
'.$codetitles[0].'
'."\n". '
'.$codetitles[$i].'
'."\n". ''."\n". '
'); } } $r->print(<
Submit:

 

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(< LON-CAPA Notification E-mail $bodytag $breadcrumbs
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('
Subject:
Message:
Recipients:
  
'); foreach my $username (sort keys %recipients) { if ($recipients{$username} =~ /\@/) { my $value=&Apache::lonnet::escape($username).':'.&Apache::lonnet::escape($recipients{$username}); $r->print(''); $numrecep ++; } } $r->print('
'.$username.''.$recipients{$username}.'
Submit:

 

'); } else { $r->print('No recipients identified'); } $r->print(''); 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(< LON-CAPA Notification E-mail $bodytag $breadcrumbs ENDONE $r->print($subject.'

'); $r->print($message.'

'); foreach my $person (@recipients) { my ($username,$email) = split/:/,$person; $r->print('user: '.&Apache::lonnet::unescape($username).' email: '.&Apache::lonnet::unescape($email).'
'); } 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;