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