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

1.4       albertel    1: #
1.40.2.1! 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>
1.40.2.1! raeburn    86: # <setcomment>1</setcomment>
1.1       raeburn    87: # <setkeys>0</setkeys>
1.19      albertel   88: # <keyauth>keyadmin:msu</keyauth>
1.1       raeburn    89: # <disresdis>1</disresdis>
                     90: # <disablechat>1</disablechat>
                     91: # <openall></openall>
                     92: # <notify_dc>1</notify_dc>
                     93: # <notify_owner>1</notify_owner>
                     94: # <owner>
                     95: #  <username>sparty</username>
                     96: #  <domain>msu</domain>
                     97: #  <authtype>krb4</authtype>
                     98: #  <autharg>MSU.EDU</autharg>
                     99: # </owner>
                    100: # <sections>
                    101: #  <section>
                    102: #   <inst>001</inst>
                    103: #   <loncapa>1</loncapa>
                    104: #  </section>
                    105: #  <section>
                    106: #   <inst>002</inst>
                    107: #   <loncapa>2</loncapa>
                    108: #  </section>
                    109: # </sections>
                    110: # <crosslists>
                    111: #  <xlist>
                    112: #   <inst>ss05zzz101001</inst>
                    113: #   <loncapa>1</loncapa>
                    114: #  </xlist>
                    115: # </crosslists>
                    116: # <users>
                    117: #  <user>
                    118: #   <username>sparty</username>
                    119: #   <domain>msu</domain>
                    120: #   <email>sparty@msu.edu</email>
                    121: #   <authtype>krb4</authtype>
                    122: #   <autharg></autharg>
                    123: #   <firstname>MSU</firstname>
                    124: #   <generation></generation>
                    125: #   <lastname>Spartan</lastname>x
                    126: #   <middlename></middlename>
                    127: #   <studentID></studentID>
1.38      raeburn   128: #   <credits></credits>
1.1       raeburn   129: #   <roles></roles>
                    130: #  </user>
                    131: #  <user>
                    132: #   <username>itds0001</username>
                    133: #   <domain>northwood5</domain>
                    134: #   <email>itds0001@msu.edu</email>
                    135: #   <authtype>int</authtype>
                    136: #   <autharg></autharg>
                    137: #   <firstname>Info</firstname>
                    138: #   <generation></generation>
                    139: #   <lastname>Techc</lastname>x
                    140: #   <middlename></middlename>
                    141: #   <studentID></studentID>
                    142: #   <roles>
                    143: #    <role id='in'>
                    144: #     <start>2005:01:01:12:10</start>
                    145: #     <end>2005:12:01:12:10</end>
                    146: #     <usec>1</usec>
                    147: #     <usec>2</usec>
                    148: #    </role>
                    149: #   </roles>
                    150: #  </user>
                    151: # </users>
                    152: #</class>
                    153: #
                    154: # Many of these are binary options (corresponding to either checkboxes or
                    155: # radio buttons in the interactive CCRS page).  Examples include:
1.40.2.1! raeburn   156: # setpolicy, setcontent, setcomment, setkeys, disableresdis, disablechat, openall,
1.39      raeburn   157: # uniquecode
1.1       raeburn   158: #
                    159: # A value of 1 between opening and closing tags is equivalent to a 
                    160: # checked checkbox or 'Yes' response in the original CCRS web page.
                    161: # A value of 0 or blank is equivalent to an unchecked box or 'No'
                    162: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
                    163: #
1.15      raeburn   164: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
1.1       raeburn   165: # no entry respectively.
1.15      raeburn   166: # 
1.33      raeburn   167: # crstype can be Course or Community
1.15      raeburn   168: #
                    169: # crsquota is the total disk space permitted for course group portfolio files
                    170: # in all course groups.
                    171: # 
1.1       raeburn   172: # For format of other parameters, refer to the interactive CCRS page
                    173: # and view how the equivalent parameter is displayed in the web form.  
                    174: #  
                    175: ##############################################################
                    176: # create_courses() - creates courses described in @$requests,
                    177: #                    files listed in @$requests are deleted
                    178: #                    after the files have been parsed.
                    179: #
1.3       raeburn   180: #                    Directory for retrieval of files listed in @$requests is: 
                    181: #                    /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
1.13      raeburn   182: #                    /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
1.1       raeburn   183: #                    
1.39      raeburn   184: # inputs (six)   -  requests - ref to array of filename(s) containing course requests 
1.1       raeburn   185: #                   courseids - ref to hash to store LON-CAPA course ids of new courses 
                    186: #                   context - auto if called from command line, web if called from browser
                    187: #                   dom - domain for which the course is being created
1.3       raeburn   188: #                   uname - username of DC who is requesting course creation
                    189: #                   udom - domain of DC who is requesting course creation
1.1       raeburn   190: #  
1.39      raeburn   191: # outputs (four)  -  output - text recording user roles added etc.
                    192: #                    logmsg - text to be logged
                    193: #                    keysmsg - text containing link(s) to manage keys page(s) 
                    194: #                    codehash - reference to hash containing courseID => unique code
                    195: #                               where unique code is a 6 character code, to distribute
                    196: #                               to students as a shortcut to the course.
1.1       raeburn   197: #############################################################
                    198: 
                    199: sub create_courses {
1.3       raeburn   200:     my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
1.1       raeburn   201:     my $output;
                    202:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
                    203: # Get role names
                    204:     my %longroles = ();
1.9       raeburn   205:     open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
1.1       raeburn   206:     my @rolesplain = <FILE>;
                    207:     close(FILE);
1.32      raeburn   208:     foreach my $item (@rolesplain) {
                    209:         if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
                    210:             $longroles{'Course'}{$1} = $2;
                    211:             $longroles{'Community'}{$1} = $3;
1.1       raeburn   212:         }
                    213:     }
1.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.40.2.1! 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 {
                    432:         $crstype = 'Course';
                    433:         $ccrole = 'cc';
                    434:         if (ref($longroles) eq 'HASH') {
                    435:             $rolenames = $longroles->{'Course'};
                    436:         }
                    437:     }
1.1       raeburn   438:     my $linefeed;
                    439:     if ($context eq 'auto') {
                    440:         $linefeed = "\n";
                    441:     } else {
                    442:         $linefeed = "<br />\n";
                    443:     }
1.29      raeburn   444:     if ($details->{'accessend'} eq '') {
1.1       raeburn   445:         $noenddate = 1;
                    446:     }
1.29      raeburn   447:     my $reshome = $details->{'reshome'};
1.1       raeburn   448:     if ($reshome eq '') {
                    449:         $reshome = '/res/'.$cdom;
                    450:     }
1.29      raeburn   451:     my $firstres =  $details->{'firstres'};
1.1       raeburn   452:     if ($firstres eq '') {
1.36      raeburn   453:         if ($crstype eq 'Community') {
                    454:             $firstres = 'nav';
                    455:         } else {
                    456:             $firstres = 'syl';
                    457:         }
1.1       raeburn   458:     }
1.29      raeburn   459:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
                    460:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   461:     }
1.5       raeburn   462:     $sectionstr =~ s/,$//;
1.1       raeburn   463: 
1.29      raeburn   464:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
                    465:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   466:     }
1.5       raeburn   467:     $xliststr =~ s/,$//;
1.1       raeburn   468: 
                    469:     my %courseinfo = (
1.29      raeburn   470:                       inst_code => $details->{'coursecode'},
                    471:                       description => $details->{'title'}
1.1       raeburn   472:                      ); 
1.29      raeburn   473:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
                    474:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
                    475:                     'authparam' => $details->{'ownerauthparam'},
                    476:                     'emailenc' => $details->{'emailenc'},
                    477:                     'udom' => $details->{'domain'},
                    478:                     'uname' => $details->{'owner'},
                    479:                     'pid' => $details->{'users'}{$owner}{'studentID'},
                    480:                     'first' => $details->{'users'}{$owner}{'firstname'},
                    481:                     'middle' => $details->{'users'}{$owner}{'middlename'},
                    482:                     'last' => $details->{'users'}{$owner}{'lastname'},
                    483:                     'gene' => $details->{'users'}{$owner}{'generation'},
1.1       raeburn   484:                     'usec' => '',
                    485:                     'end' => '',
                    486:                     'start' => '',
1.29      raeburn   487:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1       raeburn   488:                     'cid' => '',
                    489:                     'context' => 'createowner',
                    490:                     'linefeed' => $linefeed,
1.32      raeburn   491:                     'role' => $ccrole,
1.5       raeburn   492:                    };
1.32      raeburn   493:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1       raeburn   494:     } else {
                    495:         $outcome = 'ok';
                    496:     }
                    497: 
1.6       raeburn   498:     if ($outcome eq 'ok') {
1.29      raeburn   499:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
                    500:             $details->{'datemode'} = 'shift';
                    501:             $details->{'dateshift'} = 365;
1.27      raeburn   502:         }
1.6       raeburn   503:         my $courseargs = {
1.29      raeburn   504:                ccuname => $details->{'owner'},
                    505:                ccdomain => $details->{'domain'},
                    506:                cdescr => $details->{'title'},
                    507:                crstype => $details->{'crstype'},
                    508:                curl => $details->{'topmap'},
1.1       raeburn   509:                course_domain => $cdom,
1.29      raeburn   510:                course_home =>  $details->{'coursehome'},
                    511:                nonstandard => $details->{'nonstandard'},
                    512:                crscode => $details->{'coursecode'},
1.38      raeburn   513:                defaultcredits => $details->{'defaultcredits'},
1.29      raeburn   514:                crsquota => $details->{'crsquota'},
1.39      raeburn   515:                uniquecode => $details->{'uniquecode'},
1.29      raeburn   516:                clonecourse => $details->{'clonecrs'},
                    517:                clonedomain => $details->{'clonedom'},
                    518:                datemode => $details->{'datemode'},
                    519:                dateshift => $details->{'dateshift'},
                    520:                crsid => $details->{'optional_id'},
                    521:                curruser => $details->{'owner'},
1.1       raeburn   522:                crssections => $sectionstr,
                    523:                crsxlist => $xliststr,
1.29      raeburn   524:                autoadds => $details->{'adds'},
                    525:                autodrops => $details->{'drops'},
                    526:                notify => $details->{'notify_owner'},
                    527:                notify_dc => $details->{'notify_dc'},
1.1       raeburn   528:                no_end_date => $noenddate,
1.29      raeburn   529:                showphotos => $details->{'showphotos'},
                    530:                authtype => $details->{'authtype'},
                    531:                autharg => $details->{'authparam'},
                    532:                enrollstart => $details->{'enrollstart'},
                    533:                enrollend => $details->{'enrollend'},
                    534:                startaccess => $details->{'accessstart'},
                    535:                endaccess => $details->{'accessend'},
                    536:                setpolicy => $details->{'setpolicy'},
                    537:                setcontent => $details->{'setcontent'},
1.40.2.1! raeburn   538:                setcomment => $details->{'setcomment'},
1.1       raeburn   539:                reshome => $reshome,
1.29      raeburn   540:                setkeys => $details->{'setkeys'},
                    541:                keyauth => $details->{'keyauth'},
                    542:                disresdis => $details->{'disresdis'},
                    543:                disablechat => $details->{'disablechat'},
                    544:                openall => $details->{'openall'},
1.1       raeburn   545:                firstres => $firstres
                    546:                };
1.40      raeburn   547:         if ($details->{'textbook'}) {
                    548:             $courseargs->{'textbook'} = $details->{'textbook'};
                    549:         }
1.20      albertel  550:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29      raeburn   551:         if (! exists($host_servers{$details->{'coursehome'}})) {
                    552:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1       raeburn   553:             return;
                    554:         }
1.39      raeburn   555:         my ($success, $msg) = 
                    556:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
                    557:                                                  $udom,$uname,$context,$cnum,$category,$coderef);
1.23      albertel  558: 	$$logmsg .= $msg;
1.24      raeburn   559:         if (!$success) {
                    560:             return;
                    561:         }
1.1       raeburn   562:     } else {
                    563:         return;
                    564:     }
                    565:     
                    566: #
1.32      raeburn   567: # Make owner a coordinator
1.1       raeburn   568: #
                    569:     if (($owner_domain) && ($owner_uname)) {
1.32      raeburn   570:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1       raeburn   571:     }
                    572: 
                    573: #
                    574: # Process other reqested users
                    575: #
1.32      raeburn   576: 
                    577:     my @courseroles = qw(st ep ta in);
                    578:     push(@courseroles,$ccrole);
1.31      raeburn   579:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
                    580:         push(@courseroles,'ad');
                    581:     }
1.1       raeburn   582:     my $stulogmsg = '';
1.29      raeburn   583:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1       raeburn   584:         my $url = '/'.$crsudom.'/'.$crsunum;
1.29      raeburn   585:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
                    586:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1       raeburn   587:             my ($username,$userdom) = split/:/,$userkey;
                    588:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31      raeburn   589:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
                    590:                 my @badroles;
                    591:                 my $firstrole = shift(@reqroles);
                    592:                 while (@reqroles > 0) { 
                    593:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    594:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    595:                             last;
                    596:                         } else {
                    597:                             push(@badroles,$firstrole);
                    598:                             $firstrole = shift(@reqroles);
                    599:                         }
                    600:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
                    601:                         last;
                    602:                     } else {
                    603:                         push(@badroles,$firstrole);
                    604:                         $firstrole = shift(@reqroles);
                    605:                     }
                    606:                 }
                    607:                 if (@badroles > 0) {
                    608:                     if (@badroles > 1) {
                    609:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
                    610:                     } else {
                    611:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
                    612:                     }
                    613:                 }
                    614:                 my $firstsec;
1.32      raeburn   615:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
1.31      raeburn   616:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    617:                 }
1.5       raeburn   618:                 my $userargs = {
1.29      raeburn   619:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                    620:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
                    621:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7       raeburn   622:                     'udom' => $userdom,
1.1       raeburn   623:                     'uname' => $username,
1.29      raeburn   624:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
                    625:                     'first' => $details->{'users'}{$userkey}{'firstname'},
                    626:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
                    627:                     'last' => $details->{'users'}{$userkey}{'lastname'},
                    628:                     'gene' => $details->{'users'}{$userkey}{'generation'},
1.1       raeburn   629:                     'usec' => $firstsec,
1.29      raeburn   630:                     'end' => $details->{'users'}{$userkey}{'end'},
                    631:                     'start' => $details->{'users'}{$userkey}{'start'},
                    632:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   633:                     'cid' => $courseid,
1.8       raeburn   634:                     'crs' => $crsunum,
                    635:                     'cdom' => $crsudom,
1.1       raeburn   636:                     'context' => 'createcourse',
                    637:                     'linefeed' => $linefeed,
1.38      raeburn   638:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
1.5       raeburn   639:                    };
1.38      raeburn   640:                 if ($userargs->{'role'} eq 'st') {
                    641:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
                    642:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
                    643:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
                    644:                     }
                    645:                 }
1.22      raeburn   646:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1       raeburn   647: # now add other roles and other sections.
                    648:                 if ($outcome eq 'ok') {
1.32      raeburn   649:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29      raeburn   650:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1       raeburn   651:                             my $curr_role = $firstrole;
1.29      raeburn   652:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    653:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                    654:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1       raeburn   655:                             $url = '/'.$crsudom.'/'.$crsunum;
                    656:                             if ($usec ne '') {
                    657:                                 $url .= '/'.$usec;
                    658:                             }
1.31      raeburn   659:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    660:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    661:                             } else {
                    662:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    663:                             }
1.1       raeburn   664:                         }
                    665:                     }
1.31      raeburn   666:                     if (@reqroles > 0) {
                    667:                         foreach my $curr_role (@reqroles) {
1.29      raeburn   668:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    669:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   670:                             if ($curr_role eq 'st') {
1.29      raeburn   671:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   672:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    673:                                 if ($usec ne '') {
                    674:                                     $url .= '/'.$usec;
                    675:                                 }
1.38      raeburn   676:                                 my $credits;
                    677:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
                    678:                                     $credits = $details->{'users'}{$userkey}{'credits'};
                    679:                                     $credits =~ s/[^\d\.]//g;
                    680:                                 }
                    681:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.32      raeburn   682:                             } elsif ($curr_role eq $ccrole) {
1.31      raeburn   683:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    684:                                 my $usec = '';
                    685:                                 $$output .=
                    686:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    687:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
                    688:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   689:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   690:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    691:                                     if ($usec ne '') {
                    692:                                         $url .= '/'.$usec;
                    693:                                     }
1.31      raeburn   694:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    695:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    696:                                             $$output .= 
                    697:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    698:                                         } else {
                    699:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    700:                                         }
                    701:                                     } else {
                    702:                                         $$output .= 
                    703:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    704:                                     }
1.1       raeburn   705:                                 }
1.31      raeburn   706:                             } else {
                    707:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   708:                             }
                    709:                         }
                    710:                     }
                    711:                 }
                    712:             } else {
1.29      raeburn   713:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
                    714:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    715:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   716:                     if ($curr_role eq 'st') {
1.29      raeburn   717:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   718:                         $url = '/'.$crsudom.'/'.$crsunum;
                    719:                         if ($usec ne '') {
                    720:                             $url .= '/'.$usec;
                    721:                         }
1.38      raeburn   722:                         my $credits;
                    723:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
                    724:                             $credits = $details->{'users'}{$userkey}{'credits'};
                    725:                             $credits =~ s/[^\d\.]//g;
                    726:                         }
                    727:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.31      raeburn   728:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
                    729:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   730:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    731:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   732:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    733:                                 if ($usec ne '') {
                    734:                                     $url .= '/'.$usec;
                    735:                                 }
1.31      raeburn   736:                                 my $stdresult;
                    737:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    738:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    739:                                         $stdresult = 
                    740:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    741:                                     } else {
                    742:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    743:                                     }
                    744:                                 } else {
                    745:                                     $stdresult = 
                    746:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    747:                                 }
1.5       raeburn   748:                                 $$output .= $stdresult;
1.1       raeburn   749:                             }
                    750:                         } else {
1.5       raeburn   751:                             $url = '/'.$crsudom.'/'.$crsunum;
1.31      raeburn   752:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    753:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    754:                                     $$output .= 
                    755:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    756:                                 } else {
                    757:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    758:                                 }
                    759:                             } else {
                    760:                                 $$output .= 
                    761:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
                    762:                             }
1.1       raeburn   763:                         }
1.31      raeburn   764:                     } else {
                    765:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   766:                     }
                    767:                 }
                    768:             }
                    769:         }
                    770:     }
                    771: 
                    772: # Information about keys.
1.29      raeburn   773:     if ($details->{'setkeys'}) {
1.1       raeburn   774:         $$keysmsg .=
1.29      raeburn   775:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1       raeburn   776:     }
                    777: # Flush the course logs so reverse user roles immediately updated
                    778:     &Apache::lonnet::flushcourselogs();
                    779:     return $courseid;
                    780: }
                    781: 
1.31      raeburn   782: sub owner_is_dc {
                    783:     my ($owner_uname,$owner_dom,$cdom) = @_;
                    784:     my $is_dc = 0;
                    785:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
                    786:                     ['active'],['dc'],[$cdom]);
                    787:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
                    788:         $is_dc = 1;
                    789:     }
                    790:     return $is_dc;
                    791: }
                    792: 
1.1       raeburn   793: #########################################################
                    794: #
                    795: # process_date()
                    796: # 
                    797: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    798: # output - corresponding UNIX time (seconds since epoch). 
                    799: #
                    800: #########################################################
                    801: 
                    802: sub process_date {
                    803:     my $timestr = shift;
                    804:     my $timestamp = '';
1.5       raeburn   805:     if ($timestr !~ /:/) {
1.1       raeburn   806:         $timestamp = '';
                    807:     } else {
1.12      albertel  808:         my @entries = split(/:/,$timestr);
1.1       raeburn   809:         for (my $j=0; $j<@entries; $j++) {
                    810:             if ( length($entries[$j]) > 1 ) {
                    811:                 $entries[$j] =~ s/^0//;
                    812:             }
1.10      raeburn   813:             $entries[$j] =~ s/\D//g;
                    814:             if ($entries[$j] < 0) {
                    815:                 $entries[$j] = 0;
                    816:             }
1.1       raeburn   817:         }
1.5       raeburn   818:         if ($entries[1] > 0) {  
                    819:             $entries[1] = $entries[1] - 1;
                    820:         }
1.10      raeburn   821:         if ($entries[5] > 60) {
                    822:             $entries[5] = 60;
                    823:         }
                    824:         if ($entries[4] > 59) {
                    825:             $entries[4] = 59;
                    826:         }
                    827:         if ($entries[3] > 23) {
                    828:             $entries[3] = 23;
                    829:         }
                    830:         if ($entries[2] > 31) {
                    831:             $entries[2] = 31;
                    832:         }
                    833:         if ($entries[1] > 11) {
                    834:             $entries[1] = 11;
                    835:         }
                    836:         if ($entries[2] == 31) {
                    837:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    838:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    839:                 $entries[2] = 30;
                    840:             }
                    841:         }
                    842:         if ($entries[1] == 1) {
                    843:             if ($entries[2] > 29) {
                    844:                 $entries[2] = 29;
                    845:             }
                    846:             if ($entries[2] == 29) {
1.12      albertel  847:                 if ($entries[0]%4 != 0) {
1.14      raeburn   848:                     $entries[2] = 28;
1.12      albertel  849:                 } elsif ( $entries[0]%100 == 0
                    850: 			  && $entries[0]%400 != 0) {
1.14      raeburn   851: 		    $entries[2] = 28;
1.12      albertel  852: 		}
1.10      raeburn   853:             }
                    854:         }     
1.1       raeburn   855:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    856:     }
                    857:     return $timestamp;
                    858: }
                    859: 
                    860: 1;

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