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

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

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