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

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

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