Annotation of loncom/interface/loncoursequeueadmin.pm, revision 1.26
1.1 raeburn 1: # The LearningOnline Network
1.19 raeburn 2: # Utilities to administer domain course requests and course self-enroll requests
1.1 raeburn 3: #
1.26 ! raeburn 4: # $Id: loncoursequeueadmin.pm,v 1.25 2010/12/08 03:38:46 raeburn Exp $
1.1 raeburn 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
1.23 raeburn 30: =pod
31:
1.1 raeburn 32: =head1 NAME
33:
34: Apache::loncoursequeueadmin.pm
35:
36: =head1 SYNOPSIS
37:
1.14 raeburn 38: Utilities used by domain coordinators to administer queued course creation requests,
39: and by course coordinators for queued self-enrollment requests, and by general
40: users to display their queued self-enrollment requests.
1.1 raeburn 41:
42: This is part of the LearningOnline Network with CAPA project
43: described at http://www.lon-capa.org.
44:
45: =head1 SUBROUTINES
46:
47: =over
48:
49: =item send_selfserve_notification()
50:
51: =item display_queued_requests()
52:
1.23 raeburn 53: =item build_queue_display()
54:
1.1 raeburn 55: =item update_request_queue()
56:
57: =item get_student_counts()
58:
1.13 raeburn 59: =item course_creation()
60:
61: =item build_batchcreatehash()
62:
63: =item can_clone_course()
64:
65: =item get_processtype()
66:
1.14 raeburn 67: =item queued_selfenrollment()
68:
1.23 raeburn 69: =item update_coursereq_status()
70:
71: =item process_official_reqs()
72:
1.1 raeburn 73: =back
74:
75: =cut
76:
77: package Apache::loncoursequeueadmin;
78:
79: use strict;
80: use Apache::lonnet;
81: use Apache::loncommon;
82: use Apache::lonmsg;
83: use Apache::lonlocal;
1.2 raeburn 84: use Apache::lonuserutils;
1.19 raeburn 85: use LONCAPA qw(:DEFAULT :match);
1.1 raeburn 86:
87: sub send_selfserve_notification {
1.2 raeburn 88: my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
1.12 raeburn 89: $approvedlist,$rejectedlist,$crstype) = @_;
1.1 raeburn 90: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
91: # so this can be localized to the recipients date display format/time zone
92: $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
1.26 ! raeburn 93: my ($msgcc,$rawsubj,@rawmsg,$subject,$message,$reviewer,$msgtxt);
! 94: my ($senderuname,$senderudom) = split(':',$sender);
1.1 raeburn 95: if ($context eq 'coursemanagers') {
96: $rawsubj = 'Self-enrollment requests processed';
97: push(@rawmsg,{
1.10 raeburn 98: mt => 'Enrollment requests in the following course: [_1] have been processed.',
1.3 raeburn 99: args => ["\n $contextdesc"],
1.1 raeburn 100: });
101: } elsif ($context eq 'domainmanagers') {
1.12 raeburn 102: $rawsubj = 'Course/Community requests reviewed';
1.1 raeburn 103: push(@rawmsg,{
1.12 raeburn 104: mt => 'Course/Community creation requests in the following domain: [_1] have been reviewed.',
1.3 raeburn 105: args => ["\n $contextdesc"],
1.1 raeburn 106: });
107: if (ref($textstr) eq 'ARRAY') {
108: push(@rawmsg,@{$textstr});
109: }
110: } elsif ($context eq 'enroller') {
111: $rawsubj = 'Enrollment request';
1.12 raeburn 112: if ($crstype eq 'community') {
113: $msgtxt = 'Your request for enrollment in the following community: [_1]requested on [_2]has been reviewed by a Coordinator.'
114: } else {
115: $msgtxt = 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.';
116: }
1.1 raeburn 117: push(@rawmsg,{
1.12 raeburn 118: mt => $msgtxt,
1.2 raeburn 119: args => ["\n ".$contextdesc.",\n",$timestamp.",\n"],
1.1 raeburn 120:
121: });
122: if (ref($textstr) eq 'ARRAY') {
123: push(@rawmsg,@{$textstr});
124: }
125: } elsif ($context eq 'courserequestor') {
1.12 raeburn 126: if ($crstype eq 'Community') {
127: $rawsubj = 'Community request';
128: $msgtxt = 'Your request for creation of the following community: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
129: } else {
130: $rawsubj = 'Course request';
131: $msgtxt = 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
132: }
1.1 raeburn 133: push(@rawmsg,{
1.12 raeburn 134: mt => $msgtxt,
1.2 raeburn 135: args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
1.1 raeburn 136:
137: });
138: if (ref($textstr) eq 'ARRAY') {
139: push(@rawmsg,@{$textstr});
140: }
1.26 ! raeburn 141: } elsif ($context eq 'pendingrequestor') {
! 142: if ($crstype eq 'Community') {
! 143: $rawsubj = 'Community request';
! 144: } else {
! 145: $rawsubj = 'Processed course request';
! 146: }
! 147: if (ref($textstr) eq 'ARRAY') {
! 148: push(@rawmsg,@{$textstr});
! 149: }
1.1 raeburn 150: } elsif ($context eq 'coursereq') {
1.12 raeburn 151: if ($crstype eq 'community') {
152: $rawsubj = 'Community request to review';
153: $msgtxt = 'Creation of the following community: [_1]was requested by [_2] on [_3].';
154: } else {
155: $rawsubj = 'Course request to review';
156: $msgtxt = 'Creation of the following course: [_1]was requested by [_2] on [_3].';
157: }
1.1 raeburn 158: push(@rawmsg,{
1.12 raeburn 159: mt => $msgtxt,
1.2 raeburn 160: args => ["\n $contextdesc\n",$textstr,$timestamp],
1.1 raeburn 161: },
162: {
1.12 raeburn 163: mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Course and community creation -> Approve or reject requests[_3]to display a list of pending requests, which you can either approve or reject.',
1.2 raeburn 164: args => ["\n","\n\n ","\n\n"],
1.1 raeburn 165: });
166: } elsif ($context eq 'selfenrollreq') {
167: $rawsubj = 'Self-enrollment request';
1.12 raeburn 168: if ($crstype eq 'community') {
169: $msgtxt = 'Enrollment in the following community: [_1] was requested by [_2] on [_3].'
170: } else {
171: $msgtxt = 'Enrollment in the following course: [_1] was requested by [_2] on [_3].'
172: }
1.1 raeburn 173: push(@rawmsg,{
1.12 raeburn 174: mt => $msgtxt,
1.2 raeburn 175: args => ["\n $contextdesc\n",$textstr,$timestamp."\n"],
176: });
1.12 raeburn 177: my $directions;
178: if ($crstype eq 'community') {
179: $directions = 'As Coordinator, use: [_1]Main Menu -> Manage Community Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
1.2 raeburn 180: } else {
1.12 raeburn 181: $directions = 'As Course Coordinator, use: [_1]Main Menu -> Manage Course Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
182: }
183: push(@rawmsg,
1.1 raeburn 184: {
1.12 raeburn 185: mt => $directions,
1.2 raeburn 186: args => [" \n\n","\n"],
1.1 raeburn 187: });
1.2 raeburn 188:
1.1 raeburn 189: }
190: my @to_notify = split(/,/,$notifylist);
191: my $numsent = 0;
192: my @recusers;
193: my @recudoms;
194: foreach my $cc (@to_notify) {
195: my ($ccname,$ccdom) = split(/:/,$cc);
196: if (!exists($msgcc->{$ccname.':'.$ccdom})) {
197: push(@recusers,$ccname);
198: push(@recudoms,$ccdom);
199: $msgcc->{$ccname.':'.$ccdom}='';
200: $numsent ++;
201: }
202: }
203: my %reciphash = (
204: cc => $msgcc,
205: );
206: my ($uname,$udom);
207: if ($sender =~ /:/) {
208: ($uname,$udom) = split(/:/,$sender);
1.2 raeburn 209: } elsif ($context eq 'course') {
1.1 raeburn 210: $uname = $sender;
211: my %courseinfo = &Apache::lonnet::coursedescription($cid);
212: $udom = $courseinfo{'num'};
213: }
214: my %sentmessage;
215: my $stamp = time;
216: my $msgcount = &Apache::lonmsg::get_uniq();
217: my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
218: $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
219: $message = '';
220: foreach my $item (@rawmsg) {
221: if (ref($item) eq 'HASH') {
222: $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
223: }
224: }
1.26 ! raeburn 225: &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms,undef,undef,undef,undef,$senderuname,$senderudom);
1.1 raeburn 226: my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
227: my $status;
228: foreach my $recip (sort(keys(%{$msgcc}))) {
229: my ($ccname,$ccdom) = split(/:/,$recip);
230: my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
231: my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
232: my $message = '';
233: foreach my $item (@rawmsg) {
234: if (ref($item) eq 'HASH') {
235: $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
236: @{$item->{args}})."\n";
237: }
238: }
1.10 raeburn 239: if ($context eq 'coursemanagers') {
1.1 raeburn 240: if ($approvedlist) {
241: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
242: }
243: if ($rejectedlist) {
244: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
245: }
246: } elsif ($context eq 'domainmanagers') {
247: if ($approvedlist) {
248: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
249: }
250: if ($rejectedlist) {
251: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
252: }
253: }
254: $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
255: }
256: $status =~ s/,$//;
257: return ($recipstatus,$status);
258: }
259:
260: sub display_queued_requests {
261: my ($context,$dom,$cnum) = @_;
1.2 raeburn 262: my ($namespace,$formaction,$nextelement,%requesthash);
1.1 raeburn 263: if ($context eq 'course') {
264: $formaction = '/adm/createuser';
265: $namespace = 'selfenrollrequests';
266: %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
1.2 raeburn 267: $nextelement = '<input type="hidden" name="state" value="done" />';
1.1 raeburn 268: } else {
269: $formaction = '/adm/createcourse';
270: $namespace = 'courserequestqueue';
1.23 raeburn 271: my $disposition = 'approval';
1.24 raeburn 272: my $nextphase = 'requestchange';
1.23 raeburn 273: if ($context eq 'pending') {
274: $disposition = 'pending';
1.24 raeburn 275: $nextphase = 'requestvalidation';
1.23 raeburn 276: }
277: %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_'.$disposition);
1.24 raeburn 278: $nextelement = '<input type="hidden" name="phase" value="'.$nextphase.'" />';
1.1 raeburn 279: }
1.23 raeburn 280: my ($output,%queue_by_date);
1.1 raeburn 281: if (keys(%requesthash) > 0) {
1.2 raeburn 282: $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
283: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.23 raeburn 284: $nextelement."\n";
1.1 raeburn 285: foreach my $item (keys(%requesthash)) {
1.23 raeburn 286: my ($timestamp,$entry,$pending);
1.1 raeburn 287: if ($context eq 'course') {
288: ($timestamp, my $usec) = split(/:/,$requesthash{$item});
289: $entry = $item.':'.$usec;
290: } else {
291: $timestamp = $requesthash{$item}{'timestamp'};
292: if (ref($requesthash{$item}) eq 'HASH') {
1.2 raeburn 293: my ($cnum,$disposition) = split('_',$item);
294: $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
1.23 raeburn 295: $requesthash{$item}{'ownerdom'}.':';
296: if ($context eq 'pending') {
297: $entry .= $requesthash{$item}{'instcode'};
298: } else {
299: $entry .= $requesthash{$item}{'crstype'};
300: }
301: $entry .= ':'.$requesthash{$item}{'description'};
1.1 raeburn 302: }
303: }
304: if ($entry ne '') {
1.23 raeburn 305: if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
306: push(@{$queue_by_date{$timestamp}},$entry);
1.1 raeburn 307: } else {
1.23 raeburn 308: $queue_by_date{$timestamp} = [$entry];
1.1 raeburn 309: }
310: }
311: }
1.23 raeburn 312: if (keys(%queue_by_date) > 0) {
313: if ($context eq 'course') {
314: $output .= '<h3>'.&mt('Self-enrollment requests queued pending approval by a Coordinator').'</h3>';
315: } elsif ($context eq 'pending') {
316: $output .= '<h3>'.&mt('Requests for official courses queued pending validation').'</h3>'.
317: '<p>'.&mt('Requests are validated against institutional data to confirm that the requestor is an instructor of record.').'<br />'.
318: &mt('Validation is attempted when the request is submitted.').' '.&mt('If unvalidated, the request will be held in a queue.').' '.&mt('Validation of pending requests is automatically repeated daily.').'</p>';
319: } else {
320: $output .= '<h3>'.&mt('Course/Community requests queued pending approval by a Domain Coordinator').'</h3>';
321: }
322: $output .= &build_queue_display($dom,$context,\%queue_by_date).
323: '<input type="hidden" name="queue" value="approval" />';
324: } else {
325: $output .= '<div class="LC_info">';
326: if ($context eq 'course') {
327: $output .= &mt('There are currently no enrollment requests awaiting approval.');
328: } elsif ($context eq 'pending') {
329: $output .= &mt('There are currently no requests for official courses awaiting validation.');
330: } elsif ($context eq 'domain') {
331: $output .= &mt('There are currently no course or community requests awaiting approval.');
332: }
333: $output .= '</div>';
334: }
335: if ($context eq 'pending') {
1.24 raeburn 336: $output .= '<br /><input type="submit" name="validationcheck" value="'.
337: &mt('Validate').'" /><br />'."\n".
1.23 raeburn 338: '<p>'.&mt('Any course/community requests which are successfully validated will be created immediately.').' '.&mt('Unvalidated requests will be listed for manual approval/rejection.').'</p>';
339: } else {
340: $output .= '<br /><input type="submit" name="processqueue" value="'.&mt('Save').'" />';
341: }
342: $output .= '</form>';
343: } else {
344: $output .= '<div class="LC_info">';
345: if ($context eq 'course') {
346: $output .= &mt('There are currently no enrollment requests awaiting approval.');
347: } elsif ($context eq 'pending') {
348: $output .= &mt('There are currently no requests for official courses awaiting validation.');
349: } else {
350: $output .= &mt('There are currently no course or community requests awaiting approval.');
351: }
352: $output .= '</div>';
353: }
354: return $output;
355: }
1.1 raeburn 356:
1.23 raeburn 357: sub build_queue_display {
358: my ($dom,$context,$queue) = @_;
359: return unless (ref($queue) eq 'HASH');
360: my %crstypes;
361: my $output = &Apache::loncommon::start_data_table().
362: &Apache::loncommon::start_data_table_header_row();
363: unless ($context eq 'pending') {
364: $output .= '<th>'.&mt('Action').'</th>';
365: }
366: $output .= '<th>'.&mt('Requestor').'</th>';
367: if ($context eq 'course') {
368: $output .= '<th>'.&mt('Section').'</th>'.
369: '<th>'.&mt('Date requested').'</th>';
370: } elsif ($context eq 'pending' || $context eq 'stillpending') {
371: $output .= '<th>'.&mt('Institutional code').'</th>'.
372: '<th>'.&mt('Date requested').'</th>'.
373: '<th>'.&mt('Details').'</th>';
374: } else {
375: %crstypes = &Apache::lonlocal::texthash (
376: official => 'Official course',
377: unofficial => 'Unofficial course',
378: community => 'Community',
379: );
380: $output .= '<th>'.&mt('Type').'</th>'.
381: '<th>'.&mt('Date requested').'</th>'.
382: '<th>'.&mt('Details').'</th>';
383: }
384: $output .= &Apache::loncommon::end_data_table_header_row();
385: my @sortedtimes = sort {$a <=> $b} (keys(%{$queue}));
386: my $count = 0;
387: foreach my $item (@sortedtimes) {
388: if (ref($queue->{$item}) eq 'ARRAY') {
389: foreach my $request (sort(@{$queue->{$item}})) {
390: my ($row,$approve,$reject,$showtime,$showsec,$namelink,
391: $detailslink,$crstype,$instcode);
392: $showtime = &Apache::lonlocal::locallocaltime($item);
393: if ($context eq 'course') {
394: my ($puname,$pudom,$pusec) = split(/:/,$request);
395: $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
396: $reject = $puname.':'.$pudom;
397: $showsec = $pusec;
398: if ($showsec eq '') {
399: $showsec = &mt('none');
400: }
401: $namelink = &Apache::loncommon::aboutmewrapper(
402: &Apache::loncommon::plainname($puname,$pudom),
403: $puname,$pudom);
404: } else {
405: my ($cnum,$ownername,$ownerdom,$type,$cdesc);
406: my $queue = 'approval';
407: if ($context eq 'pending' || $context eq 'stillpending') {
408: ($cnum,$ownername,$ownerdom,$instcode,$cdesc)=split(/:/,$request,5);
409: $queue = 'pending';
1.1 raeburn 410: } else {
1.23 raeburn 411: ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
1.1 raeburn 412: $crstype = $type;
413: if (defined($crstypes{$type})) {
414: $crstype = $crstypes{$type};
415: }
416: }
1.23 raeburn 417: $detailslink='<a href="javascript:opencoursereqdisplay('.
418: "'$dom','$cnum','$queue'".');">'.$cdesc.'</a>';
419: $approve = $count.':'.$cnum;
420: $reject = $cnum;
421: $namelink = &Apache::loncommon::aboutmewrapper(
422: &Apache::loncommon::plainname($ownername,$ownerdom),
423: $ownername,$ownerdom);
424: }
425: unless ($context eq 'pending') {
1.1 raeburn 426: $row = '<td><span class="LC_nobreak"><label>'.
427: '<input type="checkbox" value="'.$approve.'" name="approvereq" />'.&mt('Approve').'</label></span><br />'.
428: '<span class="LC_nobreak"><label>'.
1.23 raeburn 429: '<input type="checkbox" value="'.$reject.'" name="rejectreq" />'.&mt('Reject').'</label></span><br /></td>';
1.1 raeburn 430: }
1.23 raeburn 431: $row .= '<td>'.$namelink.'</td>'."\n";
432: if ($context eq 'course') {
433: $row .= '<td>'.$showsec.'</td>'."\n".
434: '<td>'.$showtime.'</td>'."\n";
435: } else {
436: if ($context eq 'pending' || $context eq 'stillpending') {
437: $row .= '<td>'.$instcode.'</td>'."\n";
438: } else {
439: $row .= '<td>'.$crstype.'</td>'."\n";
440: }
441: $row .= '<td>'.$showtime.'</td>'."\n".
442: '<td>'.$detailslink.'</td>'."\n";
443: }
444: $output .= &Apache::loncommon::start_data_table_row()."\n".
445: $row.
446: &Apache::loncommon::end_data_table_row()."\n";
447: $count ++;
1.1 raeburn 448: }
449: }
450: }
1.23 raeburn 451: $output .= &Apache::loncommon::end_data_table();
1.1 raeburn 452: return $output;
453: }
454:
455: sub update_request_queue {
456: my ($context,$cdom,$cnum,$coursedesc) = @_;
457: my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
1.26 ! raeburn 458: $stucounts,$idx,$classlist,%requesthash,$cid,$domdesc,$now,
! 459: $sender,$approvedmsg,$rejectedmsg,$beneficiary,
1.2 raeburn 460: @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
1.19 raeburn 461: @processing_errors,@warn_approves,@warn_rejects,@approvals,@warn_dels,
1.5 raeburn 462: @rejections,@rejectionerrors,@nopermissions,%courseroles,
1.25 raeburn 463: %communityroles,%domdefs,%approvalmsg,%rejectionmsg,$crstype,$queue,$firsturl);
1.1 raeburn 464: @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
465: @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
466: $now = time;
467: $sender = $env{'user.name'}.':'.$env{'user.domain'};
468: if ($context eq 'course') {
469: $namespace = 'selfenrollrequests';
470: $beneficiary = 'enroller';
471: $cid = $env{'request.course.id'};
1.12 raeburn 472: $crstype = lc(&Apache::loncommon::course_type());
1.26 ! raeburn 473: $firsturl = &course_portal_url($cnum,$cdom);
1.1 raeburn 474: %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
475: $access_start = $env{'course.'.$cid.'.internal.selfenroll_start_access'};
476: $access_end = $env{'course.'.$cid.'.internal.selfenroll_end_access'};
477: $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
478: $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
479: $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
480: ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
481: $approvedmsg = [{
482: mt => 'Your request for enrollment has been approved.',
483: },
484: {
485: mt => 'Visit [_1], to log-in and access the course',
1.25 raeburn 486: args => [$firsturl],
1.1 raeburn 487: }];
488: $rejectedmsg = [{
489: mt => 'Your request for enrollment has not been approved.',
490: }];
491: } else {
1.2 raeburn 492: $domdesc = &Apache::lonnet::domain($cdom);
493: $namespace = 'courserequestqueue';
494: $beneficiary = 'courserequestor';
1.23 raeburn 495: $queue = 'approval';
496: if ($env{'form.queue'} eq 'pending') {
497: $queue = 'pending';
498: }
499: %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_'.$queue);
1.26 ! raeburn 500: $firsturl= &course_portal_url($cnum,$cdom);
1.2 raeburn 501: my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
502: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
503: if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
504: $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
505: }
506: }
1.12 raeburn 507: $approvalmsg{'course'} =
508: [{
1.2 raeburn 509: mt => 'Your course request has been approved.',
510: },
511: {
512: mt => 'Visit [_1], to log-in and access the course',
1.25 raeburn 513: args => [$firsturl],
1.2 raeburn 514: }];
1.12 raeburn 515: $rejectionmsg{'course'} =
516: [{
1.2 raeburn 517: mt => 'Your course request has not been approved.',
518: }];
1.12 raeburn 519:
520: $approvalmsg{'community'} =
521: [{
522: mt => 'Your community request has been approved.',
523: },
524: {
525: mt => 'Visit [_1], to log-in and access the community',
1.25 raeburn 526: args => [$firsturl],
1.12 raeburn 527: }];
528:
529: $rejectionmsg{'community'} =
530: [{
531: mt => 'Your community request has not been approved.',
532: }];
533:
1.2 raeburn 534: %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
535: my @roles = &Apache::lonuserutils::roles_by_context('course');
536: foreach my $role (@roles) {
537: $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
538: }
539: foreach my $role (@roles) {
540: $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
541: }
542:
1.1 raeburn 543: }
544: foreach my $item (sort {$a <=> $b} @approvals) {
545: if ($context eq 'course') {
546: my ($num,$uname,$udom,$usec) = split(/:/,$item);
547: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
548: if ($uhome ne 'no_host') {
549: if (exists($requesthash{$uname.':'.$udom})) {
550: if (exists($classlist->{$uname.':'.$udom})) {
551: if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
552: if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
553: ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
554: push(@existing,$uname.':'.$udom);
555: next;
556: }
557: }
558: }
559: } else {
560: push(@missingreq,$uname.':'.$udom);
561: next;
562: }
563: if (!grep(/^\Q$item\E$/,@rejections)) {
564: if ($limit eq 'allstudents') {
565: if ($stucounts->{$limit} >= $cap) {
566: push(@limitexceeded,$uname.':'.$udom);
567: last;
568: }
569: } elsif ($limit eq 'selfenrolled') {
570: if ($stucounts->{$limit} >= $cap) {
571: push(@limitexceeded,$uname.':'.$udom);
572: last;
573: }
574: }
575: my $result =
576: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
577: if ($result eq 'ok') {
1.2 raeburn 578: push(@completed,$uname.':'.$udom);
1.1 raeburn 579: $stucounts->{'allstudents'} ++;
580: $stucounts->{'selfenrolled'} ++;
581: &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
1.26 ! raeburn 582: $cid,$coursedesc,$now,$beneficiary,$sender,
! 583: undef,undef,$crstype);
1.2 raeburn 584: my %userrequest = (
585: $cdom.'_'.$cnum => {
586: timestamp => $now,
587: section => $usec,
588: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
589: status => 'approved',
590: }
591: );
1.1 raeburn 592: my $userresult =
593: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
594: if ($userresult ne 'ok') {
595: push(@warn_approves,$uname.':'.$udom);
596: }
597: } else {
1.2 raeburn 598: push(@processing_errors,$uname.':'.$udom);
1.1 raeburn 599: }
600: }
601: } else {
602: push(@invalidusers,$uname.':'.$udom);
603: }
604: } else {
1.2 raeburn 605: my ($num,$cnum) = split(':',$item);
1.23 raeburn 606: if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
1.2 raeburn 607: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
1.23 raeburn 608: my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
609: my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
610: $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
611: my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
1.2 raeburn 612: my $longroles = \%courseroles;
613: if ($crstype eq 'community') {
614: $longroles = \%communityroles;
615: }
1.5 raeburn 616: my $cancreate;
617: if ($cdom eq $ownerdom) {
618: if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
619: undef,'requestcourses')) {
620: $cancreate = 1;
621: }
622: } else {
623: my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
624: if ($userenv{'reqcrsotherdom.'.$crstype}) {
625: my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
626: if (grep(/^\Q$cdom\E:/,@doms)) {
627: $cancreate = 1;
628: }
629: }
630: }
631: if ($cancreate) {
1.2 raeburn 632: my $requestkey = $cdom.'_'.$cnum;
633: my %history =
634: &Apache::lonnet::restore($requestkey,'courserequests',
635: $ownerdom,$ownername);
636: if ((ref($history{'details'}) eq 'HASH') &&
1.23 raeburn 637: ($history{'disposition'} eq $queue)) {
1.2 raeburn 638: my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
639: my $result = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
640: \$newusermsg,\$addresult,\$enrollcount,
641: \$response,\$keysmsg,\%domdefs,$longroles);
642: if ($result eq 'created') {
1.12 raeburn 643: if ($crstype eq 'community') {
644: $approvedmsg = $approvalmsg{'community'};
645: } else {
646: $approvedmsg = $approvalmsg{'course'};
647: }
1.2 raeburn 648: push(@completed,$cnum);
1.19 raeburn 649:
1.23 raeburn 650: unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
1.19 raeburn 651: push(@warn_dels,$cnum);
652: }
1.26 ! raeburn 653: &send_selfserve_notification($ownername.':'.$ownerdom,
! 654: $approvedmsg,$cid,$coursedesc,$now,
! 655: $beneficiary,$sender,undef,undef,$crstype);
1.2 raeburn 656: my %reqhash = (
657: reqtime => $history{'reqtime'},
658: crstype => $history{'crstype'},
659: details => $history{'details'},
660: disposition => $history{'disposition'},
661: status => 'created',
662: adjudicator => $env{'user.name'}.':'.
663: $env{'user.domain'},
664: );
665: my $userresult =
666: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
667: 'courserequests',$ownerdom,$ownername);
668: if ($userresult eq 'ok') {
669: my %status = (
670: 'status:'.$cdom.':'.$cnum => 'created'
671: );
672: my $statusresult =
673: &Apache::lonnet::put('courserequests',\%status,
674: $ownerdom,$ownername);
675: if ($statusresult ne 'ok') {
676: push(@warn_approves,$cnum);
677: }
678: }
679: if ($userresult ne 'ok') {
680: push(@warn_approves,$cnum);
681: }
682: } else {
683: push(@processing_errors,$cnum);
684: }
685: } else {
686: push(@processing_errors,$cnum);
687: }
688: } else {
1.5 raeburn 689: push(@nopermissions,$cnum);
1.2 raeburn 690: }
691: } else {
692: push(@existing,$cnum);
693: }
694: } else {
695: push(@missingreq,$cnum);
696: }
1.1 raeburn 697: }
698: }
1.2 raeburn 699: my @changes = (@completed,@rejections);
700: if ($context eq 'domain') {
1.23 raeburn 701: @changes = map {$_.'_'.$queue} (@changes);
1.2 raeburn 702: }
1.1 raeburn 703: if (@rejections) {
1.3 raeburn 704: foreach my $item (@rejections) {
1.1 raeburn 705: if ($context eq 'course') {
1.3 raeburn 706: my $user = $item;
1.2 raeburn 707: &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
1.26 ! raeburn 708: $now,$beneficiary,$sender,undef,undef,
! 709: $crstype);
1.1 raeburn 710: my ($uname,$udom) = split(/:/,$user);
711: my %userrequest = (
712: $cdom.'_'.$cnum => {
713: timestamp => $now,
714: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
715: status => 'rejected',
716: }
717: );
718: my $userresult =
719: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
720: if ($userresult ne 'ok') {
721: push(@warn_rejects,$user);
722: }
723: } else {
1.3 raeburn 724: my $cnum = $item;
1.23 raeburn 725: if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
1.3 raeburn 726: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
727: my $requestkey = $cdom.'_'.$cnum;
1.23 raeburn 728: my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
729: my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
730: my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
731: $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
1.12 raeburn 732: if ($crstype eq 'community') {
733: $rejectedmsg = $rejectionmsg{'community'};
734: } else {
735: $rejectedmsg = $rejectionmsg{'course'};
736: }
1.2 raeburn 737: &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
738: $cid,$coursedesc,$now,$beneficiary,
1.12 raeburn 739: $sender,undef,undef,$crstype);
1.2 raeburn 740: my %history =
1.3 raeburn 741: &Apache::lonnet::restore($requestkey,'courserequests',
1.2 raeburn 742: $ownerdom,$ownername);
743: if ((ref($history{'details'}) eq 'HASH') &&
1.23 raeburn 744: ($history{'disposition'} eq $queue)) {
1.2 raeburn 745: my %reqhash = (
746: reqtime => $history{'reqtime'},
747: crstype => $history{'crstype'},
748: details => $history{'details'},
749: disposition => $history{'disposition'},
750: status => 'rejected',
751: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
752: );
753: my $userresult =
1.3 raeburn 754: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
755: 'courserequests',$ownerdom,$ownername);
756: if ($userresult eq 'ok') {
757: my %status = (
758: 'status:'.$cdom.':'.$cnum => 'rejected'
759: );
760: my $statusresult =
761: &Apache::lonnet::put('courserequests',\%status,
762: $ownerdom,$ownername);
763: if ($statusresult ne 'ok') {
764: push(@warn_rejects,$cnum);
765: }
766: } else {
767: push(@warn_rejects,$cnum);
1.2 raeburn 768: }
1.23 raeburn 769: unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
1.19 raeburn 770: push(@warn_dels,$cnum);
771: }
1.3 raeburn 772: } else {
773: push(@warn_rejects,$cnum);
1.2 raeburn 774: }
1.3 raeburn 775: } else {
776: push(@existing,$cnum);
1.2 raeburn 777: }
1.3 raeburn 778: } else {
779: push(@rejectionerrors,$cnum);
1.2 raeburn 780: }
1.1 raeburn 781: }
782: }
783: }
784: if (@changes) {
785: my $delresult;
786: if ($context eq 'course') {
787: $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
788: } else {
789: $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
790: }
791: if ($delresult eq 'ok') {
792: my $namelink =
793: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
794: my ($chgmsg,$approvedlist,$rejectedlist);
795: if ($context eq 'course') {
796: $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
1.2 raeburn 797: if (@completed) {
798: $approvedlist = join("\n",@completed);
1.12 raeburn 799: if ($crstype eq 'community') {
800: $output .= '<p>'.&mt('The following were enrolled in the community:').'<ul>';
801: } else {
802: $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
803: }
1.2 raeburn 804: foreach my $user (@completed) {
1.1 raeburn 805: my ($uname,$udom) = split(/:/,$user);
806: my $userlink =
807: &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
808: $output .= '<li>'.$userlink.'</li>';
809: }
810: $output .= '</ul></p>';
811: }
812: if (@rejections) {
813: $rejectedlist = join("\n",@rejections);
814: $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
815: foreach my $user (@rejections) {
816: $output .= '<li>'.$user.'</li>';
817: }
818: $output .= '</ul></p>';
819: }
1.2 raeburn 820: if ($notifylist ne '') {
821: &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
822: $now,'coursemanagers',$sender,
1.12 raeburn 823: $approvedlist,$rejectedlist,$crstype);
1.2 raeburn 824: }
1.1 raeburn 825: } else {
1.12 raeburn 826: $chgmsg = "'Action was taken on the following course and community requests by [_1].',$namelink";
1.2 raeburn 827: if (@completed) {
828: $approvedlist = join("\n",@completed);
1.12 raeburn 829: $output .= '<p>'.&mt('The following courses/communities were created:').'<ul>';
1.2 raeburn 830: foreach my $cnum (@completed) {
831: my $showcourse;
1.23 raeburn 832: if (ref($requesthash{$cnum.'_'.$queue})) {
833: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2 raeburn 834: } else {
835: $showcourse = $cnum;
836: }
837: my $syllabuslink =
838: &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
839: $output .= '<li>'.$syllabuslink.'</li>';
840: }
841: $output .= '</ul></p>';
842: }
843: if (@rejections) {
844: $rejectedlist = join("\n",@rejections);
845: $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
846: foreach my $cnum (@rejections) {
847: my $showcourse;
1.23 raeburn 848: if (ref($requesthash{$cnum.'_'.$queue})) {
849: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2 raeburn 850: } else {
851: $showcourse = $cnum;
852: }
853: $output .= '<li>'.$showcourse.'</li>';
854: }
855: $output .= '</ul></p>';
856: }
857: if ($notifylist ne '') {
858: &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
859: $now,'domainmanagers',$sender,
1.12 raeburn 860: $approvedlist,$rejectedlist,$crstype);
1.2 raeburn 861: }
1.1 raeburn 862: }
863: }
864: }
865: if (@existing) {
866: if ($context eq 'course') {
867: $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
868: foreach my $user (@existing) {
869: $output .= '<li>'.$user.'</li>';
870: }
871: $output .= '</ul></p>';
872: } else {
1.12 raeburn 873: $output .= '<p>'.&mt('The following course/community creation requests were deleted because the course or community has already been created:').'<ul>';
1.2 raeburn 874: foreach my $cnum (@existing) {
875: my $showcourse;
876: my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
877: if ($coursehash{'description'} ne '') {
878: $showcourse = $coursehash{'description'};
879: } else {
880: $showcourse = $cnum;
881: }
882: $output .= '<li>'.$showcourse.'</li>';
883: }
884: $output .= '</ul></p>';
1.1 raeburn 885: }
886: }
887: if (@missingreq) {
888: if ($context eq 'course') {
889: $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
890: foreach my $user (@missingreq) {
891: $output .= '<li>'.$user.'</li>';
892: }
893: $output .= '</ul></p>';
1.2 raeburn 894: } else {
1.12 raeburn 895: $output .= '<p>'.&mt('The following course/community creation requests were ignored because the request is no longer in the queue:').'<ul>';
1.2 raeburn 896: foreach my $cnum (@missingreq) {
897: $output .= '<li>'.$cnum.'</li>';
898: }
899: $output .= '</ul></p>';
900:
1.1 raeburn 901: }
902: }
903: if (@invalidusers) {
904: if ($context eq 'course') {
905: $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
906: foreach my $user (@invalidusers) {
907: $output .= '<li>'.$user.'</li>';
908: }
909: $output .= '</ul></p>';
910: }
911: }
912: if (@limitexceeded) {
913: if ($context eq 'course') {
914: $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
915: foreach my $user (@limitexceeded) {
916: $output .= '<li>'.$user.'</li>';
917: }
918: $output .= '</ul></p>';
919: }
920: }
1.5 raeburn 921: if (@nopermissions) {
1.12 raeburn 922: $output .= '<p>'.&mt('The following course/community creation requests could not be processed because the owner does not have rights to create this type of course:').'<ul>';
1.5 raeburn 923: foreach my $cnum (@nopermissions) {
924: my $showcourse;
1.23 raeburn 925: if (ref($requesthash{$cnum.'_'.$queue})) {
926: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.5 raeburn 927: } else {
928: $showcourse = $cnum;
929: }
930: $output .= '<li>'.$showcourse.'</li>';
931: }
932: $output .= '</ul></p>';
933: }
1.2 raeburn 934: if (@processing_errors) {
1.1 raeburn 935: if ($context eq 'course') {
936: $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
1.2 raeburn 937: foreach my $user (@processing_errors) {
1.1 raeburn 938: $output .= '<li>'.$user.'</li>';
939: }
940: $output .= '</ul></p>';
941: } else {
1.12 raeburn 942: $output .= '<p>'.&mt('The following course/community creation requests could not be processed because an error occurred:').'<ul>';
1.2 raeburn 943: foreach my $cnum (@processing_errors) {
944: my $showcourse;
1.23 raeburn 945: if (ref($requesthash{$cnum.'_'.$queue})) {
946: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2 raeburn 947: } else {
948: $showcourse = $cnum;
949: }
950: $output .= '<li>'.$showcourse.'</li>';
951: }
952: $output .= '</ul></p>';
1.1 raeburn 953: }
954: }
1.3 raeburn 955: if (@rejectionerrors) {
1.12 raeburn 956: $output .= '<p>'.&mt('The following course/community creation request rejections could not be fully processed because an error occurred:').'<ul>';
1.3 raeburn 957: foreach my $cnum (@rejectionerrors) {
958: my $showcourse;
1.23 raeburn 959: if (ref($requesthash{$cnum.'_'.$queue})) {
960: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.3 raeburn 961: } else {
962: $showcourse = $cnum;
963: }
964: $output .= '<li>'.$showcourse.'</li>';
965: }
966: $output .= '</ul></p>';
967: }
1.2 raeburn 968: if (@warn_approves || @warn_rejects) {
1.1 raeburn 969: if ($context eq 'course') {
970: $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
971: foreach my $user (@warn_approves) {
972: $output .= '<li>'.$user.'</li>';
973: }
974: $output .= '</ul></p>';
975: } else {
1.12 raeburn 976: $output .= '<p>'.&mt("For the following course/community requests an error occurred when updating the requestor's own requests record:").'<ul>';
1.2 raeburn 977: foreach my $cnum (@warn_approves,@warn_rejects) {
978: my $showcourse;
1.23 raeburn 979: if (ref($requesthash{$cnum.'_'.$queue})) {
980: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2 raeburn 981: } else {
982: $showcourse = $cnum;
983: }
984: $output .= '<li>'.$showcourse.'</li>';
1.1 raeburn 985: }
986: $output .= '</ul></p>';
987: }
988: }
1.19 raeburn 989: if (@warn_dels) {
990: $output .= '<p>'.&mt("For the following course/community requests an error occurred when removing requests for the following from the pending queue:").'<ul>';
991: foreach my $cnum (@warn_dels) {
992: my $showcourse;
1.23 raeburn 993: if (ref($requesthash{$cnum.'_'.$queue})) {
994: $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.19 raeburn 995: } else {
996: $showcourse = $cnum;
997: }
998: $output .= '<li>'.$showcourse.'</li>';
999: }
1000: $output .= '</ul></p>';
1001: }
1.1 raeburn 1002: return $output;
1003: }
1004:
1.26 ! raeburn 1005: sub course_portal_url {
! 1006: my ($cnum,$cdom) = @_;
! 1007: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
! 1008: my $hostname = &Apache::lonnet::hostname($chome);
! 1009: my $protocol = $Apache::lonnet::protocol{$chome};
! 1010: $protocol = 'http' if ($protocol ne 'https');
! 1011: my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
! 1012: my $firsturl;
! 1013: if ($domdefaults{'portal_def'}) {
! 1014: $firsturl = $domdefaults{'portal_def'};
! 1015: } else {
! 1016: $firsturl = $protocol.'://'.$hostname;
! 1017: }
! 1018: return $firsturl;
! 1019: }
! 1020:
1.1 raeburn 1021: sub get_student_counts {
1022: my ($cdom,$cnum) = @_;
1023: my (%idx,%stucounts);
1024: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1025: $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
1026: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
1027: while (my ($student,$data) = each(%$classlist)) {
1028: if (($data->[$idx{'status'}] eq 'Active') ||
1029: ($data->[$idx{'status'}] eq 'Future')) {
1030: if ($data->[$idx{'type'}] eq 'selfenroll') {
1031: $stucounts{'selfenroll'} ++;
1032: }
1033: $stucounts{'allstudents'} ++;
1034: }
1035: }
1036: return (\%stucounts,\%idx,$classlist);
1037: }
1038:
1.2 raeburn 1039: sub course_creation {
1040: my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
1041: $keysmsg,$domdefs,$longroles) = @_;
1042: unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') &&
1043: (ref($longroles) eq 'HASH')) {
1044: return 'error: Invalid request';
1045: }
1046: my ($result,$ownername,$ownerdom);
1047: my $crstype = $details->{'crstype'};
1048: if ($context eq 'domain') {
1049: $ownername = $details->{'owner'};
1050: $ownerdom = $details->{'domain'};
1051: } else {
1052: $ownername = $env{'user.name'};
1053: $ownerdom = $env{'user.domain'};
1054: }
1055: my $owneremail;
1056: my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
1057: foreach my $email ('permanentemail','critnotification','notification') {
1058: $owneremail = $emails{$email};
1059: last if ($owneremail ne '');
1060: }
1.8 raeburn 1061: my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
1.2 raeburn 1062: my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
1.22 raeburn 1063: \%reqdetails,$longroles,$logmsg,$newusermsg,$addresult,
1064: $enrollcount,$output,$keysmsg,$ownerdom,$ownername,$cnum,$crstype);
1.2 raeburn 1065: if ($cid eq "/$dom/$cnum") {
1066: $result = 'created';
1067: } else {
1068: $result = 'error: '.$cid;
1069: }
1070: return $result;
1071: }
1072:
1073: sub build_batchcreatehash {
1.8 raeburn 1074: my ($dom,$context,$details,$owneremail,$domdefs) = @_;
1.2 raeburn 1075: my %batchhash;
1076: my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
1077: if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
1.17 raeburn 1078: my $emailenc = &escape($owneremail);
1.2 raeburn 1079: my $owner = $details->{'owner'}.':'.$details->{'domain'};
1080: foreach my $item (@items) {
1081: $batchhash{$item} = $details->{$item};
1082: }
1083: $batchhash{'title'} = $details->{'cdescr'};
1084: $batchhash{'coursecode'} = $details->{'instcode'};
1085: $batchhash{'emailenc'} = $emailenc;
1086: $batchhash{'adds'} = $details->{'autoadds'};
1087: $batchhash{'drops'} = $details->{'autodrops'};
1088: $batchhash{'authtype'} = $domdefs->{'auth_def'};
1089: $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
1090: if ($details->{'crstype'} eq 'community') {
1091: $batchhash{'crstype'} = 'Community';
1092: } else {
1093: $batchhash{'crstype'} = 'Course';
1094: }
1.8 raeburn 1095: my ($owner_firstname,$owner_lastname);
1096: if ($context eq 'domain') {
1097: my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
1098: $details->{'owner'},
1099: 'firstname','lastname');
1100: $owner_firstname = $userenv{'firstname'};
1101: $owner_lastname = $userenv{'lastname'};
1102: } else {
1103: $owner_firstname = $env{'environment.firstname'};
1104: $owner_lastname = $env{'environment.lastname'};
1105: }
1106: if (ref($details->{'personnel'}) eq 'HASH') {
1107: %{$batchhash{'users'}} = %{$details->{'personnel'}};
1108: if (ref($batchhash{'users'}) eq 'HASH') {
1109: foreach my $userkey (keys(%{$batchhash{'users'}})) {
1110: if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
1111: if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
1112: foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
1113: my $start = '';
1114: my $end = '';
1115: if ($role eq 'st') {
1116: $start = $details->{'accessstart'};
1117: $end = $details->{'accessend'};
1118: }
1119: $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
1120: $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
1121: }
1122: }
1123: }
1124: }
1125: }
1126: }
1127: $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
1128: $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
1129: $batchhash{'users'}{$owner}{emailenc} = $emailenc;
1130: $batchhash{'users'}{$owner}{owneremail} = $owneremail;
1.2 raeburn 1131: }
1132: return %batchhash;
1133: }
1134:
1.4 raeburn 1135: sub can_clone_course {
1.11 raeburn 1136: my ($uname,$udom,$clonecrs,$clonedom,$crstype) = @_;
1.4 raeburn 1137: my $canclone;
1.11 raeburn 1138: my $ccrole = 'cc';
1.12 raeburn 1139: if ($crstype eq 'community') {
1.11 raeburn 1140: $ccrole = 'co';
1141: }
1.4 raeburn 1142: my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
1.11 raeburn 1143: [$ccrole],[$clonedom]);
1144: if (exists($roleshash{$clonecrs.':'.$clonedom.':'.$ccrole})) {
1.4 raeburn 1145: $canclone = 1;
1146: } else {
1147: my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
1148: my $cloners = $courseenv{'cloners'};
1149: if ($cloners ne '') {
1150: my @cloneable = split(',',$cloners);
1151: if (grep(/^\*$/,@cloneable)) {
1152: $canclone = 1;
1153: }
1154: if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
1155: $canclone = 1;
1156: }
1157: if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
1158: $canclone = 1;
1159: }
1160: }
1.18 raeburn 1161: unless ($canclone) {
1162: if (&Apache::lonnet::is_course_owner($clonedom,$clonecrs,$uname,$udom)) {
1.20 bisitz 1163: $canclone = 1;
1.18 raeburn 1164: }
1165: }
1.4 raeburn 1166: }
1167: return $canclone;
1168: }
1169:
1.13 raeburn 1170: sub get_processtype {
1171: my ($uname,$udom,$isadv,$dom,$crstype,$inststatuses,$domconfig) = @_;
1172: return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
1173: if ($uname eq '' || $udom eq '') {
1174: $uname = $env{'user.name'};
1175: $udom = $env{'user.domain'};
1176: $isadv = $env{'user.adv'};
1177: }
1178: my (%userenv,%settings,$val);
1179: my @options = ('autolimit','validate','approval');
1180: if ($dom eq $udom) {
1181: %userenv =
1182: &Apache::lonnet::userenvironment($udom,$uname,'requestcourses.'.$crstype,'inststatus');
1183: if ($userenv{'requestcourses.'.$crstype}) {
1184: $val = $userenv{'requestcourses.'.$crstype};
1185: @{$inststatuses} = ('_custom_');
1186: } else {
1187: my ($task,%alltasks);
1188: if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
1189: %settings = %{$domconfig->{'requestcourses'}};
1190: if (ref($settings{$crstype}) eq 'HASH') {
1191: if (($isadv) && ($settings{$crstype}{'_LC_adv'} ne '')) {
1192: $val = $settings{$crstype}{'_LC_adv'};
1193: @{$inststatuses} = ('_LC_adv_');
1194: } else {
1195: if ($userenv{'inststatus'} ne '') {
1196: @{$inststatuses} = split(',',$userenv{'inststatus'});
1197: } else {
1198: @{$inststatuses} = ('default');
1199: }
1200: foreach my $status (@{$inststatuses}) {
1201: if (exists($settings{$crstype}{$status})) {
1202: my $value = $settings{$crstype}{$status};
1203: next unless ($value);
1204: unless (exists($alltasks{$value})) {
1205: if (ref($alltasks{$value}) eq 'ARRAY') {
1206: unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
1207: push(@{$alltasks{$value}},$status);
1208: }
1209: } else {
1210: @{$alltasks{$value}} = ($status);
1211: }
1212: }
1213: }
1214: }
1215: my $maxlimit = 0;
1216:
1217: foreach my $key (sort(keys(%alltasks))) {
1218: if ($key =~ /^autolimit=(\d*)$/) {
1219: if ($1 eq '') {
1220: $val ='autolimit=';
1221: last;
1222: } elsif ($1 > $maxlimit) {
1223: $maxlimit = $1;
1224: }
1225: }
1226: }
1227: if ($maxlimit) {
1228: $val = 'autolimit='.$maxlimit;
1229: } else {
1230: foreach my $option (@options) {
1231: if ($alltasks{$option}) {
1232: $val = $option;
1233: last;
1234: }
1235: }
1236: }
1237: }
1238: }
1239: }
1240: }
1241: } else {
1242: %userenv = &Apache::lonnet::userenvironment($udom,$uname,'reqcrsotherdom.'.$crstype);
1243: if ($userenv{'reqcrsotherdom.'.$crstype}) {
1244: my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
1245: my $optregex = join('|',@options);
1246: foreach my $item (@doms) {
1247: my ($extdom,$extopt) = split(':',$item);
1248: if ($extdom eq $dom) {
1249: if ($extopt =~ /^($optregex)(=?\d*)$/) {
1250: $val = $1.$2;
1251: }
1252: last;
1253: }
1254: }
1255: @{$inststatuses} = ('_external_');
1256: }
1257: }
1258: return $val;
1259: }
1260:
1.14 raeburn 1261: sub queued_selfenrollment {
1262: my ($notitle) = @_;
1263: my $output;
1264: my %selfenrollrequests = &Apache::lonnet::dump('selfenrollrequests');
1265: my %reqs_by_date;
1266: foreach my $item (keys(%selfenrollrequests)) {
1267: if (ref($selfenrollrequests{$item}) eq 'HASH') {
1268: if ($selfenrollrequests{$item}{'status'} eq 'request') {
1269: if ($selfenrollrequests{$item}{'timestamp'}) {
1270: push(@{$reqs_by_date{$selfenrollrequests{$item}{'timestamp'}}},$item);
1271: }
1272: }
1273: }
1274: }
1275: if (keys(%reqs_by_date)) {
1276: unless ($notitle) {
1277: $output .= '<b>'.&mt('Enrollment requests pending Course Coordinator approval').'</b><br />';
1278: }
1279: $output .= &Apache::loncommon::start_data_table().
1280: &Apache::loncommon::start_data_table_header_row().
1281: '<th>'.&mt('Date requested').'</th><th>'.&mt('Course title').'</th>'.
1282: '<th>'.&mt('User role').'</th><th>'.&mt('Section').'</th>'.
1283: &Apache::loncommon::end_data_table_header_row();
1284: my @sorted = sort { $a <=> $b } (keys(%reqs_by_date));
1285: foreach my $item (@sorted) {
1286: if (ref($reqs_by_date{$item}) eq 'ARRAY') {
1287: foreach my $crs (@{$reqs_by_date{$item}}) {
1288: my %courseinfo = &Apache::lonnet::coursedescription($crs);
1289: my $usec = $selfenrollrequests{$crs}{'section'};
1.16 raeburn 1290: my $rolename = &Apache::lonnet::plaintext('st',$courseinfo{'type'},$crs);
1.14 raeburn 1291: if ($usec eq '') {
1292: $usec = &mt('No section');
1293: }
1294: $output .= &Apache::loncommon::start_data_table_row().
1295: '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
1296: '<td>'.$courseinfo{'description'}.'</td>'.
1297: '<td>'.$rolename.'</td><td>'.$usec.'</td>'.
1298: &Apache::loncommon::end_data_table_row();
1299: }
1300: }
1301: }
1302: $output .= &Apache::loncommon::end_data_table();
1303: }
1304: return $output;
1305: }
1306:
1.19 raeburn 1307: sub update_coursereq_status {
1.26 ! raeburn 1308: my ($reqhash,$dom,$cnum,$reqstatus,$context,$udom,$uname) = @_;
1.19 raeburn 1309: my ($storeresult,$statusresult,$output);
1310: my $requestkey = $dom.'_'.$cnum;
1311: if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
1312: $storeresult = &Apache::lonnet::store_userdata($reqhash,$requestkey,
1.26 ! raeburn 1313: 'courserequests',$udom,$uname);
1.19 raeburn 1314: if ($storeresult eq 'ok') {
1315: my %status = (
1316: 'status:'.$dom.':'.$cnum => $reqstatus,
1317: );
1.26 ! raeburn 1318: $statusresult = &Apache::lonnet::put('courserequests',\%status,$udom,$uname);
1.19 raeburn 1319: }
1320: } else {
1321: $storeresult = 'error: invalid requestkey format';
1322: }
1323: if ($storeresult ne 'ok') {
1324: $output = &mt('An error occurred saving a record of the details of your request: [_1].',$storeresult);
1325: if ($context eq 'domain') {
1326: $output .= "\n";
1327: } else {
1328: $output = '<span class="LC_warning">'.$output.'</span><br />';
1329: }
1.26 ! raeburn 1330: &Apache::lonnet::logthis("Error saving course request - $requestkey for $uname:$udom - $storeresult");
1.19 raeburn 1331: } elsif ($statusresult ne 'ok') {
1332: $output = &mt('An error occurred saving a record of the status of your request: [_1].',$statusresult);
1333: if ($context eq 'domain') {
1334: $output .= "\n";
1335: } else {
1336: $output = '<span class="LC_warning">'.$output.'</span><br />';
1337: }
1.26 ! raeburn 1338: &Apache::lonnet::logthis("Error saving course request status for $requestkey (for $uname:$udom) - $statusresult");
1.19 raeburn 1339: }
1340: return ($storeresult,$output);
1341: }
1342:
1.23 raeburn 1343: sub process_official_reqs {
1.26 ! raeburn 1344: my ($context,$dom,$dcname,$dcdom) = @_;
1.23 raeburn 1345: my $reqsnamespace = 'courserequestqueue';
1346: my %requesthash =
1.24 raeburn 1347: &Apache::lonnet::dump_dom($reqsnamespace,$dom,'_pending');
1.23 raeburn 1348: my (%newcids,%longroles,%stillpending);
1349: my @courseroles = ('cc','in','ta','ep','ad','st');
1350: foreach my $role (@courseroles) {
1351: $longroles{$role}=&Apache::lonnet::plaintext($role);
1352: }
1353: my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
1354: my ($output,$linefeed);
1355: if ($context eq 'auto') {
1356: $linefeed = "\n";
1357: } else {
1358: $linefeed = '<br />'."\n";
1359: }
1360: foreach my $key (keys(%requesthash)) {
1361: my ($cnum,$status) = split('_',$key);
1362: next if (&Apache::lonnet::homeserver($cnum,$dom) ne 'no_host');
1363: if (ref($requesthash{$key}) eq 'HASH') {
1364: my $ownername = $requesthash{$key}{'ownername'};
1365: my $ownerdom = $requesthash{$key}{'ownerdom'};
1366: next if (&Apache::lonnet::homeserver($ownername,$ownerdom) eq 'no_host');
1367: my $inststatus;
1368: my %userenv =
1369: &Apache::lonnet::get('environment',['inststatus'],
1370: $ownerdom,$ownername);
1371: my ($tmp) = keys(%userenv);
1372: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1373: $inststatus = $userenv{'inststatus'};
1374: } else {
1375: undef(%userenv);
1376: }
1377: my $reqkey = $dom.'_'.$cnum;
1378: my %history = &Apache::lonnet::restore($reqkey,'courserequests',
1379: $ownerdom,$ownername);
1380: if (ref($history{'details'}) eq 'HASH') {
1381: my $instcode = $history{'details'}{'instcode'};
1382: my $crstype = $history{'details'}{'crstype'};
1383: my $reqtime = $history{'details'}{'reqtime'};
1384: my $cdescr = $history{'details'}{'cdescr'};
1385: my @currsec;
1386: my $sections = $history{'details'}{'sections'};
1387: if (ref($sections) eq 'HASH') {
1388: foreach my $i (sort(keys(%{$sections}))) {
1389: if (ref($sections->{$i}) eq 'HASH') {
1390: my $sec = $sections->{$i}{'inst'};
1391: if (!grep(/^\Q$sec\E$/,@currsec)) {
1392: push(@currsec,$sec);
1393: }
1394: }
1395: }
1396: }
1397: my $instseclist = join(',',@currsec);
1398: my ($validationchk,$disposition,$reqstatus,$message,
1399: $validation,$validationerror);
1400: $validationchk =
1401: &Apache::lonnet::auto_courserequest_validation($dom,
1402: $ownername.':'.$ownerdom,$crstype,$inststatus,
1403: $instcode,$instseclist);
1404: if ($validationchk =~ /:/) {
1405: ($validation,$message) = split(':',$validationchk);
1406: } else {
1407: $validation = $validationchk;
1408: }
1409: if ($validation =~ /^error(.*)$/) {
1410: $disposition = 'approval';
1411: $validationerror = $1;
1412: } else {
1413: $disposition = $validation;
1414: }
1415: $reqstatus = $disposition;
1416: if ($disposition eq 'process') {
1417: my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
1418: my $result = &course_creation($dom,$cnum,'domain',$history{'details'},\$logmsg,\$newusermsg,\$addresult,\$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
1419: if ($result eq 'created') {
1420: $disposition = 'created';
1421: $reqstatus = 'created';
1.26 ! raeburn 1422: my $cid = $dom.'_'.$cnum;
! 1423: push(@{$newcids{$instcode}},$cid);
! 1424: if ($dcname && $dcdom) {
! 1425: my $firsturl = &course_portal_url($cnum,$dom);
! 1426: my $beneficiary = 'pendingrequestor';
! 1427: my $now = time;
! 1428: my $owner = $ownername.':'.$ownerdom;
! 1429: my $approvedmsg =
! 1430: [{
! 1431: mt => 'Your request course, queued pending validation has now been created.',
! 1432: },
! 1433: {
! 1434: mt => 'Visit [_1], to log-in and access the course.',
! 1435: args => [$firsturl],
! 1436: }];
! 1437: my $sender = $dcname.':'.$dcdom;
! 1438: &send_selfserve_notification($owner,$approvedmsg,
! 1439: $cid,$cdescr,$now,
! 1440: $beneficiary,$sender,
! 1441: undef,undef,$crstype);
! 1442: }
1.23 raeburn 1443: }
1444: } elsif ($disposition eq 'rejected') {
1445: $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] rejected when validating.',$instcode,$ownername.':'.$ownerdom,$inststatus).$linefeed;
1446: } elsif ($disposition eq 'approval') {
1447: $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] switched to "approval by DC" because of validation error: [_4].',$instcode,$ownername.':'.$ownerdom,$inststatus,$validationerror).$linefeed;
1448:
1449: my $requestid = $cnum.'_'.$disposition;
1450: my $request = {
1451: $requestid => {
1452: timestamp => $reqtime,
1453: crstype => $crstype,
1454: ownername => $ownername,
1455: ownerdom => $ownerdom,
1456: description => $cdescr,
1457: },
1458: };
1459: my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,$dom);
1460: unless ($putresult eq 'ok') {
1461: $output .= &mt("An error occurred saving the modified course request for [_1] submitted by [_2] in the domain's courserequestqueue.db.",$instcode,$ownername.':'.$ownerdom).$linefeed;
1462: }
1463: } elsif ($disposition eq 'pending') {
1464: my $instcode = $requesthash{$key}{'instcode'};
1465: my $description = $requesthash{$key}{'description'};
1466: my $timestamp = $requesthash{$key}{'timestamp'};
1467: my $entry = $cnum.':'.$ownername.':'.$ownerdom.':'.
1468: $instcode.':'.$description;
1469: if (ref($stillpending{$timestamp}) eq 'ARRAY') {
1470: push(@{$stillpending{$timestamp}},$entry);
1471: } else {
1472: $stillpending{$timestamp} = [$entry];
1473: }
1474: }
1475: unless ($disposition eq 'pending') {
1476: my ($statusresult,$output) =
1477: &update_coursereq_status(\%requesthash,$dom,$cnum,
1.26 ! raeburn 1478: $reqstatus,'domain',$ownerdom,
! 1479: $ownername);
1.23 raeburn 1480: unless (&Apache::lonnet::del_dom($reqsnamespace,[$cnum.'_pending'],$dom) eq 'ok') {
1481: $output .= &mt('An error occurred when removing the request for [_1] submitted by [_2] from the pending queue.',$instcode,$ownername.':'.$ownerdom).$linefeed;
1482: }
1483: }
1484: }
1485: }
1486: }
1487: foreach my $key (sort(keys(%newcids))) {
1488: if (ref($newcids{$key}) eq 'ARRAY') {
1489: $output .= "created course from queued request: $key - ".join(', ',@{$newcids{$key}}).$linefeed;
1490: my $newcourse = &LONCAPA::escape($key.':'.$newcids{$key});
1491: }
1492: }
1493: unless ($context eq 'auto') {
1494: if (keys(%stillpending) > 0) {
1495: $output .= '<form method="post" name="changequeue" action="/adm/createcourse" />'."\n".
1496: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1497: '<input type="hidden" name="phase" value="requestchange" />'.
1498: '<p>'.&mt('For the following requests, the requestor could [_1]not[_2] be validated as official course personnel, so the request remains in the pending queue.','<b>','</b>').'<br />'.&mt('Requests may be left in the queue, or you can manually approve or reject them.').'</p>'.
1499: &build_queue_display($dom,'stillpending',\%stillpending).
1500: '<br /><input type="hidden" name="queue" value="pending" />'."\n".
1501: '<input type="submit" name="processqueue" value="'.&mt('Save').'" />'.
1502: '</form>';
1503: }
1504: }
1505: return $output;
1506: }
1507:
1.1 raeburn 1508: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>