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

1.4       albertel    1: #
1.15    ! raeburn     2: # $Id: batchcreatecourse.pm,v 1.14 2006/05/31 12:57:26 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::Constants; 
                     33: use Apache::lonnet;
1.2       raeburn    34: use Apache::loncommon;
1.1       raeburn    35: use Apache::loncreatecourse;
1.2       raeburn    36: use Apache::loncreateuser;
1.1       raeburn    37: use Apache::lonlocal;
                     38: 
1.9       raeburn    39: use strict;
                     40: 
1.1       raeburn    41: # Collection of routines used for batch creation of courses and users.
                     42: # &create_courses() should be called by an Autocreate.pl
                     43: # script via a cron entry, or alternatively from a web page, after upload 
                     44: # of a file containing an XML description of a course request (lonbatchccrs.pm).
                     45: # 
                     46: # XML file(s) describing courses that are to be created in domain $dom are stored in
1.3       raeburn    47: # /home/httpd/perl/tmp/addcourse/$dom
1.1       raeburn    48: #
                     49: # &create_courses() will create an account for the course owner 
                     50: # (if one does not currently exist), will create the course (cloning if necessary),
                     51: # and will add additional instructional staff (creating accounts if necessary).
                     52: #
                     53: # Example of XML file (which could contain more than one class to be created):
                     54: #
                     55: #<?xml version="1.0" encoding="UTF-8"?>
                     56: #<!DOCTYPE text>
                     57: #<class id="ss05ubw101">
                     58: # <title>Underwater Basket Weaving</title>
1.15    ! raeburn    59: # <crstype>Course</coursetype>
1.1       raeburn    60: # <coursecode>ss05ubw101</coursecode>
                     61: # <coursehome>msul1</coursehome>
                     62: # <coursedomain>msu</coursedomain>
                     63: # <reshome>/res/msu/</reshome>
                     64: # <optional_id></optional_id>
                     65: # <adds>1</adds>
                     66: # <drops>1</drops>
                     67: # <enrollstart>2005:01:04:10:30</enrollstart>
                     68: # <enrollend>2005:07:04:20:30</enrollend>
                     69: # <accessstart>2005:01:10:10:30</accessstart>
                     70: # <accessend>2005:05:31:10:30</accessend>
                     71: # <authentication>
                     72: #  <method>krb4</method>
                     73: #  <param>MSU.EDU</param>
                     74: # </authentication>
                     75: # <nonstandard></nonstandard>
                     76: # <topmap></topmap>
                     77: # <firstres>nav</firstres>
1.15    ! raeburn    78: # <crsquota>20</crsquota>
1.1       raeburn    79: # <clonecrs>466011437c34194msul1</clonecrs>
                     80: # <clonedom>msu</clonedom>
                     81: # <showphotos></showphotos>
                     82: # <setpolicy>1</setpolicy>
                     83: # <setcontent>1</setcontent>
                     84: # <setkeys>0</setkeys>
                     85: # <keyauth>keyadmin@msu</keyauth>
                     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);
                    222:                 foreach my $num (sort keys %details) {
                    223:                     my $courseid = &build_course($dom,$num,$context,\%details,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
1.5       raeburn   224:                     $$courseids{$courseid} = $details{$num}{'class'};
1.1       raeburn   225:                 }
                    226:             }
                    227:         }
                    228:     }
                    229:     return ($output,$logmsg,$keysmsg);
                    230: }
                    231: 
                    232: #############################################################
                    233: #
                    234: # parse_coursereqs() 
                    235: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
                    236: #              - details - reference to hash containing extracted information.
                    237: # outputs (none)
                    238: #
                    239: ############################################################
                    240: 
                    241: sub parse_coursereqs {
                    242:     my ($coursefile,$details) = @_;
                    243: #   Note all start and end dates should be in this format:
                    244: #   YYYY:MM:DD:HH:MM:SS (:separators required).
                    245:     my $uname = '';
                    246:     my @state = ();
                    247:     my $num = 0;
                    248:     my $secid = 0;
                    249:     my $xlist = 0;
                    250:     my $userkey = '';
                    251:     my $role = '';
1.15    ! raeburn   252:     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','crstype','crsquota');
1.1       raeburn   253:     my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
                    254:     my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
                    255:     my $p = HTML::Parser->new
                    256:     (
                    257:         xml_mode => 1,
                    258:         start_h =>
                    259:             [sub {
                    260:                  my ($tagname, $attr) = @_;
                    261:                  push(@state, $tagname);
                    262:                  if ("@state" eq "class") {
                    263:                      %{$$details{$num}} = ();
                    264:                      $$details{$num}{'class'} = $attr->{id};
                    265:                      %{$$details{$num}{'users'}} = ();
                    266:                      %{$$details{$num}{'sections'}} = ();
                    267:                      $secid = 0;
                    268:                      $xlist = 0;
                    269:                  }
                    270:                  if ("@state" eq "class users user roles role") {
                    271:                      $role = $attr->{id};
                    272:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    273:                          push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);  
                    274:                          %{$$details{$num}{'users'}{$userkey}{$role}} = ();
                    275:                          @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
                    276:                      }
                    277:                  }
                    278:                  if ("@state" eq "class sections section") {
                    279:                      $secid ++;
                    280:                      %{$$details{$num}{'sections'}{$secid}} = ();
                    281:                  }
                    282:                  if ("@state" eq "class crosslists xlist") {
                    283:                      $xlist ++;
                    284:                      %{$$details{$num}{'crosslists'}{$xlist}} = ();
                    285:                  }
                    286:             }, "tagname, attr"],
                    287:          text_h =>
                    288:              [sub {
                    289:                  my ($text) = @_;
                    290:                  if ("@state" eq "class owner username") {
                    291:                      $$details{$num}{'owner'} = $text;
                    292:                  } elsif ("@state" eq "class owner domain") {
                    293:                     $$details{$num}{'domain'} = $text;
                    294:                  } elsif ("@state" eq "class sections section inst") {
                    295:                     $$details{$num}{'sections'}{$secid}{'inst'} = $text;
                    296:                  } elsif ("@state" eq "class sections section loncapa") {
                    297:                     $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
                    298:                  } elsif ("@state" eq "class crosslists xlist inst") {
                    299:                     $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
                    300:                  } elsif ("@state" eq "class crosslists xlist loncapa") {
                    301:                     $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
                    302:                  } elsif ("@state" eq "class owner authtype") {
                    303:                     $$details{$num}{'ownerauthtype'} = $text;
                    304:                  } elsif ("@state" eq "class owner autharg") {
1.5       raeburn   305:                     $$details{$num}{'ownerauthparam'} = $text;
1.1       raeburn   306:                  } elsif ("@state" eq "class authentication method") {
                    307:                     $$details{$num}{'authtype'} = $text;
                    308:                  } elsif ("@state" eq "class authentication param") {
                    309:                     $$details{$num}{'authparam'} = $text;
                    310:                  } elsif ("@state" eq "class users user username") {
                    311:                     $userkey = $text;
                    312:                  } elsif ("@state" eq "class users user domain") {
                    313:                     $userkey .= ':'.$text;
                    314:                     %{$$details{$num}{'users'}{$userkey}} = ();
                    315:                     @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
                    316:                  } elsif ("@state" eq "class users user email") {
                    317:                     $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
                    318:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &Apache::lonnet::escape($text); 
                    319:                  } elsif ("@state" eq "class users user roles role start") {
                    320:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    321:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
                    322:                      }
                    323:                  } elsif ("@state" eq "class users user roles role end") {
                    324:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    325:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
                    326:                      }
                    327:                  } elsif ("@state" eq "class users user roles role usec") {
                    328:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    329:                          unless ($text eq '') {
                    330:                              push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
                    331:                          }
                    332:                      }
                    333:                  } else {
                    334:                      foreach my $item (@items) {
                    335:                          if ("@state" eq "class $item") {
                    336:                              $$details{$num}{$item} = $text;
                    337:                          }
                    338:                      }
                    339:                      foreach my $item (@dateitems) {
                    340:                          if ("@state" eq "class $item") {
                    341:                              $$details{$num}{$item} = &process_date($text);
                    342:                          }
                    343:                      }
                    344:                      foreach my $item (@useritems) {
                    345:                          if ("@state" eq "class users user $item") {
                    346:                              $$details{$num}{'users'}{$userkey}{$item} = $text;
                    347:                          }
                    348:                      }
                    349:                  }
                    350:                }, "dtext"],
                    351:          end_h =>
                    352:                [sub {
                    353:                    my ($tagname) = @_;
                    354:                    if ("@state" eq "class") {
                    355:                        $num ++;
                    356:                    }
                    357:                    pop @state;
                    358:                 }, "tagname"],
                    359:     );
                    360: 
                    361:     $p->parse_file($coursefile);
                    362:     $p->eof;
                    363:     return;
                    364: }
                    365: 
                    366: #########################################################
                    367: #
                    368: # build_course() 
                    369: #
                    370: # inputs
                    371: #   domain
                    372: #   course request number
                    373: #   context - auto if called from command line, web if called from DC web interface
                    374: #   ref to hash of course creation information
                    375: #   ref to hash of role descriptions
                    376: #   ref to scalar used to accumulate log messages
                    377: #   ref to scalar used to accumulate messages sent to new users
                    378: #   ref to scalar used to accumulate results of new user additions
                    379: #   ref to hash of enrollment counts for different roles
                    380: #   ref to scalar used to accumulate iformation about added roles
                    381: #   ref to scalar used to accumulate 
                    382: #
                    383: # outputs
                    384: #   LON-CAPA courseID for new (created) course
                    385: #
                    386: #########################################################
                    387: 
                    388: sub build_course {
1.9       raeburn   389:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname) = @_;
1.1       raeburn   390:     my $owner_uname = $$details{$num}{'owner'};
                    391:     my $owner_domain = $$details{$num}{'domain'};
                    392:     my $owner = $owner_uname.':'.$owner_domain;
                    393:     my $sectionstr = '';
                    394:     my $xliststr = '';
                    395:     my $noenddate = '';
                    396:     my $outcome;
                    397:     my ($courseid,$crsudom,$crsunum);
                    398:     my $linefeed;
                    399:     if ($context eq 'auto') {
                    400:         $linefeed = "\n";
                    401:     } else {
                    402:         $linefeed = "<br />\n";
                    403:     }
                    404:     if ($$details{$num}{'accessend'} eq '') {
                    405:         $noenddate = 1;
                    406:     }
                    407:     my $reshome = $$details{$num}{'reshome'};
                    408:     if ($reshome eq '') {
                    409:         $reshome = '/res/'.$cdom;
                    410:     }
                    411:     my $firstres =  $$details{$num}{'firstres'};
                    412:     if ($firstres eq '') {
                    413:         $firstres = 'syl';
                    414:     }
                    415:     foreach my $secid (sort keys %{$$details{$num}{'sections'}}) {
1.5       raeburn   416:         $sectionstr .= $$details{$num}{'sections'}{$secid}{'inst'}.':'.$$details{$num}{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   417:     }
1.5       raeburn   418:     $sectionstr =~ s/,$//;
1.1       raeburn   419: 
                    420:     foreach my $xlist (sort keys %{$$details{$num}{'crosslists'}}) {
1.5       raeburn   421:         $xliststr .= $$details{$num}{'crosslists'}{$xlist}{'inst'}.':'.$$details{$num}{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   422:     }
1.5       raeburn   423:     $xliststr =~ s/,$//;
1.1       raeburn   424: 
                    425:     my %courseinfo = (
                    426:                       inst_code => $$details{$num}{'coursecode'},
                    427:                       description => $$details{$num}{'title'}
                    428:                      ); 
                    429:     if (&Apache::lonnet::homeserver($$details{$num}{'owner'},$$details{$num}{'domain'}) eq 'no_host') { # Add user if no account
1.5       raeburn   430:         my $ownerargs = {'auth' => $$details{$num}{'ownerauthtype'},
1.1       raeburn   431:                     'authparam' => $$details{$num}{'ownerauthparam'},
                    432:                     'emailenc' => $$details{$num}{'emailenc'},
1.7       raeburn   433:                     'udom' => $$details{$num}{'domain'},
1.1       raeburn   434:                     'uname' => $$details{$num}{'owner'},
1.9       raeburn   435:                     'pid' => $$details{$num}{'users'}{$owner}{'studentID'},
                    436:                     'first' => $$details{$num}{'users'}{$owner}{'firstname'},
                    437:                     'middle' => $$details{$num}{'users'}{$owner}{'middlename'},
                    438:                     'last' => $$details{$num}{'users'}{$owner}{'lastname'},
                    439:                     'gene' => $$details{$num}{'users'}{$owner}{'generation'},
1.1       raeburn   440:                     'usec' => '',
                    441:                     'end' => '',
                    442:                     'start' => '',
                    443:                     'emailaddr' => $$details{$num}{'users'}{$owner}{'email'},
                    444:                     'cid' => '',
                    445:                     'context' => 'createowner',
                    446:                     'linefeed' => $linefeed,
1.5       raeburn   447:                     'role' => 'cc',
                    448:                    };
1.6       raeburn   449:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo);
1.1       raeburn   450:     } else {
                    451:         $outcome = 'ok';
                    452:     }
                    453: 
1.6       raeburn   454:     if ($outcome eq 'ok') {
                    455:         my $courseargs = {
1.1       raeburn   456:                ccuname => $$details{$num}{'owner'},
                    457:                ccdomain => $$details{$num}{'domain'},
                    458:                cdescr => $$details{$num}{'title'},
1.15    ! raeburn   459:                crstype => $$details{$num}{'crstype'},
1.1       raeburn   460:                curl => $$details{$num}{'topmap'},
                    461:                course_domain => $cdom,
                    462:                course_home =>  $$details{$num}{'coursehome'},
                    463:                nonstandard => $$details{$num}{'nonstandard'},
                    464:                crscode => $$details{$num}{'coursecode'},
1.15    ! raeburn   465:                crsquota => $$details{$num}{'crsquota'},
1.1       raeburn   466:                clonecourse => $$details{$num}{'clonecrs'},
                    467:                clonedomain => $$details{$num}{'clonedom'},
                    468:                crsid => $$details{$num}{'optional_id'},
                    469:                curruser => $$details{$num}{'owner'},
                    470:                crssections => $sectionstr,
                    471:                crsxlist => $xliststr,
                    472:                autoadds => $$details{$num}{'adds'},
                    473:                autodrops => $$details{$num}{'drops'},
                    474:                notify => $$details{$num}{'notify_owner'},
                    475:                notify_dc => $$details{$num}{'notify_dc'},
                    476:                no_end_date => $noenddate,
                    477:                showphotos => $$details{$num}{'showphotos'},
                    478:                authtype => $$details{$num}{'authtype'},
                    479:                autharg => $$details{$num}{'authparam'},
                    480:                enrollstart => $$details{$num}{'enrollstart'},
                    481:                enrollend => $$details{$num}{'enrollend'},
                    482:                startaccess => $$details{$num}{'accessstart'},
                    483:                endaccess => $$details{$num}{'accessend'},
1.9       raeburn   484:                setpolicy => $$details{$num}{'setpolicy'},
1.1       raeburn   485:                setcontent => $$details{$num}{'setcontent'},
                    486:                reshome => $reshome,
                    487:                setkeys => $$details{$num}{'setkeys'},
                    488:                keyauth => $$details{$num}{'keyauth'},
                    489:                disresdis => $$details{$num}{'disresdis'},
                    490:                disablechat => $$details{$num}{'disablechat'},
                    491:                openall => $$details{$num}{'openall'},
                    492:                firstres => $firstres
                    493:                };
                    494: 
                    495:         my %host_servers = &Apache::loncommon::get_library_servers($cdom);
                    496:         if (! exists($host_servers{$$details{$num}{'coursehome'}})) {
                    497:             $$logmsg .= &mt('Invalid home server for course').': '.$$details{$num}{'coursehome'};
                    498:             return;
                    499:         }
                    500: 
1.3       raeburn   501:         &Apache::loncreatecourse::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname);
1.1       raeburn   502:     } else {
                    503:         return;
                    504:     }
                    505:     
                    506: #
                    507: # Make owner a course coordinator
                    508: #
                    509:     if (($owner_domain) && ($owner_uname)) {
                    510:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,'cc');
                    511:     }
                    512: 
                    513: #
                    514: # Process other reqested users
                    515: #
                    516:     my $stulogmsg = '';
                    517:     foreach my $userkey (sort keys %{$$details{$num}{'users'}}) {
                    518:         my $url = '/'.$crsudom.'/'.$crsunum;
                    519:         if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 0) {
                    520:             my ($username,$userdom) = split/:/,$userkey;
                    521:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
                    522:                 my $firstrole = $$details{$num}{'users'}{$userkey}{'roles'}[0];
1.8       raeburn   523:                 my $firstsec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[0];
1.5       raeburn   524:                 my $userargs = {
                    525:                     'auth' => $$details{$num}{'users'}{$userkey}{'authtype'},
                    526:                     'authparam' => $$details{$num}{'users'}{$userkey}{'autharg'},
1.1       raeburn   527:                     'emailenc' => $$details{$num}{'users'}{$userkey}{'emailenc'},
1.7       raeburn   528:                     'udom' => $userdom,
1.1       raeburn   529:                     'uname' => $username,
                    530:                     'pid' => $$details{$num}{'users'}{$userkey}{'studentID'},
1.8       raeburn   531:                     'first' => $$details{$num}{'users'}{$userkey}{'firstname'},
                    532:                     'middle' => $$details{$num}{'users'}{$userkey}{'middlename'},
                    533:                     'last' => $$details{$num}{'users'}{$userkey}{'lastname'},
                    534:                     'gene' => $$details{$num}{'users'}{$userkey}{'generation'},
1.1       raeburn   535:                     'usec' => $firstsec,
                    536:                     'end' => $$details{$num}{'users'}{$userkey}{'end'},
                    537:                     'start' => $$details{$num}{'users'}{$userkey}{'start'},
1.8       raeburn   538:                     'emailaddr' => $$details{$num}{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   539:                     'cid' => $courseid,
1.8       raeburn   540:                     'crs' => $crsunum,
                    541:                     'cdom' => $crsudom,
1.1       raeburn   542:                     'context' => 'createcourse',
                    543:                     'linefeed' => $linefeed,
                    544:                     'role' => $$details{$num}{'users'}{$userkey}{'roles'}[0], 
1.5       raeburn   545:                    };
1.6       raeburn   546:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo);
1.1       raeburn   547: # now add other roles and other sections.
                    548:                 if ($outcome eq 'ok') {
                    549:                     if (($firstrole ne 'st') && (@{$$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
                    550:                         for (my $i=1; $i<@{$$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
                    551:                             my $curr_role = $firstrole;
                    552:                             my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    553:                             my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    554:                             my $usec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[$i];
                    555:                             $url = '/'.$crsudom.'/'.$crsunum;
                    556:                             if ($usec ne '') {
                    557:                                 $url .= '/'.$usec;
                    558:                             }
                    559:                             $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    560:                         }
                    561:                     }
                    562:                     if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 1) {
                    563:                         for (my $j=1; $j<@{$$details{$num}{'users'}{$userkey}{'roles'}}; $j++) {
                    564:                             my $curr_role = $$details{$num}{'users'}{$userkey}{'roles'}[$j];
                    565:                             my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    566:                             my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    567:                             if ($curr_role eq 'st') {
                    568:                                 my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
                    569:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    570:                                 if ($usec ne '') {
                    571:                                     $url .= '/'.$usec;
                    572:                                 }
                    573:                                 $$output .= &Apache::loncreateuser::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    574:                             } else {
                    575:                                 foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
                    576:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    577:                                     if ($usec ne '') {
                    578:                                         $url .= '/'.$usec;
                    579:                                     }
                    580:                                     $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    581:                                 }
                    582:                             }
                    583:                         }
                    584:                     }
                    585:                 }
                    586:             } else {
                    587:                 foreach my $curr_role (@{$$details{$num}{'users'}{$userkey}{'roles'}}) {
                    588:                     my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
                    589:                     my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
                    590:                     if ($curr_role eq 'st') {
                    591:                         my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
                    592:                         $url = '/'.$crsudom.'/'.$crsunum;
                    593:                         if ($usec ne '') {
                    594:                             $url .= '/'.$usec;
                    595:                         }
                    596:                         $$output .= &Apache::loncreateuser::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    597:                     } else {
                    598:                         if (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    599:                             foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
                    600:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    601:                                 if ($usec ne '') {
                    602:                                     $url .= '/'.$usec;
                    603:                                 }
1.5       raeburn   604:                                 my $stdresult = &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec);
                    605:                                 $$output .= $stdresult;
1.1       raeburn   606:                             }
                    607:                         } else {
1.5       raeburn   608:                             $url = '/'.$crsudom.'/'.$crsunum;
1.1       raeburn   609:                             $$output .= &Apache::loncreateuser::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'');
                    610:                         }
                    611:                     }
                    612:                 }
                    613:             }
                    614:         }
                    615:     }
                    616: 
                    617: # Information about keys.
                    618:     if ($$details{$num}{'setkeys'}) {
                    619:         $$keysmsg .=
                    620:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$$details{$num}{'title'}.$linefeed;
                    621:     }
                    622: # Flush the course logs so reverse user roles immediately updated
                    623:     &Apache::lonnet::flushcourselogs();
                    624:     return $courseid;
                    625: }
                    626: 
                    627: #########################################################
                    628: #
                    629: # process_date()
                    630: # 
                    631: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    632: # output - corresponding UNIX time (seconds since epoch). 
                    633: #
                    634: #########################################################
                    635: 
                    636: sub process_date {
                    637:     my $timestr = shift;
                    638:     my $timestamp = '';
1.5       raeburn   639:     if ($timestr !~ /:/) {
1.1       raeburn   640:         $timestamp = '';
                    641:     } else {
1.12      albertel  642:         my @entries = split(/:/,$timestr);
1.1       raeburn   643:         for (my $j=0; $j<@entries; $j++) {
                    644:             if ( length($entries[$j]) > 1 ) {
                    645:                 $entries[$j] =~ s/^0//;
                    646:             }
1.10      raeburn   647:             $entries[$j] =~ s/\D//g;
                    648:             if ($entries[$j] < 0) {
                    649:                 $entries[$j] = 0;
                    650:             }
1.1       raeburn   651:         }
1.5       raeburn   652:         if ($entries[1] > 0) {  
                    653:             $entries[1] = $entries[1] - 1;
                    654:         }
1.10      raeburn   655:         if ($entries[5] > 60) {
                    656:             $entries[5] = 60;
                    657:         }
                    658:         if ($entries[4] > 59) {
                    659:             $entries[4] = 59;
                    660:         }
                    661:         if ($entries[3] > 23) {
                    662:             $entries[3] = 23;
                    663:         }
                    664:         if ($entries[2] > 31) {
                    665:             $entries[2] = 31;
                    666:         }
                    667:         if ($entries[1] > 11) {
                    668:             $entries[1] = 11;
                    669:         }
                    670:         if ($entries[2] == 31) {
                    671:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    672:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    673:                 $entries[2] = 30;
                    674:             }
                    675:         }
                    676:         if ($entries[1] == 1) {
                    677:             if ($entries[2] > 29) {
                    678:                 $entries[2] = 29;
                    679:             }
                    680:             if ($entries[2] == 29) {
1.12      albertel  681:                 if ($entries[0]%4 != 0) {
1.14      raeburn   682:                     $entries[2] = 28;
1.12      albertel  683:                 } elsif ( $entries[0]%100 == 0
                    684: 			  && $entries[0]%400 != 0) {
1.14      raeburn   685: 		    $entries[2] = 28;
1.12      albertel  686: 		}
1.10      raeburn   687:             }
                    688:         }     
1.1       raeburn   689:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    690:     }
                    691:     return $timestamp;
                    692: }
                    693: 
                    694: 1;

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