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

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

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