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

1.4       albertel    1: #
1.41    ! raeburn     2: # $Id: batchcreatecourse.pm,v 1.40 2014/01/03 18:42:16 raeburn Exp $
1.4       albertel    3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: 
1.1       raeburn    27: package LONCAPA::batchcreatecourse;
                     28: use LONCAPA::Configuration;
                     29: use LONCAPA::Enrollment;
                     30: use HTML::Parser;
                     31: use Time::Local;
                     32: use Apache::lonnet;
1.2       raeburn    33: use Apache::loncommon;
1.1       raeburn    34: use Apache::lonlocal;
1.21      raeburn    35: use LONCAPA qw(:match);
1.1       raeburn    36: 
1.9       raeburn    37: use strict;
                     38: 
1.1       raeburn    39: # Collection of routines used for batch creation of courses and users.
                     40: # &create_courses() should be called by an Autocreate.pl
                     41: # script via a cron entry, or alternatively from a web page, after upload 
                     42: # of a file containing an XML description of a course request (lonbatchccrs.pm).
                     43: # 
                     44: # XML file(s) describing courses that are to be created in domain $dom are stored in
1.3       raeburn    45: # /home/httpd/perl/tmp/addcourse/$dom
1.1       raeburn    46: #
                     47: # &create_courses() will create an account for the course owner 
                     48: # (if one does not currently exist), will create the course (cloning if necessary),
                     49: # and will add additional instructional staff (creating accounts if necessary).
                     50: #
                     51: # Example of XML file (which could contain more than one class to be created):
                     52: #
                     53: #<?xml version="1.0" encoding="UTF-8"?>
                     54: #<!DOCTYPE text>
                     55: #<class id="ss05ubw101">
                     56: # <title>Underwater Basket Weaving</title>
1.16      raeburn    57: # <crstype>Course</crstype>
1.1       raeburn    58: # <coursecode>ss05ubw101</coursecode>
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.41    ! raeburn   166: # crstype can be Course, Community or Placement
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 {
1.41    ! raeburn   431:         if ($details->{'crstype'} eq 'Placement') {
        !           432:             $crstype = $details->{'crstype'};
        !           433:         } else {
        !           434:             $crstype = 'Course';
        !           435:         }
1.32      raeburn   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';
1.41    ! raeburn   458:         } elsif ($crstype eq 'Placement') {
        !           459:             $firstres = 'blank'; 
1.36      raeburn   460:         } else {
                    461:             $firstres = 'syl';
                    462:         }
1.1       raeburn   463:     }
1.29      raeburn   464:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
                    465:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   466:     }
1.5       raeburn   467:     $sectionstr =~ s/,$//;
1.1       raeburn   468: 
1.29      raeburn   469:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
                    470:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   471:     }
1.5       raeburn   472:     $xliststr =~ s/,$//;
1.1       raeburn   473: 
                    474:     my %courseinfo = (
1.29      raeburn   475:                       inst_code => $details->{'coursecode'},
                    476:                       description => $details->{'title'}
1.1       raeburn   477:                      ); 
1.29      raeburn   478:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
                    479:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
                    480:                     'authparam' => $details->{'ownerauthparam'},
                    481:                     'emailenc' => $details->{'emailenc'},
                    482:                     'udom' => $details->{'domain'},
                    483:                     'uname' => $details->{'owner'},
                    484:                     'pid' => $details->{'users'}{$owner}{'studentID'},
                    485:                     'first' => $details->{'users'}{$owner}{'firstname'},
                    486:                     'middle' => $details->{'users'}{$owner}{'middlename'},
                    487:                     'last' => $details->{'users'}{$owner}{'lastname'},
                    488:                     'gene' => $details->{'users'}{$owner}{'generation'},
1.1       raeburn   489:                     'usec' => '',
                    490:                     'end' => '',
                    491:                     'start' => '',
1.29      raeburn   492:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1       raeburn   493:                     'cid' => '',
                    494:                     'context' => 'createowner',
                    495:                     'linefeed' => $linefeed,
1.32      raeburn   496:                     'role' => $ccrole,
1.5       raeburn   497:                    };
1.32      raeburn   498:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1       raeburn   499:     } else {
                    500:         $outcome = 'ok';
                    501:     }
                    502: 
1.6       raeburn   503:     if ($outcome eq 'ok') {
1.29      raeburn   504:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
                    505:             $details->{'datemode'} = 'shift';
                    506:             $details->{'dateshift'} = 365;
1.27      raeburn   507:         }
1.6       raeburn   508:         my $courseargs = {
1.29      raeburn   509:                ccuname => $details->{'owner'},
                    510:                ccdomain => $details->{'domain'},
                    511:                cdescr => $details->{'title'},
                    512:                crstype => $details->{'crstype'},
                    513:                curl => $details->{'topmap'},
1.1       raeburn   514:                course_domain => $cdom,
1.29      raeburn   515:                course_home =>  $details->{'coursehome'},
                    516:                nonstandard => $details->{'nonstandard'},
                    517:                crscode => $details->{'coursecode'},
1.38      raeburn   518:                defaultcredits => $details->{'defaultcredits'},
1.29      raeburn   519:                crsquota => $details->{'crsquota'},
1.39      raeburn   520:                uniquecode => $details->{'uniquecode'},
1.29      raeburn   521:                clonecourse => $details->{'clonecrs'},
                    522:                clonedomain => $details->{'clonedom'},
                    523:                datemode => $details->{'datemode'},
                    524:                dateshift => $details->{'dateshift'},
                    525:                crsid => $details->{'optional_id'},
                    526:                curruser => $details->{'owner'},
1.1       raeburn   527:                crssections => $sectionstr,
                    528:                crsxlist => $xliststr,
1.29      raeburn   529:                autoadds => $details->{'adds'},
                    530:                autodrops => $details->{'drops'},
                    531:                notify => $details->{'notify_owner'},
                    532:                notify_dc => $details->{'notify_dc'},
1.1       raeburn   533:                no_end_date => $noenddate,
1.29      raeburn   534:                showphotos => $details->{'showphotos'},
                    535:                authtype => $details->{'authtype'},
                    536:                autharg => $details->{'authparam'},
                    537:                enrollstart => $details->{'enrollstart'},
                    538:                enrollend => $details->{'enrollend'},
                    539:                startaccess => $details->{'accessstart'},
                    540:                endaccess => $details->{'accessend'},
                    541:                setpolicy => $details->{'setpolicy'},
                    542:                setcontent => $details->{'setcontent'},
1.1       raeburn   543:                reshome => $reshome,
1.29      raeburn   544:                setkeys => $details->{'setkeys'},
                    545:                keyauth => $details->{'keyauth'},
                    546:                disresdis => $details->{'disresdis'},
                    547:                disablechat => $details->{'disablechat'},
                    548:                openall => $details->{'openall'},
1.1       raeburn   549:                firstres => $firstres
                    550:                };
1.40      raeburn   551:         if ($details->{'textbook'}) {
                    552:             $courseargs->{'textbook'} = $details->{'textbook'};
                    553:         }
1.20      albertel  554:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29      raeburn   555:         if (! exists($host_servers{$details->{'coursehome'}})) {
                    556:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1       raeburn   557:             return;
                    558:         }
1.39      raeburn   559:         my ($success, $msg) = 
                    560:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
                    561:                                                  $udom,$uname,$context,$cnum,$category,$coderef);
1.23      albertel  562: 	$$logmsg .= $msg;
1.24      raeburn   563:         if (!$success) {
                    564:             return;
                    565:         }
1.1       raeburn   566:     } else {
                    567:         return;
                    568:     }
                    569:     
                    570: #
1.32      raeburn   571: # Make owner a coordinator
1.1       raeburn   572: #
                    573:     if (($owner_domain) && ($owner_uname)) {
1.32      raeburn   574:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1       raeburn   575:     }
                    576: 
                    577: #
                    578: # Process other reqested users
                    579: #
1.32      raeburn   580: 
                    581:     my @courseroles = qw(st ep ta in);
                    582:     push(@courseroles,$ccrole);
1.31      raeburn   583:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
                    584:         push(@courseroles,'ad');
                    585:     }
1.1       raeburn   586:     my $stulogmsg = '';
1.29      raeburn   587:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1       raeburn   588:         my $url = '/'.$crsudom.'/'.$crsunum;
1.29      raeburn   589:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
                    590:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1       raeburn   591:             my ($username,$userdom) = split/:/,$userkey;
                    592:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31      raeburn   593:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
                    594:                 my @badroles;
                    595:                 my $firstrole = shift(@reqroles);
                    596:                 while (@reqroles > 0) { 
                    597:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    598:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    599:                             last;
                    600:                         } else {
                    601:                             push(@badroles,$firstrole);
                    602:                             $firstrole = shift(@reqroles);
                    603:                         }
                    604:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
                    605:                         last;
                    606:                     } else {
                    607:                         push(@badroles,$firstrole);
                    608:                         $firstrole = shift(@reqroles);
                    609:                     }
                    610:                 }
                    611:                 if (@badroles > 0) {
                    612:                     if (@badroles > 1) {
                    613:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
                    614:                     } else {
                    615:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
                    616:                     }
                    617:                 }
                    618:                 my $firstsec;
1.32      raeburn   619:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
1.31      raeburn   620:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    621:                 }
1.5       raeburn   622:                 my $userargs = {
1.29      raeburn   623:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                    624:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
                    625:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7       raeburn   626:                     'udom' => $userdom,
1.1       raeburn   627:                     'uname' => $username,
1.29      raeburn   628:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
                    629:                     'first' => $details->{'users'}{$userkey}{'firstname'},
                    630:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
                    631:                     'last' => $details->{'users'}{$userkey}{'lastname'},
                    632:                     'gene' => $details->{'users'}{$userkey}{'generation'},
1.1       raeburn   633:                     'usec' => $firstsec,
1.29      raeburn   634:                     'end' => $details->{'users'}{$userkey}{'end'},
                    635:                     'start' => $details->{'users'}{$userkey}{'start'},
                    636:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   637:                     'cid' => $courseid,
1.8       raeburn   638:                     'crs' => $crsunum,
                    639:                     'cdom' => $crsudom,
1.1       raeburn   640:                     'context' => 'createcourse',
                    641:                     'linefeed' => $linefeed,
1.38      raeburn   642:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
1.5       raeburn   643:                    };
1.38      raeburn   644:                 if ($userargs->{'role'} eq 'st') {
                    645:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
                    646:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
                    647:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
                    648:                     }
                    649:                 }
1.22      raeburn   650:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1       raeburn   651: # now add other roles and other sections.
                    652:                 if ($outcome eq 'ok') {
1.32      raeburn   653:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29      raeburn   654:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1       raeburn   655:                             my $curr_role = $firstrole;
1.29      raeburn   656:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    657:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                    658:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1       raeburn   659:                             $url = '/'.$crsudom.'/'.$crsunum;
                    660:                             if ($usec ne '') {
                    661:                                 $url .= '/'.$usec;
                    662:                             }
1.31      raeburn   663:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    664:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    665:                             } else {
                    666:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    667:                             }
1.1       raeburn   668:                         }
                    669:                     }
1.31      raeburn   670:                     if (@reqroles > 0) {
                    671:                         foreach my $curr_role (@reqroles) {
1.29      raeburn   672:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    673:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   674:                             if ($curr_role eq 'st') {
1.29      raeburn   675:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   676:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    677:                                 if ($usec ne '') {
                    678:                                     $url .= '/'.$usec;
                    679:                                 }
1.38      raeburn   680:                                 my $credits;
                    681:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
                    682:                                     $credits = $details->{'users'}{$userkey}{'credits'};
                    683:                                     $credits =~ s/[^\d\.]//g;
                    684:                                 }
                    685:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.32      raeburn   686:                             } elsif ($curr_role eq $ccrole) {
1.31      raeburn   687:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    688:                                 my $usec = '';
                    689:                                 $$output .=
                    690:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    691:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
                    692:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   693:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   694:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    695:                                     if ($usec ne '') {
                    696:                                         $url .= '/'.$usec;
                    697:                                     }
1.31      raeburn   698:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    699:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    700:                                             $$output .= 
                    701:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    702:                                         } else {
                    703:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    704:                                         }
                    705:                                     } else {
                    706:                                         $$output .= 
                    707:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    708:                                     }
1.1       raeburn   709:                                 }
1.31      raeburn   710:                             } else {
                    711:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   712:                             }
                    713:                         }
                    714:                     }
                    715:                 }
                    716:             } else {
1.29      raeburn   717:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
                    718:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    719:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   720:                     if ($curr_role eq 'st') {
1.29      raeburn   721:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   722:                         $url = '/'.$crsudom.'/'.$crsunum;
                    723:                         if ($usec ne '') {
                    724:                             $url .= '/'.$usec;
                    725:                         }
1.38      raeburn   726:                         my $credits;
                    727:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
                    728:                             $credits = $details->{'users'}{$userkey}{'credits'};
                    729:                             $credits =~ s/[^\d\.]//g;
                    730:                         }
                    731:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.31      raeburn   732:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
                    733:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   734:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    735:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   736:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    737:                                 if ($usec ne '') {
                    738:                                     $url .= '/'.$usec;
                    739:                                 }
1.31      raeburn   740:                                 my $stdresult;
                    741:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    742:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    743:                                         $stdresult = 
                    744:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    745:                                     } else {
                    746:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    747:                                     }
                    748:                                 } else {
                    749:                                     $stdresult = 
                    750:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    751:                                 }
1.5       raeburn   752:                                 $$output .= $stdresult;
1.1       raeburn   753:                             }
                    754:                         } else {
1.5       raeburn   755:                             $url = '/'.$crsudom.'/'.$crsunum;
1.31      raeburn   756:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    757:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    758:                                     $$output .= 
                    759:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    760:                                 } else {
                    761:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    762:                                 }
                    763:                             } else {
                    764:                                 $$output .= 
                    765:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
                    766:                             }
1.1       raeburn   767:                         }
1.31      raeburn   768:                     } else {
                    769:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   770:                     }
                    771:                 }
                    772:             }
                    773:         }
                    774:     }
                    775: 
                    776: # Information about keys.
1.29      raeburn   777:     if ($details->{'setkeys'}) {
1.1       raeburn   778:         $$keysmsg .=
1.29      raeburn   779:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1       raeburn   780:     }
                    781: # Flush the course logs so reverse user roles immediately updated
                    782:     &Apache::lonnet::flushcourselogs();
                    783:     return $courseid;
                    784: }
                    785: 
1.31      raeburn   786: sub owner_is_dc {
                    787:     my ($owner_uname,$owner_dom,$cdom) = @_;
                    788:     my $is_dc = 0;
                    789:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
                    790:                     ['active'],['dc'],[$cdom]);
                    791:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
                    792:         $is_dc = 1;
                    793:     }
                    794:     return $is_dc;
                    795: }
                    796: 
1.1       raeburn   797: #########################################################
                    798: #
                    799: # process_date()
                    800: # 
                    801: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    802: # output - corresponding UNIX time (seconds since epoch). 
                    803: #
                    804: #########################################################
                    805: 
                    806: sub process_date {
                    807:     my $timestr = shift;
                    808:     my $timestamp = '';
1.5       raeburn   809:     if ($timestr !~ /:/) {
1.1       raeburn   810:         $timestamp = '';
                    811:     } else {
1.12      albertel  812:         my @entries = split(/:/,$timestr);
1.1       raeburn   813:         for (my $j=0; $j<@entries; $j++) {
                    814:             if ( length($entries[$j]) > 1 ) {
                    815:                 $entries[$j] =~ s/^0//;
                    816:             }
1.10      raeburn   817:             $entries[$j] =~ s/\D//g;
                    818:             if ($entries[$j] < 0) {
                    819:                 $entries[$j] = 0;
                    820:             }
1.1       raeburn   821:         }
1.5       raeburn   822:         if ($entries[1] > 0) {  
                    823:             $entries[1] = $entries[1] - 1;
                    824:         }
1.10      raeburn   825:         if ($entries[5] > 60) {
                    826:             $entries[5] = 60;
                    827:         }
                    828:         if ($entries[4] > 59) {
                    829:             $entries[4] = 59;
                    830:         }
                    831:         if ($entries[3] > 23) {
                    832:             $entries[3] = 23;
                    833:         }
                    834:         if ($entries[2] > 31) {
                    835:             $entries[2] = 31;
                    836:         }
                    837:         if ($entries[1] > 11) {
                    838:             $entries[1] = 11;
                    839:         }
                    840:         if ($entries[2] == 31) {
                    841:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    842:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    843:                 $entries[2] = 30;
                    844:             }
                    845:         }
                    846:         if ($entries[1] == 1) {
                    847:             if ($entries[2] > 29) {
                    848:                 $entries[2] = 29;
                    849:             }
                    850:             if ($entries[2] == 29) {
1.12      albertel  851:                 if ($entries[0]%4 != 0) {
1.14      raeburn   852:                     $entries[2] = 28;
1.12      albertel  853:                 } elsif ( $entries[0]%100 == 0
                    854: 			  && $entries[0]%400 != 0) {
1.14      raeburn   855: 		    $entries[2] = 28;
1.12      albertel  856: 		}
1.10      raeburn   857:             }
                    858:         }     
1.1       raeburn   859:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    860:     }
                    861:     return $timestamp;
                    862: }
                    863: 
                    864: 1;

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