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