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

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

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