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

1.4       albertel    1: #
1.32    ! raeburn     2: # $Id: batchcreatecourse.pm,v 1.31 2009/09/05 20:20:17 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);
1.32    ! raeburn   200:     foreach my $item (@rolesplain) {
        !           201:         if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
        !           202:             $longroles{'Course'}{$1} = $2;
        !           203:             $longroles{'Community'}{$1} = $3;
1.1       raeburn   204:         }
                    205:     }
                    206:     my ($logmsg,$keysmsg,$newusermsg,$addresult);
                    207:     my %enrollcount = ();
                    208:     my $newcoursedir = $$perlvarref{'lonDaemons'}.'/tmp/addcourse/'.$dom.'/'.$context;
1.3       raeburn   209:     if ($context eq 'auto') {
                    210:         $newcoursedir .= '/pending';
                    211:     } else {
                    212:         if ($uname && $udom) {
1.13      raeburn   213:             $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
1.3       raeburn   214:         } else {
                    215:             $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
1.1       raeburn   216:         }
                    217:     }
                    218:     if (@{$requests} > 0) {
                    219:         foreach my $request (@{$requests}) {
                    220:             my %details = ();
                    221:             if (-e $newcoursedir.'/'.$request) {
                    222:                 &parse_coursereqs($newcoursedir.'/'.$request, \%details);
1.29      raeburn   223:                 foreach my $num (sort(keys(%details))) {
                    224:                     my $reqdetails = $details{$num};
                    225:                     my $courseid = &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
1.25      raeburn   226:                     if ($courseid =~m{^/$match_domain/$match_courseid}) {
1.21      raeburn   227:                         $$courseids{$courseid} = $details{$num}{'class'};
                    228:                     }
1.1       raeburn   229:                 }
                    230:             }
                    231:         }
                    232:     }
                    233:     return ($output,$logmsg,$keysmsg);
                    234: }
                    235: 
                    236: #############################################################
                    237: #
                    238: # parse_coursereqs() 
                    239: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
                    240: #              - details - reference to hash containing extracted information.
                    241: # outputs (none)
                    242: #
                    243: ############################################################
                    244: 
                    245: sub parse_coursereqs {
                    246:     my ($coursefile,$details) = @_;
                    247: #   Note all start and end dates should be in this format:
                    248: #   YYYY:MM:DD:HH:MM:SS (:separators required).
                    249:     my $uname = '';
                    250:     my @state = ();
                    251:     my $num = 0;
                    252:     my $secid = 0;
                    253:     my $xlist = 0;
                    254:     my $userkey = '';
                    255:     my $role = '';
1.27      raeburn   256:     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   257:     my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
                    258:     my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
                    259:     my $p = HTML::Parser->new
                    260:     (
                    261:         xml_mode => 1,
                    262:         start_h =>
                    263:             [sub {
                    264:                  my ($tagname, $attr) = @_;
                    265:                  push(@state, $tagname);
                    266:                  if ("@state" eq "class") {
                    267:                      %{$$details{$num}} = ();
                    268:                      $$details{$num}{'class'} = $attr->{id};
                    269:                      %{$$details{$num}{'users'}} = ();
                    270:                      %{$$details{$num}{'sections'}} = ();
                    271:                      $secid = 0;
                    272:                      $xlist = 0;
                    273:                  }
                    274:                  if ("@state" eq "class users user roles role") {
                    275:                      $role = $attr->{id};
                    276:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    277:                          push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);  
                    278:                          %{$$details{$num}{'users'}{$userkey}{$role}} = ();
                    279:                          @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
                    280:                      }
                    281:                  }
                    282:                  if ("@state" eq "class sections section") {
                    283:                      $secid ++;
                    284:                      %{$$details{$num}{'sections'}{$secid}} = ();
                    285:                  }
                    286:                  if ("@state" eq "class crosslists xlist") {
                    287:                      $xlist ++;
                    288:                      %{$$details{$num}{'crosslists'}{$xlist}} = ();
                    289:                  }
                    290:             }, "tagname, attr"],
                    291:          text_h =>
                    292:              [sub {
                    293:                  my ($text) = @_;
                    294:                  if ("@state" eq "class owner username") {
                    295:                      $$details{$num}{'owner'} = $text;
                    296:                  } elsif ("@state" eq "class owner domain") {
                    297:                     $$details{$num}{'domain'} = $text;
                    298:                  } elsif ("@state" eq "class sections section inst") {
                    299:                     $$details{$num}{'sections'}{$secid}{'inst'} = $text;
                    300:                  } elsif ("@state" eq "class sections section loncapa") {
                    301:                     $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
                    302:                  } elsif ("@state" eq "class crosslists xlist inst") {
                    303:                     $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
                    304:                  } elsif ("@state" eq "class crosslists xlist loncapa") {
                    305:                     $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
                    306:                  } elsif ("@state" eq "class owner authtype") {
                    307:                     $$details{$num}{'ownerauthtype'} = $text;
                    308:                  } elsif ("@state" eq "class owner autharg") {
1.5       raeburn   309:                     $$details{$num}{'ownerauthparam'} = $text;
1.1       raeburn   310:                  } elsif ("@state" eq "class authentication method") {
                    311:                     $$details{$num}{'authtype'} = $text;
                    312:                  } elsif ("@state" eq "class authentication param") {
                    313:                     $$details{$num}{'authparam'} = $text;
                    314:                  } elsif ("@state" eq "class users user username") {
                    315:                     $userkey = $text;
                    316:                  } elsif ("@state" eq "class users user domain") {
                    317:                     $userkey .= ':'.$text;
                    318:                     %{$$details{$num}{'users'}{$userkey}} = ();
                    319:                     @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
                    320:                  } elsif ("@state" eq "class users user email") {
                    321:                     $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
                    322:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &Apache::lonnet::escape($text); 
                    323:                  } elsif ("@state" eq "class users user roles role start") {
                    324:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    325:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
                    326:                      }
                    327:                  } elsif ("@state" eq "class users user roles role end") {
                    328:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    329:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
                    330:                      }
                    331:                  } elsif ("@state" eq "class users user roles role usec") {
                    332:                      if ($role =~ /^(st|ad|ep|ta|in|cc)$/) {
                    333:                          unless ($text eq '') {
                    334:                              push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
                    335:                          }
                    336:                      }
                    337:                  } else {
                    338:                      foreach my $item (@items) {
                    339:                          if ("@state" eq "class $item") {
                    340:                              $$details{$num}{$item} = $text;
                    341:                          }
                    342:                      }
                    343:                      foreach my $item (@dateitems) {
                    344:                          if ("@state" eq "class $item") {
                    345:                              $$details{$num}{$item} = &process_date($text);
                    346:                          }
                    347:                      }
                    348:                      foreach my $item (@useritems) {
                    349:                          if ("@state" eq "class users user $item") {
                    350:                              $$details{$num}{'users'}{$userkey}{$item} = $text;
                    351:                          }
                    352:                      }
                    353:                  }
                    354:                }, "dtext"],
                    355:          end_h =>
                    356:                [sub {
                    357:                    my ($tagname) = @_;
                    358:                    if ("@state" eq "class") {
                    359:                        $num ++;
                    360:                    }
                    361:                    pop @state;
                    362:                 }, "tagname"],
                    363:     );
                    364: 
                    365:     $p->parse_file($coursefile);
                    366:     $p->eof;
                    367:     return;
                    368: }
                    369: 
                    370: #########################################################
                    371: #
                    372: # build_course() 
                    373: #
                    374: # inputs
1.28      raeburn   375: #   course domain
1.1       raeburn   376: #   course request number
                    377: #   context - auto if called from command line, web if called from DC web interface
                    378: #   ref to hash of course creation information
                    379: #   ref to hash of role descriptions
                    380: #   ref to scalar used to accumulate log messages
                    381: #   ref to scalar used to accumulate messages sent to new users
                    382: #   ref to scalar used to accumulate results of new user additions
                    383: #   ref to hash of enrollment counts for different roles
1.28      raeburn   384: #   ref to scalar used to accumulate information about added roles
                    385: #   ref to scalar used to accumulate
                    386: #   ref to scalar used to accumulate information about access keys
                    387: #   domain of DC creating course
                    388: #   username of DC creating course   
                    389: #   optional course number, if unique course number already obtained (e.g., for
                    390: #       course requests submitted via course request form. 
1.1       raeburn   391: #
                    392: # outputs
                    393: #   LON-CAPA courseID for new (created) course
                    394: #
                    395: #########################################################
                    396: 
                    397: sub build_course {
1.29      raeburn   398:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category) = @_;
                    399:     return unless (ref($details) eq 'HASH');
                    400:     my $owner_uname = $details->{'owner'};
                    401:     my $owner_domain = $details->{'domain'};
1.1       raeburn   402:     my $owner = $owner_uname.':'.$owner_domain;
                    403:     my $sectionstr = '';
                    404:     my $xliststr = '';
                    405:     my $noenddate = '';
                    406:     my $outcome;
1.32    ! raeburn   407:     my ($courseid,$crsudom,$crsunum,$crstype,$ccrole,$rolenames);
        !           408:     if ($details->{'crstype'} eq 'Community') {
        !           409:         $crstype = $details->{'crstype'};
        !           410:         $ccrole ='co';
        !           411:         if (ref($longroles) eq 'HASH') {
        !           412:             $rolenames = $longroles->{'Community'};
        !           413:         }
        !           414:     } else {
        !           415:         $crstype = 'Course';
        !           416:         $ccrole = 'cc';
        !           417:         if (ref($longroles) eq 'HASH') {
        !           418:             $rolenames = $longroles->{'Course'};
        !           419:         }
        !           420:     }
1.1       raeburn   421:     my $linefeed;
                    422:     if ($context eq 'auto') {
                    423:         $linefeed = "\n";
                    424:     } else {
                    425:         $linefeed = "<br />\n";
                    426:     }
1.29      raeburn   427:     if ($details->{'accessend'} eq '') {
1.1       raeburn   428:         $noenddate = 1;
                    429:     }
1.29      raeburn   430:     my $reshome = $details->{'reshome'};
1.1       raeburn   431:     if ($reshome eq '') {
                    432:         $reshome = '/res/'.$cdom;
                    433:     }
1.29      raeburn   434:     my $firstres =  $details->{'firstres'};
1.1       raeburn   435:     if ($firstres eq '') {
                    436:         $firstres = 'syl';
                    437:     }
1.29      raeburn   438:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
                    439:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   440:     }
1.5       raeburn   441:     $sectionstr =~ s/,$//;
1.1       raeburn   442: 
1.29      raeburn   443:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
                    444:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   445:     }
1.5       raeburn   446:     $xliststr =~ s/,$//;
1.1       raeburn   447: 
                    448:     my %courseinfo = (
1.29      raeburn   449:                       inst_code => $details->{'coursecode'},
                    450:                       description => $details->{'title'}
1.1       raeburn   451:                      ); 
1.29      raeburn   452:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
                    453:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
                    454:                     'authparam' => $details->{'ownerauthparam'},
                    455:                     'emailenc' => $details->{'emailenc'},
                    456:                     'udom' => $details->{'domain'},
                    457:                     'uname' => $details->{'owner'},
                    458:                     'pid' => $details->{'users'}{$owner}{'studentID'},
                    459:                     'first' => $details->{'users'}{$owner}{'firstname'},
                    460:                     'middle' => $details->{'users'}{$owner}{'middlename'},
                    461:                     'last' => $details->{'users'}{$owner}{'lastname'},
                    462:                     'gene' => $details->{'users'}{$owner}{'generation'},
1.1       raeburn   463:                     'usec' => '',
                    464:                     'end' => '',
                    465:                     'start' => '',
1.29      raeburn   466:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1       raeburn   467:                     'cid' => '',
                    468:                     'context' => 'createowner',
                    469:                     'linefeed' => $linefeed,
1.32    ! raeburn   470:                     'role' => $ccrole,
1.5       raeburn   471:                    };
1.32    ! raeburn   472:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1       raeburn   473:     } else {
                    474:         $outcome = 'ok';
                    475:     }
                    476: 
1.6       raeburn   477:     if ($outcome eq 'ok') {
1.29      raeburn   478:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
                    479:             $details->{'datemode'} = 'shift';
                    480:             $details->{'dateshift'} = 365;
1.27      raeburn   481:         }
1.6       raeburn   482:         my $courseargs = {
1.29      raeburn   483:                ccuname => $details->{'owner'},
                    484:                ccdomain => $details->{'domain'},
                    485:                cdescr => $details->{'title'},
                    486:                crstype => $details->{'crstype'},
                    487:                curl => $details->{'topmap'},
1.1       raeburn   488:                course_domain => $cdom,
1.29      raeburn   489:                course_home =>  $details->{'coursehome'},
                    490:                nonstandard => $details->{'nonstandard'},
                    491:                crscode => $details->{'coursecode'},
                    492:                crsquota => $details->{'crsquota'},
                    493:                clonecourse => $details->{'clonecrs'},
                    494:                clonedomain => $details->{'clonedom'},
                    495:                datemode => $details->{'datemode'},
                    496:                dateshift => $details->{'dateshift'},
                    497:                crsid => $details->{'optional_id'},
                    498:                curruser => $details->{'owner'},
1.1       raeburn   499:                crssections => $sectionstr,
                    500:                crsxlist => $xliststr,
1.29      raeburn   501:                autoadds => $details->{'adds'},
                    502:                autodrops => $details->{'drops'},
                    503:                notify => $details->{'notify_owner'},
                    504:                notify_dc => $details->{'notify_dc'},
1.1       raeburn   505:                no_end_date => $noenddate,
1.29      raeburn   506:                showphotos => $details->{'showphotos'},
                    507:                authtype => $details->{'authtype'},
                    508:                autharg => $details->{'authparam'},
                    509:                enrollstart => $details->{'enrollstart'},
                    510:                enrollend => $details->{'enrollend'},
                    511:                startaccess => $details->{'accessstart'},
                    512:                endaccess => $details->{'accessend'},
                    513:                setpolicy => $details->{'setpolicy'},
                    514:                setcontent => $details->{'setcontent'},
1.1       raeburn   515:                reshome => $reshome,
1.29      raeburn   516:                setkeys => $details->{'setkeys'},
                    517:                keyauth => $details->{'keyauth'},
                    518:                disresdis => $details->{'disresdis'},
                    519:                disablechat => $details->{'disablechat'},
                    520:                openall => $details->{'openall'},
1.1       raeburn   521:                firstres => $firstres
                    522:                };
1.20      albertel  523:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29      raeburn   524:         if (! exists($host_servers{$details->{'coursehome'}})) {
                    525:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1       raeburn   526:             return;
                    527:         }
1.30      raeburn   528:         my ($success, $msg) = &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname,$context,$cnum,$category);
1.23      albertel  529: 	$$logmsg .= $msg;
1.24      raeburn   530:         if (!$success) {
                    531:             return;
                    532:         }
1.1       raeburn   533:     } else {
                    534:         return;
                    535:     }
                    536:     
                    537: #
1.32    ! raeburn   538: # Make owner a coordinator
1.1       raeburn   539: #
                    540:     if (($owner_domain) && ($owner_uname)) {
1.32    ! raeburn   541:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1       raeburn   542:     }
                    543: 
                    544: #
                    545: # Process other reqested users
                    546: #
1.32    ! raeburn   547: 
        !           548:     my @courseroles = qw(st ep ta in);
        !           549:     push(@courseroles,$ccrole);
1.31      raeburn   550:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
                    551:         push(@courseroles,'ad');
                    552:     }
1.1       raeburn   553:     my $stulogmsg = '';
1.29      raeburn   554:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1       raeburn   555:         my $url = '/'.$crsudom.'/'.$crsunum;
1.29      raeburn   556:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
                    557:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1       raeburn   558:             my ($username,$userdom) = split/:/,$userkey;
                    559:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31      raeburn   560:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
                    561:                 my @badroles;
                    562:                 my $firstrole = shift(@reqroles);
                    563:                 while (@reqroles > 0) { 
                    564:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    565:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    566:                             last;
                    567:                         } else {
                    568:                             push(@badroles,$firstrole);
                    569:                             $firstrole = shift(@reqroles);
                    570:                         }
                    571:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
                    572:                         last;
                    573:                     } else {
                    574:                         push(@badroles,$firstrole);
                    575:                         $firstrole = shift(@reqroles);
                    576:                     }
                    577:                 }
                    578:                 if (@badroles > 0) {
                    579:                     if (@badroles > 1) {
                    580:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
                    581:                     } else {
                    582:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
                    583:                     }
                    584:                 }
                    585:                 my $firstsec;
1.32    ! raeburn   586:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
1.31      raeburn   587:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    588:                 }
1.5       raeburn   589:                 my $userargs = {
1.29      raeburn   590:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                    591:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
                    592:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7       raeburn   593:                     'udom' => $userdom,
1.1       raeburn   594:                     'uname' => $username,
1.29      raeburn   595:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
                    596:                     'first' => $details->{'users'}{$userkey}{'firstname'},
                    597:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
                    598:                     'last' => $details->{'users'}{$userkey}{'lastname'},
                    599:                     'gene' => $details->{'users'}{$userkey}{'generation'},
1.1       raeburn   600:                     'usec' => $firstsec,
1.29      raeburn   601:                     'end' => $details->{'users'}{$userkey}{'end'},
                    602:                     'start' => $details->{'users'}{$userkey}{'start'},
                    603:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   604:                     'cid' => $courseid,
1.8       raeburn   605:                     'crs' => $crsunum,
                    606:                     'cdom' => $crsudom,
1.1       raeburn   607:                     'context' => 'createcourse',
                    608:                     'linefeed' => $linefeed,
1.29      raeburn   609:                     'role' => $details->{'users'}{$userkey}{'roles'}[0], 
1.5       raeburn   610:                    };
1.22      raeburn   611:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1       raeburn   612: # now add other roles and other sections.
                    613:                 if ($outcome eq 'ok') {
1.32    ! raeburn   614:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29      raeburn   615:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1       raeburn   616:                             my $curr_role = $firstrole;
1.29      raeburn   617:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    618:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                    619:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1       raeburn   620:                             $url = '/'.$crsudom.'/'.$crsunum;
                    621:                             if ($usec ne '') {
                    622:                                 $url .= '/'.$usec;
                    623:                             }
1.31      raeburn   624:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    625:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    626:                             } else {
                    627:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    628:                             }
1.1       raeburn   629:                         }
                    630:                     }
1.31      raeburn   631:                     if (@reqroles > 0) {
                    632:                         foreach my $curr_role (@reqroles) {
1.29      raeburn   633:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    634:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   635:                             if ($curr_role eq 'st') {
1.29      raeburn   636:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   637:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    638:                                 if ($usec ne '') {
                    639:                                     $url .= '/'.$usec;
                    640:                                 }
1.22      raeburn   641:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
1.32    ! raeburn   642:                             } elsif ($curr_role eq $ccrole) {
1.31      raeburn   643:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    644:                                 my $usec = '';
                    645:                                 $$output .=
                    646:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    647:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
                    648:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   649:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   650:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    651:                                     if ($usec ne '') {
                    652:                                         $url .= '/'.$usec;
                    653:                                     }
1.31      raeburn   654:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    655:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    656:                                             $$output .= 
                    657:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    658:                                         } else {
                    659:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    660:                                         }
                    661:                                     } else {
                    662:                                         $$output .= 
                    663:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    664:                                     }
1.1       raeburn   665:                                 }
1.31      raeburn   666:                             } else {
                    667:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   668:                             }
                    669:                         }
                    670:                     }
                    671:                 }
                    672:             } else {
1.29      raeburn   673:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
                    674:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    675:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   676:                     if ($curr_role eq 'st') {
1.29      raeburn   677:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   678:                         $url = '/'.$crsudom.'/'.$crsunum;
                    679:                         if ($usec ne '') {
                    680:                             $url .= '/'.$usec;
                    681:                         }
1.22      raeburn   682:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
1.31      raeburn   683:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
                    684:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   685:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    686:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   687:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    688:                                 if ($usec ne '') {
                    689:                                     $url .= '/'.$usec;
                    690:                                 }
1.31      raeburn   691:                                 my $stdresult;
                    692:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    693:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    694:                                         $stdresult = 
                    695:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    696:                                     } else {
                    697:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    698:                                     }
                    699:                                 } else {
                    700:                                     $stdresult = 
                    701:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    702:                                 }
1.5       raeburn   703:                                 $$output .= $stdresult;
1.1       raeburn   704:                             }
                    705:                         } else {
1.5       raeburn   706:                             $url = '/'.$crsudom.'/'.$crsunum;
1.31      raeburn   707:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    708:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    709:                                     $$output .= 
                    710:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    711:                                 } else {
                    712:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    713:                                 }
                    714:                             } else {
                    715:                                 $$output .= 
                    716:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
                    717:                             }
1.1       raeburn   718:                         }
1.31      raeburn   719:                     } else {
                    720:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   721:                     }
                    722:                 }
                    723:             }
                    724:         }
                    725:     }
                    726: 
                    727: # Information about keys.
1.29      raeburn   728:     if ($details->{'setkeys'}) {
1.1       raeburn   729:         $$keysmsg .=
1.29      raeburn   730:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1       raeburn   731:     }
                    732: # Flush the course logs so reverse user roles immediately updated
                    733:     &Apache::lonnet::flushcourselogs();
                    734:     return $courseid;
                    735: }
                    736: 
1.31      raeburn   737: sub owner_is_dc {
                    738:     my ($owner_uname,$owner_dom,$cdom) = @_;
                    739:     my $is_dc = 0;
                    740:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
                    741:                     ['active'],['dc'],[$cdom]);
                    742:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
                    743:         $is_dc = 1;
                    744:     }
                    745:     return $is_dc;
                    746: }
                    747: 
1.1       raeburn   748: #########################################################
                    749: #
                    750: # process_date()
                    751: # 
                    752: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    753: # output - corresponding UNIX time (seconds since epoch). 
                    754: #
                    755: #########################################################
                    756: 
                    757: sub process_date {
                    758:     my $timestr = shift;
                    759:     my $timestamp = '';
1.5       raeburn   760:     if ($timestr !~ /:/) {
1.1       raeburn   761:         $timestamp = '';
                    762:     } else {
1.12      albertel  763:         my @entries = split(/:/,$timestr);
1.1       raeburn   764:         for (my $j=0; $j<@entries; $j++) {
                    765:             if ( length($entries[$j]) > 1 ) {
                    766:                 $entries[$j] =~ s/^0//;
                    767:             }
1.10      raeburn   768:             $entries[$j] =~ s/\D//g;
                    769:             if ($entries[$j] < 0) {
                    770:                 $entries[$j] = 0;
                    771:             }
1.1       raeburn   772:         }
1.5       raeburn   773:         if ($entries[1] > 0) {  
                    774:             $entries[1] = $entries[1] - 1;
                    775:         }
1.10      raeburn   776:         if ($entries[5] > 60) {
                    777:             $entries[5] = 60;
                    778:         }
                    779:         if ($entries[4] > 59) {
                    780:             $entries[4] = 59;
                    781:         }
                    782:         if ($entries[3] > 23) {
                    783:             $entries[3] = 23;
                    784:         }
                    785:         if ($entries[2] > 31) {
                    786:             $entries[2] = 31;
                    787:         }
                    788:         if ($entries[1] > 11) {
                    789:             $entries[1] = 11;
                    790:         }
                    791:         if ($entries[2] == 31) {
                    792:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    793:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    794:                 $entries[2] = 30;
                    795:             }
                    796:         }
                    797:         if ($entries[1] == 1) {
                    798:             if ($entries[2] > 29) {
                    799:                 $entries[2] = 29;
                    800:             }
                    801:             if ($entries[2] == 29) {
1.12      albertel  802:                 if ($entries[0]%4 != 0) {
1.14      raeburn   803:                     $entries[2] = 28;
1.12      albertel  804:                 } elsif ( $entries[0]%100 == 0
                    805: 			  && $entries[0]%400 != 0) {
1.14      raeburn   806: 		    $entries[2] = 28;
1.12      albertel  807: 		}
1.10      raeburn   808:             }
                    809:         }     
1.1       raeburn   810:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    811:     }
                    812:     return $timestamp;
                    813: }
                    814: 
                    815: 1;

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