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

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

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