File:  [LON-CAPA] / loncom / interface / loncreatecourse.pm
Revision 1.80: download - view: text, annotated - select for diffs
Fri Jul 8 14:27:14 2005 UTC (18 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #3695: clone hist files, too

    1: # The LearningOnline Network
    2: # Create a course
    3: #
    4: # $Id: loncreatecourse.pm,v 1.80 2005/07/08 14:27:14 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::loncreatecourse;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http);
   34: use Apache::lonnet;
   35: use Apache::loncommon;
   36: use Apache::lonratedt;
   37: use Apache::londocs;
   38: use Apache::lonlocal;
   39: use Apache::londropadd;
   40: use lib '/home/httpd/lib/perl';
   41: 
   42: # ================================================ Get course directory listing
   43: 
   44: my @output=();
   45: 
   46: sub crsdirlist {
   47:     my ($courseid,$which)=@_;
   48:     @output=();
   49:     return &innercrsdirlist($courseid,$which);
   50: }
   51: 
   52: sub innercrsdirlist {
   53:     my ($courseid,$which,$path)=@_;
   54:     my $dirptr=16384;
   55:     unless ($which) { $which=''; } else { $which.='/'; }
   56:     unless ($path)  { $path=''; } else { $path.='/'; }
   57:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   58:     my @listing=&Apache::lonnet::dirlist
   59: 	($which,$crsdata{'domain'},$crsdata{'num'},
   60: 	 &Apache::loncommon::propath($crsdata{'domain'},$crsdata{'num'}));
   61:     foreach (@listing) {
   62: 	unless ($_=~/^\./) {
   63: 	    my @unpackline = split (/\&/,$_);
   64: 	    if ($unpackline[3]&$dirptr) {
   65: # is a directory, recurse
   66: 		&innercrsdirlist($courseid,$which.$unpackline[0],
   67: 				            $path.$unpackline[0]);
   68: 	    } else { 
   69: # is a file, put into output
   70: 		push (@output,$path.$unpackline[0]);
   71: 	    }
   72: 	}
   73:     }
   74:     return @output;
   75: }
   76: 
   77: # ============================================================= Read a userfile
   78: 
   79: sub readfile {
   80:     my ($courseid,$which)=@_;
   81:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   82:     return &Apache::lonnet::getfile('/uploaded/'.$crsdata{'domain'}.'/'.
   83: 				    $crsdata{'num'}.'/'.$which);
   84: }
   85: 
   86: # ============================================================ Write a userfile
   87: 
   88: sub writefile {
   89:     (my $courseid, my $which,$env{'form.output'})=@_;
   90:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   91:     return &Apache::lonnet::finishuserfileupload(
   92: 					  $crsdata{'num'},$crsdata{'domain'},
   93: 					  'output',$which);
   94: }
   95: 
   96: # ===================================================================== Rewrite
   97: 
   98: sub rewritefile {
   99:     my ($contents,%rewritehash)=@_;
  100:     foreach (keys %rewritehash) {
  101: 	my $pattern=$_;
  102: 	$pattern=~s/(\W)/\\$1/gs;
  103: 	my $new=$rewritehash{$_};
  104: 	$contents=~s/$pattern/$new/gs;
  105:     }
  106:     return $contents;
  107: }
  108: 
  109: # ============================================================= Copy a userfile
  110: 
  111: sub copyfile {
  112:     my ($origcrsid,$newcrsid,$which)=@_;
  113:     unless ($which=~/\.sequence$/) {
  114: 	return &writefile($newcrsid,$which,
  115: 		      &readfile($origcrsid,$which));
  116:     } else {
  117: 	my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  118: 	my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  119: 	return &writefile($newcrsid,$which,
  120: 		 &rewritefile(
  121:                      &readfile($origcrsid,$which),
  122: 	    (
  123:        '/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
  124:     => '/uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/',
  125:        '/public/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
  126:     => '/public/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/'
  127:             )));
  128:     }
  129: }
  130: 
  131: # =============================================================== Copy a dbfile
  132: 
  133: sub copydb {
  134:     my ($origcrsid,$newcrsid,$which)=@_;
  135:     $which=~s/\.db$//;
  136:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  137:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  138:     my %data=&Apache::lonnet::dump
  139: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
  140:     foreach my $key (keys(%data)) {
  141: 	if ($key=~/^internal./) { delete($data{$key}); }
  142:     }
  143:     return &Apache::lonnet::put
  144: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
  145: }
  146: 
  147: # ========================================================== Copy resourcesdata
  148: 
  149: sub copyresourcedb {
  150:     my ($origcrsid,$newcrsid)=@_;
  151:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  152:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  153:     my %data=&Apache::lonnet::dump
  154: 	('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
  155:     $origcrsid=~s/^\///;
  156:     $origcrsid=~s/\//\_/;
  157:     $newcrsid=~s/^\///;
  158:     $newcrsid=~s/\//\_/;
  159:     my %newdata=();
  160:     undef %newdata;
  161:     my $startdate=$data{$origcrsid.'.0.opendate'};
  162:     my $today=time;
  163:     my $delta=0;
  164:     if ($startdate) {
  165: 	my $oneday=60*60*24;
  166: 	$delta=$today-$startdate;
  167: 	$delta=int($delta/$oneday)*$oneday;
  168:     }
  169: # ugly retro fix for broken version of types
  170:     foreach (keys %data) {
  171: 	if ($_=~/\wtype$/) {
  172: 	    my $newkey=$_;
  173: 	    $newkey=~s/type$/\.type/;
  174: 	    $data{$newkey}=$data{$_};
  175: 	    delete $data{$_};
  176: 	}
  177:     }
  178: # adjust symbs
  179:     my $pattern='uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/';
  180:     $pattern=~s/(\W)/\\$1/gs;
  181:     my $new=    'uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/';
  182:     foreach (keys %data) {
  183: 	if ($_=~/$pattern/) {
  184: 	    my $newkey=$_;
  185: 	    $newkey=~s/$pattern/$new/;
  186: 	    $data{$newkey}=$data{$_};
  187: 	    delete $data{$_};
  188: 	}
  189:     }
  190: # adjust dates
  191:     foreach (keys %data) {
  192: 	my $thiskey=$_;
  193: 	$thiskey=~s/^$origcrsid/$newcrsid/;
  194: 	$newdata{$thiskey}=$data{$_};
  195: 	if ($data{$_.'.type'}=~/^date_(start|end)$/) {
  196: 	    $newdata{$thiskey}=$newdata{$thiskey}+$delta;
  197: 	}
  198:     }
  199:     return &Apache::lonnet::put
  200: 	('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
  201: }
  202: 
  203: # ========================================================== Copy all userfiles
  204: 
  205: sub copyuserfiles {
  206:     my ($origcrsid,$newcrsid)=@_;
  207:     foreach (&crsdirlist($origcrsid,'userfiles')) {
  208: 	if ($_ !~m|^scantron_|) {
  209: 	    &copyfile($origcrsid,$newcrsid,$_);
  210: 	}
  211:     }
  212: }
  213: # ========================================================== Copy all userfiles
  214: 
  215: sub copydbfiles {
  216:     my ($origcrsid,$newcrsid)=@_;
  217:     foreach (&crsdirlist($origcrsid)) {
  218: 	if ($_=~/\.db$/) {
  219: 	    unless 
  220:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata)/) {
  221: 		 &copydb($origcrsid,$newcrsid,$_);
  222: 		 my $histfile=$_;
  223: 		 $histfile=~s/\.db$/\.hist/;
  224: 		 &copyfile($origcrsid,$newcrsid,$histfile);
  225: 	     }
  226: 	}
  227:     }
  228: }
  229: 
  230: # ======================================================= Copy all course files
  231: 
  232: sub copycoursefiles {
  233:     my ($origcrsid,$newcrsid)=@_;
  234:     &copyuserfiles($origcrsid,$newcrsid);
  235:     &copydbfiles($origcrsid,$newcrsid);
  236:     &copyresourcedb($origcrsid,$newcrsid);
  237: }
  238: 
  239: # ===================================================== Phase one: fill-in form
  240: 
  241: sub print_course_creation_page {
  242:     my $r=shift;
  243:     my $defdom=$env{'request.role.domain'};
  244:     my %host_servers = &Apache::loncommon::get_library_servers($defdom);
  245:     my $course_home = '<select name="course_home" size="1">'."\n";
  246:     foreach my $server (sort(keys(%host_servers))) {
  247:         $course_home .= qq{<option value="$server"};
  248:         if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
  249:             $course_home .= " selected ";
  250:         }
  251:         $course_home .= qq{>$server $host_servers{$server}</option>};
  252:     }
  253:     $course_home .= "\n</select>\n";
  254:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  255:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  256:     my $helplink=&Apache::loncommon::help_open_topic('Create_Course',&mt('Help on Creating Courses'));
  257:     my $cloneform=&Apache::loncommon::select_dom_form
  258: 	($env{'request.role.domain'},'clonedomain').
  259: 		     &Apache::loncommon::selectcourse_link
  260: 		     ('ccrs','clonecourse','clonedomain');
  261:     my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  262:     my $starttime = time;
  263:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
  264:     my $enroll_table = &Apache::londropadd::date_setting_table($starttime,$endtime,'create_enrolldates');
  265:     my $access_table = &Apache::londropadd::date_setting_table($starttime,$endtime,'create_defaultdates');
  266:     my ($krbdef,$krbdefdom) =
  267:     &Apache::loncommon::get_kerberos_defaults($defdom);
  268:     my $javascript_validations=&Apache::londropadd::javascript_validations('createcourse',$krbdefdom);
  269:     my %param = ( formname => 'document.ccrs',
  270:                    kerb_def_dom => $krbdefdom,
  271:                    kerb_def_auth => $krbdef
  272:                 );
  273:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  274:     my $intform = &Apache::loncommon::authform_internal(%param);
  275:     my $locform = &Apache::loncommon::authform_local(%param);
  276:     my %lt=&Apache::lonlocal::texthash(
  277: 		    'cinf' => "Course Information",
  278:                     'ctit' => "Course Title",
  279:                     'chsr' => "Course Home Server",
  280:                     'cidn' => "Course ID/Number",
  281:                     'opt'  => "optional",
  282:                     'iinf' => "Institutional Information",
  283:                     'stat' => "The following entries will be used to identify the course according to the naming scheme adopted by your institution. Your choices will be used to map an internal LON-CAPA course ID to the corresponding course section ID(s) used by the office responsible for providing official class lists for courses at your institution. This mapping is required if you choose to employ automatic population of class lists.",
  284:                     'ccod' => "Course Code",
  285:                     'toin' => "to interface with institutional data, e.g., fs03glg231 for Fall 2003 Geology 231",
  286:                     'snid' => "Section Numbers and corresponding LON-CAPA section/group IDs",
  287:                     'csli' => "a comma separated list of institutional section numbers, each separated by a colon from the (optional) corresponding section/group ID to be used in LON-CAPA e.g., 001:1,002:2",
  288:                     'crcs' => "Crosslisted courses",
  289:                     'cscs' => "a comma separated list of course sections crosslisted with the current course, with each entry including the institutional course section name followed by a colon and then the (optional) groupID to be used in LON-CAPA, e.g., fs03ent231001:ent1,fs03bot231001:bot1,fs03zol231002:zol2",
  290:                     'crco' => "Course Content",
  291:                     'cncr' => "Completely new course",
  292:                     'cecr' => "Clone an existing course", 
  293:                     'map'  => "Map",
  294:                     'smap' => "Select Map",
  295:                     'sacr' => "Do NOT generate as standard course",
  296:                     'ocik' => "only check if you know what you are doing",
  297:                     'fres' => "First Resource",
  298:                     'stco' => "standard courses only",
  299:                     'blnk' => "Blank",
  300:                     'sllb' => "Syllabus",
  301:                     'navi' => "Navigate",
  302:                     'cid'  => "Course ID",
  303:                     'dmn'  => "Domain",
  304:                     'asov' => "Additional settings, if specified below, will override cloned settings",
  305:                     'assp' => "Assessment Parameters",
  306:                     'oaas' => "Open all assessments",
  307:                     'mssg' => "Messaging",
  308:                     'scpf' => "Set course policy feedback to Course Coordinator",
  309:                     'scfc' => "Set content feedback to Course Coordinator",
  310:                     'cmmn' => "Communication",
  311:                     'dsrd' => "Disable student resource discussion",
  312:                     'dsuc' => "Disable student use of chatrooms",
  313:                     'acco' => "Access Control",
  314:                     'snak' => "Students need access key to enter course",
  315: 		    'kaut' => 
  316: 		    'Key authority (<tt>id@domain</tt>) if other than course',
  317:                     'cc'   => "Course Coordinator",
  318:                     'user' => "Username",
  319:                     'ierc' => "Immediately expire own role as Course Coordinator",
  320:                     'aens' => "Automated enrollment settings",
  321:                     'aesc' => "The following settings control automatic enrollment of students in this class based on information available for this specific course from your institution's official classlists.",
  322:                     'aadd' => "Automated adds",
  323:                     'yes'  => "Yes",
  324:                     'no'   => "No",
  325:                     'audr' => "Automated drops",
  326:                     'dacu' => "Duration of automated classlist updates",
  327:                     'dacc' => "Default start and end dates for student access",
  328:                     'psam' => "Please select the authentication mechanism",
  329:                     'pcda' => "Please choose the default authentication method to be used by new users added to this LON-CAPA domain by the automated enrollment process",
  330:                     'nech' => "Notification of enrollment changes",
  331:                     'nccl' => "Notification to course coordinator via LON-CAPA message when enrollment changes occur during the automated update?",
  332:                     'ndcl' => "Notification to domain coordinator via LON-CAPA message when enrollment changes occur during the automated update?",
  333:                     'irsp' => "Include retrieval of student photographs?",
  334: 		    'rshm' => 'Resource Space Home',
  335:                     'opco' => "Open Course"
  336: 				       );
  337:     my $html=&Apache::lonxml::xmlbegin();
  338:     $r->print(<<ENDDOCUMENT);
  339: $html
  340: <head>
  341: <script language="JavaScript" type="text/javascript">
  342: var editbrowser = null;
  343: function openbrowser(formname,elementname) {
  344:     var url = '/res/?';
  345:     if (editbrowser == null) {
  346:         url += 'launch=1&';
  347:     }
  348:     url += 'catalogmode=interactive&';
  349:     url += 'mode=edit&';
  350:     url += 'form=' + formname + '&';
  351:     url += 'element=' + elementname + '&';
  352:     url += 'only=sequence' + '';
  353:     var title = 'Browser';
  354:     var options = 'scrollbars=1,resizable=1,menubar=0';
  355:     options += ',width=700,height=600';
  356:     editbrowser = open(url,title,options,'1');
  357:     editbrowser.focus();
  358: }
  359: $javascript_validations
  360: </script>
  361: $coursebrowserjs
  362: <title>The LearningOnline Network with CAPA</title>
  363: </head>
  364: $bodytag
  365: $helplink
  366: <form action="/adm/createcourse" method="post" name="ccrs">
  367: <h2>$lt{'cinf'}</h2>
  368: <p>
  369: <label><b>$lt{'ctit'}:</b>
  370: <input type="text" size="50" name="title" /></label>
  371: </p><p>
  372: <label>
  373:     <b>$lt{'chsr'}:</b>$course_home
  374: </label>
  375: </p><p>
  376: <label>
  377:     <b>$lt{'cidn'} ($lt{'opt'})</b>
  378:     <input type="text" size="30" name="crsid" />
  379: </label>
  380: </p><p>
  381: <h2>$lt{'iinf'}</h2>
  382: <p>
  383: $lt{'stat'}
  384: </p><p>
  385: <label>
  386:     <b>$lt{'ccod'}</b>
  387:     <input type="text" size="30" name="crscode" />
  388: </label>
  389: <br/>
  390: ($lt{'toin'})
  391: </p><p>
  392: <label>
  393:     <b>$lt{'snid'}</b>
  394:     <input type="text" size="30" name="crssections" />
  395: </label>
  396: <br/>
  397: ($lt{'csli'})
  398: </p><p>
  399: <label>
  400:     <b>$lt{'crcs'}</b>
  401:     <input type="text" size="30" name="crsxlist" />
  402: </label>
  403: <br/>
  404: ($lt{'cscs'})
  405: </p>
  406: <h2>$lt{'crco'}</h2>
  407: <table border="2">
  408: <tr><th>$lt{'cncr'}</th><th>$lt{'cecr'}</th></tr>
  409: <tr><td>
  410: <p>
  411: <label>
  412:     <b>$lt{'map'}:</b>
  413:     <input type="text" size="50" name="topmap" />
  414: </label>
  415: <a href="javascript:openbrowser('ccrs','topmap')">$lt{'smap'}</a>
  416: </p><p>
  417: <label for="nonstd"><b>$lt{'sacr'}</b></label>
  418: <br />
  419: ($lt{'ocik'}):
  420: <input id="nonstd" type="checkbox" name="nonstandard" />
  421: </p><p>
  422: <b>$lt{'fres'}</b><br />($lt{'stco'}):
  423: <label>
  424:     <input type="radio" name="firstres" value="blank" />$lt{'blnk'}
  425: </label>
  426: &nbsp;
  427: <label>
  428:     <input type="radio" name="firstres" value="syl" checked />$lt{'sllb'}
  429: </label>
  430: &nbsp;
  431: <label>
  432:     <input type="radio" name="firstres" value="nav" />$lt{'navi'}
  433: </label>
  434: </p>
  435: </td><td>
  436: <label>
  437:     $lt{'cid'}: <input type="text" size="25" name="clonecourse" value="" />
  438: </label>
  439: <br />
  440: <label>
  441:     $lt{'dmn'}: $cloneform
  442: </label>
  443: <br />
  444: &nbsp;<br />
  445: $lt{'asov'}.
  446: </td></tr>
  447: </table>
  448: <h2>$lt{'assp'}</h2>
  449: <p>
  450: <label>
  451:     <b>$lt{'oaas'}: </b>
  452:     <input type="checkbox" name="openall" />
  453: </label>
  454: </p>
  455: <h2>$lt{'mssg'}</h2>
  456: <p>
  457: <label>
  458:     <b>$lt{'scpf'}: </b>
  459:     <input type="checkbox" name="setpolicy" checked />
  460: </label>
  461: <br />
  462: <label>
  463:     <b>$lt{'scfc'}: </b>
  464:     <input type="checkbox" name="setcontent" checked />
  465: </label>
  466: </p>
  467: <h2>$lt{'cmmn'}</h2>
  468: <p>
  469: <label>
  470:     <b>$lt{'dsrd'}: </b>
  471:     <input type="checkbox" name="disresdis" />
  472: </label>
  473: <br />
  474: <label>
  475:     <b>$lt{'dsuc'}: </b>
  476:     <input type="checkbox" name="disablechat" />
  477: </label>
  478: </p>
  479: <h2>$lt{'acco'}</h2>
  480: <p>
  481: <label>
  482:     <b>$lt{'snak'}: </b>
  483:     <input type="checkbox" name="setkeys" />
  484: </label>
  485: <br />
  486: <label>
  487:     <b>$lt{'kaut'}: </b>
  488:     <input type="text" size="30" name="keyauth" />
  489: </label>
  490: </p>
  491: <h2>$lt{'rshm'}</h2>
  492: <p>
  493: <label>
  494:     <b>$lt{'rshm'}: </b>
  495:     <input type="text" name="reshome" size="30" value="/res/$defdom/" />
  496: </label>
  497: </p>
  498: <p>
  499: <h2>$lt{'aens'}</h2>
  500: $lt{'aesc'}
  501: </p>
  502: <p>
  503: <b>$lt{'aadd'}</b>
  504: <label><input type="radio" name="autoadds" value="1" />$lt{'yes'}</label> 
  505: <label><input type="radio" name="autoadds" value="0" checked="true" />$lt{'no'}
  506: </label>
  507: </p><p>
  508: <b>$lt{'audr'}</b>
  509: <label><input type="radio" name="autodrops" value="1" />$lt{'yes'}</label> 
  510: <label><input type="radio" name="autodrops" value="0" checked="true" />$lt{'no'}</label>
  511: </p><p>
  512: <b>$lt{'dacu'}</b>
  513: $enroll_table
  514: </p><p>
  515: <b>$lt{'dacc'}</b>
  516: $access_table
  517: <p></p>
  518: <b>$lt{'psam'}.</b><br />
  519: $lt{'pcda'}.
  520: </p><p>
  521: $krbform
  522: <br />
  523: $intform
  524: <br />
  525: $locform
  526: </p><p>
  527: <b>$lt{'nech'}</b><br />
  528: $lt{'nccl'}<br/>
  529: <label>
  530:     <input type="radio" name="notify_owner" value="1" />$lt{'yes'}
  531: </label> 
  532: <label>
  533:     <input type="radio" name="notify_owner" value="0" checked="true" />$lt{'no'}
  534: </label>
  535: <br />
  536: $lt{'ndcl'}<br/>
  537: <label>
  538:     <input type="radio" name="notify_dc" value="1" />$lt{'yes'}
  539: </label>
  540: <label>
  541:     <input type="radio" name="notify_dc" value="0" checked="true" />$lt{'no'}
  542: </label>
  543: </p><p>
  544: <b>$lt{'irsp'}</b>
  545: <label>
  546:     <input type="radio" name="showphotos" value="1" />$lt{'yes'}
  547: </label> 
  548: <label>
  549:     <input type="radio" name="showphotos" value="0" checked="true" />$lt{'no'}
  550: </label>
  551: </p>
  552: <hr />
  553: <h2>$lt{'cc'}</h2>
  554: <p>
  555: <label>
  556:     <b>$lt{'user'}:</b> <input type="text" size="15" name="ccuname" />
  557: </label>
  558: </p><p>
  559: <label>
  560:     <b>$lt{'dmn'}:</b> $domform
  561: </label>
  562: </p>
  563: <p>
  564: <input type="hidden" name="phase" value="two" />
  565: <input type="button" onClick="verify_message(this.form)" value="$lt{'opco'}" />
  566: </p>
  567: </form>
  568: </body>
  569: </html>
  570: ENDDOCUMENT
  571: }
  572: 
  573: # ====================================================== Phase two: make course
  574: 
  575: sub create_course {
  576:     my $r=shift;
  577:     my $ccuname=$env{'form.ccuname'};
  578:     my $ccdomain=$env{'form.ccdomain'};
  579:     $ccuname=~s/\W//g;
  580:     $ccdomain=~s/\W//g;
  581: 
  582:     my $enrollstart = &Apache::lonhtmlcommon::get_date_from_form('startenroll');
  583:     my $enrollend   = &Apache::lonhtmlcommon::get_date_from_form('endenroll');
  584:     my $startaccess = &Apache::lonhtmlcommon::get_date_from_form('startaccess');
  585:     my $endaccess = &Apache::lonhtmlcommon::get_date_from_form('endaccess');
  586: 
  587:     my $autharg;
  588:     my $authtype;
  589: 
  590:     if ($env{'form.login'} eq 'krb') {
  591:         $authtype = 'krb';
  592:         $authtype .=$env{'form.krbver'};
  593:         $autharg = $env{'form.krbarg'};
  594:     } elsif ($env{'form.login'} eq 'int') {
  595:         $authtype ='internal';
  596:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
  597:             $autharg = $env{'form.intarg'};
  598:         }
  599:     } elsif ($env{'form.login'} eq 'loc') {
  600:         $authtype = 'localauth';
  601:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
  602:             $autharg = $env{'form.locarg'};
  603:         }
  604:     }
  605: 
  606:     my $logmsg;
  607:     my $html=&Apache::lonxml::xmlbegin();
  608:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  609:     $r->print(<<ENDENHEAD);
  610: $html
  611: <head>
  612: <title>The LearningOnline Network with CAPA</title>
  613: </head>
  614: $bodytag
  615: ENDENHEAD
  616: 
  617:     my $args = {
  618:                ccuname => $ccuname,
  619:                ccdomain => $ccdomain,
  620:                cdescr => $env{'form.title'},
  621:                curl => $env{'form.topmap'},
  622:                course_domain => $env{'request.role.domain'},
  623:                course_home =>  $env{'form.course_home'},
  624:                nonstandard => $env{'form.nonstandard'},
  625:                crscode => $env{'form.crscode'},
  626:                clonecourse => $env{'form.clonecourse'},
  627:                clonedomain => $env{'form.clonedomain'},
  628:                crsid => $env{'form.crsid'},
  629:                curruser => $env{'user.name'},
  630:                crssections => $env{'form.crssections'},
  631:                crsxlist => $env{'form.crsxlist'},
  632:                autoadds => $env{'form.autoadds'},
  633:                autodrops => $env{'form.autodrops'},
  634:                notify_owner => $env{'form.notify_owner'},
  635:                notify_dc => $env{'form.notify_dc'},
  636:                no_end_date => $env{'form.no_end_date'},
  637:                showphotos => $env{'form.showphotos'},
  638:                authtype => $authtype,
  639:                autharg => $autharg,
  640:                enrollstart => $enrollstart,
  641:                enrollend => $enrollend,
  642:                startaccess => $startaccess,
  643:                endaccess => $endaccess,
  644:                setpolicy => $env{'form.setpolicy'},
  645:                setcontent => $env{'form.setcontent'},
  646:                reshome => $env{'form.reshome'},
  647:                setkeys => $env{'form.setkeys'},
  648:                keyauth => $env{'form.keyauth'},
  649:                disresdis => $env{'form.disresdis'},
  650:                disablechat => $env{'form.disablechat'},
  651:                openall => $env{'form.openall'},
  652:                firstres => $env{'form.firstres'}
  653:                };
  654: 
  655:     #
  656:     # Verify data
  657:     #
  658:     # Check the veracity of the course coordinator
  659:     if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
  660: 	$r->print('<form action="/adm/createuser" method="post" name="crtuser">');
  661:         $r->print(&mt('No such user').' '.$ccuname.' '.&mt('at').' '.$ccdomain.'.<br />');
  662: 	$r->print(&mt("Please click Back on your browser and select another user, or "));
  663: 	$r->print('
  664: 	    <input type="hidden" name="phase" value="get_user_info" />
  665:             <input type="hidden" name="ccuname" value="'.$ccuname.'" />
  666:             <input type="hidden" name="ccdomain" value="'.$ccdomain.'" />
  667:             <input name="userrole" type="submit" value="'.
  668: 		  &mt('Create User').'" />
  669: 	</form></body></html>');
  670: 	return;
  671:     }
  672:     # Check the proposed home server for the course
  673:     my %host_servers = &Apache::loncommon::get_library_servers
  674:         ($env{'request.role.domain'});
  675:     if (! exists($host_servers{$env{'form.course_home'}})) {
  676:         $r->print(&mt('Invalid home server for course').': '.
  677:                   $env{'form.course_home'}.'</body></html>');
  678:         return;
  679:     }
  680:     my ($courseid,$crsudom,$crsunum);
  681:     $r->print(&construct_course($args,\$logmsg,\$courseid,\$crsudom,\$crsunum,$env{'user.domain'},$env{'user.name'}));
  682: 
  683: #
  684: # Make the requested user a course coordinator
  685: #
  686:    if (($ccdomain) && ($ccuname)) {
  687:     $r->print(&mt('Assigning role of course coordinator to').' '.
  688:                $ccuname.' at '.$ccdomain.': '.
  689:     &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
  690:    }
  691:     if ($env{'form.setkeys'}) {
  692:         $r->print(
  693:  '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a></p>');
  694:     }
  695: # Flush the course logs so reverse user roles immediately updated
  696:     &Apache::lonnet::flushcourselogs();
  697:     $r->print('<p>'.&mt('Roles will be active at next login').'.</p></body></html>');
  698: }
  699: 
  700: sub construct_course {
  701:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname) = @_;
  702:     my $outcome;
  703: 
  704: #
  705: # Open course
  706: #
  707:     my %cenv=();
  708:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
  709:                                                $args->{'cdescr'},
  710:                                                $args->{'curl'},
  711:                                                $args->{'course_home'},
  712:                                                $args->{'nonstandard'},
  713:                                                $args->{'crscode'},
  714:                                                $args->{'ccuname'});
  715: 
  716:     # Note: The testing routines depend on this being output; see 
  717:     # Utils::Course. This needs to at least be output as a comment
  718:     # if anyone ever decides to not show this, and Utils::Course::new
  719:     # will need to be suitably modified.
  720:     $outcome .= 'New LON-CAPA Course ID: '.$$courseid.'<br>';
  721: #
  722: # Check if created correctly
  723: #
  724:     ($$crsudom,$$crsunum)=($$courseid=~/^\/(\w+)\/(\w+)$/);
  725:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
  726:     $outcome .= &mt('Created on').': '.$crsuhome.'<br>';
  727: #
  728: # Are we cloning?
  729: #
  730:     my $cloneid='';
  731:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
  732: 	$cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
  733:         my ($clonecrsudom,$clonecrsunum)=($cloneid=~/^\/(\w+)\/(\w+)$/);
  734: 	my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
  735: 	if ($clonehome eq 'no_host') {
  736: 	    $outcome .=
  737:     '<br /><font color="red">'.&mt('Attempting to clone non-existing course').' '.$cloneid.'</font>';
  738: 	} else {
  739: 	    $outcome .= 
  740:     '<br /><font color="green">'.&mt('Cloning course from').' '.$clonehome.'</font>';
  741: 	    my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
  742: # Copy all files
  743: 	    &copycoursefiles($cloneid,$$courseid);
  744: # Restore URL
  745: 	    $cenv{'url'}=$oldcenv{'url'};
  746: # Restore title
  747: 	    $cenv{'description'}=$oldcenv{'description'};
  748: # restore grading mode
  749: 	    if (defined($oldcenv{'grading'})) {
  750: 		$cenv{'grading'}=$oldcenv{'grading'};
  751: 	    }
  752: # Mark as cloned
  753: 	    $cenv{'clonedfrom'}=$cloneid;
  754: 	    delete($cenv{'default_enrollment_start_date'});
  755: 	    delete($cenv{'default_enrollment_end_date'});
  756: 	}
  757:     }
  758: #
  759: # Set environment (will override cloned, if existing)
  760: #
  761:     my @sections = ();
  762:     my @xlists = ();
  763:     if ($args->{'crsid'}) {
  764:         $cenv{'courseid'}=$args->{'crsid'};
  765:     }
  766:     if ($args->{'crscode'}) {
  767:         $cenv{'internal.coursecode'}=$args->{'crscode'};
  768:     }
  769:     if ($args->{'ccuname'}) {
  770:         $cenv{'internal.courseowner'} = $args->{'ccuname'};
  771:     } else {
  772:         $cenv{'internal.courseowner'} = $args->{'curruser'};
  773:     }
  774: 
  775:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
  776:     if ($args->{'crssections'}) {
  777:         $cenv{'internal.sectionnums'} = '';
  778:         if ($args->{'crssections'} =~ m/,/) {
  779:             @sections = split/,/,$args->{'crssections'};
  780:         } else {
  781:             $sections[0] = $args->{'crssections'};
  782:         }
  783:         if (@sections > 0) {
  784:             foreach my $item (@sections) {
  785:                 my ($sec,$gp) = split/:/,$item;
  786:                 my $class = $args->{'crscode'}.$sec;
  787:                 my $addcheck = &Apache::lonnet::auto_new_course($crsunum,$crsudom,$class,$cenv{'internal.courseowner'});
  788:                 $cenv{'internal.sectionnums'} .= $item.',';
  789:                 unless ($addcheck eq 'ok') {
  790:                     push @badclasses, $class;
  791:                 }
  792:             }
  793:             $cenv{'internal.sectionnums'} =~ s/,$//;
  794:         }
  795:     }
  796: # do not hide course coordinator from staff listing, 
  797: # even if privileged
  798:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
  799: # add crosslistings
  800:     if ($args->{'crsxlist'}) {
  801:         $cenv{'internal.crosslistings'}='';
  802:         if ($args->{'crsxlist'} =~ m/,/) {
  803:             @xlists = split/,/,$args->{'crsxlist'};
  804:         } else {
  805:             $xlists[0] = $args->{'crsxlist'};
  806:         }
  807:         if (@xlists > 0) {
  808:             foreach my $item (@xlists) {
  809:                 my ($xl,$gp) = split/:/,$item;
  810:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
  811:                 $cenv{'internal.crosslistings'} .= $item.',';
  812:                 unless ($addcheck eq 'ok') {
  813:                     push @badclasses, $xl;
  814:                 }
  815:             }
  816:             $cenv{'internal.crosslistings'} =~ s/,$//;
  817:         }
  818:     }
  819:     if ($args->{'autoadds'}) {
  820:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
  821:     }
  822:     if ($args->{'autodrops'}) {
  823:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
  824:     }
  825: # check for notification of enrollment changes
  826:     my @notified = ();
  827:     if ($args->{'notify_owner'}) {
  828:         if ($args->{'ccuname'} ne '') {
  829:             push(@notified,$args->{'ccuname'}.'@'.$args->{'ccdomain'});
  830:         }
  831:     }
  832:     if ($args->{'notify_dc'}) {
  833:         if ($uname ne '') { 
  834:             push(@notified,$uname.'@'.$udom);
  835:         }
  836:     }
  837:     if (@notified > 0) {
  838:         my $notifylist;
  839:         if (@notified > 1) {
  840:             $notifylist = join(',',@notified);
  841:         } else {
  842:             $notifylist = $notified[0];
  843:         }
  844:         $cenv{'internal.notifylist'} = $notifylist;
  845:     }
  846:     if (@badclasses > 0) {
  847:         my %lt=&Apache::lonlocal::texthash(
  848:                 'tclb' => 'The courses listed below were included as sections or crosslistings affiliated with your new LON-CAPA course.  However, if automated course roster updates are enabled for this class, these particular sections/crosslistings will not contribute towards enrollment, because the user identified as the course owner for this LON-CAPA course',
  849:                 'dnhr' => 'does not have rights to access enrollment in these classes',
  850:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
  851:         );
  852:         $outcome .= '<font color="red">'.$lt{'tclb'}.' ('.$cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.' ('.$lt{'adby'}.').<br /><ul>'."\n";
  853:         foreach (@badclasses) {
  854:             $outcome .= "<li>$_</li>\n";
  855:         }
  856:         $outcome .= "</ul><br /><br /></font>\n";
  857:     }
  858:     if ($args->{'no_end_date'}) {
  859:         $args->{'endaccess'} = 0;
  860:     }
  861:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
  862:     $cenv{'internal.autoend'}=$args->{'enrollend'};
  863:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
  864:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
  865:     if ($args->{'showphotos'}) {
  866:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
  867:     }
  868:     $cenv{'internal.authtype'} = $args->{'authtype'};
  869:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
  870:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
  871:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
  872:             $outcome .= '<font color="red" size="+1">'.
  873:                       &mt('As you did not include the default Kerberos domain to be used for authentication in this class, the institutional data used by the automated enrollment process must include the Kerberos domain for each new student').'</font></p>';
  874:         }
  875:     }
  876:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
  877:        if ($args->{'setpolicy'}) {
  878:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
  879:        }
  880:        if ($args->{'setcontent'}) {
  881:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
  882:        }
  883:     }
  884:     if ($args->{'reshome'}) {
  885: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
  886: 	$cenv{'reshome'}=~s/\/+$/\//;
  887:     }
  888: #
  889: # course has keyed access
  890: #
  891:     if ($args->{'setkeys'}) {
  892:        $cenv{'keyaccess'}='yes';
  893:     }
  894: # if specified, key authority is not course, but user
  895: # only active if keyaccess is yes
  896:     if ($args->{'keyauth'}) {
  897: 	$args->{'keyauth'}=~s/[^\w\@]//g;
  898: 	if ($args->{'keyauth'}) {
  899: 	    $cenv{'keyauth'}=$args->{'keyauth'};
  900: 	}
  901:     }
  902: 
  903:     if ($args->{'disresdis'}) {
  904:         $cenv{'pch.roles.denied'}='st';
  905:     }
  906:     if ($args->{'disablechat'}) {
  907:         $cenv{'plc.roles.denied'}='st';
  908:     }
  909: 
  910:     # Record we've not yet viewed the Course Initialization Helper for this 
  911:     # course
  912:     $cenv{'course.helper.not.run'} = 1;
  913:     #
  914:     # Use new Randomseed
  915:     #
  916:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
  917:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
  918:     #
  919:     # The encryption code and receipt prefix for this course
  920:     #
  921:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
  922:     $cenv{'internal.encpref'}=100+int(9*rand(99));
  923:     #
  924:     # By default, use standard grading
  925:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
  926: 
  927:     $outcome .= ('<br />'.&mt('Setting environment').': '.                 
  928:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).'<br>');
  929: #
  930: # Open all assignments
  931: #
  932:     if ($args->{'openall'}) {
  933:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
  934:        my %storecontent = ($storeunder         => time,
  935:                            $storeunder.'.type' => 'date_start');
  936:        
  937:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
  938:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).'<br>';
  939:    }
  940: #
  941: # Set first page
  942: #
  943:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
  944: 	    || ($cloneid)) {
  945: 	$outcome .= &mt('Setting first resource').': ';
  946:         my ($errtext,$fatal)=
  947:            &Apache::londocs::mapread($$crsunum,$$crsudom,'default.sequence');
  948:         $outcome .= ($fatal?$errtext:'read ok').' - ';
  949:         my $title; my $url;
  950:         if ($args->{'firstres'} eq 'syl') {
  951: 	    $title='Syllabus';
  952:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
  953:         } else {
  954:             $title='Navigate Contents';
  955:             $url='/adm/navmaps';
  956:         }
  957:         $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
  958:         ($errtext,$fatal)=
  959:            &Apache::londocs::storemap($$crsunum,$$crsudom,'default.sequence');
  960:         $outcome .= ($fatal?$errtext:'write ok').'<br>';
  961:     }
  962:     return $outcome;
  963: }
  964: 
  965: # ===================================================================== Handler
  966: sub handler {
  967:     my $r = shift;
  968: 
  969:     if ($r->header_only) {
  970:        &Apache::loncommon::content_type($r,'text/html');
  971:        $r->send_http_header;
  972:        return OK;
  973:     }
  974: 
  975:     if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
  976:        &Apache::loncommon::content_type($r,'text/html');
  977:        $r->send_http_header;
  978: 
  979:        if ($env{'form.phase'} eq 'two') {
  980:            &create_course($r);
  981:        } else {
  982: 	   &print_course_creation_page($r);
  983:        }
  984:    } else {
  985:       $env{'user.error.msg'}=
  986:         "/adm/createcourse:ccc:0:0:Cannot create courses";
  987:       return HTTP_NOT_ACCEPTABLE; 
  988:    }
  989:    return OK;
  990: } 
  991: 
  992: 1;
  993: __END__

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