Annotation of loncom/interface/loncoursequeueadmin.pm, revision 1.4
1.1 raeburn 1: # The LearningOnline Network
2: # Utilities to administer domain course requests and course self-enroll requests
3: #
1.4 ! raeburn 4: # $Id: loncoursequeueadmin.pm,v 1.3 2009/08/16 23:04:42 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:
30: =head1 NAME
31:
32: Apache::loncoursequeueadmin.pm
33:
34: =head1 SYNOPSIS
35:
36: Adminitsration utilities used by domain coordinators for queued course creation requests, and by course coordinators for queued self-enrollment requests.
37:
38: This is part of the LearningOnline Network with CAPA project
39: described at http://www.lon-capa.org.
40:
41: =head1 SUBROUTINES
42:
43: =over
44:
45: =item send_selfserve_notification()
46:
47: =item display_queued_requests()
48:
49: =item update_request_queue()
50:
51: =item get_student_counts()
52:
53: =back
54:
55: =cut
56:
57: package Apache::loncoursequeueadmin;
58:
59: use strict;
60: use Apache::Constants qw(:common :http);
61: use Apache::lonnet;
62: use Apache::loncommon;
63: use Apache::lonmsg;
64: use Apache::lonlocal;
1.2 raeburn 65: use Apache::lonuserutils;
1.1 raeburn 66: use LONCAPA;
67:
68: sub send_selfserve_notification {
1.2 raeburn 69: my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
1.1 raeburn 70: $approvedlist,$rejectedlist) = @_;
71: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
72: # so this can be localized to the recipients date display format/time zone
73: $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
74: my $msgcc;
1.2 raeburn 75: my ($rawsubj,@rawmsg,$subject,$message,$reviewer);
1.1 raeburn 76: if ($context eq 'coursemanagers') {
77: $rawsubj = 'Self-enrollment requests processed';
78: push(@rawmsg,{
1.2 raeburn 79: mt => 'Enrollment requests in the following course: [_1]have been processed.',
1.3 raeburn 80: args => ["\n $contextdesc"],
1.1 raeburn 81: });
82: } elsif ($context eq 'domainmanagers') {
1.2 raeburn 83: $rawsubj = 'Course requests reviewed';
1.1 raeburn 84: push(@rawmsg,{
1.2 raeburn 85: mt => 'Course creation requests in the following domain: [_1]have been reviewed.',
1.3 raeburn 86: args => ["\n $contextdesc"],
1.1 raeburn 87: });
88: if (ref($textstr) eq 'ARRAY') {
89: push(@rawmsg,@{$textstr});
90: }
91: } elsif ($context eq 'enroller') {
92: $rawsubj = 'Enrollment request';
93: push(@rawmsg,{
1.2 raeburn 94: mt => 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.',
95: args => ["\n ".$contextdesc.",\n",$timestamp.",\n"],
1.1 raeburn 96:
97: });
98: if (ref($textstr) eq 'ARRAY') {
99: push(@rawmsg,@{$textstr});
100: }
101: } elsif ($context eq 'courserequestor') {
102: $rawsubj = 'Course request';
103: push(@rawmsg,{
1.2 raeburn 104: mt => 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.',
105: args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
1.1 raeburn 106:
107: });
108: if (ref($textstr) eq 'ARRAY') {
109: push(@rawmsg,@{$textstr});
110: }
111: } elsif ($context eq 'coursereq') {
1.2 raeburn 112: $rawsubj = 'Course request to review',
1.1 raeburn 113: push(@rawmsg,{
1.2 raeburn 114: mt => 'Creation of the following course: [_1]was requested by [_2] on [_3].',
115: args => ["\n $contextdesc\n",$textstr,$timestamp],
1.1 raeburn 116: },
117: {
1.2 raeburn 118: mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create a new course -> Approve or reject course requests[_3]to display a list of pending requests, which you can either approve or reject.',
119: args => ["\n","\n\n ","\n\n"],
1.1 raeburn 120: });
121: } elsif ($context eq 'selfenrollreq') {
122: $rawsubj = 'Self-enrollment request';
123: push(@rawmsg,{
124: mt => 'Enrollment in the following course: [_1] was requested by [_2] on [_3].',
1.2 raeburn 125: args => ["\n $contextdesc\n",$textstr,$timestamp."\n"],
126: });
127: if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Community') {
128: push(@rawmsg,
129: {
130: mt =>'As 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.',
131: args => [" \n\n","\n"],
132: });
133: } else {
134: push(@rawmsg,
1.1 raeburn 135: {
1.2 raeburn 136: mt =>'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.',
137: args => [" \n\n","\n"],
1.1 raeburn 138: });
1.2 raeburn 139:
140: }
1.1 raeburn 141: }
142: my @to_notify = split(/,/,$notifylist);
143: my $numsent = 0;
144: my @recusers;
145: my @recudoms;
146: foreach my $cc (@to_notify) {
147: my ($ccname,$ccdom) = split(/:/,$cc);
148: if (!exists($msgcc->{$ccname.':'.$ccdom})) {
149: push(@recusers,$ccname);
150: push(@recudoms,$ccdom);
151: $msgcc->{$ccname.':'.$ccdom}='';
152: $numsent ++;
153: }
154: }
155: my %reciphash = (
156: cc => $msgcc,
157: );
158: my ($uname,$udom);
159: if ($sender =~ /:/) {
160: ($uname,$udom) = split(/:/,$sender);
1.2 raeburn 161: } elsif ($context eq 'course') {
1.1 raeburn 162: $uname = $sender;
163: my %courseinfo = &Apache::lonnet::coursedescription($cid);
164: $udom = $courseinfo{'num'};
165: }
166: my %sentmessage;
167: my $stamp = time;
168: my $msgcount = &Apache::lonmsg::get_uniq();
169: my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
170: $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
171: $message = '';
172: foreach my $item (@rawmsg) {
173: if (ref($item) eq 'HASH') {
174: $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
175: }
176: }
177: &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms);
178: my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
179: my $status;
180: foreach my $recip (sort(keys(%{$msgcc}))) {
181: my ($ccname,$ccdom) = split(/:/,$recip);
182: my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
183: my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
184: my $message = '';
185: foreach my $item (@rawmsg) {
186: if (ref($item) eq 'HASH') {
187: $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
188: @{$item->{args}})."\n";
189: }
190: }
191: if ($context eq 'managers') {
192: if ($approvedlist) {
193: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
194: }
195: if ($rejectedlist) {
196: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
197: }
198: } elsif ($context eq 'domainmanagers') {
199: if ($approvedlist) {
200: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
201: }
202: if ($rejectedlist) {
203: $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
204: }
205: }
206: $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
207: }
208: $status =~ s/,$//;
209: return ($recipstatus,$status);
210: }
211:
212: sub display_queued_requests {
213: my ($context,$dom,$cnum) = @_;
1.2 raeburn 214: my ($namespace,$formaction,$nextelement,%requesthash);
1.1 raeburn 215: if ($context eq 'course') {
216: $formaction = '/adm/createuser';
217: $namespace = 'selfenrollrequests';
218: %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
1.2 raeburn 219: $nextelement = '<input type="hidden" name="state" value="done" />';
1.1 raeburn 220: } else {
221: $formaction = '/adm/createcourse';
222: $namespace = 'courserequestqueue';
223: %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,undef,'_approval');
1.2 raeburn 224: $nextelement = '<input type="hidden" name="phase" value="requestchange" />';
1.1 raeburn 225: }
226: my ($output,%queue_by_date,%crstypes);
227: if (keys(%requesthash) > 0) {
1.2 raeburn 228: $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
229: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
230: $nextelement."\n".
1.1 raeburn 231: &Apache::loncommon::start_data_table().
232: &Apache::loncommon::start_data_table_header_row().
233: '<th>'.&mt('Action').'</th>'.
234: '<th>'.&mt('Requestor').'</th>';
235: if ($context eq 'course') {
236: $output .= '<th>'.&mt('Section').'</th>'.
237: '<th>'.&mt('Date requested').'</th>';
238: %crstypes = &Apache::lonlocal::texthash (
239: official => 'Official course',
240: unofficial => 'Unofficial course',
241: community => 'Community',
242: );
243: } else {
244: $output .= '<th>'.&mt('Type').'</th>'.
245: '<th>'.&mt('Date requested').'</th>'.
246: '<th>'.&mt('Details').'</th>';
247: }
248: $output .= &Apache::loncommon::end_data_table_header_row();
249: foreach my $item (keys(%requesthash)) {
250: my ($timestamp,$entry);
251: if ($context eq 'course') {
252: ($timestamp, my $usec) = split(/:/,$requesthash{$item});
253: $entry = $item.':'.$usec;
254: } else {
255: $timestamp = $requesthash{$item}{'timestamp'};
256: if (ref($requesthash{$item}) eq 'HASH') {
1.2 raeburn 257: my ($cnum,$disposition) = split('_',$item);
258: $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
1.1 raeburn 259: $requesthash{$item}{'ownerdom'}.':'.
260: $requesthash{$item}{'crstype'}.':'.
261: $requesthash{$item}{'description'};
262: }
263: }
264: if ($entry ne '') {
265: if (exists($queue_by_date{$timestamp})) {
266: if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
267: push(@{$queue_by_date{$timestamp}},$entry);
268: }
269: } else {
270: @{$queue_by_date{$timestamp}} = ($entry);
271: }
272: }
273: }
274: my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
275: my $count = 0;
276: foreach my $item (@sortedtimes) {
277: if (ref($queue_by_date{$item}) eq 'ARRAY') {
278: foreach my $request (sort(@{$queue_by_date{$item}})) {
279: my ($row,$approve,$reject,$showtime,$showsec,$namelink,
280: $detailslink,$crstype);
281: $showtime = &Apache::lonlocal::locallocaltime($item);
282: if ($context eq 'course') {
283: my ($puname,$pudom,$pusec) = split(/:/,$request);
284: $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
285: $reject = $puname.':'.$pudom;
286: $showsec = $pusec;
287: if ($showsec eq '') {
288: $showsec = &mt('none');
289: }
290: $namelink = &Apache::loncommon::aboutmewrapper(
291: &Apache::loncommon::plainname($puname,$pudom),
292: $puname,$pudom);
293:
294: } else {
1.2 raeburn 295: my ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
1.1 raeburn 296: $detailslink='<a href="javascript::opencoursereqdisplay('.
297: "'$dom".'_'."$cnum'".');">'.$cdesc.'</a>';
298: $crstype = $type;
299: if (defined($crstypes{$type})) {
300: $crstype = $crstypes{$type};
301: }
302: $approve = $count.':'.$cnum;
303: $reject = $cnum;
304: $namelink = &Apache::loncommon::aboutmewrapper(
305: &Apache::loncommon::plainname($ownername,$ownerdom),
306: $ownername,$ownerdom);
307: }
308: $row = '<td><span class="LC_nobreak"><label>'.
309: '<input type="checkbox" value="'.$approve.'" name="approvereq" />'.&mt('Approve').'</label></span><br />'.
310: '<span class="LC_nobreak"><label>'.
311: '<input type="checkbox" value="'.$reject.'" name="rejectreq" />'.&mt('Reject').'</label></span><br /></td>'.
312: '<td>'.$namelink.'</td>'."\n";
313: if ($context eq 'course') {
314: $row .= '<td>'.$showsec.'</td>'."\n".
315: '<td>'.$showtime.'</td>'."\n";
316: } else {
317: $row .= '<td>'.$crstype.'</td>'."\n".
318: '<td>'.$showtime.'</td>'."\n".
319: '<td>'.$detailslink.'</td>'."\n";
320: }
321: $output .= &Apache::loncommon::start_data_table_row()."\n".
322: $row.
323: &Apache::loncommon::end_data_table_row()."\n";
324: $count ++;
325: }
326: }
327: }
328: $output .= &Apache::loncommon::end_data_table().
329: '<input type="submit" name="processqueue" value="'.&mt('Save').
330: '" /></form>';
331: } else {
332: if ($context eq 'course') {
333: $output .= &mt('There are currently no enrollment requests.');
334: } else {
335: $output .= &mt('There are currently no course requests awaiting approval.');
336: }
337: }
338: return $output;
339: }
340:
341: sub update_request_queue {
342: my ($context,$cdom,$cnum,$coursedesc) = @_;
343: my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
1.2 raeburn 344: $stucounts,$idx,$classlist,%requesthash,$cid,$hostname,$protocol,
345: $domdesc,$now,$sender,$approvedmsg,$rejectedmsg,$beneficiary,
346: @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
347: @processing_errors,@warn_approves,@warn_rejects,@approvals,
1.3 raeburn 348: @rejections,@rejectionerrors,%courseroles,%communityroles,%domdefs);
1.1 raeburn 349: @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
350: @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
351: $now = time;
352: $sender = $env{'user.name'}.':'.$env{'user.domain'};
353: if ($context eq 'course') {
354: $namespace = 'selfenrollrequests';
355: $beneficiary = 'enroller';
356: $cid = $env{'request.course.id'};
357: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
358: $hostname = &Apache::lonnet::hostname($chome);
359: $protocol = $Apache::lonnet::protocol{$chome};
360: $protocol = 'http' if ($protocol ne 'https');
361: %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
362: $access_start = $env{'course.'.$cid.'.internal.selfenroll_start_access'};
363: $access_end = $env{'course.'.$cid.'.internal.selfenroll_end_access'};
364: $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
365: $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
366: $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
367: ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
368: $approvedmsg = [{
369: mt => 'Your request for enrollment has been approved.',
370: },
371: {
372: mt => 'Visit [_1], to log-in and access the course',
373: args => [$protocol.'://'.$hostname],
374: }];
375: $rejectedmsg = [{
376: mt => 'Your request for enrollment has not been approved.',
377: }];
378: } else {
1.2 raeburn 379: $domdesc = &Apache::lonnet::domain($cdom);
380: $namespace = 'courserequestqueue';
381: $beneficiary = 'courserequestor';
382: %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,undef,'_approval');
383: my $chome = &Apache::lonnet::domain($cdom,'primary');
384: $hostname = &Apache::lonnet::hostname($chome);
385: $protocol = $Apache::lonnet::protocol{$chome};
386: $protocol = 'http' if ($protocol ne 'https');
387: my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
388: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
389: if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
390: $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
391: }
392: }
393: $approvedmsg = [{
394: mt => 'Your course request has been approved.',
395: },
396: {
397: mt => 'Visit [_1], to log-in and access the course',
398: args => [$protocol.'://'.$hostname],
399: }];
400: $rejectedmsg = [{
401: mt => 'Your course request has not been approved.',
402: }];
403: %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
404: my @roles = &Apache::lonuserutils::roles_by_context('course');
405: foreach my $role (@roles) {
406: $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
407: }
408: foreach my $role (@roles) {
409: $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
410: }
411:
1.1 raeburn 412: }
413: foreach my $item (sort {$a <=> $b} @approvals) {
414: if ($context eq 'course') {
415: my ($num,$uname,$udom,$usec) = split(/:/,$item);
416: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
417: if ($uhome ne 'no_host') {
418: if (exists($requesthash{$uname.':'.$udom})) {
419:
420: if (exists($classlist->{$uname.':'.$udom})) {
421: if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
422: if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
423: ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
424: push(@existing,$uname.':'.$udom);
425: next;
426: }
427: }
428: }
429: } else {
430: push(@missingreq,$uname.':'.$udom);
431: next;
432: }
433: if (!grep(/^\Q$item\E$/,@rejections)) {
434: if ($limit eq 'allstudents') {
435: if ($stucounts->{$limit} >= $cap) {
436: push(@limitexceeded,$uname.':'.$udom);
437: last;
438: }
439: } elsif ($limit eq 'selfenrolled') {
440: if ($stucounts->{$limit} >= $cap) {
441: push(@limitexceeded,$uname.':'.$udom);
442: last;
443: }
444: }
445: my $result =
446: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
447: if ($result eq 'ok') {
1.2 raeburn 448: push(@completed,$uname.':'.$udom);
1.1 raeburn 449: $stucounts->{'allstudents'} ++;
450: $stucounts->{'selfenrolled'} ++;
451: &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
452: $cid,$coursedesc,$now,$beneficiary,$sender);
1.2 raeburn 453: my %userrequest = (
454: $cdom.'_'.$cnum => {
455: timestamp => $now,
456: section => $usec,
457: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
458: status => 'approved',
459: }
460: );
1.1 raeburn 461: my $userresult =
462: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
463: if ($userresult ne 'ok') {
464: push(@warn_approves,$uname.':'.$udom);
465: }
466: } else {
1.2 raeburn 467: push(@processing_errors,$uname.':'.$udom);
1.1 raeburn 468: }
469: }
470: } else {
471: push(@invalidusers,$uname.':'.$udom);
472: }
473: } else {
1.2 raeburn 474: my ($num,$cnum) = split(':',$item);
475: if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
476: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
477: my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
478: my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
479: my $crstype = $requesthash{$cnum.'_approval'}{'crstype'};
480: my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
481: my $longroles = \%courseroles;
482: if ($crstype eq 'community') {
483: $longroles = \%communityroles;
484: }
485: if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
486: undef,'requestcourses')) {
487: my $requestkey = $cdom.'_'.$cnum;
488: my %history =
489: &Apache::lonnet::restore($requestkey,'courserequests',
490: $ownerdom,$ownername);
491: if ((ref($history{'details'}) eq 'HASH') &&
492: ($history{'disposition'} eq 'approval')) {
493: my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
494: my $result = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
495: \$newusermsg,\$addresult,\$enrollcount,
496: \$response,\$keysmsg,\%domdefs,$longroles);
497: if ($result eq 'created') {
498: push(@completed,$cnum);
499: &send_selfserve_notification($ownername.':'.$ownerdom,$approvedmsg,
500: $cid,$coursedesc,$now,$beneficiary,$sender);
501: my %reqhash = (
502: reqtime => $history{'reqtime'},
503: crstype => $history{'crstype'},
504: details => $history{'details'},
505: disposition => $history{'disposition'},
506: status => 'created',
507: adjudicator => $env{'user.name'}.':'.
508: $env{'user.domain'},
509: );
510: my $userresult =
511: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
512: 'courserequests',$ownerdom,$ownername);
513: if ($userresult eq 'ok') {
514: my %status = (
515: 'status:'.$cdom.':'.$cnum => 'created'
516: );
517: my $statusresult =
518: &Apache::lonnet::put('courserequests',\%status,
519: $ownerdom,$ownername);
520: if ($statusresult ne 'ok') {
521: push(@warn_approves,$cnum);
522: }
523: }
524: if ($userresult ne 'ok') {
525: push(@warn_approves,$cnum);
526: }
527: } else {
528: push(@processing_errors,$cnum);
529: }
530: } else {
531: push(@processing_errors,$cnum);
532: }
533: } else {
534: push(@processing_errors,$cnum);
535: }
536: } else {
537: push(@existing,$cnum);
538: }
539: } else {
540: push(@missingreq,$cnum);
541: }
1.1 raeburn 542: }
543: }
1.2 raeburn 544: my @changes = (@completed,@rejections);
545: if ($context eq 'domain') {
546: @changes = map {$_.'_approval'} (@changes);
547: }
1.1 raeburn 548: if (@rejections) {
1.3 raeburn 549: foreach my $item (@rejections) {
1.1 raeburn 550: if ($context eq 'course') {
1.3 raeburn 551: my $user = $item;
1.2 raeburn 552: &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
553: $now,$beneficiary,$sender);
1.1 raeburn 554: my ($uname,$udom) = split(/:/,$user);
555: my %userrequest = (
556: $cdom.'_'.$cnum => {
557: timestamp => $now,
558: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
559: status => 'rejected',
560: }
561: );
562: my $userresult =
563: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
564: if ($userresult ne 'ok') {
565: push(@warn_rejects,$user);
566: }
567: } else {
1.3 raeburn 568: my $cnum = $item;
569: if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
570: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
571: my $requestkey = $cdom.'_'.$cnum;
1.2 raeburn 572: my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
573: my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
574: my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
575: &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
576: $cid,$coursedesc,$now,$beneficiary,
577: $sender);
578: my %history =
1.3 raeburn 579: &Apache::lonnet::restore($requestkey,'courserequests',
1.2 raeburn 580: $ownerdom,$ownername);
581: if ((ref($history{'details'}) eq 'HASH') &&
582: ($history{'disposition'} eq 'approval')) {
583: my %reqhash = (
584: reqtime => $history{'reqtime'},
585: crstype => $history{'crstype'},
586: details => $history{'details'},
587: disposition => $history{'disposition'},
588: status => 'rejected',
589: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
590: );
591: my $userresult =
1.3 raeburn 592: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
593: 'courserequests',$ownerdom,$ownername);
594: if ($userresult eq 'ok') {
595: my %status = (
596: 'status:'.$cdom.':'.$cnum => 'rejected'
597: );
598: my $statusresult =
599: &Apache::lonnet::put('courserequests',\%status,
600: $ownerdom,$ownername);
601: if ($statusresult ne 'ok') {
602: push(@warn_rejects,$cnum);
603: }
604: } else {
605: push(@warn_rejects,$cnum);
1.2 raeburn 606: }
1.3 raeburn 607: } else {
608: push(@warn_rejects,$cnum);
1.2 raeburn 609: }
1.3 raeburn 610: } else {
611: push(@existing,$cnum);
1.2 raeburn 612: }
1.3 raeburn 613: } else {
614: push(@rejectionerrors,$cnum);
1.2 raeburn 615: }
1.1 raeburn 616: }
617: }
618: }
619: if (@changes) {
620: my $delresult;
621: if ($context eq 'course') {
622: $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
623: } else {
624: $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
625: }
626: if ($delresult eq 'ok') {
627: my $namelink =
628: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
629: my ($chgmsg,$approvedlist,$rejectedlist);
630: if ($context eq 'course') {
631: $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
1.2 raeburn 632: if (@completed) {
633: $approvedlist = join("\n",@completed);
1.1 raeburn 634: $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
1.2 raeburn 635: foreach my $user (@completed) {
1.1 raeburn 636: my ($uname,$udom) = split(/:/,$user);
637: my $userlink =
638: &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
639: $output .= '<li>'.$userlink.'</li>';
640: }
641: $output .= '</ul></p>';
642: }
643: if (@rejections) {
644: $rejectedlist = join("\n",@rejections);
645: $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
646: foreach my $user (@rejections) {
647: $output .= '<li>'.$user.'</li>';
648: }
649: $output .= '</ul></p>';
650: }
1.2 raeburn 651: if ($notifylist ne '') {
652: &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
653: $now,'coursemanagers',$sender,
654: $approvedlist,$rejectedlist);
655: }
1.1 raeburn 656: } else {
1.2 raeburn 657: $chgmsg = "'Action was taken on the following course requests by [_1].',$namelink";
658: if (@completed) {
659: $approvedlist = join("\n",@completed);
660: $output .= '<p>'.&mt('The following courses were created:').'<ul>';
661: foreach my $cnum (@completed) {
662: my $showcourse;
663: if (ref($requesthash{$cnum.'_approval'})) {
664: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
665: } else {
666: $showcourse = $cnum;
667: }
668: my $syllabuslink =
669: &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
670: $output .= '<li>'.$syllabuslink.'</li>';
671: }
672: $output .= '</ul></p>';
673: }
674: if (@rejections) {
675: $rejectedlist = join("\n",@rejections);
676: $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
677: foreach my $cnum (@rejections) {
678: my $showcourse;
679: if (ref($requesthash{$cnum.'_approval'})) {
680: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
681: } else {
682: $showcourse = $cnum;
683: }
684: $output .= '<li>'.$showcourse.'</li>';
685: }
686: $output .= '</ul></p>';
687: }
688: if ($notifylist ne '') {
689: &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
690: $now,'domainmanagers',$sender,
691: $approvedlist,$rejectedlist);
692: }
1.1 raeburn 693: }
694: }
695: }
696: if (@existing) {
697: if ($context eq 'course') {
698: $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
699: foreach my $user (@existing) {
700: $output .= '<li>'.$user.'</li>';
701: }
702: $output .= '</ul></p>';
703: } else {
1.2 raeburn 704: $output .= '<p>'.&mt('The following course creation requests were deleted because the course has already been created:').'<ul>';
705: foreach my $cnum (@existing) {
706: my $showcourse;
707: my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
708: if ($coursehash{'description'} ne '') {
709: $showcourse = $coursehash{'description'};
710: } else {
711: $showcourse = $cnum;
712: }
713: $output .= '<li>'.$showcourse.'</li>';
714: }
715: $output .= '</ul></p>';
1.1 raeburn 716: }
717: }
718: if (@missingreq) {
719: if ($context eq 'course') {
720: $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
721: foreach my $user (@missingreq) {
722: $output .= '<li>'.$user.'</li>';
723: }
724: $output .= '</ul></p>';
1.2 raeburn 725: } else {
726: $output .= '<p>'.&mt('The following course creation requests were ignored because the request is no longer in the course request queue:').'<ul>';
727: foreach my $cnum (@missingreq) {
728: $output .= '<li>'.$cnum.'</li>';
729: }
730: $output .= '</ul></p>';
731:
1.1 raeburn 732: }
733: }
734: if (@invalidusers) {
735: if ($context eq 'course') {
736: $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
737: foreach my $user (@invalidusers) {
738: $output .= '<li>'.$user.'</li>';
739: }
740: $output .= '</ul></p>';
741: }
742: }
743: if (@limitexceeded) {
744: if ($context eq 'course') {
745: $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
746: foreach my $user (@limitexceeded) {
747: $output .= '<li>'.$user.'</li>';
748: }
749: $output .= '</ul></p>';
750: }
751: }
1.2 raeburn 752: if (@processing_errors) {
1.1 raeburn 753: if ($context eq 'course') {
754: $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
1.2 raeburn 755: foreach my $user (@processing_errors) {
1.1 raeburn 756: $output .= '<li>'.$user.'</li>';
757: }
758: $output .= '</ul></p>';
759: } else {
1.2 raeburn 760: $output .= '<p>'.&mt('The following course creation requests could not be processed because an error occurred:').'<ul>';
761: foreach my $cnum (@processing_errors) {
762: my $showcourse;
763: if (ref($requesthash{$cnum.'_approval'})) {
764: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
765: } else {
766: $showcourse = $cnum;
767: }
768: $output .= '<li>'.$showcourse.'</li>';
769: }
770: $output .= '</ul></p>';
1.1 raeburn 771: }
772: }
1.3 raeburn 773: if (@rejectionerrors) {
774: $output .= '<p>'.&mt('The following course creation request rejections could not be fully processed because an error occurred:').'<ul>';
775: foreach my $cnum (@rejectionerrors) {
776: my $showcourse;
777: if (ref($requesthash{$cnum.'_approval'})) {
778: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
779: } else {
780: $showcourse = $cnum;
781: }
782: $output .= '<li>'.$showcourse.'</li>';
783: }
784: $output .= '</ul></p>';
785: }
1.2 raeburn 786: if (@warn_approves || @warn_rejects) {
1.1 raeburn 787: if ($context eq 'course') {
788: $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
789: foreach my $user (@warn_approves) {
790: $output .= '<li>'.$user.'</li>';
791: }
792: $output .= '</ul></p>';
793: } else {
1.2 raeburn 794: $output .= '<p>'.&mt("For the following course requests an error occurred when updating the requestor's own course requests record:").'<ul>';
795: foreach my $cnum (@warn_approves,@warn_rejects) {
796: my $showcourse;
797: if (ref($requesthash{$cnum.'_approval'})) {
798: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
799: } else {
800: $showcourse = $cnum;
801: }
802: $output .= '<li>'.$showcourse.'</li>';
1.1 raeburn 803: }
804: $output .= '</ul></p>';
805: }
806: }
807: return $output;
808: }
809:
810: sub get_student_counts {
811: my ($cdom,$cnum) = @_;
812: my (%idx,%stucounts);
813: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
814: $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
815: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
816: while (my ($student,$data) = each(%$classlist)) {
817: if (($data->[$idx{'status'}] eq 'Active') ||
818: ($data->[$idx{'status'}] eq 'Future')) {
819: if ($data->[$idx{'type'}] eq 'selfenroll') {
820: $stucounts{'selfenroll'} ++;
821: }
822: $stucounts{'allstudents'} ++;
823: }
824: }
825: return (\%stucounts,\%idx,$classlist);
826: }
827:
1.2 raeburn 828: sub course_creation {
829: my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
830: $keysmsg,$domdefs,$longroles) = @_;
831: unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') &&
832: (ref($longroles) eq 'HASH')) {
833: return 'error: Invalid request';
834: }
835: my ($result,$ownername,$ownerdom);
836: my $crstype = $details->{'crstype'};
837: if ($context eq 'domain') {
838: $ownername = $details->{'owner'};
839: $ownerdom = $details->{'domain'};
840: } else {
841: $ownername = $env{'user.name'};
842: $ownerdom = $env{'user.domain'};
843: }
844: my $type = 'Course';
845: if ($crstype eq 'community') {
846: $type = 'Community';
847: }
848: my $owneremail;
849: my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
850: foreach my $email ('permanentemail','critnotification','notification') {
851: $owneremail = $emails{$email};
852: last if ($owneremail ne '');
853: }
854: my %reqdetails = &build_batchcreatehash($dom,$details,$owneremail,$domdefs);
855: my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
856: \%reqdetails,$longroles,\$logmsg,\$newusermsg,\$addresult,
857: \$enrollcount,\$output,\$keysmsg,$ownerdom,$ownername,$cnum,$crstype);
858: if ($cid eq "/$dom/$cnum") {
859: $result = 'created';
860: } else {
861: $result = 'error: '.$cid;
862: }
863: return $result;
864: }
865:
866: sub build_batchcreatehash {
867: my ($dom,$details,$owneremail,$domdefs) = @_;
868: my %batchhash;
869: my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
870: if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
871: my $emailenc = &Apache::lonnet::escape($owneremail);
872: my $owner = $details->{'owner'}.':'.$details->{'domain'};
873: foreach my $item (@items) {
874: $batchhash{$item} = $details->{$item};
875: }
876: $batchhash{'title'} = $details->{'cdescr'};
877: $batchhash{'coursecode'} = $details->{'instcode'};
878: $batchhash{'emailenc'} = $emailenc;
879: $batchhash{'adds'} = $details->{'autoadds'};
880: $batchhash{'drops'} = $details->{'autodrops'};
881: $batchhash{'authtype'} = $domdefs->{'auth_def'};
882: $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
883: if ($details->{'crstype'} eq 'community') {
884: $batchhash{'crstype'} = 'Community';
885: } else {
886: $batchhash{'crstype'} = 'Course';
887: }
888: $batchhash{'users'}{$details->{$owner}} = {
889: firstname => $env{'environment.first'},
890: lastname => $env{'environment.last'},
891: emailenc => $emailenc,
892: email => $owneremail,
893: };
894: }
895: return %batchhash;
896: }
897:
1.4 ! raeburn 898: sub can_clone_course {
! 899: my ($uname,$udom,$clonecrs,$clonedom) = @_;
! 900: my $canclone;
! 901: my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
! 902: ['cc'],[$clonedom]);
! 903: if (exists($roleshash{$clonedom.':'.$clonecrs.':cc'})) {
! 904: $canclone = 1;
! 905: } else {
! 906: my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
! 907: my $cloners = $courseenv{'cloners'};
! 908: if ($cloners ne '') {
! 909: my @cloneable = split(',',$cloners);
! 910: if (grep(/^\*$/,@cloneable)) {
! 911: $canclone = 1;
! 912: }
! 913: if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
! 914: $canclone = 1;
! 915: }
! 916: if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
! 917: $canclone = 1;
! 918: }
! 919: }
! 920: }
! 921: return $canclone;
! 922: }
! 923:
1.1 raeburn 924: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>