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

1.4       albertel    1: #
1.39    ! raeburn     2: # $Id: batchcreatecourse.pm,v 1.38 2013/03/01 04:49:15 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>
1.38      raeburn    59: # <defaultcredits>3</defaultcredits>
1.1       raeburn    60: # <coursehome>msul1</coursehome>
                     61: # <coursedomain>msu</coursedomain>
                     62: # <reshome>/res/msu/</reshome>
                     63: # <optional_id></optional_id>
                     64: # <adds>1</adds>
                     65: # <drops>1</drops>
                     66: # <enrollstart>2005:01:04:10:30</enrollstart>
                     67: # <enrollend>2005:07:04:20:30</enrollend>
                     68: # <accessstart>2005:01:10:10:30</accessstart>
                     69: # <accessend>2005:05:31:10:30</accessend>
                     70: # <authentication>
                     71: #  <method>krb4</method>
                     72: #  <param>MSU.EDU</param>
                     73: # </authentication>
                     74: # <nonstandard></nonstandard>
                     75: # <topmap></topmap>
                     76: # <firstres>nav</firstres>
1.15      raeburn    77: # <crsquota>20</crsquota>
1.39    ! raeburn    78: # <uniquecode>1</uniquecode>
1.1       raeburn    79: # <clonecrs>466011437c34194msul1</clonecrs>
                     80: # <clonedom>msu</clonedom>
1.27      raeburn    81: # <datemode>shift</datemode>
                     82: # <dateshift>365</dateshift>
1.1       raeburn    83: # <showphotos></showphotos>
                     84: # <setpolicy>1</setpolicy>
                     85: # <setcontent>1</setcontent>
                     86: # <setkeys>0</setkeys>
1.19      albertel   87: # <keyauth>keyadmin:msu</keyauth>
1.1       raeburn    88: # <disresdis>1</disresdis>
                     89: # <disablechat>1</disablechat>
                     90: # <openall></openall>
                     91: # <notify_dc>1</notify_dc>
                     92: # <notify_owner>1</notify_owner>
                     93: # <owner>
                     94: #  <username>sparty</username>
                     95: #  <domain>msu</domain>
                     96: #  <authtype>krb4</authtype>
                     97: #  <autharg>MSU.EDU</autharg>
                     98: # </owner>
                     99: # <sections>
                    100: #  <section>
                    101: #   <inst>001</inst>
                    102: #   <loncapa>1</loncapa>
                    103: #  </section>
                    104: #  <section>
                    105: #   <inst>002</inst>
                    106: #   <loncapa>2</loncapa>
                    107: #  </section>
                    108: # </sections>
                    109: # <crosslists>
                    110: #  <xlist>
                    111: #   <inst>ss05zzz101001</inst>
                    112: #   <loncapa>1</loncapa>
                    113: #  </xlist>
                    114: # </crosslists>
                    115: # <users>
                    116: #  <user>
                    117: #   <username>sparty</username>
                    118: #   <domain>msu</domain>
                    119: #   <email>sparty@msu.edu</email>
                    120: #   <authtype>krb4</authtype>
                    121: #   <autharg></autharg>
                    122: #   <firstname>MSU</firstname>
                    123: #   <generation></generation>
                    124: #   <lastname>Spartan</lastname>x
                    125: #   <middlename></middlename>
                    126: #   <studentID></studentID>
1.38      raeburn   127: #   <credits></credits>
1.1       raeburn   128: #   <roles></roles>
                    129: #  </user>
                    130: #  <user>
                    131: #   <username>itds0001</username>
                    132: #   <domain>northwood5</domain>
                    133: #   <email>itds0001@msu.edu</email>
                    134: #   <authtype>int</authtype>
                    135: #   <autharg></autharg>
                    136: #   <firstname>Info</firstname>
                    137: #   <generation></generation>
                    138: #   <lastname>Techc</lastname>x
                    139: #   <middlename></middlename>
                    140: #   <studentID></studentID>
                    141: #   <roles>
                    142: #    <role id='in'>
                    143: #     <start>2005:01:01:12:10</start>
                    144: #     <end>2005:12:01:12:10</end>
                    145: #     <usec>1</usec>
                    146: #     <usec>2</usec>
                    147: #    </role>
                    148: #   </roles>
                    149: #  </user>
                    150: # </users>
                    151: #</class>
                    152: #
                    153: # Many of these are binary options (corresponding to either checkboxes or
                    154: # radio buttons in the interactive CCRS page).  Examples include:
1.39    ! raeburn   155: # setpolicy, setcontent, setkeys, disableresdis, disablechat, openall,
        !           156: # uniquecode
1.1       raeburn   157: #
                    158: # A value of 1 between opening and closing tags is equivalent to a 
                    159: # checked checkbox or 'Yes' response in the original CCRS web page.
                    160: # A value of 0 or blank is equivalent to an unchecked box or 'No'
                    161: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
                    162: #
1.15      raeburn   163: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
1.1       raeburn   164: # no entry respectively.
1.15      raeburn   165: # 
1.33      raeburn   166: # crstype can be Course or Community
1.15      raeburn   167: #
                    168: # crsquota is the total disk space permitted for course group portfolio files
                    169: # in all course groups.
                    170: # 
1.1       raeburn   171: # For format of other parameters, refer to the interactive CCRS page
                    172: # and view how the equivalent parameter is displayed in the web form.  
                    173: #  
                    174: ##############################################################
                    175: # create_courses() - creates courses described in @$requests,
                    176: #                    files listed in @$requests are deleted
                    177: #                    after the files have been parsed.
                    178: #
1.3       raeburn   179: #                    Directory for retrieval of files listed in @$requests is: 
                    180: #                    /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
1.13      raeburn   181: #                    /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
1.1       raeburn   182: #                    
1.39    ! raeburn   183: # inputs (six)   -  requests - ref to array of filename(s) containing course requests 
1.1       raeburn   184: #                   courseids - ref to hash to store LON-CAPA course ids of new courses 
                    185: #                   context - auto if called from command line, web if called from browser
                    186: #                   dom - domain for which the course is being created
1.3       raeburn   187: #                   uname - username of DC who is requesting course creation
                    188: #                   udom - domain of DC who is requesting course creation
1.1       raeburn   189: #  
1.39    ! raeburn   190: # outputs (four)  -  output - text recording user roles added etc.
        !           191: #                    logmsg - text to be logged
        !           192: #                    keysmsg - text containing link(s) to manage keys page(s) 
        !           193: #                    codehash - reference to hash containing courseID => unique code
        !           194: #                               where unique code is a 6 character code, to distribute
        !           195: #                               to students as a shortcut to the course.
1.1       raeburn   196: #############################################################
                    197: 
                    198: sub create_courses {
1.3       raeburn   199:     my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
1.1       raeburn   200:     my $output;
                    201:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
                    202: # Get role names
                    203:     my %longroles = ();
1.9       raeburn   204:     open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
1.1       raeburn   205:     my @rolesplain = <FILE>;
                    206:     close(FILE);
1.32      raeburn   207:     foreach my $item (@rolesplain) {
                    208:         if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
                    209:             $longroles{'Course'}{$1} = $2;
                    210:             $longroles{'Community'}{$1} = $3;
1.1       raeburn   211:         }
                    212:     }
1.39    ! raeburn   213:     my ($logmsg,$keysmsg,$newusermsg,$addresult,%codehash);
1.1       raeburn   214:     my %enrollcount = ();
1.37      foxr      215:     my $newcoursedir = LONCAPA::tempdir().'/addcourse/'.$dom.'/'.$context;
1.3       raeburn   216:     if ($context eq 'auto') {
                    217:         $newcoursedir .= '/pending';
                    218:     } else {
                    219:         if ($uname && $udom) {
1.13      raeburn   220:             $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
1.3       raeburn   221:         } else {
                    222:             $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
1.1       raeburn   223:         }
                    224:     }
                    225:     if (@{$requests} > 0) {
                    226:         foreach my $request (@{$requests}) {
                    227:             my %details = ();
                    228:             if (-e $newcoursedir.'/'.$request) {
                    229:                 &parse_coursereqs($newcoursedir.'/'.$request, \%details);
1.29      raeburn   230:                 foreach my $num (sort(keys(%details))) {
                    231:                     my $reqdetails = $details{$num};
1.39    ! raeburn   232:                     my $code;
        !           233:                     my $courseid = 
        !           234:                         &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,\$newusermsg,
        !           235:                                       \$addresult,\%enrollcount,\$output,\$keysmsg,undef,undef,undef,undef,\$code);
1.25      raeburn   236:                     if ($courseid =~m{^/$match_domain/$match_courseid}) {
1.21      raeburn   237:                         $$courseids{$courseid} = $details{$num}{'class'};
1.39    ! raeburn   238:                         if ($code) {
        !           239:                             $codehash{$courseid} = $code;
        !           240:                         }
1.21      raeburn   241:                     }
1.1       raeburn   242:                 }
                    243:             }
                    244:         }
                    245:     }
1.39    ! raeburn   246:     return ($output,$logmsg,$keysmsg,\%codehash);
1.1       raeburn   247: }
                    248: 
                    249: #############################################################
                    250: #
                    251: # parse_coursereqs() 
                    252: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
                    253: #              - details - reference to hash containing extracted information.
                    254: # outputs (none)
                    255: #
                    256: ############################################################
                    257: 
                    258: sub parse_coursereqs {
                    259:     my ($coursefile,$details) = @_;
                    260: #   Note all start and end dates should be in this format:
                    261: #   YYYY:MM:DD:HH:MM:SS (:separators required).
                    262:     my $uname = '';
                    263:     my @state = ();
                    264:     my $num = 0;
                    265:     my $secid = 0;
                    266:     my $xlist = 0;
                    267:     my $userkey = '';
                    268:     my $role = '';
1.39    ! raeburn   269:     my @items = ('title','optional_id','coursecode','defaultcredits','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','uniquecode');
1.33      raeburn   270:     my @possroles = qw(st ad ep ta in cc co);
1.1       raeburn   271:     my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
                    272:     my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
                    273:     my $p = HTML::Parser->new
                    274:     (
                    275:         xml_mode => 1,
                    276:         start_h =>
                    277:             [sub {
                    278:                  my ($tagname, $attr) = @_;
                    279:                  push(@state, $tagname);
                    280:                  if ("@state" eq "class") {
                    281:                      %{$$details{$num}} = ();
                    282:                      $$details{$num}{'class'} = $attr->{id};
                    283:                      %{$$details{$num}{'users'}} = ();
                    284:                      %{$$details{$num}{'sections'}} = ();
                    285:                      $secid = 0;
                    286:                      $xlist = 0;
                    287:                  }
                    288:                  if ("@state" eq "class users user roles role") {
                    289:                      $role = $attr->{id};
1.33      raeburn   290:                      if (grep(/^\Q$role\E$/,@possroles)) {
1.1       raeburn   291:                          push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);  
                    292:                          %{$$details{$num}{'users'}{$userkey}{$role}} = ();
                    293:                          @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
                    294:                      }
                    295:                  }
                    296:                  if ("@state" eq "class sections section") {
                    297:                      $secid ++;
                    298:                      %{$$details{$num}{'sections'}{$secid}} = ();
                    299:                  }
                    300:                  if ("@state" eq "class crosslists xlist") {
                    301:                      $xlist ++;
                    302:                      %{$$details{$num}{'crosslists'}{$xlist}} = ();
                    303:                  }
                    304:             }, "tagname, attr"],
                    305:          text_h =>
                    306:              [sub {
                    307:                  my ($text) = @_;
                    308:                  if ("@state" eq "class owner username") {
                    309:                      $$details{$num}{'owner'} = $text;
                    310:                  } elsif ("@state" eq "class owner domain") {
                    311:                     $$details{$num}{'domain'} = $text;
                    312:                  } elsif ("@state" eq "class sections section inst") {
                    313:                     $$details{$num}{'sections'}{$secid}{'inst'} = $text;
                    314:                  } elsif ("@state" eq "class sections section loncapa") {
                    315:                     $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
                    316:                  } elsif ("@state" eq "class crosslists xlist inst") {
                    317:                     $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
                    318:                  } elsif ("@state" eq "class crosslists xlist loncapa") {
                    319:                     $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
                    320:                  } elsif ("@state" eq "class owner authtype") {
                    321:                     $$details{$num}{'ownerauthtype'} = $text;
                    322:                  } elsif ("@state" eq "class owner autharg") {
1.5       raeburn   323:                     $$details{$num}{'ownerauthparam'} = $text;
1.1       raeburn   324:                  } elsif ("@state" eq "class authentication method") {
                    325:                     $$details{$num}{'authtype'} = $text;
                    326:                  } elsif ("@state" eq "class authentication param") {
                    327:                     $$details{$num}{'authparam'} = $text;
                    328:                  } elsif ("@state" eq "class users user username") {
                    329:                     $userkey = $text;
                    330:                  } elsif ("@state" eq "class users user domain") {
                    331:                     $userkey .= ':'.$text;
                    332:                     %{$$details{$num}{'users'}{$userkey}} = ();
                    333:                     @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
                    334:                  } elsif ("@state" eq "class users user email") {
                    335:                     $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
1.35      bisitz    336:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text); 
1.1       raeburn   337:                  } elsif ("@state" eq "class users user roles role start") {
1.33      raeburn   338:                      if (grep(/^\Q$role\E$/,@possroles)) {
1.1       raeburn   339:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
                    340:                      }
                    341:                  } elsif ("@state" eq "class users user roles role end") {
1.33      raeburn   342:                      if (grep(/^\Q$role\E$/,@possroles)) {
1.1       raeburn   343:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
                    344:                      }
                    345:                  } elsif ("@state" eq "class users user roles role usec") {
1.33      raeburn   346:                      if (grep(/^\Q$role\E$/,@possroles)) {
1.1       raeburn   347:                          unless ($text eq '') {
                    348:                              push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
                    349:                          }
                    350:                      }
                    351:                  } else {
                    352:                      foreach my $item (@items) {
                    353:                          if ("@state" eq "class $item") {
                    354:                              $$details{$num}{$item} = $text;
                    355:                          }
                    356:                      }
                    357:                      foreach my $item (@dateitems) {
                    358:                          if ("@state" eq "class $item") {
                    359:                              $$details{$num}{$item} = &process_date($text);
                    360:                          }
                    361:                      }
                    362:                      foreach my $item (@useritems) {
                    363:                          if ("@state" eq "class users user $item") {
                    364:                              $$details{$num}{'users'}{$userkey}{$item} = $text;
                    365:                          }
                    366:                      }
                    367:                  }
                    368:                }, "dtext"],
                    369:          end_h =>
                    370:                [sub {
                    371:                    my ($tagname) = @_;
                    372:                    if ("@state" eq "class") {
                    373:                        $num ++;
                    374:                    }
                    375:                    pop @state;
                    376:                 }, "tagname"],
                    377:     );
                    378: 
                    379:     $p->parse_file($coursefile);
                    380:     $p->eof;
                    381:     return;
                    382: }
                    383: 
                    384: #########################################################
                    385: #
                    386: # build_course() 
                    387: #
                    388: # inputs
1.28      raeburn   389: #   course domain
1.1       raeburn   390: #   course request number
                    391: #   context - auto if called from command line, web if called from DC web interface
                    392: #   ref to hash of course creation information
                    393: #   ref to hash of role descriptions
                    394: #   ref to scalar used to accumulate log messages
                    395: #   ref to scalar used to accumulate messages sent to new users
                    396: #   ref to scalar used to accumulate results of new user additions
                    397: #   ref to hash of enrollment counts for different roles
1.28      raeburn   398: #   ref to scalar used to accumulate information about added roles
                    399: #   ref to scalar used to accumulate information about access keys
                    400: #   domain of DC creating course
                    401: #   username of DC creating course   
                    402: #   optional course number, if unique course number already obtained (e.g., for
1.39    ! raeburn   403: #       course requests submitted via course request form.
        !           404: #   optional category
        !           405: #   optional ref to scalar for six character unique identifier
1.1       raeburn   406: #
                    407: # outputs
                    408: #   LON-CAPA courseID for new (created) course
                    409: #
                    410: #########################################################
                    411: 
                    412: sub build_course {
1.39    ! raeburn   413:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,
        !           414:         $enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category,$coderef) = @_;
1.29      raeburn   415:     return unless (ref($details) eq 'HASH');
                    416:     my $owner_uname = $details->{'owner'};
                    417:     my $owner_domain = $details->{'domain'};
1.1       raeburn   418:     my $owner = $owner_uname.':'.$owner_domain;
                    419:     my $sectionstr = '';
                    420:     my $xliststr = '';
                    421:     my $noenddate = '';
                    422:     my $outcome;
1.32      raeburn   423:     my ($courseid,$crsudom,$crsunum,$crstype,$ccrole,$rolenames);
                    424:     if ($details->{'crstype'} eq 'Community') {
                    425:         $crstype = $details->{'crstype'};
                    426:         $ccrole ='co';
                    427:         if (ref($longroles) eq 'HASH') {
                    428:             $rolenames = $longroles->{'Community'};
                    429:         }
                    430:     } else {
                    431:         $crstype = 'Course';
                    432:         $ccrole = 'cc';
                    433:         if (ref($longroles) eq 'HASH') {
                    434:             $rolenames = $longroles->{'Course'};
                    435:         }
                    436:     }
1.1       raeburn   437:     my $linefeed;
                    438:     if ($context eq 'auto') {
                    439:         $linefeed = "\n";
                    440:     } else {
                    441:         $linefeed = "<br />\n";
                    442:     }
1.29      raeburn   443:     if ($details->{'accessend'} eq '') {
1.1       raeburn   444:         $noenddate = 1;
                    445:     }
1.29      raeburn   446:     my $reshome = $details->{'reshome'};
1.1       raeburn   447:     if ($reshome eq '') {
                    448:         $reshome = '/res/'.$cdom;
                    449:     }
1.29      raeburn   450:     my $firstres =  $details->{'firstres'};
1.1       raeburn   451:     if ($firstres eq '') {
1.36      raeburn   452:         if ($crstype eq 'Community') {
                    453:             $firstres = 'nav';
                    454:         } else {
                    455:             $firstres = 'syl';
                    456:         }
1.1       raeburn   457:     }
1.29      raeburn   458:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
                    459:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   460:     }
1.5       raeburn   461:     $sectionstr =~ s/,$//;
1.1       raeburn   462: 
1.29      raeburn   463:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
                    464:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   465:     }
1.5       raeburn   466:     $xliststr =~ s/,$//;
1.1       raeburn   467: 
                    468:     my %courseinfo = (
1.29      raeburn   469:                       inst_code => $details->{'coursecode'},
                    470:                       description => $details->{'title'}
1.1       raeburn   471:                      ); 
1.29      raeburn   472:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
                    473:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
                    474:                     'authparam' => $details->{'ownerauthparam'},
                    475:                     'emailenc' => $details->{'emailenc'},
                    476:                     'udom' => $details->{'domain'},
                    477:                     'uname' => $details->{'owner'},
                    478:                     'pid' => $details->{'users'}{$owner}{'studentID'},
                    479:                     'first' => $details->{'users'}{$owner}{'firstname'},
                    480:                     'middle' => $details->{'users'}{$owner}{'middlename'},
                    481:                     'last' => $details->{'users'}{$owner}{'lastname'},
                    482:                     'gene' => $details->{'users'}{$owner}{'generation'},
1.1       raeburn   483:                     'usec' => '',
                    484:                     'end' => '',
                    485:                     'start' => '',
1.29      raeburn   486:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1       raeburn   487:                     'cid' => '',
                    488:                     'context' => 'createowner',
                    489:                     'linefeed' => $linefeed,
1.32      raeburn   490:                     'role' => $ccrole,
1.5       raeburn   491:                    };
1.32      raeburn   492:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1       raeburn   493:     } else {
                    494:         $outcome = 'ok';
                    495:     }
                    496: 
1.6       raeburn   497:     if ($outcome eq 'ok') {
1.29      raeburn   498:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
                    499:             $details->{'datemode'} = 'shift';
                    500:             $details->{'dateshift'} = 365;
1.27      raeburn   501:         }
1.6       raeburn   502:         my $courseargs = {
1.29      raeburn   503:                ccuname => $details->{'owner'},
                    504:                ccdomain => $details->{'domain'},
                    505:                cdescr => $details->{'title'},
                    506:                crstype => $details->{'crstype'},
                    507:                curl => $details->{'topmap'},
1.1       raeburn   508:                course_domain => $cdom,
1.29      raeburn   509:                course_home =>  $details->{'coursehome'},
                    510:                nonstandard => $details->{'nonstandard'},
                    511:                crscode => $details->{'coursecode'},
1.38      raeburn   512:                defaultcredits => $details->{'defaultcredits'},
1.29      raeburn   513:                crsquota => $details->{'crsquota'},
1.39    ! raeburn   514:                uniquecode => $details->{'uniquecode'},
1.29      raeburn   515:                clonecourse => $details->{'clonecrs'},
                    516:                clonedomain => $details->{'clonedom'},
                    517:                datemode => $details->{'datemode'},
                    518:                dateshift => $details->{'dateshift'},
                    519:                crsid => $details->{'optional_id'},
                    520:                curruser => $details->{'owner'},
1.1       raeburn   521:                crssections => $sectionstr,
                    522:                crsxlist => $xliststr,
1.29      raeburn   523:                autoadds => $details->{'adds'},
                    524:                autodrops => $details->{'drops'},
                    525:                notify => $details->{'notify_owner'},
                    526:                notify_dc => $details->{'notify_dc'},
1.1       raeburn   527:                no_end_date => $noenddate,
1.29      raeburn   528:                showphotos => $details->{'showphotos'},
                    529:                authtype => $details->{'authtype'},
                    530:                autharg => $details->{'authparam'},
                    531:                enrollstart => $details->{'enrollstart'},
                    532:                enrollend => $details->{'enrollend'},
                    533:                startaccess => $details->{'accessstart'},
                    534:                endaccess => $details->{'accessend'},
                    535:                setpolicy => $details->{'setpolicy'},
                    536:                setcontent => $details->{'setcontent'},
1.1       raeburn   537:                reshome => $reshome,
1.29      raeburn   538:                setkeys => $details->{'setkeys'},
                    539:                keyauth => $details->{'keyauth'},
                    540:                disresdis => $details->{'disresdis'},
                    541:                disablechat => $details->{'disablechat'},
                    542:                openall => $details->{'openall'},
1.1       raeburn   543:                firstres => $firstres
                    544:                };
1.20      albertel  545:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29      raeburn   546:         if (! exists($host_servers{$details->{'coursehome'}})) {
                    547:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1       raeburn   548:             return;
                    549:         }
1.39    ! raeburn   550:         my ($success, $msg) = 
        !           551:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
        !           552:                                                  $udom,$uname,$context,$cnum,$category,$coderef);
1.23      albertel  553: 	$$logmsg .= $msg;
1.24      raeburn   554:         if (!$success) {
                    555:             return;
                    556:         }
1.1       raeburn   557:     } else {
                    558:         return;
                    559:     }
                    560:     
                    561: #
1.32      raeburn   562: # Make owner a coordinator
1.1       raeburn   563: #
                    564:     if (($owner_domain) && ($owner_uname)) {
1.32      raeburn   565:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1       raeburn   566:     }
                    567: 
                    568: #
                    569: # Process other reqested users
                    570: #
1.32      raeburn   571: 
                    572:     my @courseroles = qw(st ep ta in);
                    573:     push(@courseroles,$ccrole);
1.31      raeburn   574:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
                    575:         push(@courseroles,'ad');
                    576:     }
1.1       raeburn   577:     my $stulogmsg = '';
1.29      raeburn   578:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1       raeburn   579:         my $url = '/'.$crsudom.'/'.$crsunum;
1.29      raeburn   580:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
                    581:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1       raeburn   582:             my ($username,$userdom) = split/:/,$userkey;
                    583:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31      raeburn   584:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
                    585:                 my @badroles;
                    586:                 my $firstrole = shift(@reqroles);
                    587:                 while (@reqroles > 0) { 
                    588:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    589:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    590:                             last;
                    591:                         } else {
                    592:                             push(@badroles,$firstrole);
                    593:                             $firstrole = shift(@reqroles);
                    594:                         }
                    595:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
                    596:                         last;
                    597:                     } else {
                    598:                         push(@badroles,$firstrole);
                    599:                         $firstrole = shift(@reqroles);
                    600:                     }
                    601:                 }
                    602:                 if (@badroles > 0) {
                    603:                     if (@badroles > 1) {
                    604:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
                    605:                     } else {
                    606:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
                    607:                     }
                    608:                 }
                    609:                 my $firstsec;
1.32      raeburn   610:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
1.31      raeburn   611:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    612:                 }
1.5       raeburn   613:                 my $userargs = {
1.29      raeburn   614:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                    615:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
                    616:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7       raeburn   617:                     'udom' => $userdom,
1.1       raeburn   618:                     'uname' => $username,
1.29      raeburn   619:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
                    620:                     'first' => $details->{'users'}{$userkey}{'firstname'},
                    621:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
                    622:                     'last' => $details->{'users'}{$userkey}{'lastname'},
                    623:                     'gene' => $details->{'users'}{$userkey}{'generation'},
1.1       raeburn   624:                     'usec' => $firstsec,
1.29      raeburn   625:                     'end' => $details->{'users'}{$userkey}{'end'},
                    626:                     'start' => $details->{'users'}{$userkey}{'start'},
                    627:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   628:                     'cid' => $courseid,
1.8       raeburn   629:                     'crs' => $crsunum,
                    630:                     'cdom' => $crsudom,
1.1       raeburn   631:                     'context' => 'createcourse',
                    632:                     'linefeed' => $linefeed,
1.38      raeburn   633:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
1.5       raeburn   634:                    };
1.38      raeburn   635:                 if ($userargs->{'role'} eq 'st') {
                    636:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
                    637:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
                    638:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
                    639:                     }
                    640:                 }
1.22      raeburn   641:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1       raeburn   642: # now add other roles and other sections.
                    643:                 if ($outcome eq 'ok') {
1.32      raeburn   644:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29      raeburn   645:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1       raeburn   646:                             my $curr_role = $firstrole;
1.29      raeburn   647:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    648:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                    649:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1       raeburn   650:                             $url = '/'.$crsudom.'/'.$crsunum;
                    651:                             if ($usec ne '') {
                    652:                                 $url .= '/'.$usec;
                    653:                             }
1.31      raeburn   654:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    655:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    656:                             } else {
                    657:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    658:                             }
1.1       raeburn   659:                         }
                    660:                     }
1.31      raeburn   661:                     if (@reqroles > 0) {
                    662:                         foreach my $curr_role (@reqroles) {
1.29      raeburn   663:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    664:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   665:                             if ($curr_role eq 'st') {
1.29      raeburn   666:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   667:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    668:                                 if ($usec ne '') {
                    669:                                     $url .= '/'.$usec;
                    670:                                 }
1.38      raeburn   671:                                 my $credits;
                    672:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
                    673:                                     $credits = $details->{'users'}{$userkey}{'credits'};
                    674:                                     $credits =~ s/[^\d\.]//g;
                    675:                                 }
                    676:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.32      raeburn   677:                             } elsif ($curr_role eq $ccrole) {
1.31      raeburn   678:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    679:                                 my $usec = '';
                    680:                                 $$output .=
                    681:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    682:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
                    683:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   684:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   685:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    686:                                     if ($usec ne '') {
                    687:                                         $url .= '/'.$usec;
                    688:                                     }
1.31      raeburn   689:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    690:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    691:                                             $$output .= 
                    692:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    693:                                         } else {
                    694:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    695:                                         }
                    696:                                     } else {
                    697:                                         $$output .= 
                    698:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    699:                                     }
1.1       raeburn   700:                                 }
1.31      raeburn   701:                             } else {
                    702:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   703:                             }
                    704:                         }
                    705:                     }
                    706:                 }
                    707:             } else {
1.29      raeburn   708:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
                    709:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    710:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   711:                     if ($curr_role eq 'st') {
1.29      raeburn   712:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   713:                         $url = '/'.$crsudom.'/'.$crsunum;
                    714:                         if ($usec ne '') {
                    715:                             $url .= '/'.$usec;
                    716:                         }
1.38      raeburn   717:                         my $credits;
                    718:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
                    719:                             $credits = $details->{'users'}{$userkey}{'credits'};
                    720:                             $credits =~ s/[^\d\.]//g;
                    721:                         }
                    722:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.31      raeburn   723:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
                    724:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   725:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    726:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   727:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    728:                                 if ($usec ne '') {
                    729:                                     $url .= '/'.$usec;
                    730:                                 }
1.31      raeburn   731:                                 my $stdresult;
                    732:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    733:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    734:                                         $stdresult = 
                    735:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    736:                                     } else {
                    737:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    738:                                     }
                    739:                                 } else {
                    740:                                     $stdresult = 
                    741:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    742:                                 }
1.5       raeburn   743:                                 $$output .= $stdresult;
1.1       raeburn   744:                             }
                    745:                         } else {
1.5       raeburn   746:                             $url = '/'.$crsudom.'/'.$crsunum;
1.31      raeburn   747:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    748:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    749:                                     $$output .= 
                    750:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    751:                                 } else {
                    752:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    753:                                 }
                    754:                             } else {
                    755:                                 $$output .= 
                    756:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
                    757:                             }
1.1       raeburn   758:                         }
1.31      raeburn   759:                     } else {
                    760:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   761:                     }
                    762:                 }
                    763:             }
                    764:         }
                    765:     }
                    766: 
                    767: # Information about keys.
1.29      raeburn   768:     if ($details->{'setkeys'}) {
1.1       raeburn   769:         $$keysmsg .=
1.29      raeburn   770:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1       raeburn   771:     }
                    772: # Flush the course logs so reverse user roles immediately updated
                    773:     &Apache::lonnet::flushcourselogs();
                    774:     return $courseid;
                    775: }
                    776: 
1.31      raeburn   777: sub owner_is_dc {
                    778:     my ($owner_uname,$owner_dom,$cdom) = @_;
                    779:     my $is_dc = 0;
                    780:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
                    781:                     ['active'],['dc'],[$cdom]);
                    782:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
                    783:         $is_dc = 1;
                    784:     }
                    785:     return $is_dc;
                    786: }
                    787: 
1.1       raeburn   788: #########################################################
                    789: #
                    790: # process_date()
                    791: # 
                    792: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    793: # output - corresponding UNIX time (seconds since epoch). 
                    794: #
                    795: #########################################################
                    796: 
                    797: sub process_date {
                    798:     my $timestr = shift;
                    799:     my $timestamp = '';
1.5       raeburn   800:     if ($timestr !~ /:/) {
1.1       raeburn   801:         $timestamp = '';
                    802:     } else {
1.12      albertel  803:         my @entries = split(/:/,$timestr);
1.1       raeburn   804:         for (my $j=0; $j<@entries; $j++) {
                    805:             if ( length($entries[$j]) > 1 ) {
                    806:                 $entries[$j] =~ s/^0//;
                    807:             }
1.10      raeburn   808:             $entries[$j] =~ s/\D//g;
                    809:             if ($entries[$j] < 0) {
                    810:                 $entries[$j] = 0;
                    811:             }
1.1       raeburn   812:         }
1.5       raeburn   813:         if ($entries[1] > 0) {  
                    814:             $entries[1] = $entries[1] - 1;
                    815:         }
1.10      raeburn   816:         if ($entries[5] > 60) {
                    817:             $entries[5] = 60;
                    818:         }
                    819:         if ($entries[4] > 59) {
                    820:             $entries[4] = 59;
                    821:         }
                    822:         if ($entries[3] > 23) {
                    823:             $entries[3] = 23;
                    824:         }
                    825:         if ($entries[2] > 31) {
                    826:             $entries[2] = 31;
                    827:         }
                    828:         if ($entries[1] > 11) {
                    829:             $entries[1] = 11;
                    830:         }
                    831:         if ($entries[2] == 31) {
                    832:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    833:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    834:                 $entries[2] = 30;
                    835:             }
                    836:         }
                    837:         if ($entries[1] == 1) {
                    838:             if ($entries[2] > 29) {
                    839:                 $entries[2] = 29;
                    840:             }
                    841:             if ($entries[2] == 29) {
1.12      albertel  842:                 if ($entries[0]%4 != 0) {
1.14      raeburn   843:                     $entries[2] = 28;
1.12      albertel  844:                 } elsif ( $entries[0]%100 == 0
                    845: 			  && $entries[0]%400 != 0) {
1.14      raeburn   846: 		    $entries[2] = 28;
1.12      albertel  847: 		}
1.10      raeburn   848:             }
                    849:         }     
1.1       raeburn   850:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    851:     }
                    852:     return $timestamp;
                    853: }
                    854: 
                    855: 1;

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