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

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

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