File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.40.2.2: download - view: text, annotated - select for diffs
Fri Jul 26 14:52:26 2019 UTC (4 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.40: preferred, unified
- For 2.11
  Backport 1.43

    1: #
    2: # $Id: batchcreatecourse.pm,v 1.40.2.2 2019/07/26 14:52:26 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,%instcodes);
  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:                         if ($details{$num}{'coursecode'} ne '') {
  243:                             push(@{$instcodes{$details{$num}{'coursecode'}}},$courseid);
  244:                         }
  245:                     }
  246:                 }
  247:             }
  248:         }
  249:     }
  250:     return ($output,$logmsg,$keysmsg,\%codehash,\%instcodes);
  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 = '';
  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');
  274:     my @possroles = qw(st ad ep ta in cc co);
  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};
  294:                      if (grep(/^\Q$role\E$/,@possroles)) {
  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") {
  327:                     $$details{$num}{'ownerauthparam'} = $text;
  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;
  340:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text); 
  341:                  } elsif ("@state" eq "class users user roles role start") {
  342:                      if (grep(/^\Q$role\E$/,@possroles)) {
  343:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
  344:                      }
  345:                  } elsif ("@state" eq "class users user roles role end") {
  346:                      if (grep(/^\Q$role\E$/,@possroles)) {
  347:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
  348:                      }
  349:                  } elsif ("@state" eq "class users user roles role usec") {
  350:                      if (grep(/^\Q$role\E$/,@possroles)) {
  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
  393: #   course domain
  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
  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
  407: #       course requests submitted via course request form.
  408: #   optional category
  409: #   optional ref to scalar for six character unique identifier
  410: #
  411: # outputs
  412: #   LON-CAPA courseID for new (created) course
  413: #
  414: #########################################################
  415: 
  416: sub build_course {
  417:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,
  418:         $enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category,$coderef) = @_;
  419:     return unless (ref($details) eq 'HASH');
  420:     my $owner_uname = $details->{'owner'};
  421:     my $owner_domain = $details->{'domain'};
  422:     my $owner = $owner_uname.':'.$owner_domain;
  423:     my $sectionstr = '';
  424:     my $xliststr = '';
  425:     my $noenddate = '';
  426:     my $outcome;
  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 {
  435:         $crstype = 'Course';
  436:         $ccrole = 'cc';
  437:         if (ref($longroles) eq 'HASH') {
  438:             $rolenames = $longroles->{'Course'};
  439:         }
  440:     }
  441:     my $linefeed;
  442:     if ($context eq 'auto') {
  443:         $linefeed = "\n";
  444:     } else {
  445:         $linefeed = "<br />\n";
  446:     }
  447:     if ($details->{'accessend'} eq '') {
  448:         $noenddate = 1;
  449:     }
  450:     my $reshome = $details->{'reshome'};
  451:     if ($reshome eq '') {
  452:         $reshome = '/res/'.$cdom;
  453:     }
  454:     my $firstres =  $details->{'firstres'};
  455:     if ($firstres eq '') {
  456:         if ($crstype eq 'Community') {
  457:             $firstres = 'nav';
  458:         } else {
  459:             $firstres = 'syl';
  460:         }
  461:     }
  462:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
  463:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
  464:     }
  465:     $sectionstr =~ s/,$//;
  466: 
  467:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
  468:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
  469:     }
  470:     $xliststr =~ s/,$//;
  471: 
  472:     my %courseinfo = (
  473:                       inst_code => $details->{'coursecode'},
  474:                       description => $details->{'title'}
  475:                      ); 
  476:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
  477:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
  478:                     'authparam' => $details->{'ownerauthparam'},
  479:                     'emailenc' => $details->{'emailenc'},
  480:                     'udom' => $details->{'domain'},
  481:                     'uname' => $details->{'owner'},
  482:                     'pid' => $details->{'users'}{$owner}{'studentID'},
  483:                     'first' => $details->{'users'}{$owner}{'firstname'},
  484:                     'middle' => $details->{'users'}{$owner}{'middlename'},
  485:                     'last' => $details->{'users'}{$owner}{'lastname'},
  486:                     'gene' => $details->{'users'}{$owner}{'generation'},
  487:                     'usec' => '',
  488:                     'end' => '',
  489:                     'start' => '',
  490:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
  491:                     'cid' => '',
  492:                     'context' => 'createowner',
  493:                     'linefeed' => $linefeed,
  494:                     'role' => $ccrole,
  495:                    };
  496:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
  497:     } else {
  498:         $outcome = 'ok';
  499:     }
  500: 
  501:     if ($outcome eq 'ok') {
  502:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
  503:             $details->{'datemode'} = 'shift';
  504:             $details->{'dateshift'} = 365;
  505:         }
  506:         my $courseargs = {
  507:                ccuname => $details->{'owner'},
  508:                ccdomain => $details->{'domain'},
  509:                cdescr => $details->{'title'},
  510:                crstype => $details->{'crstype'},
  511:                curl => $details->{'topmap'},
  512:                course_domain => $cdom,
  513:                course_home =>  $details->{'coursehome'},
  514:                nonstandard => $details->{'nonstandard'},
  515:                crscode => $details->{'coursecode'},
  516:                defaultcredits => $details->{'defaultcredits'},
  517:                crsquota => $details->{'crsquota'},
  518:                uniquecode => $details->{'uniquecode'},
  519:                clonecourse => $details->{'clonecrs'},
  520:                clonedomain => $details->{'clonedom'},
  521:                datemode => $details->{'datemode'},
  522:                dateshift => $details->{'dateshift'},
  523:                crsid => $details->{'optional_id'},
  524:                curruser => $details->{'owner'},
  525:                crssections => $sectionstr,
  526:                crsxlist => $xliststr,
  527:                autoadds => $details->{'adds'},
  528:                autodrops => $details->{'drops'},
  529:                notify => $details->{'notify_owner'},
  530:                notify_dc => $details->{'notify_dc'},
  531:                no_end_date => $noenddate,
  532:                showphotos => $details->{'showphotos'},
  533:                authtype => $details->{'authtype'},
  534:                autharg => $details->{'authparam'},
  535:                enrollstart => $details->{'enrollstart'},
  536:                enrollend => $details->{'enrollend'},
  537:                startaccess => $details->{'accessstart'},
  538:                endaccess => $details->{'accessend'},
  539:                setpolicy => $details->{'setpolicy'},
  540:                setcontent => $details->{'setcontent'},
  541:                setcomment => $details->{'setcomment'},
  542:                reshome => $reshome,
  543:                setkeys => $details->{'setkeys'},
  544:                keyauth => $details->{'keyauth'},
  545:                disresdis => $details->{'disresdis'},
  546:                disablechat => $details->{'disablechat'},
  547:                openall => $details->{'openall'},
  548:                firstres => $firstres
  549:                };
  550:         if ($details->{'textbook'}) {
  551:             $courseargs->{'textbook'} = $details->{'textbook'};
  552:         }
  553:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
  554:         if (! exists($host_servers{$details->{'coursehome'}})) {
  555:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
  556:             return;
  557:         }
  558:         my ($success, $msg) = 
  559:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
  560:                                                  $udom,$uname,$context,$cnum,$category,$coderef);
  561: 	$$logmsg .= $msg;
  562:         if (!$success) {
  563:             return;
  564:         }
  565:     } else {
  566:         return;
  567:     }
  568:     
  569: #
  570: # Make owner a coordinator
  571: #
  572:     if (($owner_domain) && ($owner_uname)) {
  573:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
  574:     }
  575: 
  576: #
  577: # Process other reqested users
  578: #
  579: 
  580:     my @courseroles = qw(st ep ta in);
  581:     push(@courseroles,$ccrole);
  582:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
  583:         push(@courseroles,'ad');
  584:     }
  585:     my $stulogmsg = '';
  586:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
  587:         my $url = '/'.$crsudom.'/'.$crsunum;
  588:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
  589:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
  590:             my ($username,$userdom) = split/:/,$userkey;
  591:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
  592:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
  593:                 my @badroles;
  594:                 my $firstrole = shift(@reqroles);
  595:                 while (@reqroles > 0) { 
  596:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  597:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  598:                             last;
  599:                         } else {
  600:                             push(@badroles,$firstrole);
  601:                             $firstrole = shift(@reqroles);
  602:                         }
  603:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
  604:                         last;
  605:                     } else {
  606:                         push(@badroles,$firstrole);
  607:                         $firstrole = shift(@reqroles);
  608:                     }
  609:                 }
  610:                 if (@badroles > 0) {
  611:                     if (@badroles > 1) {
  612:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
  613:                     } else {
  614:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
  615:                     }
  616:                 }
  617:                 my $firstsec;
  618:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
  619:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
  620:                 }
  621:                 my $userargs = {
  622:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
  623:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
  624:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
  625:                     'udom' => $userdom,
  626:                     'uname' => $username,
  627:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
  628:                     'first' => $details->{'users'}{$userkey}{'firstname'},
  629:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
  630:                     'last' => $details->{'users'}{$userkey}{'lastname'},
  631:                     'gene' => $details->{'users'}{$userkey}{'generation'},
  632:                     'usec' => $firstsec,
  633:                     'end' => $details->{'users'}{$userkey}{'end'},
  634:                     'start' => $details->{'users'}{$userkey}{'start'},
  635:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
  636:                     'cid' => $courseid,
  637:                     'crs' => $crsunum,
  638:                     'cdom' => $crsudom,
  639:                     'context' => 'createcourse',
  640:                     'linefeed' => $linefeed,
  641:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
  642:                    };
  643:                 if ($userargs->{'role'} eq 'st') {
  644:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
  645:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
  646:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
  647:                     }
  648:                 }
  649:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
  650: # now add other roles and other sections.
  651:                 if ($outcome eq 'ok') {
  652:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
  653:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
  654:                             my $curr_role = $firstrole;
  655:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  656:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  657:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
  658:                             $url = '/'.$crsudom.'/'.$crsunum;
  659:                             if ($usec ne '') {
  660:                                 $url .= '/'.$usec;
  661:                             }
  662:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  663:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  664:                             } else {
  665:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  666:                             }
  667:                         }
  668:                     }
  669:                     if (@reqroles > 0) {
  670:                         foreach my $curr_role (@reqroles) {
  671:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  672:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  673:                             if ($curr_role eq 'st') {
  674:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  675:                                 $url = '/'.$crsudom.'/'.$crsunum;
  676:                                 if ($usec ne '') {
  677:                                     $url .= '/'.$usec;
  678:                                 }
  679:                                 my $credits;
  680:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
  681:                                     $credits = $details->{'users'}{$userkey}{'credits'};
  682:                                     $credits =~ s/[^\d\.]//g;
  683:                                 }
  684:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  685:                             } elsif ($curr_role eq $ccrole) {
  686:                                 $url = '/'.$crsudom.'/'.$crsunum;
  687:                                 my $usec = '';
  688:                                 $$output .=
  689:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  690:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
  691:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  692:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  693:                                     $url = '/'.$crsudom.'/'.$crsunum;
  694:                                     if ($usec ne '') {
  695:                                         $url .= '/'.$usec;
  696:                                     }
  697:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  698:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  699:                                             $$output .= 
  700:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  701:                                         } else {
  702:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  703:                                         }
  704:                                     } else {
  705:                                         $$output .= 
  706:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  707:                                     }
  708:                                 }
  709:                             } else {
  710:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  711:                             }
  712:                         }
  713:                     }
  714:                 }
  715:             } else {
  716:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
  717:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  718:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  719:                     if ($curr_role eq 'st') {
  720:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  721:                         $url = '/'.$crsudom.'/'.$crsunum;
  722:                         if ($usec ne '') {
  723:                             $url .= '/'.$usec;
  724:                         }
  725:                         my $credits;
  726:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
  727:                             $credits = $details->{'users'}{$userkey}{'credits'};
  728:                             $credits =~ s/[^\d\.]//g;
  729:                         }
  730:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  731:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
  732:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  733:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
  734:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  735:                                 $url = '/'.$crsudom.'/'.$crsunum;
  736:                                 if ($usec ne '') {
  737:                                     $url .= '/'.$usec;
  738:                                 }
  739:                                 my $stdresult;
  740:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
  741:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  742:                                         $stdresult = 
  743:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  744:                                     } else {
  745:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  746:                                     }
  747:                                 } else {
  748:                                     $stdresult = 
  749:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  750:                                 }
  751:                                 $$output .= $stdresult;
  752:                             }
  753:                         } else {
  754:                             $url = '/'.$crsudom.'/'.$crsunum;
  755:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  756:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  757:                                     $$output .= 
  758:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  759:                                 } else {
  760:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  761:                                 }
  762:                             } else {
  763:                                 $$output .= 
  764:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
  765:                             }
  766:                         }
  767:                     } else {
  768:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  769:                     }
  770:                 }
  771:             }
  772:         }
  773:     }
  774: 
  775: # Information about keys.
  776:     if ($details->{'setkeys'}) {
  777:         $$keysmsg .=
  778:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
  779:     }
  780: # Flush the course logs so reverse user roles immediately updated
  781:     &Apache::lonnet::flushcourselogs();
  782:     return $courseid;
  783: }
  784: 
  785: sub owner_is_dc {
  786:     my ($owner_uname,$owner_dom,$cdom) = @_;
  787:     my $is_dc = 0;
  788:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
  789:                     ['active'],['dc'],[$cdom]);
  790:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
  791:         $is_dc = 1;
  792:     }
  793:     return $is_dc;
  794: }
  795: 
  796: #########################################################
  797: #
  798: # process_date()
  799: # 
  800: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
  801: # output - corresponding UNIX time (seconds since epoch). 
  802: #
  803: #########################################################
  804: 
  805: sub process_date {
  806:     my $timestr = shift;
  807:     my $timestamp = '';
  808:     if ($timestr !~ /:/) {
  809:         $timestamp = '';
  810:     } else {
  811:         my @entries = split(/:/,$timestr);
  812:         for (my $j=0; $j<@entries; $j++) {
  813:             if ( length($entries[$j]) > 1 ) {
  814:                 $entries[$j] =~ s/^0//;
  815:             }
  816:             $entries[$j] =~ s/\D//g;
  817:             if ($entries[$j] < 0) {
  818:                 $entries[$j] = 0;
  819:             }
  820:         }
  821:         if ($entries[1] > 0) {  
  822:             $entries[1] = $entries[1] - 1;
  823:         }
  824:         if ($entries[5] > 60) {
  825:             $entries[5] = 60;
  826:         }
  827:         if ($entries[4] > 59) {
  828:             $entries[4] = 59;
  829:         }
  830:         if ($entries[3] > 23) {
  831:             $entries[3] = 23;
  832:         }
  833:         if ($entries[2] > 31) {
  834:             $entries[2] = 31;
  835:         }
  836:         if ($entries[1] > 11) {
  837:             $entries[1] = 11;
  838:         }
  839:         if ($entries[2] == 31) {
  840:             if (($entries[1] == 3) || ($entries[1] == 5) ||
  841:                 ($entries[1] == 8) || ($entries[1] == 10)) {
  842:                 $entries[2] = 30;
  843:             }
  844:         }
  845:         if ($entries[1] == 1) {
  846:             if ($entries[2] > 29) {
  847:                 $entries[2] = 29;
  848:             }
  849:             if ($entries[2] == 29) {
  850:                 if ($entries[0]%4 != 0) {
  851:                     $entries[2] = 28;
  852:                 } elsif ( $entries[0]%100 == 0
  853: 			  && $entries[0]%400 != 0) {
  854: 		    $entries[2] = 28;
  855: 		}
  856:             }
  857:         }     
  858:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
  859:     }
  860:     return $timestamp;
  861: }
  862: 
  863: 1;

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