Annotation of loncom/interface/loncoursequeueadmin.pm, revision 1.9.2.2
1.1 raeburn 1: # The LearningOnline Network
2: # Utilities to administer domain course requests and course self-enroll requests
3: #
1.9.2.2 ! raeburn 4: # $Id: loncoursequeueadmin.pm,v 1.9.2.1 2009/10/04 15:29:58 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';
1.6 raeburn 223: %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_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.7 raeburn 296: $detailslink='<a href="javascript:opencoursereqdisplay('.
297: "'$dom','$cnum'".');">'.$cdesc.'</a>';
1.1 raeburn 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.5 raeburn 348: @rejections,@rejectionerrors,@nopermissions,%courseroles,
1.9.2.1 raeburn 349: %communityroles,%domdefs,@warn_coursereqs);
1.1 raeburn 350: @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
351: @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
352: $now = time;
353: $sender = $env{'user.name'}.':'.$env{'user.domain'};
354: if ($context eq 'course') {
355: $namespace = 'selfenrollrequests';
356: $beneficiary = 'enroller';
357: $cid = $env{'request.course.id'};
358: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
359: $hostname = &Apache::lonnet::hostname($chome);
360: $protocol = $Apache::lonnet::protocol{$chome};
361: $protocol = 'http' if ($protocol ne 'https');
362: %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
363: $access_start = $env{'course.'.$cid.'.internal.selfenroll_start_access'};
364: $access_end = $env{'course.'.$cid.'.internal.selfenroll_end_access'};
365: $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
366: $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
367: $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
368: ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
369: $approvedmsg = [{
370: mt => 'Your request for enrollment has been approved.',
371: },
372: {
373: mt => 'Visit [_1], to log-in and access the course',
374: args => [$protocol.'://'.$hostname],
375: }];
376: $rejectedmsg = [{
377: mt => 'Your request for enrollment has not been approved.',
378: }];
379: } else {
1.2 raeburn 380: $domdesc = &Apache::lonnet::domain($cdom);
381: $namespace = 'courserequestqueue';
382: $beneficiary = 'courserequestor';
1.6 raeburn 383: %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_approval');
1.2 raeburn 384: my $chome = &Apache::lonnet::domain($cdom,'primary');
385: $hostname = &Apache::lonnet::hostname($chome);
386: $protocol = $Apache::lonnet::protocol{$chome};
387: $protocol = 'http' if ($protocol ne 'https');
388: my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
389: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
390: if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
391: $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
392: }
393: }
394: $approvedmsg = [{
395: mt => 'Your course request has been approved.',
396: },
397: {
398: mt => 'Visit [_1], to log-in and access the course',
399: args => [$protocol.'://'.$hostname],
400: }];
401: $rejectedmsg = [{
402: mt => 'Your course request has not been approved.',
403: }];
404: %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
405: my @roles = &Apache::lonuserutils::roles_by_context('course');
406: foreach my $role (@roles) {
407: $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
408: }
409: foreach my $role (@roles) {
410: $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
411: }
412:
1.1 raeburn 413: }
414: foreach my $item (sort {$a <=> $b} @approvals) {
415: if ($context eq 'course') {
416: my ($num,$uname,$udom,$usec) = split(/:/,$item);
417: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
418: if ($uhome ne 'no_host') {
419: if (exists($requesthash{$uname.':'.$udom})) {
420:
421: if (exists($classlist->{$uname.':'.$udom})) {
422: if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
423: if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
424: ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
425: push(@existing,$uname.':'.$udom);
426: next;
427: }
428: }
429: }
430: } else {
431: push(@missingreq,$uname.':'.$udom);
432: next;
433: }
434: if (!grep(/^\Q$item\E$/,@rejections)) {
435: if ($limit eq 'allstudents') {
436: if ($stucounts->{$limit} >= $cap) {
437: push(@limitexceeded,$uname.':'.$udom);
438: last;
439: }
440: } elsif ($limit eq 'selfenrolled') {
441: if ($stucounts->{$limit} >= $cap) {
442: push(@limitexceeded,$uname.':'.$udom);
443: last;
444: }
445: }
446: my $result =
447: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
448: if ($result eq 'ok') {
1.2 raeburn 449: push(@completed,$uname.':'.$udom);
1.1 raeburn 450: $stucounts->{'allstudents'} ++;
451: $stucounts->{'selfenrolled'} ++;
452: &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
453: $cid,$coursedesc,$now,$beneficiary,$sender);
1.2 raeburn 454: my %userrequest = (
455: $cdom.'_'.$cnum => {
456: timestamp => $now,
457: section => $usec,
458: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
459: status => 'approved',
460: }
461: );
1.1 raeburn 462: my $userresult =
463: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
464: if ($userresult ne 'ok') {
465: push(@warn_approves,$uname.':'.$udom);
1.9.2.1 raeburn 466: } elsif ($udom eq 'gci') {
467: my %changehash = (
1.9.2.2 ! raeburn 468: 'reqcrsotherdom.unofficial' => 'gcitest:autolimit=',
1.9.2.1 raeburn 469: );
1.9.2.2 ! raeburn 470: my $reqresult = &Apache::lonnet::put('environment',\%changehash,
1.9.2.1 raeburn 471: $udom,$uname);
472: if ($reqresult ne 'ok') {
473: push(@warn_coursereqs,$uname.':'.$udom);
474: }
1.1 raeburn 475: }
476: } else {
1.2 raeburn 477: push(@processing_errors,$uname.':'.$udom);
1.1 raeburn 478: }
479: }
480: } else {
481: push(@invalidusers,$uname.':'.$udom);
482: }
483: } else {
1.2 raeburn 484: my ($num,$cnum) = split(':',$item);
485: if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
486: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
487: my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
488: my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
489: my $crstype = $requesthash{$cnum.'_approval'}{'crstype'};
490: my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
491: my $longroles = \%courseroles;
492: if ($crstype eq 'community') {
493: $longroles = \%communityroles;
494: }
1.5 raeburn 495: my $cancreate;
496: if ($cdom eq $ownerdom) {
497: if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
498: undef,'requestcourses')) {
499: $cancreate = 1;
500: }
501: } else {
502: my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
503: if ($userenv{'reqcrsotherdom.'.$crstype}) {
504: my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
505: if (grep(/^\Q$cdom\E:/,@doms)) {
506: $cancreate = 1;
507: }
508: }
509: }
510: if ($cancreate) {
1.2 raeburn 511: my $requestkey = $cdom.'_'.$cnum;
512: my %history =
513: &Apache::lonnet::restore($requestkey,'courserequests',
514: $ownerdom,$ownername);
515: if ((ref($history{'details'}) eq 'HASH') &&
516: ($history{'disposition'} eq 'approval')) {
517: my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
518: my $result = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
519: \$newusermsg,\$addresult,\$enrollcount,
520: \$response,\$keysmsg,\%domdefs,$longroles);
521: if ($result eq 'created') {
522: push(@completed,$cnum);
523: &send_selfserve_notification($ownername.':'.$ownerdom,$approvedmsg,
524: $cid,$coursedesc,$now,$beneficiary,$sender);
525: my %reqhash = (
526: reqtime => $history{'reqtime'},
527: crstype => $history{'crstype'},
528: details => $history{'details'},
529: disposition => $history{'disposition'},
530: status => 'created',
531: adjudicator => $env{'user.name'}.':'.
532: $env{'user.domain'},
533: );
534: my $userresult =
535: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
536: 'courserequests',$ownerdom,$ownername);
537: if ($userresult eq 'ok') {
538: my %status = (
539: 'status:'.$cdom.':'.$cnum => 'created'
540: );
541: my $statusresult =
542: &Apache::lonnet::put('courserequests',\%status,
543: $ownerdom,$ownername);
544: if ($statusresult ne 'ok') {
545: push(@warn_approves,$cnum);
546: }
547: }
548: if ($userresult ne 'ok') {
549: push(@warn_approves,$cnum);
550: }
551: } else {
552: push(@processing_errors,$cnum);
553: }
554: } else {
555: push(@processing_errors,$cnum);
556: }
557: } else {
1.5 raeburn 558: push(@nopermissions,$cnum);
1.2 raeburn 559: }
560: } else {
561: push(@existing,$cnum);
562: }
563: } else {
564: push(@missingreq,$cnum);
565: }
1.1 raeburn 566: }
567: }
1.2 raeburn 568: my @changes = (@completed,@rejections);
569: if ($context eq 'domain') {
570: @changes = map {$_.'_approval'} (@changes);
571: }
1.1 raeburn 572: if (@rejections) {
1.3 raeburn 573: foreach my $item (@rejections) {
1.1 raeburn 574: if ($context eq 'course') {
1.3 raeburn 575: my $user = $item;
1.2 raeburn 576: &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
577: $now,$beneficiary,$sender);
1.1 raeburn 578: my ($uname,$udom) = split(/:/,$user);
579: my %userrequest = (
580: $cdom.'_'.$cnum => {
581: timestamp => $now,
582: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
583: status => 'rejected',
584: }
585: );
586: my $userresult =
587: &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
588: if ($userresult ne 'ok') {
589: push(@warn_rejects,$user);
590: }
591: } else {
1.3 raeburn 592: my $cnum = $item;
593: if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
594: if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
595: my $requestkey = $cdom.'_'.$cnum;
1.2 raeburn 596: my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
597: my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
598: my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
599: &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
600: $cid,$coursedesc,$now,$beneficiary,
601: $sender);
602: my %history =
1.3 raeburn 603: &Apache::lonnet::restore($requestkey,'courserequests',
1.2 raeburn 604: $ownerdom,$ownername);
605: if ((ref($history{'details'}) eq 'HASH') &&
606: ($history{'disposition'} eq 'approval')) {
607: my %reqhash = (
608: reqtime => $history{'reqtime'},
609: crstype => $history{'crstype'},
610: details => $history{'details'},
611: disposition => $history{'disposition'},
612: status => 'rejected',
613: adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
614: );
615: my $userresult =
1.3 raeburn 616: &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
617: 'courserequests',$ownerdom,$ownername);
618: if ($userresult eq 'ok') {
619: my %status = (
620: 'status:'.$cdom.':'.$cnum => 'rejected'
621: );
622: my $statusresult =
623: &Apache::lonnet::put('courserequests',\%status,
624: $ownerdom,$ownername);
625: if ($statusresult ne 'ok') {
626: push(@warn_rejects,$cnum);
627: }
628: } else {
629: push(@warn_rejects,$cnum);
1.2 raeburn 630: }
1.3 raeburn 631: } else {
632: push(@warn_rejects,$cnum);
1.2 raeburn 633: }
1.3 raeburn 634: } else {
635: push(@existing,$cnum);
1.2 raeburn 636: }
1.3 raeburn 637: } else {
638: push(@rejectionerrors,$cnum);
1.2 raeburn 639: }
1.1 raeburn 640: }
641: }
642: }
643: if (@changes) {
644: my $delresult;
645: if ($context eq 'course') {
646: $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
647: } else {
648: $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
649: }
650: if ($delresult eq 'ok') {
651: my $namelink =
652: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
653: my ($chgmsg,$approvedlist,$rejectedlist);
654: if ($context eq 'course') {
655: $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
1.2 raeburn 656: if (@completed) {
657: $approvedlist = join("\n",@completed);
1.1 raeburn 658: $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
1.2 raeburn 659: foreach my $user (@completed) {
1.1 raeburn 660: my ($uname,$udom) = split(/:/,$user);
661: my $userlink =
662: &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
663: $output .= '<li>'.$userlink.'</li>';
664: }
665: $output .= '</ul></p>';
666: }
667: if (@rejections) {
668: $rejectedlist = join("\n",@rejections);
669: $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
670: foreach my $user (@rejections) {
671: $output .= '<li>'.$user.'</li>';
672: }
673: $output .= '</ul></p>';
674: }
1.2 raeburn 675: if ($notifylist ne '') {
676: &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
677: $now,'coursemanagers',$sender,
678: $approvedlist,$rejectedlist);
679: }
1.1 raeburn 680: } else {
1.2 raeburn 681: $chgmsg = "'Action was taken on the following course requests by [_1].',$namelink";
682: if (@completed) {
683: $approvedlist = join("\n",@completed);
684: $output .= '<p>'.&mt('The following courses were created:').'<ul>';
685: foreach my $cnum (@completed) {
686: my $showcourse;
687: if (ref($requesthash{$cnum.'_approval'})) {
688: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
689: } else {
690: $showcourse = $cnum;
691: }
692: my $syllabuslink =
693: &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
694: $output .= '<li>'.$syllabuslink.'</li>';
695: }
696: $output .= '</ul></p>';
697: }
698: if (@rejections) {
699: $rejectedlist = join("\n",@rejections);
700: $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
701: foreach my $cnum (@rejections) {
702: my $showcourse;
703: if (ref($requesthash{$cnum.'_approval'})) {
704: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
705: } else {
706: $showcourse = $cnum;
707: }
708: $output .= '<li>'.$showcourse.'</li>';
709: }
710: $output .= '</ul></p>';
711: }
712: if ($notifylist ne '') {
713: &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
714: $now,'domainmanagers',$sender,
715: $approvedlist,$rejectedlist);
716: }
1.1 raeburn 717: }
718: }
719: }
720: if (@existing) {
721: if ($context eq 'course') {
722: $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
723: foreach my $user (@existing) {
724: $output .= '<li>'.$user.'</li>';
725: }
726: $output .= '</ul></p>';
727: } else {
1.2 raeburn 728: $output .= '<p>'.&mt('The following course creation requests were deleted because the course has already been created:').'<ul>';
729: foreach my $cnum (@existing) {
730: my $showcourse;
731: my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
732: if ($coursehash{'description'} ne '') {
733: $showcourse = $coursehash{'description'};
734: } else {
735: $showcourse = $cnum;
736: }
737: $output .= '<li>'.$showcourse.'</li>';
738: }
739: $output .= '</ul></p>';
1.1 raeburn 740: }
741: }
742: if (@missingreq) {
743: if ($context eq 'course') {
744: $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
745: foreach my $user (@missingreq) {
746: $output .= '<li>'.$user.'</li>';
747: }
748: $output .= '</ul></p>';
1.2 raeburn 749: } else {
750: $output .= '<p>'.&mt('The following course creation requests were ignored because the request is no longer in the course request queue:').'<ul>';
751: foreach my $cnum (@missingreq) {
752: $output .= '<li>'.$cnum.'</li>';
753: }
754: $output .= '</ul></p>';
755:
1.1 raeburn 756: }
757: }
758: if (@invalidusers) {
759: if ($context eq 'course') {
760: $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
761: foreach my $user (@invalidusers) {
762: $output .= '<li>'.$user.'</li>';
763: }
764: $output .= '</ul></p>';
765: }
766: }
767: if (@limitexceeded) {
768: if ($context eq 'course') {
769: $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
770: foreach my $user (@limitexceeded) {
771: $output .= '<li>'.$user.'</li>';
772: }
773: $output .= '</ul></p>';
774: }
775: }
1.5 raeburn 776: if (@nopermissions) {
777: $output .= '<p>'.&mt('The following course creation requests could not be processed because the course owner does hot have rights to create this type of course:').'<ul>';
778: foreach my $cnum (@nopermissions) {
779: my $showcourse;
780: if (ref($requesthash{$cnum.'_approval'})) {
781: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
782: } else {
783: $showcourse = $cnum;
784: }
785: $output .= '<li>'.$showcourse.'</li>';
786: }
787: $output .= '</ul></p>';
788: }
1.2 raeburn 789: if (@processing_errors) {
1.1 raeburn 790: if ($context eq 'course') {
791: $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
1.2 raeburn 792: foreach my $user (@processing_errors) {
1.1 raeburn 793: $output .= '<li>'.$user.'</li>';
794: }
795: $output .= '</ul></p>';
796: } else {
1.2 raeburn 797: $output .= '<p>'.&mt('The following course creation requests could not be processed because an error occurred:').'<ul>';
798: foreach my $cnum (@processing_errors) {
799: my $showcourse;
800: if (ref($requesthash{$cnum.'_approval'})) {
801: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
802: } else {
803: $showcourse = $cnum;
804: }
805: $output .= '<li>'.$showcourse.'</li>';
806: }
807: $output .= '</ul></p>';
1.1 raeburn 808: }
809: }
1.3 raeburn 810: if (@rejectionerrors) {
811: $output .= '<p>'.&mt('The following course creation request rejections could not be fully processed because an error occurred:').'<ul>';
812: foreach my $cnum (@rejectionerrors) {
813: my $showcourse;
814: if (ref($requesthash{$cnum.'_approval'})) {
815: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
816: } else {
817: $showcourse = $cnum;
818: }
819: $output .= '<li>'.$showcourse.'</li>';
820: }
821: $output .= '</ul></p>';
822: }
1.2 raeburn 823: if (@warn_approves || @warn_rejects) {
1.1 raeburn 824: if ($context eq 'course') {
825: $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
826: foreach my $user (@warn_approves) {
827: $output .= '<li>'.$user.'</li>';
828: }
829: $output .= '</ul></p>';
830: } else {
1.2 raeburn 831: $output .= '<p>'.&mt("For the following course requests an error occurred when updating the requestor's own course requests record:").'<ul>';
832: foreach my $cnum (@warn_approves,@warn_rejects) {
833: my $showcourse;
834: if (ref($requesthash{$cnum.'_approval'})) {
835: $showcourse = $requesthash{$cnum.'_approval'}{'description'};
836: } else {
837: $showcourse = $cnum;
838: }
839: $output .= '<li>'.$showcourse.'</li>';
1.1 raeburn 840: }
841: $output .= '</ul></p>';
842: }
843: }
1.9.2.1 raeburn 844: if (@warn_coursereqs) {
845: $output .= '<p>'..&mt("For the following users, an error occurred when setting rights to request creation of Concept Test courses:").'<ul>';
846: foreach my $user (@warn_coursereqs) {
847: $output .= '<li>'.$user.'</li>';
848: }
849: $output .= '</ul></p>';
850: }
1.1 raeburn 851: return $output;
852: }
853:
854: sub get_student_counts {
855: my ($cdom,$cnum) = @_;
856: my (%idx,%stucounts);
857: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
858: $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
859: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
860: while (my ($student,$data) = each(%$classlist)) {
861: if (($data->[$idx{'status'}] eq 'Active') ||
862: ($data->[$idx{'status'}] eq 'Future')) {
863: if ($data->[$idx{'type'}] eq 'selfenroll') {
864: $stucounts{'selfenroll'} ++;
865: }
866: $stucounts{'allstudents'} ++;
867: }
868: }
869: return (\%stucounts,\%idx,$classlist);
870: }
871:
1.2 raeburn 872: sub course_creation {
873: my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
874: $keysmsg,$domdefs,$longroles) = @_;
875: unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') &&
876: (ref($longroles) eq 'HASH')) {
877: return 'error: Invalid request';
878: }
879: my ($result,$ownername,$ownerdom);
880: my $crstype = $details->{'crstype'};
881: if ($context eq 'domain') {
882: $ownername = $details->{'owner'};
883: $ownerdom = $details->{'domain'};
884: } else {
885: $ownername = $env{'user.name'};
886: $ownerdom = $env{'user.domain'};
887: }
888: my $type = 'Course';
889: if ($crstype eq 'community') {
890: $type = 'Community';
891: }
892: my $owneremail;
893: my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
894: foreach my $email ('permanentemail','critnotification','notification') {
895: $owneremail = $emails{$email};
896: last if ($owneremail ne '');
897: }
1.8 raeburn 898: my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
1.2 raeburn 899: my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
900: \%reqdetails,$longroles,\$logmsg,\$newusermsg,\$addresult,
901: \$enrollcount,\$output,\$keysmsg,$ownerdom,$ownername,$cnum,$crstype);
902: if ($cid eq "/$dom/$cnum") {
903: $result = 'created';
904: } else {
905: $result = 'error: '.$cid;
906: }
907: return $result;
908: }
909:
910: sub build_batchcreatehash {
1.8 raeburn 911: my ($dom,$context,$details,$owneremail,$domdefs) = @_;
1.2 raeburn 912: my %batchhash;
913: my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
914: if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
915: my $emailenc = &Apache::lonnet::escape($owneremail);
916: my $owner = $details->{'owner'}.':'.$details->{'domain'};
917: foreach my $item (@items) {
918: $batchhash{$item} = $details->{$item};
919: }
920: $batchhash{'title'} = $details->{'cdescr'};
921: $batchhash{'coursecode'} = $details->{'instcode'};
922: $batchhash{'emailenc'} = $emailenc;
923: $batchhash{'adds'} = $details->{'autoadds'};
924: $batchhash{'drops'} = $details->{'autodrops'};
925: $batchhash{'authtype'} = $domdefs->{'auth_def'};
926: $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
927: if ($details->{'crstype'} eq 'community') {
928: $batchhash{'crstype'} = 'Community';
929: } else {
930: $batchhash{'crstype'} = 'Course';
931: }
1.8 raeburn 932: my ($owner_firstname,$owner_lastname);
933: if ($context eq 'domain') {
934: my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
935: $details->{'owner'},
936: 'firstname','lastname');
937: $owner_firstname = $userenv{'firstname'};
938: $owner_lastname = $userenv{'lastname'};
939: } else {
940: $owner_firstname = $env{'environment.firstname'};
941: $owner_lastname = $env{'environment.lastname'};
942: }
943: if (ref($details->{'personnel'}) eq 'HASH') {
944: %{$batchhash{'users'}} = %{$details->{'personnel'}};
945: if (ref($batchhash{'users'}) eq 'HASH') {
946: foreach my $userkey (keys(%{$batchhash{'users'}})) {
947: if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
948: if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
949: foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
950: my $start = '';
951: my $end = '';
952: if ($role eq 'st') {
953: $start = $details->{'accessstart'};
954: $end = $details->{'accessend'};
955: }
956: $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
957: $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
958: }
959: }
960: }
961: }
962: }
963: }
964: $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
965: $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
966: $batchhash{'users'}{$owner}{emailenc} = $emailenc;
967: $batchhash{'users'}{$owner}{owneremail} = $owneremail;
1.2 raeburn 968: }
969: return %batchhash;
970: }
971:
1.4 raeburn 972: sub can_clone_course {
973: my ($uname,$udom,$clonecrs,$clonedom) = @_;
974: my $canclone;
975: my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
976: ['cc'],[$clonedom]);
1.9 raeburn 977: if (exists($roleshash{$clonecrs.':'.$clonedom.':cc'})) {
1.4 raeburn 978: $canclone = 1;
979: } else {
980: my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
981: my $cloners = $courseenv{'cloners'};
982: if ($cloners ne '') {
983: my @cloneable = split(',',$cloners);
984: if (grep(/^\*$/,@cloneable)) {
985: $canclone = 1;
986: }
987: if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
988: $canclone = 1;
989: }
990: if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
991: $canclone = 1;
992: }
993: }
994: }
995: return $canclone;
996: }
997:
1.1 raeburn 998: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>