File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.38: download - view: text, annotated - select for diffs
Fri Mar 1 04:49:15 2013 UTC (11 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC1, HEAD
- Course credits for a particular institutional code can be provided as a
  third item returned by localenroll::validate_instcode()
- Course credits can be specified for an official course (or retrieved from
  localenroll::validate_instcode) or an unofficial course as part of a course
  request
- Credits earned for a course can also be different for a specific student
  (e.g., retrieved from institutional classlist data), or specified when
  CC adds a student to a course via the web GUI.

    1: #
    2: # $Id: batchcreatecourse.pm,v 1.38 2013/03/01 04:49:15 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: # <clonecrs>466011437c34194msul1</clonecrs>
   79: # <clonedom>msu</clonedom>
   80: # <datemode>shift</datemode>
   81: # <dateshift>365</dateshift>
   82: # <showphotos></showphotos>
   83: # <setpolicy>1</setpolicy>
   84: # <setcontent>1</setcontent>
   85: # <setkeys>0</setkeys>
   86: # <keyauth>keyadmin:msu</keyauth>
   87: # <disresdis>1</disresdis>
   88: # <disablechat>1</disablechat>
   89: # <openall></openall>
   90: # <notify_dc>1</notify_dc>
   91: # <notify_owner>1</notify_owner>
   92: # <owner>
   93: #  <username>sparty</username>
   94: #  <domain>msu</domain>
   95: #  <authtype>krb4</authtype>
   96: #  <autharg>MSU.EDU</autharg>
   97: # </owner>
   98: # <sections>
   99: #  <section>
  100: #   <inst>001</inst>
  101: #   <loncapa>1</loncapa>
  102: #  </section>
  103: #  <section>
  104: #   <inst>002</inst>
  105: #   <loncapa>2</loncapa>
  106: #  </section>
  107: # </sections>
  108: # <crosslists>
  109: #  <xlist>
  110: #   <inst>ss05zzz101001</inst>
  111: #   <loncapa>1</loncapa>
  112: #  </xlist>
  113: # </crosslists>
  114: # <users>
  115: #  <user>
  116: #   <username>sparty</username>
  117: #   <domain>msu</domain>
  118: #   <email>sparty@msu.edu</email>
  119: #   <authtype>krb4</authtype>
  120: #   <autharg></autharg>
  121: #   <firstname>MSU</firstname>
  122: #   <generation></generation>
  123: #   <lastname>Spartan</lastname>x
  124: #   <middlename></middlename>
  125: #   <studentID></studentID>
  126: #   <credits></credits>
  127: #   <roles></roles>
  128: #  </user>
  129: #  <user>
  130: #   <username>itds0001</username>
  131: #   <domain>northwood5</domain>
  132: #   <email>itds0001@msu.edu</email>
  133: #   <authtype>int</authtype>
  134: #   <autharg></autharg>
  135: #   <firstname>Info</firstname>
  136: #   <generation></generation>
  137: #   <lastname>Techc</lastname>x
  138: #   <middlename></middlename>
  139: #   <studentID></studentID>
  140: #   <roles>
  141: #    <role id='in'>
  142: #     <start>2005:01:01:12:10</start>
  143: #     <end>2005:12:01:12:10</end>
  144: #     <usec>1</usec>
  145: #     <usec>2</usec>
  146: #    </role>
  147: #   </roles>
  148: #  </user>
  149: # </users>
  150: #</class>
  151: #
  152: # Many of these are binary options (corresponding to either checkboxes or
  153: # radio buttons in the interactive CCRS page).  Examples include:
  154: # setpolicy, setcontent, setkeys, disableresdis, disablechat, openall
  155: #
  156: # A value of 1 between opening and closing tags is equivalent to a 
  157: # checked checkbox or 'Yes' response in the original CCRS web page.
  158: # A value of 0 or blank is equivalent to an unchecked box or 'No'
  159: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
  160: #
  161: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
  162: # no entry respectively.
  163: # 
  164: # crstype can be Course or Community
  165: #
  166: # crsquota is the total disk space permitted for course group portfolio files
  167: # in all course groups.
  168: # 
  169: # For format of other parameters, refer to the interactive CCRS page
  170: # and view how the equivalent parameter is displayed in the web form.  
  171: #  
  172: ##############################################################
  173: # create_courses() - creates courses described in @$requests,
  174: #                    files listed in @$requests are deleted
  175: #                    after the files have been parsed.
  176: #
  177: #                    Directory for retrieval of files listed in @$requests is: 
  178: #                    /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
  179: #                    /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
  180: #                    
  181: # inputs (five)  -  requests - ref to array of filename(s) containing course requests 
  182: #                   courseids - ref to hash to store LON-CAPA course ids of new courses 
  183: #                   context - auto if called from command line, web if called from browser
  184: #                   dom - domain for which the course is being created
  185: #                   uname - username of DC who is requesting course creation
  186: #                   udom - domain of DC who is requesting course creation
  187: #  
  188: # outputs (three)  -  output - text recording user roles added etc.
  189: #                     logmsg - text to be logged
  190: #                     keysmsg - text containing link(s) to manage keys page(s) 
  191: #############################################################
  192: 
  193: sub create_courses {
  194:     my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
  195:     my $output;
  196:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
  197: # Get role names
  198:     my %longroles = ();
  199:     open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
  200:     my @rolesplain = <FILE>;
  201:     close(FILE);
  202:     foreach my $item (@rolesplain) {
  203:         if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
  204:             $longroles{'Course'}{$1} = $2;
  205:             $longroles{'Community'}{$1} = $3;
  206:         }
  207:     }
  208:     my ($logmsg,$keysmsg,$newusermsg,$addresult);
  209:     my %enrollcount = ();
  210:     my $newcoursedir = LONCAPA::tempdir().'/addcourse/'.$dom.'/'.$context;
  211:     if ($context eq 'auto') {
  212:         $newcoursedir .= '/pending';
  213:     } else {
  214:         if ($uname && $udom) {
  215:             $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
  216:         } else {
  217:             $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
  218:         }
  219:     }
  220:     if (@{$requests} > 0) {
  221:         foreach my $request (@{$requests}) {
  222:             my %details = ();
  223:             if (-e $newcoursedir.'/'.$request) {
  224:                 &parse_coursereqs($newcoursedir.'/'.$request, \%details);
  225:                 foreach my $num (sort(keys(%details))) {
  226:                     my $reqdetails = $details{$num};
  227:                     my $courseid = &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
  228:                     if ($courseid =~m{^/$match_domain/$match_courseid}) {
  229:                         $$courseids{$courseid} = $details{$num}{'class'};
  230:                     }
  231:                 }
  232:             }
  233:         }
  234:     }
  235:     return ($output,$logmsg,$keysmsg);
  236: }
  237: 
  238: #############################################################
  239: #
  240: # parse_coursereqs() 
  241: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
  242: #              - details - reference to hash containing extracted information.
  243: # outputs (none)
  244: #
  245: ############################################################
  246: 
  247: sub parse_coursereqs {
  248:     my ($coursefile,$details) = @_;
  249: #   Note all start and end dates should be in this format:
  250: #   YYYY:MM:DD:HH:MM:SS (:separators required).
  251:     my $uname = '';
  252:     my @state = ();
  253:     my $num = 0;
  254:     my $secid = 0;
  255:     my $xlist = 0;
  256:     my $userkey = '';
  257:     my $role = '';
  258:     my @items = ('title','optional_id','coursecode','defaultcredits','coursehome','reshome','nonstandard','adds','drops','topmap','firstres','clonecrs','clonedom','datemode','dateshift','showphotos','setpolicy','setcontent','setkeys','keyauth','disresdis','disablechat','openall','notify_owner','notify_dc','crstype','crsquota');
  259:     my @possroles = qw(st ad ep ta in cc co);
  260:     my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
  261:     my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
  262:     my $p = HTML::Parser->new
  263:     (
  264:         xml_mode => 1,
  265:         start_h =>
  266:             [sub {
  267:                  my ($tagname, $attr) = @_;
  268:                  push(@state, $tagname);
  269:                  if ("@state" eq "class") {
  270:                      %{$$details{$num}} = ();
  271:                      $$details{$num}{'class'} = $attr->{id};
  272:                      %{$$details{$num}{'users'}} = ();
  273:                      %{$$details{$num}{'sections'}} = ();
  274:                      $secid = 0;
  275:                      $xlist = 0;
  276:                  }
  277:                  if ("@state" eq "class users user roles role") {
  278:                      $role = $attr->{id};
  279:                      if (grep(/^\Q$role\E$/,@possroles)) {
  280:                          push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);  
  281:                          %{$$details{$num}{'users'}{$userkey}{$role}} = ();
  282:                          @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
  283:                      }
  284:                  }
  285:                  if ("@state" eq "class sections section") {
  286:                      $secid ++;
  287:                      %{$$details{$num}{'sections'}{$secid}} = ();
  288:                  }
  289:                  if ("@state" eq "class crosslists xlist") {
  290:                      $xlist ++;
  291:                      %{$$details{$num}{'crosslists'}{$xlist}} = ();
  292:                  }
  293:             }, "tagname, attr"],
  294:          text_h =>
  295:              [sub {
  296:                  my ($text) = @_;
  297:                  if ("@state" eq "class owner username") {
  298:                      $$details{$num}{'owner'} = $text;
  299:                  } elsif ("@state" eq "class owner domain") {
  300:                     $$details{$num}{'domain'} = $text;
  301:                  } elsif ("@state" eq "class sections section inst") {
  302:                     $$details{$num}{'sections'}{$secid}{'inst'} = $text;
  303:                  } elsif ("@state" eq "class sections section loncapa") {
  304:                     $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
  305:                  } elsif ("@state" eq "class crosslists xlist inst") {
  306:                     $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
  307:                  } elsif ("@state" eq "class crosslists xlist loncapa") {
  308:                     $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
  309:                  } elsif ("@state" eq "class owner authtype") {
  310:                     $$details{$num}{'ownerauthtype'} = $text;
  311:                  } elsif ("@state" eq "class owner autharg") {
  312:                     $$details{$num}{'ownerauthparam'} = $text;
  313:                  } elsif ("@state" eq "class authentication method") {
  314:                     $$details{$num}{'authtype'} = $text;
  315:                  } elsif ("@state" eq "class authentication param") {
  316:                     $$details{$num}{'authparam'} = $text;
  317:                  } elsif ("@state" eq "class users user username") {
  318:                     $userkey = $text;
  319:                  } elsif ("@state" eq "class users user domain") {
  320:                     $userkey .= ':'.$text;
  321:                     %{$$details{$num}{'users'}{$userkey}} = ();
  322:                     @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
  323:                  } elsif ("@state" eq "class users user email") {
  324:                     $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
  325:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text); 
  326:                  } elsif ("@state" eq "class users user roles role start") {
  327:                      if (grep(/^\Q$role\E$/,@possroles)) {
  328:                          $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
  329:                      }
  330:                  } elsif ("@state" eq "class users user roles role end") {
  331:                      if (grep(/^\Q$role\E$/,@possroles)) {
  332:                          $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
  333:                      }
  334:                  } elsif ("@state" eq "class users user roles role usec") {
  335:                      if (grep(/^\Q$role\E$/,@possroles)) {
  336:                          unless ($text eq '') {
  337:                              push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
  338:                          }
  339:                      }
  340:                  } else {
  341:                      foreach my $item (@items) {
  342:                          if ("@state" eq "class $item") {
  343:                              $$details{$num}{$item} = $text;
  344:                          }
  345:                      }
  346:                      foreach my $item (@dateitems) {
  347:                          if ("@state" eq "class $item") {
  348:                              $$details{$num}{$item} = &process_date($text);
  349:                          }
  350:                      }
  351:                      foreach my $item (@useritems) {
  352:                          if ("@state" eq "class users user $item") {
  353:                              $$details{$num}{'users'}{$userkey}{$item} = $text;
  354:                          }
  355:                      }
  356:                  }
  357:                }, "dtext"],
  358:          end_h =>
  359:                [sub {
  360:                    my ($tagname) = @_;
  361:                    if ("@state" eq "class") {
  362:                        $num ++;
  363:                    }
  364:                    pop @state;
  365:                 }, "tagname"],
  366:     );
  367: 
  368:     $p->parse_file($coursefile);
  369:     $p->eof;
  370:     return;
  371: }
  372: 
  373: #########################################################
  374: #
  375: # build_course() 
  376: #
  377: # inputs
  378: #   course domain
  379: #   course request number
  380: #   context - auto if called from command line, web if called from DC web interface
  381: #   ref to hash of course creation information
  382: #   ref to hash of role descriptions
  383: #   ref to scalar used to accumulate log messages
  384: #   ref to scalar used to accumulate messages sent to new users
  385: #   ref to scalar used to accumulate results of new user additions
  386: #   ref to hash of enrollment counts for different roles
  387: #   ref to scalar used to accumulate information about added roles
  388: #   ref to scalar used to accumulate
  389: #   ref to scalar used to accumulate information about access keys
  390: #   domain of DC creating course
  391: #   username of DC creating course   
  392: #   optional course number, if unique course number already obtained (e.g., for
  393: #       course requests submitted via course request form. 
  394: #
  395: # outputs
  396: #   LON-CAPA courseID for new (created) course
  397: #
  398: #########################################################
  399: 
  400: sub build_course {
  401:     my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category) = @_;
  402:     return unless (ref($details) eq 'HASH');
  403:     my $owner_uname = $details->{'owner'};
  404:     my $owner_domain = $details->{'domain'};
  405:     my $owner = $owner_uname.':'.$owner_domain;
  406:     my $sectionstr = '';
  407:     my $xliststr = '';
  408:     my $noenddate = '';
  409:     my $outcome;
  410:     my ($courseid,$crsudom,$crsunum,$crstype,$ccrole,$rolenames);
  411:     if ($details->{'crstype'} eq 'Community') {
  412:         $crstype = $details->{'crstype'};
  413:         $ccrole ='co';
  414:         if (ref($longroles) eq 'HASH') {
  415:             $rolenames = $longroles->{'Community'};
  416:         }
  417:     } else {
  418:         $crstype = 'Course';
  419:         $ccrole = 'cc';
  420:         if (ref($longroles) eq 'HASH') {
  421:             $rolenames = $longroles->{'Course'};
  422:         }
  423:     }
  424:     my $linefeed;
  425:     if ($context eq 'auto') {
  426:         $linefeed = "\n";
  427:     } else {
  428:         $linefeed = "<br />\n";
  429:     }
  430:     if ($details->{'accessend'} eq '') {
  431:         $noenddate = 1;
  432:     }
  433:     my $reshome = $details->{'reshome'};
  434:     if ($reshome eq '') {
  435:         $reshome = '/res/'.$cdom;
  436:     }
  437:     my $firstres =  $details->{'firstres'};
  438:     if ($firstres eq '') {
  439:         if ($crstype eq 'Community') {
  440:             $firstres = 'nav';
  441:         } else {
  442:             $firstres = 'syl';
  443:         }
  444:     }
  445:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
  446:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
  447:     }
  448:     $sectionstr =~ s/,$//;
  449: 
  450:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
  451:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
  452:     }
  453:     $xliststr =~ s/,$//;
  454: 
  455:     my %courseinfo = (
  456:                       inst_code => $details->{'coursecode'},
  457:                       description => $details->{'title'}
  458:                      ); 
  459:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
  460:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
  461:                     'authparam' => $details->{'ownerauthparam'},
  462:                     'emailenc' => $details->{'emailenc'},
  463:                     'udom' => $details->{'domain'},
  464:                     'uname' => $details->{'owner'},
  465:                     'pid' => $details->{'users'}{$owner}{'studentID'},
  466:                     'first' => $details->{'users'}{$owner}{'firstname'},
  467:                     'middle' => $details->{'users'}{$owner}{'middlename'},
  468:                     'last' => $details->{'users'}{$owner}{'lastname'},
  469:                     'gene' => $details->{'users'}{$owner}{'generation'},
  470:                     'usec' => '',
  471:                     'end' => '',
  472:                     'start' => '',
  473:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
  474:                     'cid' => '',
  475:                     'context' => 'createowner',
  476:                     'linefeed' => $linefeed,
  477:                     'role' => $ccrole,
  478:                    };
  479:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
  480:     } else {
  481:         $outcome = 'ok';
  482:     }
  483: 
  484:     if ($outcome eq 'ok') {
  485:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
  486:             $details->{'datemode'} = 'shift';
  487:             $details->{'dateshift'} = 365;
  488:         }
  489:         my $courseargs = {
  490:                ccuname => $details->{'owner'},
  491:                ccdomain => $details->{'domain'},
  492:                cdescr => $details->{'title'},
  493:                crstype => $details->{'crstype'},
  494:                curl => $details->{'topmap'},
  495:                course_domain => $cdom,
  496:                course_home =>  $details->{'coursehome'},
  497:                nonstandard => $details->{'nonstandard'},
  498:                crscode => $details->{'coursecode'},
  499:                defaultcredits => $details->{'defaultcredits'},
  500:                crsquota => $details->{'crsquota'},
  501:                clonecourse => $details->{'clonecrs'},
  502:                clonedomain => $details->{'clonedom'},
  503:                datemode => $details->{'datemode'},
  504:                dateshift => $details->{'dateshift'},
  505:                crsid => $details->{'optional_id'},
  506:                curruser => $details->{'owner'},
  507:                crssections => $sectionstr,
  508:                crsxlist => $xliststr,
  509:                autoadds => $details->{'adds'},
  510:                autodrops => $details->{'drops'},
  511:                notify => $details->{'notify_owner'},
  512:                notify_dc => $details->{'notify_dc'},
  513:                no_end_date => $noenddate,
  514:                showphotos => $details->{'showphotos'},
  515:                authtype => $details->{'authtype'},
  516:                autharg => $details->{'authparam'},
  517:                enrollstart => $details->{'enrollstart'},
  518:                enrollend => $details->{'enrollend'},
  519:                startaccess => $details->{'accessstart'},
  520:                endaccess => $details->{'accessend'},
  521:                setpolicy => $details->{'setpolicy'},
  522:                setcontent => $details->{'setcontent'},
  523:                reshome => $reshome,
  524:                setkeys => $details->{'setkeys'},
  525:                keyauth => $details->{'keyauth'},
  526:                disresdis => $details->{'disresdis'},
  527:                disablechat => $details->{'disablechat'},
  528:                openall => $details->{'openall'},
  529:                firstres => $firstres
  530:                };
  531:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
  532:         if (! exists($host_servers{$details->{'coursehome'}})) {
  533:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
  534:             return;
  535:         }
  536:         my ($success, $msg) = &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname,$context,$cnum,$category);
  537: 	$$logmsg .= $msg;
  538:         if (!$success) {
  539:             return;
  540:         }
  541:     } else {
  542:         return;
  543:     }
  544:     
  545: #
  546: # Make owner a coordinator
  547: #
  548:     if (($owner_domain) && ($owner_uname)) {
  549:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
  550:     }
  551: 
  552: #
  553: # Process other reqested users
  554: #
  555: 
  556:     my @courseroles = qw(st ep ta in);
  557:     push(@courseroles,$ccrole);
  558:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
  559:         push(@courseroles,'ad');
  560:     }
  561:     my $stulogmsg = '';
  562:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
  563:         my $url = '/'.$crsudom.'/'.$crsunum;
  564:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
  565:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
  566:             my ($username,$userdom) = split/:/,$userkey;
  567:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
  568:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
  569:                 my @badroles;
  570:                 my $firstrole = shift(@reqroles);
  571:                 while (@reqroles > 0) { 
  572:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  573:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  574:                             last;
  575:                         } else {
  576:                             push(@badroles,$firstrole);
  577:                             $firstrole = shift(@reqroles);
  578:                         }
  579:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
  580:                         last;
  581:                     } else {
  582:                         push(@badroles,$firstrole);
  583:                         $firstrole = shift(@reqroles);
  584:                     }
  585:                 }
  586:                 if (@badroles > 0) {
  587:                     if (@badroles > 1) {
  588:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
  589:                     } else {
  590:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
  591:                     }
  592:                 }
  593:                 my $firstsec;
  594:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
  595:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
  596:                 }
  597:                 my $userargs = {
  598:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
  599:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
  600:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
  601:                     'udom' => $userdom,
  602:                     'uname' => $username,
  603:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
  604:                     'first' => $details->{'users'}{$userkey}{'firstname'},
  605:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
  606:                     'last' => $details->{'users'}{$userkey}{'lastname'},
  607:                     'gene' => $details->{'users'}{$userkey}{'generation'},
  608:                     'usec' => $firstsec,
  609:                     'end' => $details->{'users'}{$userkey}{'end'},
  610:                     'start' => $details->{'users'}{$userkey}{'start'},
  611:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
  612:                     'cid' => $courseid,
  613:                     'crs' => $crsunum,
  614:                     'cdom' => $crsudom,
  615:                     'context' => 'createcourse',
  616:                     'linefeed' => $linefeed,
  617:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
  618:                    };
  619:                 if ($userargs->{'role'} eq 'st') {
  620:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
  621:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
  622:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
  623:                     }
  624:                 }
  625:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
  626: # now add other roles and other sections.
  627:                 if ($outcome eq 'ok') {
  628:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
  629:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
  630:                             my $curr_role = $firstrole;
  631:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  632:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  633:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
  634:                             $url = '/'.$crsudom.'/'.$crsunum;
  635:                             if ($usec ne '') {
  636:                                 $url .= '/'.$usec;
  637:                             }
  638:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  639:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  640:                             } else {
  641:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  642:                             }
  643:                         }
  644:                     }
  645:                     if (@reqroles > 0) {
  646:                         foreach my $curr_role (@reqroles) {
  647:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  648:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  649:                             if ($curr_role eq 'st') {
  650:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  651:                                 $url = '/'.$crsudom.'/'.$crsunum;
  652:                                 if ($usec ne '') {
  653:                                     $url .= '/'.$usec;
  654:                                 }
  655:                                 my $credits;
  656:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
  657:                                     $credits = $details->{'users'}{$userkey}{'credits'};
  658:                                     $credits =~ s/[^\d\.]//g;
  659:                                 }
  660:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  661:                             } elsif ($curr_role eq $ccrole) {
  662:                                 $url = '/'.$crsudom.'/'.$crsunum;
  663:                                 my $usec = '';
  664:                                 $$output .=
  665:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  666:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
  667:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  668:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  669:                                     $url = '/'.$crsudom.'/'.$crsunum;
  670:                                     if ($usec ne '') {
  671:                                         $url .= '/'.$usec;
  672:                                     }
  673:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  674:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  675:                                             $$output .= 
  676:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  677:                                         } else {
  678:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  679:                                         }
  680:                                     } else {
  681:                                         $$output .= 
  682:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  683:                                     }
  684:                                 }
  685:                             } else {
  686:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  687:                             }
  688:                         }
  689:                     }
  690:                 }
  691:             } else {
  692:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
  693:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  694:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  695:                     if ($curr_role eq 'st') {
  696:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  697:                         $url = '/'.$crsudom.'/'.$crsunum;
  698:                         if ($usec ne '') {
  699:                             $url .= '/'.$usec;
  700:                         }
  701:                         my $credits;
  702:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
  703:                             $credits = $details->{'users'}{$userkey}{'credits'};
  704:                             $credits =~ s/[^\d\.]//g;
  705:                         }
  706:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  707:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
  708:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  709:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
  710:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  711:                                 $url = '/'.$crsudom.'/'.$crsunum;
  712:                                 if ($usec ne '') {
  713:                                     $url .= '/'.$usec;
  714:                                 }
  715:                                 my $stdresult;
  716:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
  717:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  718:                                         $stdresult = 
  719:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  720:                                     } else {
  721:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  722:                                     }
  723:                                 } else {
  724:                                     $stdresult = 
  725:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  726:                                 }
  727:                                 $$output .= $stdresult;
  728:                             }
  729:                         } else {
  730:                             $url = '/'.$crsudom.'/'.$crsunum;
  731:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  732:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  733:                                     $$output .= 
  734:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  735:                                 } else {
  736:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  737:                                 }
  738:                             } else {
  739:                                 $$output .= 
  740:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
  741:                             }
  742:                         }
  743:                     } else {
  744:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  745:                     }
  746:                 }
  747:             }
  748:         }
  749:     }
  750: 
  751: # Information about keys.
  752:     if ($details->{'setkeys'}) {
  753:         $$keysmsg .=
  754:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
  755:     }
  756: # Flush the course logs so reverse user roles immediately updated
  757:     &Apache::lonnet::flushcourselogs();
  758:     return $courseid;
  759: }
  760: 
  761: sub owner_is_dc {
  762:     my ($owner_uname,$owner_dom,$cdom) = @_;
  763:     my $is_dc = 0;
  764:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
  765:                     ['active'],['dc'],[$cdom]);
  766:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
  767:         $is_dc = 1;
  768:     }
  769:     return $is_dc;
  770: }
  771: 
  772: #########################################################
  773: #
  774: # process_date()
  775: # 
  776: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
  777: # output - corresponding UNIX time (seconds since epoch). 
  778: #
  779: #########################################################
  780: 
  781: sub process_date {
  782:     my $timestr = shift;
  783:     my $timestamp = '';
  784:     if ($timestr !~ /:/) {
  785:         $timestamp = '';
  786:     } else {
  787:         my @entries = split(/:/,$timestr);
  788:         for (my $j=0; $j<@entries; $j++) {
  789:             if ( length($entries[$j]) > 1 ) {
  790:                 $entries[$j] =~ s/^0//;
  791:             }
  792:             $entries[$j] =~ s/\D//g;
  793:             if ($entries[$j] < 0) {
  794:                 $entries[$j] = 0;
  795:             }
  796:         }
  797:         if ($entries[1] > 0) {  
  798:             $entries[1] = $entries[1] - 1;
  799:         }
  800:         if ($entries[5] > 60) {
  801:             $entries[5] = 60;
  802:         }
  803:         if ($entries[4] > 59) {
  804:             $entries[4] = 59;
  805:         }
  806:         if ($entries[3] > 23) {
  807:             $entries[3] = 23;
  808:         }
  809:         if ($entries[2] > 31) {
  810:             $entries[2] = 31;
  811:         }
  812:         if ($entries[1] > 11) {
  813:             $entries[1] = 11;
  814:         }
  815:         if ($entries[2] == 31) {
  816:             if (($entries[1] == 3) || ($entries[1] == 5) ||
  817:                 ($entries[1] == 8) || ($entries[1] == 10)) {
  818:                 $entries[2] = 30;
  819:             }
  820:         }
  821:         if ($entries[1] == 1) {
  822:             if ($entries[2] > 29) {
  823:                 $entries[2] = 29;
  824:             }
  825:             if ($entries[2] == 29) {
  826:                 if ($entries[0]%4 != 0) {
  827:                     $entries[2] = 28;
  828:                 } elsif ( $entries[0]%100 == 0
  829: 			  && $entries[0]%400 != 0) {
  830: 		    $entries[2] = 28;
  831: 		}
  832:             }
  833:         }     
  834:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
  835:     }
  836:     return $timestamp;
  837: }
  838: 
  839: 1;

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