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

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

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