1: #
2: # $Id: batchcreatecourse.pm,v 1.43 2019/07/26 02:28:28 raeburn Exp $
3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
26:
27: package LONCAPA::batchcreatecourse;
28: use LONCAPA::Configuration;
29: use LONCAPA::Enrollment;
30: use HTML::Parser;
31: use Time::Local;
32: use Apache::lonnet;
33: use Apache::loncommon;
34: use Apache::lonlocal;
35: use LONCAPA qw(:match);
36:
37: use strict;
38:
39: # Collection of routines used for batch creation of courses and users.
40: # &create_courses() should be called by an Autocreate.pl
41: # script via a cron entry, or alternatively from a web page, after upload
42: # of a file containing an XML description of a course request (lonbatchccrs.pm).
43: #
44: # XML file(s) describing courses that are to be created in domain $dom are stored in
45: # /home/httpd/perl/tmp/addcourse/$dom
46: #
47: # &create_courses() will create an account for the course owner
48: # (if one does not currently exist), will create the course (cloning if necessary),
49: # and will add additional instructional staff (creating accounts if necessary).
50: #
51: # Example of XML file (which could contain more than one class to be created):
52: #
53: #<?xml version="1.0" encoding="UTF-8"?>
54: #<!DOCTYPE text>
55: #<class id="ss05ubw101">
56: # <title>Underwater Basket Weaving</title>
57: # <crstype>Course</crstype>
58: # <coursecode>ss05ubw101</coursecode>
59: # <defaultcredits>3</defaultcredits>
60: # <coursehome>msul1</coursehome>
61: # <coursedomain>msu</coursedomain>
62: # <reshome>/res/msu/</reshome>
63: # <optional_id></optional_id>
64: # <adds>1</adds>
65: # <drops>1</drops>
66: # <enrollstart>2005:01:04:10:30</enrollstart>
67: # <enrollend>2005:07:04:20:30</enrollend>
68: # <accessstart>2005:01:10:10:30</accessstart>
69: # <accessend>2005:05:31:10:30</accessend>
70: # <authentication>
71: # <method>krb4</method>
72: # <param>MSU.EDU</param>
73: # </authentication>
74: # <nonstandard></nonstandard>
75: # <topmap></topmap>
76: # <firstres>nav</firstres>
77: # <crsquota>20</crsquota>
78: # <uniquecode>1</uniquecode>
79: # <clonecrs>466011437c34194msul1</clonecrs>
80: # <clonedom>msu</clonedom>
81: # <datemode>shift</datemode>
82: # <dateshift>365</dateshift>
83: # <showphotos></showphotos>
84: # <setpolicy>1</setpolicy>
85: # <setcontent>1</setcontent>
86: # <setcomment>1</setcomment>
87: # <setkeys>0</setkeys>
88: # <keyauth>keyadmin:msu</keyauth>
89: # <disresdis>1</disresdis>
90: # <disablechat>1</disablechat>
91: # <openall></openall>
92: # <notify_dc>1</notify_dc>
93: # <notify_owner>1</notify_owner>
94: # <owner>
95: # <username>sparty</username>
96: # <domain>msu</domain>
97: # <authtype>krb4</authtype>
98: # <autharg>MSU.EDU</autharg>
99: # </owner>
100: # <sections>
101: # <section>
102: # <inst>001</inst>
103: # <loncapa>1</loncapa>
104: # </section>
105: # <section>
106: # <inst>002</inst>
107: # <loncapa>2</loncapa>
108: # </section>
109: # </sections>
110: # <crosslists>
111: # <xlist>
112: # <inst>ss05zzz101001</inst>
113: # <loncapa>1</loncapa>
114: # </xlist>
115: # </crosslists>
116: # <users>
117: # <user>
118: # <username>sparty</username>
119: # <domain>msu</domain>
120: # <email>sparty@msu.edu</email>
121: # <authtype>krb4</authtype>
122: # <autharg></autharg>
123: # <firstname>MSU</firstname>
124: # <generation></generation>
125: # <lastname>Spartan</lastname>x
126: # <middlename></middlename>
127: # <studentID></studentID>
128: # <credits></credits>
129: # <roles></roles>
130: # </user>
131: # <user>
132: # <username>itds0001</username>
133: # <domain>northwood5</domain>
134: # <email>itds0001@msu.edu</email>
135: # <authtype>int</authtype>
136: # <autharg></autharg>
137: # <firstname>Info</firstname>
138: # <generation></generation>
139: # <lastname>Techc</lastname>x
140: # <middlename></middlename>
141: # <studentID></studentID>
142: # <roles>
143: # <role id='in'>
144: # <start>2005:01:01:12:10</start>
145: # <end>2005:12:01:12:10</end>
146: # <usec>1</usec>
147: # <usec>2</usec>
148: # </role>
149: # </roles>
150: # </user>
151: # </users>
152: #</class>
153: #
154: # Many of these are binary options (corresponding to either checkboxes or
155: # radio buttons in the interactive CCRS page). Examples include:
156: # setpolicy, setcontent, setcomment, setkeys, disableresdis, disablechat, openall,
157: # uniquecode
158: #
159: # A value of 1 between opening and closing tags is equivalent to a
160: # checked checkbox or 'Yes' response in the original CCRS web page.
161: # A value of 0 or blank is equivalent to an unchecked box or 'No'
162: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
163: #
164: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
165: # no entry respectively.
166: #
167: # crstype can be Course, Community or Placement
168: #
169: # crsquota is the total disk space permitted for course group portfolio files
170: # in all course groups.
171: #
172: # For format of other parameters, refer to the interactive CCRS page
173: # and view how the equivalent parameter is displayed in the web form.
174: #
175: ##############################################################
176: # create_courses() - creates courses described in @$requests,
177: # files listed in @$requests are deleted
178: # after the files have been parsed.
179: #
180: # Directory for retrieval of files listed in @$requests is:
181: # /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
182: # /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
183: #
184: # inputs (six) - requests - ref to array of filename(s) containing course requests
185: # courseids - ref to hash to store LON-CAPA course ids of new courses
186: # context - auto if called from command line, web if called from browser
187: # dom - domain for which the course is being created
188: # uname - username of DC who is requesting course creation
189: # udom - domain of DC who is requesting course creation
190: #
191: # outputs (four) - output - text recording user roles added etc.
192: # logmsg - text to be logged
193: # keysmsg - text containing link(s) to manage keys page(s)
194: # codehash - reference to hash containing courseID => unique code
195: # where unique code is a 6 character code, to distribute
196: # to students as a shortcut to the course.
197: #############################################################
198:
199: sub create_courses {
200: my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
201: my $output;
202: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
203: # Get role names
204: my %longroles = ();
205: open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
206: my @rolesplain = <FILE>;
207: close(FILE);
208: foreach my $item (@rolesplain) {
209: if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
210: $longroles{'Course'}{$1} = $2;
211: $longroles{'Community'}{$1} = $3;
212: }
213: }
214: my ($logmsg,$keysmsg,$newusermsg,$addresult,%codehash,%instcodes);
215: my %enrollcount = ();
216: my $newcoursedir = LONCAPA::tempdir().'/addcourse/'.$dom.'/'.$context;
217: if ($context eq 'auto') {
218: $newcoursedir .= '/pending';
219: } else {
220: if ($uname && $udom) {
221: $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
222: } else {
223: $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
224: }
225: }
226: if (@{$requests} > 0) {
227: foreach my $request (@{$requests}) {
228: my %details = ();
229: if (-e $newcoursedir.'/'.$request) {
230: &parse_coursereqs($newcoursedir.'/'.$request, \%details);
231: foreach my $num (sort(keys(%details))) {
232: my $reqdetails = $details{$num};
233: my $code;
234: my $courseid =
235: &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,\$newusermsg,
236: \$addresult,\%enrollcount,\$output,\$keysmsg,undef,undef,undef,undef,\$code);
237: if ($courseid =~m{^/$match_domain/$match_courseid}) {
238: $$courseids{$courseid} = $details{$num}{'class'};
239: if ($code) {
240: $codehash{$courseid} = $code;
241: }
242: if ($details{$num}{'coursecode'} ne '') {
243: push(@{$instcodes{$details{$num}{'coursecode'}}},$courseid);
244: }
245: }
246: }
247: }
248: }
249: }
250: return ($output,$logmsg,$keysmsg,\%codehash,\%instcodes);
251: }
252:
253: #############################################################
254: #
255: # parse_coursereqs()
256: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
257: # - details - reference to hash containing extracted information.
258: # outputs (none)
259: #
260: ############################################################
261:
262: sub parse_coursereqs {
263: my ($coursefile,$details) = @_;
264: # Note all start and end dates should be in this format:
265: # YYYY:MM:DD:HH:MM:SS (:separators required).
266: my $uname = '';
267: my @state = ();
268: my $num = 0;
269: my $secid = 0;
270: my $xlist = 0;
271: my $userkey = '';
272: my $role = '';
273: my @items = ('title','optional_id','coursecode','defaultcredits','coursehome','reshome','nonstandard','adds','drops','topmap','firstres','clonecrs','clonedom','datemode','dateshift','showphotos','setpolicy','setcontent','setcomment','setkeys','keyauth','disresdis','disablechat','openall','notify_owner','notify_dc','crstype','crsquota','uniquecode');
274: my @possroles = qw(st ad ep ta in cc co);
275: my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
276: my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
277: my $p = HTML::Parser->new
278: (
279: xml_mode => 1,
280: start_h =>
281: [sub {
282: my ($tagname, $attr) = @_;
283: push(@state, $tagname);
284: if ("@state" eq "class") {
285: %{$$details{$num}} = ();
286: $$details{$num}{'class'} = $attr->{id};
287: %{$$details{$num}{'users'}} = ();
288: %{$$details{$num}{'sections'}} = ();
289: $secid = 0;
290: $xlist = 0;
291: }
292: if ("@state" eq "class users user roles role") {
293: $role = $attr->{id};
294: if (grep(/^\Q$role\E$/,@possroles)) {
295: push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);
296: %{$$details{$num}{'users'}{$userkey}{$role}} = ();
297: @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
298: }
299: }
300: if ("@state" eq "class sections section") {
301: $secid ++;
302: %{$$details{$num}{'sections'}{$secid}} = ();
303: }
304: if ("@state" eq "class crosslists xlist") {
305: $xlist ++;
306: %{$$details{$num}{'crosslists'}{$xlist}} = ();
307: }
308: }, "tagname, attr"],
309: text_h =>
310: [sub {
311: my ($text) = @_;
312: if ("@state" eq "class owner username") {
313: $$details{$num}{'owner'} = $text;
314: } elsif ("@state" eq "class owner domain") {
315: $$details{$num}{'domain'} = $text;
316: } elsif ("@state" eq "class sections section inst") {
317: $$details{$num}{'sections'}{$secid}{'inst'} = $text;
318: } elsif ("@state" eq "class sections section loncapa") {
319: $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
320: } elsif ("@state" eq "class crosslists xlist inst") {
321: $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
322: } elsif ("@state" eq "class crosslists xlist loncapa") {
323: $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
324: } elsif ("@state" eq "class owner authtype") {
325: $$details{$num}{'ownerauthtype'} = $text;
326: } elsif ("@state" eq "class owner autharg") {
327: $$details{$num}{'ownerauthparam'} = $text;
328: } elsif ("@state" eq "class authentication method") {
329: $$details{$num}{'authtype'} = $text;
330: } elsif ("@state" eq "class authentication param") {
331: $$details{$num}{'authparam'} = $text;
332: } elsif ("@state" eq "class users user username") {
333: $userkey = $text;
334: } elsif ("@state" eq "class users user domain") {
335: $userkey .= ':'.$text;
336: %{$$details{$num}{'users'}{$userkey}} = ();
337: @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
338: } elsif ("@state" eq "class users user email") {
339: $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
340: $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text);
341: } elsif ("@state" eq "class users user roles role start") {
342: if (grep(/^\Q$role\E$/,@possroles)) {
343: $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
344: }
345: } elsif ("@state" eq "class users user roles role end") {
346: if (grep(/^\Q$role\E$/,@possroles)) {
347: $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
348: }
349: } elsif ("@state" eq "class users user roles role usec") {
350: if (grep(/^\Q$role\E$/,@possroles)) {
351: unless ($text eq '') {
352: push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
353: }
354: }
355: } else {
356: foreach my $item (@items) {
357: if ("@state" eq "class $item") {
358: $$details{$num}{$item} = $text;
359: }
360: }
361: foreach my $item (@dateitems) {
362: if ("@state" eq "class $item") {
363: $$details{$num}{$item} = &process_date($text);
364: }
365: }
366: foreach my $item (@useritems) {
367: if ("@state" eq "class users user $item") {
368: $$details{$num}{'users'}{$userkey}{$item} = $text;
369: }
370: }
371: }
372: }, "dtext"],
373: end_h =>
374: [sub {
375: my ($tagname) = @_;
376: if ("@state" eq "class") {
377: $num ++;
378: }
379: pop @state;
380: }, "tagname"],
381: );
382:
383: $p->parse_file($coursefile);
384: $p->eof;
385: return;
386: }
387:
388: #########################################################
389: #
390: # build_course()
391: #
392: # inputs
393: # course domain
394: # course request number
395: # context - auto if called from command line, web if called from DC web interface
396: # ref to hash of course creation information
397: # ref to hash of role descriptions
398: # ref to scalar used to accumulate log messages
399: # ref to scalar used to accumulate messages sent to new users
400: # ref to scalar used to accumulate results of new user additions
401: # ref to hash of enrollment counts for different roles
402: # ref to scalar used to accumulate information about added roles
403: # ref to scalar used to accumulate information about access keys
404: # domain of DC creating course
405: # username of DC creating course
406: # optional course number, if unique course number already obtained (e.g., for
407: # course requests submitted via course request form.
408: # optional category
409: # optional ref to scalar for six character unique identifier
410: #
411: # outputs
412: # LON-CAPA courseID for new (created) course
413: #
414: #########################################################
415:
416: sub build_course {
417: my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,
418: $enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category,$coderef) = @_;
419: return unless (ref($details) eq 'HASH');
420: my $owner_uname = $details->{'owner'};
421: my $owner_domain = $details->{'domain'};
422: my $owner = $owner_uname.':'.$owner_domain;
423: my $sectionstr = '';
424: my $xliststr = '';
425: my $noenddate = '';
426: my $outcome;
427: my ($courseid,$crsudom,$crsunum,$crstype,$ccrole,$rolenames);
428: if ($details->{'crstype'} eq 'Community') {
429: $crstype = $details->{'crstype'};
430: $ccrole ='co';
431: if (ref($longroles) eq 'HASH') {
432: $rolenames = $longroles->{'Community'};
433: }
434: } else {
435: if ($details->{'crstype'} eq 'Placement') {
436: $crstype = $details->{'crstype'};
437: } else {
438: $crstype = 'Course';
439: }
440: $ccrole = 'cc';
441: if (ref($longroles) eq 'HASH') {
442: $rolenames = $longroles->{'Course'};
443: }
444: }
445: my $linefeed;
446: if ($context eq 'auto') {
447: $linefeed = "\n";
448: } else {
449: $linefeed = "<br />\n";
450: }
451: if ($details->{'accessend'} eq '') {
452: $noenddate = 1;
453: }
454: my $reshome = $details->{'reshome'};
455: if ($reshome eq '') {
456: $reshome = '/res/'.$cdom;
457: }
458: my $firstres = $details->{'firstres'};
459: if ($firstres eq '') {
460: if ($crstype eq 'Community') {
461: $firstres = 'nav';
462: } elsif ($crstype eq 'Placement') {
463: $firstres = 'blank';
464: } else {
465: $firstres = 'syl';
466: }
467: }
468: foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
469: $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
470: }
471: $sectionstr =~ s/,$//;
472:
473: foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
474: $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
475: }
476: $xliststr =~ s/,$//;
477:
478: my %courseinfo = (
479: inst_code => $details->{'coursecode'},
480: description => $details->{'title'}
481: );
482: if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
483: my $ownerargs = {'auth' => $details->{'ownerauthtype'},
484: 'authparam' => $details->{'ownerauthparam'},
485: 'emailenc' => $details->{'emailenc'},
486: 'udom' => $details->{'domain'},
487: 'uname' => $details->{'owner'},
488: 'pid' => $details->{'users'}{$owner}{'studentID'},
489: 'first' => $details->{'users'}{$owner}{'firstname'},
490: 'middle' => $details->{'users'}{$owner}{'middlename'},
491: 'last' => $details->{'users'}{$owner}{'lastname'},
492: 'gene' => $details->{'users'}{$owner}{'generation'},
493: 'usec' => '',
494: 'end' => '',
495: 'start' => '',
496: 'emailaddr' => $details->{'users'}{$owner}{'email'},
497: 'cid' => '',
498: 'context' => 'createowner',
499: 'linefeed' => $linefeed,
500: 'role' => $ccrole,
501: };
502: $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
503: } else {
504: $outcome = 'ok';
505: }
506:
507: if ($outcome eq 'ok') {
508: if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
509: $details->{'datemode'} = 'shift';
510: $details->{'dateshift'} = 365;
511: }
512: my $courseargs = {
513: ccuname => $details->{'owner'},
514: ccdomain => $details->{'domain'},
515: cdescr => $details->{'title'},
516: crstype => $details->{'crstype'},
517: curl => $details->{'topmap'},
518: course_domain => $cdom,
519: course_home => $details->{'coursehome'},
520: nonstandard => $details->{'nonstandard'},
521: crscode => $details->{'coursecode'},
522: defaultcredits => $details->{'defaultcredits'},
523: crsquota => $details->{'crsquota'},
524: uniquecode => $details->{'uniquecode'},
525: clonecourse => $details->{'clonecrs'},
526: clonedomain => $details->{'clonedom'},
527: datemode => $details->{'datemode'},
528: dateshift => $details->{'dateshift'},
529: crsid => $details->{'optional_id'},
530: curruser => $details->{'owner'},
531: crssections => $sectionstr,
532: crsxlist => $xliststr,
533: autoadds => $details->{'adds'},
534: autodrops => $details->{'drops'},
535: notify => $details->{'notify_owner'},
536: notify_dc => $details->{'notify_dc'},
537: no_end_date => $noenddate,
538: showphotos => $details->{'showphotos'},
539: authtype => $details->{'authtype'},
540: autharg => $details->{'authparam'},
541: enrollstart => $details->{'enrollstart'},
542: enrollend => $details->{'enrollend'},
543: startaccess => $details->{'accessstart'},
544: endaccess => $details->{'accessend'},
545: setpolicy => $details->{'setpolicy'},
546: setcontent => $details->{'setcontent'},
547: setcomment => $details->{'setcomment'},
548: reshome => $reshome,
549: setkeys => $details->{'setkeys'},
550: keyauth => $details->{'keyauth'},
551: disresdis => $details->{'disresdis'},
552: disablechat => $details->{'disablechat'},
553: openall => $details->{'openall'},
554: firstres => $firstres
555: };
556: if ($details->{'textbook'}) {
557: $courseargs->{'textbook'} = $details->{'textbook'};
558: }
559: my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
560: if (! exists($host_servers{$details->{'coursehome'}})) {
561: $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
562: return;
563: }
564: my ($success, $msg) =
565: &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
566: $udom,$uname,$context,$cnum,$category,$coderef);
567: $$logmsg .= $msg;
568: if (!$success) {
569: return;
570: }
571: } else {
572: return;
573: }
574:
575: #
576: # Make owner a coordinator
577: #
578: if (($owner_domain) && ($owner_uname)) {
579: &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
580: }
581:
582: #
583: # Process other reqested users
584: #
585:
586: my @courseroles = qw(st ep ta in);
587: push(@courseroles,$ccrole);
588: if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
589: push(@courseroles,'ad');
590: }
591: my $stulogmsg = '';
592: foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
593: my $url = '/'.$crsudom.'/'.$crsunum;
594: next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');
595: if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
596: my ($username,$userdom) = split/:/,$userkey;
597: if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
598: my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
599: my @badroles;
600: my $firstrole = shift(@reqroles);
601: while (@reqroles > 0) {
602: if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
603: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
604: last;
605: } else {
606: push(@badroles,$firstrole);
607: $firstrole = shift(@reqroles);
608: }
609: } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
610: last;
611: } else {
612: push(@badroles,$firstrole);
613: $firstrole = shift(@reqroles);
614: }
615: }
616: if (@badroles > 0) {
617: if (@badroles > 1) {
618: $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
619: } else {
620: $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]);
621: }
622: }
623: my $firstsec;
624: unless (($firstrole eq $ccrole) || ($firstrole eq '')) {
625: $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
626: }
627: my $userargs = {
628: 'auth' => $details->{'users'}{$userkey}{'authtype'},
629: 'authparam' => $details->{'users'}{$userkey}{'autharg'},
630: 'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
631: 'udom' => $userdom,
632: 'uname' => $username,
633: 'pid' => $details->{'users'}{$userkey}{'studentID'},
634: 'first' => $details->{'users'}{$userkey}{'firstname'},
635: 'middle' => $details->{'users'}{$userkey}{'middlename'},
636: 'last' => $details->{'users'}{$userkey}{'lastname'},
637: 'gene' => $details->{'users'}{$userkey}{'generation'},
638: 'usec' => $firstsec,
639: 'end' => $details->{'users'}{$userkey}{'end'},
640: 'start' => $details->{'users'}{$userkey}{'start'},
641: 'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
642: 'cid' => $courseid,
643: 'crs' => $crsunum,
644: 'cdom' => $crsudom,
645: 'context' => 'createcourse',
646: 'linefeed' => $linefeed,
647: 'role' => $details->{'users'}{$userkey}{'roles'}[0],
648: };
649: if ($userargs->{'role'} eq 'st') {
650: if (exists($details->{'users'}{$userkey}{'credits'})) {
651: $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
652: $userargs->{'credits'} =~ s/[^\d\.]//g;
653: }
654: }
655: $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
656: # now add other roles and other sections.
657: if ($outcome eq 'ok') {
658: if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
659: for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
660: my $curr_role = $firstrole;
661: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
662: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
663: my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
664: $url = '/'.$crsudom.'/'.$crsunum;
665: if ($usec ne '') {
666: $url .= '/'.$usec;
667: }
668: if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
669: $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
670: } else {
671: $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
672: }
673: }
674: }
675: if (@reqroles > 0) {
676: foreach my $curr_role (@reqroles) {
677: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
678: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
679: if ($curr_role eq 'st') {
680: my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
681: $url = '/'.$crsudom.'/'.$crsunum;
682: if ($usec ne '') {
683: $url .= '/'.$usec;
684: }
685: my $credits;
686: if (exists($details->{'users'}{$userkey}{'credits'})) {
687: $credits = $details->{'users'}{$userkey}{'credits'};
688: $credits =~ s/[^\d\.]//g;
689: }
690: $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
691: } elsif ($curr_role eq $ccrole) {
692: $url = '/'.$crsudom.'/'.$crsunum;
693: my $usec = '';
694: $$output .=
695: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
696: } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
697: ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
698: foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
699: $url = '/'.$crsudom.'/'.$crsunum;
700: if ($usec ne '') {
701: $url .= '/'.$usec;
702: }
703: if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
704: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
705: $$output .=
706: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
707: } else {
708: $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
709: }
710: } else {
711: $$output .=
712: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
713: }
714: }
715: } else {
716: $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
717: }
718: }
719: }
720: }
721: } else {
722: foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
723: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
724: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
725: if ($curr_role eq 'st') {
726: my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
727: $url = '/'.$crsudom.'/'.$crsunum;
728: if ($usec ne '') {
729: $url .= '/'.$usec;
730: }
731: my $credits;
732: if (exists($details->{'users'}{$userkey}{'credits'})) {
733: $credits = $details->{'users'}{$userkey}{'credits'};
734: $credits =~ s/[^\d\.]//g;
735: }
736: $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
737: } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
738: ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
739: if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
740: foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
741: $url = '/'.$crsudom.'/'.$crsunum;
742: if ($usec ne '') {
743: $url .= '/'.$usec;
744: }
745: my $stdresult;
746: if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
747: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
748: $stdresult =
749: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
750: } else {
751: $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
752: }
753: } else {
754: $stdresult =
755: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
756: }
757: $$output .= $stdresult;
758: }
759: } else {
760: $url = '/'.$crsudom.'/'.$crsunum;
761: if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
762: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
763: $$output .=
764: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
765: } else {
766: $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
767: }
768: } else {
769: $$output .=
770: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
771: }
772: }
773: } else {
774: $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
775: }
776: }
777: }
778: }
779: }
780:
781: # Information about keys.
782: if ($details->{'setkeys'}) {
783: $$keysmsg .=
784: '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
785: }
786: # Flush the course logs so reverse user roles immediately updated
787: &Apache::lonnet::flushcourselogs();
788: return $courseid;
789: }
790:
791: sub owner_is_dc {
792: my ($owner_uname,$owner_dom,$cdom) = @_;
793: my $is_dc = 0;
794: my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
795: ['active'],['dc'],[$cdom]);
796: if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
797: $is_dc = 1;
798: }
799: return $is_dc;
800: }
801:
802: #########################################################
803: #
804: # process_date()
805: #
806: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
807: # output - corresponding UNIX time (seconds since epoch).
808: #
809: #########################################################
810:
811: sub process_date {
812: my $timestr = shift;
813: my $timestamp = '';
814: if ($timestr !~ /:/) {
815: $timestamp = '';
816: } else {
817: my @entries = split(/:/,$timestr);
818: for (my $j=0; $j<@entries; $j++) {
819: if ( length($entries[$j]) > 1 ) {
820: $entries[$j] =~ s/^0//;
821: }
822: $entries[$j] =~ s/\D//g;
823: if ($entries[$j] < 0) {
824: $entries[$j] = 0;
825: }
826: }
827: if ($entries[1] > 0) {
828: $entries[1] = $entries[1] - 1;
829: }
830: if ($entries[5] > 60) {
831: $entries[5] = 60;
832: }
833: if ($entries[4] > 59) {
834: $entries[4] = 59;
835: }
836: if ($entries[3] > 23) {
837: $entries[3] = 23;
838: }
839: if ($entries[2] > 31) {
840: $entries[2] = 31;
841: }
842: if ($entries[1] > 11) {
843: $entries[1] = 11;
844: }
845: if ($entries[2] == 31) {
846: if (($entries[1] == 3) || ($entries[1] == 5) ||
847: ($entries[1] == 8) || ($entries[1] == 10)) {
848: $entries[2] = 30;
849: }
850: }
851: if ($entries[1] == 1) {
852: if ($entries[2] > 29) {
853: $entries[2] = 29;
854: }
855: if ($entries[2] == 29) {
856: if ($entries[0]%4 != 0) {
857: $entries[2] = 28;
858: } elsif ( $entries[0]%100 == 0
859: && $entries[0]%400 != 0) {
860: $entries[2] = 28;
861: }
862: }
863: }
864: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
865: }
866: return $timestamp;
867: }
868:
869: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>