File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.40.2.1: download - view: text, annotated - select for diffs
Thu Sep 1 01:05:05 2016 UTC (7 years, 8 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
Diff to branchpoint 1.40: preferred, unified
- For 2.11
  - Backport 1.42

    1: #
    2: # $Id: batchcreatecourse.pm,v 1.40.2.1 2016/09/01 01:05:05 raeburn Exp $
    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: 
   27: package LONCAPA::batchcreatecourse;
   28: use LONCAPA::Configuration;
   29: use LONCAPA::Enrollment;
   30: use HTML::Parser;
   31: use Time::Local;
   32: use Apache::lonnet;
   33: use Apache::loncommon;
   34: use Apache::lonlocal;
   35: use LONCAPA qw(:match);
   36: 
   37: use strict;
   38: 
   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
   45: # /home/httpd/perl/tmp/addcourse/$dom
   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>
   57: # <crstype>Course</crstype>
   58: # <coursecode>ss05ubw101</coursecode>
   59: # <defaultcredits>3</defaultcredits>
   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>
   77: # <crsquota>20</crsquota>
   78: # <uniquecode>1</uniquecode>
   79: # <clonecrs>466011437c34194msul1</clonecrs>
   80: # <clonedom>msu</clonedom>
   81: # <datemode>shift</datemode>
   82: # <dateshift>365</dateshift>
   83: # <showphotos></showphotos>
   84: # <setpolicy>1</setpolicy>
   85: # <setcontent>1</setcontent>
   86: # <setcomment>1</setcomment>
   87: # <setkeys>0</setkeys>
   88: # <keyauth>keyadmin:msu</keyauth>
   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>
  128: #   <credits></credits>
  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:
  156: # setpolicy, setcontent, setcomment, setkeys, disableresdis, disablechat, openall,
  157: # uniquecode
  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: #
  164: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
  165: # no entry respectively.
  166: # 
  167: # crstype can be Course or Community
  168: #
  169: # crsquota is the total disk space permitted for course group portfolio files
  170: # in all course groups.
  171: # 
  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: #
  180: #                    Directory for retrieval of files listed in @$requests is: 
  181: #                    /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
  182: #                    /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
  183: #                    
  184: # inputs (six)   -  requests - ref to array of filename(s) containing course requests 
  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
  188: #                   uname - username of DC who is requesting course creation
  189: #                   udom - domain of DC who is requesting course creation
  190: #  
  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.
  197: #############################################################
  198: 
  199: sub create_courses {
  200:     my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
  201:     my $output;
  202:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
  203: # Get role names
  204:     my %longroles = ();
  205:     open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
  206:     my @rolesplain = <FILE>;
  207:     close(FILE);
  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;
  212:         }
  213:     }
  214:     my ($logmsg,$keysmsg,$newusermsg,$addresult,%codehash);
  215:     my %enrollcount = ();
  216:     my $newcoursedir = LONCAPA::tempdir().'/addcourse/'.$dom.'/'.$context;
  217:     if ($context eq 'auto') {
  218:         $newcoursedir .= '/pending';
  219:     } else {
  220:         if ($uname && $udom) {
  221:             $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
  222:         } else {
  223:             $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
  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);
  231:                 foreach my $num (sort(keys(%details))) {
  232:                     my $reqdetails = $details{$num};
  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);
  237:                     if ($courseid =~m{^/$match_domain/$match_courseid}) {
  238:                         $$courseids{$courseid} = $details{$num}{'class'};
  239:                         if ($code) {
  240:                             $codehash{$courseid} = $code;
  241:                         }
  242:                     }
  243:                 }
  244:             }
  245:         }
  246:     }
  247:     return ($output,$logmsg,$keysmsg,\%codehash);
  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 = '';
  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');
  271:     my @possroles = qw(st ad ep ta in cc co);
  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};
  291:                      if (grep(/^\Q$role\E$/,@possroles)) {
  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") {
  324:                     $$details{$num}{'ownerauthparam'} = $text;
  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;
  337:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text); 
  338:                  } elsif ("@state" eq "class users user roles role start") {
  339:                      if (grep(/^\Q$role\E$/,@possroles)) {
  340:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
  341:                      }
  342:                  } elsif ("@state" eq "class users user roles role end") {
  343:                      if (grep(/^\Q$role\E$/,@possroles)) {
  344:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
  345:                      }
  346:                  } elsif ("@state" eq "class users user roles role usec") {
  347:                      if (grep(/^\Q$role\E$/,@possroles)) {
  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
  390: #   course domain
  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
  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
  404: #       course requests submitted via course request form.
  405: #   optional category
  406: #   optional ref to scalar for six character unique identifier
  407: #
  408: # outputs
  409: #   LON-CAPA courseID for new (created) course
  410: #
  411: #########################################################
  412: 
  413: sub build_course {
  414:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,
  415:         $enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category,$coderef) = @_;
  416:     return unless (ref($details) eq 'HASH');
  417:     my $owner_uname = $details->{'owner'};
  418:     my $owner_domain = $details->{'domain'};
  419:     my $owner = $owner_uname.':'.$owner_domain;
  420:     my $sectionstr = '';
  421:     my $xliststr = '';
  422:     my $noenddate = '';
  423:     my $outcome;
  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:     }
  438:     my $linefeed;
  439:     if ($context eq 'auto') {
  440:         $linefeed = "\n";
  441:     } else {
  442:         $linefeed = "<br />\n";
  443:     }
  444:     if ($details->{'accessend'} eq '') {
  445:         $noenddate = 1;
  446:     }
  447:     my $reshome = $details->{'reshome'};
  448:     if ($reshome eq '') {
  449:         $reshome = '/res/'.$cdom;
  450:     }
  451:     my $firstres =  $details->{'firstres'};
  452:     if ($firstres eq '') {
  453:         if ($crstype eq 'Community') {
  454:             $firstres = 'nav';
  455:         } else {
  456:             $firstres = 'syl';
  457:         }
  458:     }
  459:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
  460:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
  461:     }
  462:     $sectionstr =~ s/,$//;
  463: 
  464:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
  465:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
  466:     }
  467:     $xliststr =~ s/,$//;
  468: 
  469:     my %courseinfo = (
  470:                       inst_code => $details->{'coursecode'},
  471:                       description => $details->{'title'}
  472:                      ); 
  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'},
  484:                     'usec' => '',
  485:                     'end' => '',
  486:                     'start' => '',
  487:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
  488:                     'cid' => '',
  489:                     'context' => 'createowner',
  490:                     'linefeed' => $linefeed,
  491:                     'role' => $ccrole,
  492:                    };
  493:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
  494:     } else {
  495:         $outcome = 'ok';
  496:     }
  497: 
  498:     if ($outcome eq 'ok') {
  499:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
  500:             $details->{'datemode'} = 'shift';
  501:             $details->{'dateshift'} = 365;
  502:         }
  503:         my $courseargs = {
  504:                ccuname => $details->{'owner'},
  505:                ccdomain => $details->{'domain'},
  506:                cdescr => $details->{'title'},
  507:                crstype => $details->{'crstype'},
  508:                curl => $details->{'topmap'},
  509:                course_domain => $cdom,
  510:                course_home =>  $details->{'coursehome'},
  511:                nonstandard => $details->{'nonstandard'},
  512:                crscode => $details->{'coursecode'},
  513:                defaultcredits => $details->{'defaultcredits'},
  514:                crsquota => $details->{'crsquota'},
  515:                uniquecode => $details->{'uniquecode'},
  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'},
  522:                crssections => $sectionstr,
  523:                crsxlist => $xliststr,
  524:                autoadds => $details->{'adds'},
  525:                autodrops => $details->{'drops'},
  526:                notify => $details->{'notify_owner'},
  527:                notify_dc => $details->{'notify_dc'},
  528:                no_end_date => $noenddate,
  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'},
  538:                setcomment => $details->{'setcomment'},
  539:                reshome => $reshome,
  540:                setkeys => $details->{'setkeys'},
  541:                keyauth => $details->{'keyauth'},
  542:                disresdis => $details->{'disresdis'},
  543:                disablechat => $details->{'disablechat'},
  544:                openall => $details->{'openall'},
  545:                firstres => $firstres
  546:                };
  547:         if ($details->{'textbook'}) {
  548:             $courseargs->{'textbook'} = $details->{'textbook'};
  549:         }
  550:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
  551:         if (! exists($host_servers{$details->{'coursehome'}})) {
  552:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
  553:             return;
  554:         }
  555:         my ($success, $msg) = 
  556:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
  557:                                                  $udom,$uname,$context,$cnum,$category,$coderef);
  558: 	$$logmsg .= $msg;
  559:         if (!$success) {
  560:             return;
  561:         }
  562:     } else {
  563:         return;
  564:     }
  565:     
  566: #
  567: # Make owner a coordinator
  568: #
  569:     if (($owner_domain) && ($owner_uname)) {
  570:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
  571:     }
  572: 
  573: #
  574: # Process other reqested users
  575: #
  576: 
  577:     my @courseroles = qw(st ep ta in);
  578:     push(@courseroles,$ccrole);
  579:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
  580:         push(@courseroles,'ad');
  581:     }
  582:     my $stulogmsg = '';
  583:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
  584:         my $url = '/'.$crsudom.'/'.$crsunum;
  585:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
  586:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
  587:             my ($username,$userdom) = split/:/,$userkey;
  588:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
  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;
  615:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
  616:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
  617:                 }
  618:                 my $userargs = {
  619:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
  620:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
  621:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
  622:                     'udom' => $userdom,
  623:                     'uname' => $username,
  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'},
  629:                     'usec' => $firstsec,
  630:                     'end' => $details->{'users'}{$userkey}{'end'},
  631:                     'start' => $details->{'users'}{$userkey}{'start'},
  632:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
  633:                     'cid' => $courseid,
  634:                     'crs' => $crsunum,
  635:                     'cdom' => $crsudom,
  636:                     'context' => 'createcourse',
  637:                     'linefeed' => $linefeed,
  638:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
  639:                    };
  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:                 }
  646:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
  647: # now add other roles and other sections.
  648:                 if ($outcome eq 'ok') {
  649:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
  650:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
  651:                             my $curr_role = $firstrole;
  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];
  655:                             $url = '/'.$crsudom.'/'.$crsunum;
  656:                             if ($usec ne '') {
  657:                                 $url .= '/'.$usec;
  658:                             }
  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:                             }
  664:                         }
  665:                     }
  666:                     if (@reqroles > 0) {
  667:                         foreach my $curr_role (@reqroles) {
  668:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  669:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  670:                             if ($curr_role eq 'st') {
  671:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  672:                                 $url = '/'.$crsudom.'/'.$crsunum;
  673:                                 if ($usec ne '') {
  674:                                     $url .= '/'.$usec;
  675:                                 }
  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);
  682:                             } elsif ($curr_role eq $ccrole) {
  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/[^/]+$})) {
  689:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  690:                                     $url = '/'.$crsudom.'/'.$crsunum;
  691:                                     if ($usec ne '') {
  692:                                         $url .= '/'.$usec;
  693:                                     }
  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:                                     }
  705:                                 }
  706:                             } else {
  707:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  708:                             }
  709:                         }
  710:                     }
  711:                 }
  712:             } else {
  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'};
  716:                     if ($curr_role eq 'st') {
  717:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  718:                         $url = '/'.$crsudom.'/'.$crsunum;
  719:                         if ($usec ne '') {
  720:                             $url .= '/'.$usec;
  721:                         }
  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);
  728:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
  729:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  730:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
  731:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  732:                                 $url = '/'.$crsudom.'/'.$crsunum;
  733:                                 if ($usec ne '') {
  734:                                     $url .= '/'.$usec;
  735:                                 }
  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:                                 }
  748:                                 $$output .= $stdresult;
  749:                             }
  750:                         } else {
  751:                             $url = '/'.$crsudom.'/'.$crsunum;
  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:                             }
  763:                         }
  764:                     } else {
  765:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  766:                     }
  767:                 }
  768:             }
  769:         }
  770:     }
  771: 
  772: # Information about keys.
  773:     if ($details->{'setkeys'}) {
  774:         $$keysmsg .=
  775:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
  776:     }
  777: # Flush the course logs so reverse user roles immediately updated
  778:     &Apache::lonnet::flushcourselogs();
  779:     return $courseid;
  780: }
  781: 
  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: 
  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 = '';
  805:     if ($timestr !~ /:/) {
  806:         $timestamp = '';
  807:     } else {
  808:         my @entries = split(/:/,$timestr);
  809:         for (my $j=0; $j<@entries; $j++) {
  810:             if ( length($entries[$j]) > 1 ) {
  811:                 $entries[$j] =~ s/^0//;
  812:             }
  813:             $entries[$j] =~ s/\D//g;
  814:             if ($entries[$j] < 0) {
  815:                 $entries[$j] = 0;
  816:             }
  817:         }
  818:         if ($entries[1] > 0) {  
  819:             $entries[1] = $entries[1] - 1;
  820:         }
  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) {
  847:                 if ($entries[0]%4 != 0) {
  848:                     $entries[2] = 28;
  849:                 } elsif ( $entries[0]%100 == 0
  850: 			  && $entries[0]%400 != 0) {
  851: 		    $entries[2] = 28;
  852: 		}
  853:             }
  854:         }     
  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>