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