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