Annotation of loncom/automation/batchcreatecourse.pm, revision 1.3

1.1       raeburn     1: package LONCAPA::batchcreatecourse;
                      2: use LONCAPA::Configuration;
                      3: use LONCAPA::Enrollment;
                      4: use HTML::Parser;
                      5: use Time::Local;
                      6: use Apache::Constants; 
                      7: use Apache::lonnet;
1.2       raeburn     8: use Apache::loncommon;
1.1       raeburn     9: use Apache::loncreatecourse;
1.2       raeburn    10: use Apache::loncreateuser;
1.1       raeburn    11: use Apache::lonlocal;
                     12: 
                     13: # Collection of routines used for batch creation of courses and users.
                     14: # &create_courses() should be called by an Autocreate.pl
                     15: # script via a cron entry, or alternatively from a web page, after upload 
                     16: # of a file containing an XML description of a course request (lonbatchccrs.pm).
                     17: # 
                     18: # XML file(s) describing courses that are to be created in domain $dom are stored in
1.3     ! raeburn    19: # /home/httpd/perl/tmp/addcourse/$dom
1.1       raeburn    20: #
                     21: # &create_courses() will create an account for the course owner 
                     22: # (if one does not currently exist), will create the course (cloning if necessary),
                     23: # and will add additional instructional staff (creating accounts if necessary).
                     24: #
                     25: # Example of XML file (which could contain more than one class to be created):
                     26: #
                     27: #<?xml version="1.0" encoding="UTF-8"?>
                     28: #<!DOCTYPE text>
                     29: #<class id="ss05ubw101">
                     30: # <title>Underwater Basket Weaving</title>
                     31: # <coursecode>ss05ubw101</coursecode>
                     32: # <coursehome>msul1</coursehome>
                     33: # <coursedomain>msu</coursedomain>
                     34: # <reshome>/res/msu/</reshome>
                     35: # <optional_id></optional_id>
                     36: # <adds>1</adds>
                     37: # <drops>1</drops>
                     38: # <enrollstart>2005:01:04:10:30</enrollstart>
                     39: # <enrollend>2005:07:04:20:30</enrollend>
                     40: # <accessstart>2005:01:10:10:30</accessstart>
                     41: # <accessend>2005:05:31:10:30</accessend>
                     42: # <authentication>
                     43: #  <method>krb4</method>
                     44: #  <param>MSU.EDU</param>
                     45: # </authentication>
                     46: # <nonstandard></nonstandard>
                     47: # <topmap></topmap>
                     48: # <firstres>nav</firstres>
                     49: # <clonecrs>466011437c34194msul1</clonecrs>
                     50: # <clonedom>msu</clonedom>
                     51: # <showphotos></showphotos>
                     52: # <setpolicy>1</setpolicy>
                     53: # <setcontent>1</setcontent>
                     54: # <setkeys>0</setkeys>
                     55: # <keyauth>keyadmin@msu</keyauth>
                     56: # <disresdis>1</disresdis>
                     57: # <disablechat>1</disablechat>
                     58: # <openall></openall>
                     59: # <notify_dc>1</notify_dc>
                     60: # <notify_owner>1</notify_owner>
                     61: # <owner>
                     62: #  <username>sparty</username>
                     63: #  <domain>msu</domain>
                     64: #  <authtype>krb4</authtype>
                     65: #  <autharg>MSU.EDU</autharg>
                     66: # </owner>
                     67: # <sections>
                     68: #  <section>
                     69: #   <inst>001</inst>
                     70: #   <loncapa>1</loncapa>
                     71: #  </section>
                     72: #  <section>
                     73: #   <inst>002</inst>
                     74: #   <loncapa>2</loncapa>
                     75: #  </section>
                     76: # </sections>
                     77: # <crosslists>
                     78: #  <xlist>
                     79: #   <inst>ss05zzz101001</inst>
                     80: #   <loncapa>1</loncapa>
                     81: #  </xlist>
                     82: # </crosslists>
                     83: # <users>
                     84: #  <user>
                     85: #   <username>sparty</username>
                     86: #   <domain>msu</domain>
                     87: #   <email>sparty@msu.edu</email>
                     88: #   <authtype>krb4</authtype>
                     89: #   <autharg></autharg>
                     90: #   <firstname>MSU</firstname>
                     91: #   <generation></generation>
                     92: #   <lastname>Spartan</lastname>x
                     93: #   <middlename></middlename>
                     94: #   <studentID></studentID>
                     95: #   <roles></roles>
                     96: #  </user>
                     97: #  <user>
                     98: #   <username>itds0001</username>
                     99: #   <domain>northwood5</domain>
                    100: #   <email>itds0001@msu.edu</email>
                    101: #   <authtype>int</authtype>
                    102: #   <autharg></autharg>
                    103: #   <firstname>Info</firstname>
                    104: #   <generation></generation>
                    105: #   <lastname>Techc</lastname>x
                    106: #   <middlename></middlename>
                    107: #   <studentID></studentID>
                    108: #   <roles>
                    109: #    <role id='in'>
                    110: #     <start>2005:01:01:12:10</start>
                    111: #     <end>2005:12:01:12:10</end>
                    112: #     <usec>1</usec>
                    113: #     <usec>2</usec>
                    114: #    </role>
                    115: #   </roles>
                    116: #  </user>
                    117: # </users>
                    118: #</class>
                    119: #
                    120: # Many of these are binary options (corresponding to either checkboxes or
                    121: # radio buttons in the interactive CCRS page).  Examples include:
                    122: # setpolicy, setcontent, setkeys, disableresdis, disablechat, openall
                    123: #
                    124: # A value of 1 between opening and closing tags is equivalent to a 
                    125: # checked checkbox or 'Yes' response in the original CCRS web page.
                    126: # A value of 0 or blank is equivalent to an unchecked box or 'No'
                    127: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
                    128: #
                    129: # firstres can be nav, syl , or blank for "Navigate Contents", Syllabus, or
                    130: # no entry respectively.
                    131: # For format of other parameters, refer to the interactive CCRS page
                    132: # and view how the equivalent parameter is displayed in the web form.  
                    133: #  
                    134: ##############################################################
                    135: # create_courses() - creates courses described in @$requests,
                    136: #                    files listed in @$requests are deleted
                    137: #                    after the files have been parsed.
                    138: #
1.3     ! raeburn   139: #                    Directory for retrieval of files listed in @$requests is: 
        !           140: #                    /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
        !           141: #                    /home/httpd/perl/tmp/addcourse/$dom/web/$udom_$uname if $context = web
1.1       raeburn   142: #                    
                    143: # inputs (five)  -  requests - ref to array of filename(s) containing course requests 
                    144: #                   courseids - ref to hash to store LON-CAPA course ids of new courses 
                    145: #                   context - auto if called from command line, web if called from browser
                    146: #                   dom - domain for which the course is being created
1.3     ! raeburn   147: #                   uname - username of DC who is requesting course creation
        !           148: #                   udom - domain of DC who is requesting course creation
1.1       raeburn   149: #  
                    150: # outputs (three)  -  output - text recording user roles added etc.
                    151: #                     logmsg - text to be logged
                    152: #                     keysmsg - text containing link(s) to manage keys page(s) 
                    153: #############################################################
                    154: 
                    155: sub create_courses {
1.3     ! raeburn   156:     my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
1.1       raeburn   157:     my $output;
                    158:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
                    159: # Get role names
                    160:     my %longroles = ();
                    161:     open(FILE,"<$perlvarref{'lonTabDir'}.'/rolesplain.tab");
                    162:     my @rolesplain = <FILE>;
                    163:     close(FILE);
                    164:     foreach (@rolesplain) {
                    165:         if ($_ =~ /^(st|ta|ex|ad|in|cc):([\w\s]+)$/) {
                    166:             $longroles{$1} = $2;
                    167:         }
                    168:     }
                    169:     my ($logmsg,$keysmsg,$newusermsg,$addresult);
                    170:     my %enrollcount = ();
                    171:     my $newcoursedir = $$perlvarref{'lonDaemons'}.'/tmp/addcourse/'.$dom.'/'.$context;
1.3     ! raeburn   172:     if ($context eq 'auto') {
        !           173:         $newcoursedir .= '/pending';
        !           174:     } else {
        !           175:         if ($uname && $udom) {
        !           176:             $newcoursedir .= '/'.$udom.'_'.$uname;
        !           177:         } else {
        !           178:             $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
1.1       raeburn   179:         }
                    180:     }
                    181:     if (@{$requests} > 0) {
                    182:         foreach my $request (@{$requests}) {
                    183:             my %details = ();
                    184:             if (-e $newcoursedir.'/'.$request) {
                    185:                 &parse_coursereqs($newcoursedir.'/'.$request, \%details);
                    186:                 foreach my $num (sort keys %details) {
                    187:                     my $courseid = &build_course($dom,$num,$context,\%details,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
                    188:                     $$courseids{$courseid} = $enrollcount;
                    189:                 }
                    190:             }
                    191:         }
                    192:     }
                    193:     return ($output,$logmsg,$keysmsg);
                    194: }
                    195: 
                    196: #############################################################
                    197: #
                    198: # parse_coursereqs() 
                    199: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
                    200: #              - details - reference to hash containing extracted information.
                    201: # outputs (none)
                    202: #
                    203: ############################################################
                    204: 
                    205: sub parse_coursereqs {
                    206:     my ($coursefile,$details) = @_;
                    207: #   Note all start and end dates should be in this format:
                    208: #   YYYY:MM:DD:HH:MM:SS (:separators required).
                    209:     my $uname = '';
                    210:     my @state = ();
                    211:     my $num = 0;
                    212:     my $secid = 0;
                    213:     my $xlist = 0;
                    214:     my $userkey = '';
                    215:     my $role = '';
                    216:     my @items = ('title','optional_id','coursecode','coursehome','reshome','nonstandard','adds','drops','topmap','firstres','clonecrs','clonedom','showphotos','setpolicy','setcontent','setkeys','keyauth','disresdis','disablechat','openall','notify_owner','notify_dc');
                    217:     my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
                    218:     my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
                    219:     my $p = HTML::Parser->new
                    220:     (
                    221:         xml_mode => 1,
                    222:         start_h =>
                    223:             [sub {
                    224:                  my ($tagname, $attr) = @_;
                    225:                  push(@state, $tagname);
                    226:                  if ("@state" eq "class") {
                    227:                      %{$$details{$num}} = ();
                    228:                      $$details{$num}{'class'} = $attr->{id};
                    229:                      %{$$details{$num}{'users'}} = ();
                    230:                      %{$$details{$num}{'sections'}} = ();
                    231:                      $secid = 0;
                    232:                      $xlist = 0;
                    233:                  }
                    234:                  if ("@state" eq "class users user roles role") {
                    235:                      $role = $attr->{id};
                    236:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    237:                          push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);  
                    238:                          %{$$details{$num}{'users'}{$userkey}{$role}} = ();
                    239:                          @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
                    240:                      }
                    241:                  }
                    242:                  if ("@state" eq "class sections section") {
                    243:                      $secid ++;
                    244:                      %{$$details{$num}{'sections'}{$secid}} = ();
                    245:                  }
                    246:                  if ("@state" eq "class crosslists xlist") {
                    247:                      $xlist ++;
                    248:                      %{$$details{$num}{'crosslists'}{$xlist}} = ();
                    249:                  }
                    250:             }, "tagname, attr"],
                    251:          text_h =>
                    252:              [sub {
                    253:                  my ($text) = @_;
                    254:                  if ("@state" eq "class owner username") {
                    255:                      $$details{$num}{'owner'} = $text;
                    256:                  } elsif ("@state" eq "class owner domain") {
                    257:                     $$details{$num}{'domain'} = $text;
                    258:                  } elsif ("@state" eq "class sections section inst") {
                    259:                     $$details{$num}{'sections'}{$secid}{'inst'} = $text;
                    260:                  } elsif ("@state" eq "class sections section loncapa") {
                    261:                     $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
                    262:                  } elsif ("@state" eq "class crosslists xlist inst") {
                    263:                     $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
                    264:                  } elsif ("@state" eq "class crosslists xlist loncapa") {
                    265:                     $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
                    266:                  } elsif ("@state" eq "class owner authtype") {
                    267:                     $$details{$num}{'ownerauthtype'} = $text;
                    268:                  } elsif ("@state" eq "class owner autharg") {
                    269:                     $$details{$num}{'ownerautharg'} = $text;
                    270:                  } elsif ("@state" eq "class authentication method") {
                    271:                     $$details{$num}{'authtype'} = $text;
                    272:                  } elsif ("@state" eq "class authentication param") {
                    273:                     $$details{$num}{'authparam'} = $text;
                    274:                  } elsif ("@state" eq "class users user username") {
                    275:                     $userkey = $text;
                    276:                  } elsif ("@state" eq "class users user domain") {
                    277:                     $userkey .= ':'.$text;
                    278:                     %{$$details{$num}{'users'}{$userkey}} = ();
                    279:                     @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
                    280:                  } elsif ("@state" eq "class users user email") {
                    281:                     $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
                    282:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &Apache::lonnet::escape($text); 
                    283:                  } elsif ("@state" eq "class users user roles role start") {
                    284:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    285:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
                    286:                      }
                    287:                  } elsif ("@state" eq "class users user roles role end") {
                    288:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    289:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
                    290:                      }
                    291:                  } elsif ("@state" eq "class users user roles role usec") {
                    292:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    293:                          unless ($text eq '') {
                    294:                              push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
                    295:                          }
                    296:                      }
                    297:                  } else {
                    298:                      foreach my $item (@items) {
                    299:                          if ("@state" eq "class $item") {
                    300:                              $$details{$num}{$item} = $text;
                    301:                          }
                    302:                      }
                    303:                      foreach my $item (@dateitems) {
                    304:                          if ("@state" eq "class $item") {
                    305:                              $$details{$num}{$item} = &process_date($text);
                    306:                          }
                    307:                      }
                    308:                      foreach my $item (@useritems) {
                    309:                          if ("@state" eq "class users user $item") {
                    310:                              $$details{$num}{'users'}{$userkey}{$item} = $text;
                    311:                          }
                    312:                      }
                    313:                  }
                    314:                }, "dtext"],
                    315:          end_h =>
                    316:                [sub {
                    317:                    my ($tagname) = @_;
                    318:                    if ("@state" eq "class") {
                    319:                        $num ++;
                    320:                    }
                    321:                    pop @state;
                    322:                 }, "tagname"],
                    323:     );
                    324: 
                    325:     $p->parse_file($coursefile);
                    326:     $p->eof;
                    327:     if (-e "$coursefile") {
                    328: #        unlink $coursefile;
                    329:     }
                    330:     return;
                    331: }
                    332: 
                    333: #########################################################
                    334: #
                    335: # build_course() 
                    336: #
                    337: # inputs
                    338: #   domain
                    339: #   course request number
                    340: #   context - auto if called from command line, web if called from DC web interface
                    341: #   ref to hash of course creation information
                    342: #   ref to hash of role descriptions
                    343: #   ref to scalar used to accumulate log messages
                    344: #   ref to scalar used to accumulate messages sent to new users
                    345: #   ref to scalar used to accumulate results of new user additions
                    346: #   ref to hash of enrollment counts for different roles
                    347: #   ref to scalar used to accumulate iformation about added roles
                    348: #   ref to scalar used to accumulate 
                    349: #
                    350: # outputs
                    351: #   LON-CAPA courseID for new (created) course
                    352: #
                    353: #########################################################
                    354: 
                    355: sub build_course {
1.3     ! raeburn   356:     my ($cdom,$num,$context,$details,$longoles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname) = @_;
1.1       raeburn   357:     my $owner_uname = $$details{$num}{'owner'};
                    358:     my $owner_domain = $$details{$num}{'domain'};
                    359:     my $owner = $owner_uname.':'.$owner_domain;
                    360:     my $sectionstr = '';
                    361:     my $xliststr = '';
                    362:     my $noenddate = '';
                    363:     my $outcome;
                    364:     my ($courseid,$crsudom,$crsunum);
                    365:     my $linefeed;
                    366:     if ($context eq 'auto') {
                    367:         $linefeed = "\n";
                    368:     } else {
                    369:         $linefeed = "<br />\n";
                    370:     }
                    371:     if ($$details{$num}{'accessend'} eq '') {
                    372:         $noenddate = 1;
                    373:     }
                    374:     my $reshome = $$details{$num}{'reshome'};
                    375:     if ($reshome eq '') {
                    376:         $reshome = '/res/'.$cdom;
                    377:     }
                    378:     my $firstres =  $$details{$num}{'firstres'};
                    379:     if ($firstres eq '') {
                    380:         $firstres = 'syl';
                    381:     }
                    382:     foreach my $secid (sort keys %{$$details{$num}{'sections'}}) {
                    383:         $sectionstr .= $$details{$num}{'sections'}{$secid}{'inst'}.':'.$$details{$num}{'sections'}{$secid}{'loncapa'};
                    384:     }
                    385: 
                    386:     foreach my $xlist (sort keys %{$$details{$num}{'crosslists'}}) {
                    387:         $xliststr .= $$details{$num}{'crosslists'}{$xlist}{'inst'}.':'.$$details{$num}{'crosslists'}{$xlist}{'loncapa'};
                    388:     }
                    389: 
                    390:     my %courseinfo = (
                    391:                       inst_code => $$details{$num}{'coursecode'},
                    392:                       description => $$details{$num}{'title'}
                    393:                      ); 
                    394:     if (&Apache::lonnet::homeserver($$details{$num}{'owner'},$$details{$num}{'domain'}) eq 'no_host') { # Add user if no account
                    395:         my $ownerargs = ('auth' => $$details{$num}{'ownerauthtype'},
                    396:                     'authparam' => $$details{$num}{'ownerauthparam'},
                    397:                     'emailenc' => $$details{$num}{'emailenc'},
                    398:                     'dom' => $$details{$num}{'domain'},
                    399:                     'uname' => $$details{$num}{'owner'},
                    400:                     'pid' => '',
                    401:                     'first' => $$details{$num}{'users'}{$owner}{'first'},
                    402:                     'middle' => $$details{$num}{'users'}{$owner}{'middle'},
                    403:                     'last' => $$details{$num}{'users'}{$owner}{'last'},
                    404:                     'gene' => $$details{$num}{'users'}{$owner}{'gene'},
                    405:                     'usec' => '',
                    406:                     'end' => '',
                    407:                     'start' => '',
                    408:                     'emailaddr' => $$details{$num}{'users'}{$owner}{'email'},
                    409:                     'cid' => '',
                    410:                     'context' => 'createowner',
                    411:                     'linefeed' => $linefeed,
                    412:                     'role' => 'cc'
                    413:                    );
                    414:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,$courseinfo);
                    415:     } else {
                    416:         $outcome = 'ok';
                    417:     }
                    418: 
                    419:     my $courseargs = {
                    420:                ccuname => $$details{$num}{'owner'},
                    421:                ccdomain => $$details{$num}{'domain'},
                    422:                cdescr => $$details{$num}{'title'},
                    423:                curl => $$details{$num}{'topmap'},
                    424:                course_domain => $cdom,
                    425:                course_home =>  $$details{$num}{'coursehome'},
                    426:                nonstandard => $$details{$num}{'nonstandard'},
                    427:                crscode => $$details{$num}{'coursecode'},
                    428:                clonecourse => $$details{$num}{'clonecrs'},
                    429:                clonedomain => $$details{$num}{'clonedom'},
                    430:                crsid => $$details{$num}{'optional_id'},
                    431:                curruser => $$details{$num}{'owner'},
                    432:                crssections => $sectionstr,
                    433:                crsxlist => $xliststr,
                    434:                autoadds => $$details{$num}{'adds'},
                    435:                autodrops => $$details{$num}{'drops'},
                    436:                notify => $$details{$num}{'notify_owner'},
                    437:                notify_dc => $$details{$num}{'notify_dc'},
                    438:                no_end_date => $noenddate,
                    439:                showphotos => $$details{$num}{'showphotos'},
                    440:                authtype => $$details{$num}{'authtype'},
                    441:                autharg => $$details{$num}{'authparam'},
                    442:                enrollstart => $$details{$num}{'enrollstart'},
                    443:                enrollend => $$details{$num}{'enrollend'},
                    444:                startaccess => $$details{$num}{'accessstart'},
                    445:                endaccess => $$details{$num}{'accessend'},
                    446:                setpolicy => $Sdetails{$num}{'setpolicy'},
                    447:                setcontent => $$details{$num}{'setcontent'},
                    448:                reshome => $reshome,
                    449:                setkeys => $$details{$num}{'setkeys'},
                    450:                keyauth => $$details{$num}{'keyauth'},
                    451:                disresdis => $$details{$num}{'disresdis'},
                    452:                disablechat => $$details{$num}{'disablechat'},
                    453:                openall => $$details{$num}{'openall'},
                    454:                firstres => $firstres
                    455:                };
                    456: 
                    457:     if ($outcome eq 'ok') {
                    458:         my %host_servers = &Apache::loncommon::get_library_servers($cdom);
                    459:         if (! exists($host_servers{$$details{$num}{'coursehome'}})) {
                    460:             $$logmsg .= &mt('Invalid home server for course').': '.$$details{$num}{'coursehome'};
                    461:             return;
                    462:         }
                    463: 
1.3     ! raeburn   464:         &Apache::loncreatecourse::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname);
1.1       raeburn   465:     } else {
                    466:         return;
                    467:     }
                    468:     
                    469: #
                    470: # Make owner a course coordinator
                    471: #
                    472:     if (($owner_domain) && ($owner_uname)) {
                    473:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,'cc');
                    474:     }
                    475: 
                    476: #
                    477: # Process other reqested users
                    478: #
                    479:     my $stulogmsg = '';
                    480:     foreach my $userkey (sort keys %{$$details{$num}{'users'}}) {
                    481:         my $url = '/'.$crsudom.'/'.$crsunum;
                    482:         if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 0) {
                    483:             my ($username,$userdom) = split/:/,$userkey;
                    484:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
                    485:                 my $firstrole = $$details{$num}{'users'}{$userkey}{'roles'}[0];
                    486:                 my $firssec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    487:                 my $userargs = ('auth' => $$details{$num}{'users'}{$userkey}{'authtype'},
                    488:                     'authparam' => $$details{$num}{'users'}{$userkey}{'authparam'},
                    489:                     'emailenc' => $$details{$num}{'users'}{$userkey}{'emailenc'},
                    490:                     'dom' => $userdom,
                    491:                     'uname' => $username,
                    492:                     'pid' => $$details{$num}{'users'}{$userkey}{'studentID'},
                    493:                     'first' => $$details{$num}{'users'}{$userkey}{'first'},
                    494:                     'middle' => $$details{$num}{'users'}{$userkey}{'middle'},
                    495:                     'last' => $$details{$num}{'users'}{$userkey}{'last'},
                    496:                     'gene' => $$details{$num}{'users'}{$userkey}{'gene'},
                    497:                     'usec' => $firstsec,
                    498:                     'end' => $$details{$num}{'users'}{$userkey}{'end'},
                    499:                     'start' => $$details{$num}{'users'}{$userkey}{'start'},
                    500:                     'emailaddr' => $$details{$num}{'users'}{$userkey}{'email'},
                    501:                     'cid' => $courseid,
                    502:                     'context' => 'createcourse',
                    503:                     'linefeed' => $linefeed,
                    504:                     'role' => $$details{$num}{'users'}{$userkey}{'roles'}[0], 
                    505:                    );
                    506:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,$courseinfo);
                    507: # now add other roles and other sections.
                    508:                 if ($outcome eq 'ok') {
                    509:                     if (($firstrole ne 'st') && (@{$$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
                    510:                         for (my $i=1; $i<@{$$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
                    511:                             my $curr_role = $firstrole;
                    512:                             my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    513:                             my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    514:                             my $usec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[$i];
                    515:                             $url = '/'.$crsudom.'/'.$crsunum;
                    516:                             if ($usec ne '') {
                    517:                                 $url .= '/'.$usec;
                    518:                             }
                    519:                             $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    520:                         }
                    521:                     }
                    522:                     if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 1) {
                    523:                         for (my $j=1; $j<@{$$details{$num}{'users'}{$userkey}{'roles'}}; $j++) {
                    524:                             my $curr_role = $$details{$num}{'users'}{$userkey}{'roles'}[$j];
                    525:                             my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    526:                             my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    527:                             if ($curr_role eq 'st') {
                    528:                                 my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
                    529:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    530:                                 if ($usec ne '') {
                    531:                                     $url .= '/'.$usec;
                    532:                                 }
                    533:                                 $$output .= &Apache::loncreateuser::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    534:                             } else {
                    535:                                 foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
                    536:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    537:                                     if ($usec ne '') {
                    538:                                         $url .= '/'.$usec;
                    539:                                     }
                    540:                                     $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    541:                                 }
                    542:                             }
                    543:                         }
                    544:                     }
                    545:                 }
                    546:             } else {
                    547:                 foreach my $curr_role (@{$$details{$num}{'users'}{$userkey}{'roles'}}) {
                    548:                     my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    549:                     my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    550:                     if ($curr_role eq 'st') {
                    551:                         my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
                    552:                         $url = '/'.$crsudom.'/'.$crsunum;
                    553:                         if ($usec ne '') {
                    554:                             $url .= '/'.$usec;
                    555:                         }
                    556:                         $$output .= &Apache::loncreateuser::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    557:                     } else {
                    558:                         if (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    559:                             foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
                    560:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    561:                                 if ($usec ne '') {
                    562:                                     $url .= '/'.$usec;
                    563:                                 }
                    564:                                 $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    565:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    566:                                 if ($usec ne '') {
                    567:                                     $url .= '/'.$usec;
                    568:                                 }
                    569:                             }
                    570:                         } else {
                    571:                             $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'');
                    572:                         }
                    573:                     }
                    574:                 }
                    575:             }
                    576:         }
                    577:     }
                    578: 
                    579: # Information about keys.
                    580:     if ($$details{$num}{'setkeys'}) {
                    581:         $$keysmsg .=
                    582:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$$details{$num}{'title'}.$linefeed;
                    583:     }
                    584: # Flush the course logs so reverse user roles immediately updated
                    585:     &Apache::lonnet::flushcourselogs();
                    586:     return $courseid;
                    587: }
                    588: 
                    589: #########################################################
                    590: #
                    591: # process_date()
                    592: # 
                    593: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    594: # output - corresponding UNIX time (seconds since epoch). 
                    595: #
                    596: #########################################################
                    597: 
                    598: sub process_date {
                    599:     my $timestr = shift;
                    600:     my $timestamp = '';
                    601:     if ($timestr eq "No end date") {
                    602:         $timestamp = '';
                    603:     } else {
                    604:         my @entries = split/:/,$timestr;
                    605:         for (my $j=0; $j<@entries; $j++) {
                    606:             if ( length($entries[$j]) > 1 ) {
                    607:                 $entries[$j] =~ s/^0//;
                    608:             }
                    609:         }
                    610:         $entries[1] = $entries[1] - 1;
                    611:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    612:     }
                    613:     return $timestamp;
                    614: }
                    615: 
                    616: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>