File:  [LON-CAPA] / loncom / interface / loncreatecourse.pm
Revision 1.31: download - view: text, annotated - select for diffs
Tue Sep 2 18:00:14 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Copying of the course files works.

    1: # The LearningOnline Network
    2: # Create a course
    3: #
    4: # $Id: loncreatecourse.pm,v 1.31 2003/09/02 18:00: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: # (My Desk
   29: #
   30: # (Internal Server Error Handler
   31: #
   32: # (Login Screen
   33: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
   34: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
   35: #
   36: # 3/1/1 Gerd Kortemeyer)
   37: #
   38: # 3/1 Gerd Kortemeyer)
   39: #
   40: # 2/14,2/16,2/17,7/6 Gerd Kortemeyer
   41: #
   42: package Apache::loncreatecourse;
   43: 
   44: use strict;
   45: use Apache::Constants qw(:common :http);
   46: use Apache::lonnet;
   47: use Apache::loncommon;
   48: use Apache::lonratedt;
   49: use Apache::londocs;
   50: 
   51: # -------------------------------------------- Return path to profile directory
   52: 
   53: sub propath {
   54:     my ($udom,$uname)=@_;
   55:     $udom=~s/\W//g;
   56:     $uname=~s/\W//g;
   57:     my $subdir=$uname.'__';
   58:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
   59:     my $proname="$Apache::lonnet::perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
   60:     return $proname;
   61: } 
   62: 
   63: # ================================================ Get course directory listing
   64: 
   65: sub crsdirlist {
   66:     my ($courseid,$which)=@_;
   67:     unless ($which) { $which=''; }
   68:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   69:     my @listing=&Apache::lonnet::dirlist
   70: 	($which,$crsdata{'domain'},$crsdata{'num'},
   71: 	 &propath($crsdata{'domain'},$crsdata{'num'}));
   72:     my @output=();
   73:     foreach (@listing) {
   74: 	unless ($_=~/^\./) {
   75: 	    push (@output,(split(/\&/,$_))[0]);
   76: 	}
   77:     }
   78:     return @output;
   79: }
   80: 
   81: # ============================================================= Read a userfile
   82: 
   83: sub readfile {
   84:     my ($courseid,$which)=@_;
   85:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   86:     return &Apache::lonnet::getfile('/uploaded/'.$crsdata{'domain'}.'/'.
   87: 				    $crsdata{'num'}.'/'.$which);
   88: }
   89: 
   90: # ============================================================ Write a userfile
   91: 
   92: sub writefile {
   93:     (my $courseid, my $which,$ENV{'form.output'})=@_;
   94:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   95:     return &Apache::lonnet::finishuserfileupload(
   96: 					  $crsdata{'num'},$crsdata{'domain'},
   97: 					  $crsdata{'home'},
   98: 					  'output',$which);
   99: }
  100: 
  101: # ============================================================= Copy a userfile
  102: 
  103: sub copyfile {
  104:     my ($origcrsid,$newcrsid,$which)=@_;
  105:     return &writefile($newcrsid,$which,&readfile($origcrsid,$which));
  106: }
  107: 
  108: # =============================================================== Copy a dbfile
  109: 
  110: sub copydb {
  111:     my ($origcrsid,$newcrsid,$which)=@_;
  112:     $which=~s/\.db$//;
  113:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  114:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  115:     my %data=&Apache::lonnet::dump
  116: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
  117:     return &Apache::lonnet::put
  118: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
  119: }
  120: 
  121: # ========================================================== Copy all userfiles
  122: 
  123: sub copyuserfiles {
  124:     my ($origcrsid,$newcrsid)=@_;
  125:     foreach (&crsdirlist($origcrsid,'userfiles')) {
  126: 	&copyfile($origcrsid,$newcrsid,$_);
  127:     }
  128: }
  129: # ========================================================== Copy all userfiles
  130: 
  131: sub copydbfiles {
  132:     my ($origcrsid,$newcrsid)=@_;
  133:     foreach (&crsdirlist($origcrsid)) {
  134: 	if ($_=~/\.db$/) {
  135: 	    unless 
  136:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate)/) {
  137: 		 &copydb($origcrsid,$newcrsid,$_);
  138: 	     }
  139: 	}
  140:     }
  141: }
  142: 
  143: # ======================================================= Copy all course files
  144: 
  145: sub copycoursefiles {
  146:     my ($origcrsid,$newcrsid)=@_;
  147:     &copyuserfiles($origcrsid,$newcrsid);
  148:     &copydbfiles($origcrsid,$newcrsid);
  149: }
  150: 
  151: # ===================================================== Phase one: fill-in form
  152: 
  153: sub print_course_creation_page {
  154:     my $r=shift;
  155:     my $defdom=$ENV{'request.role.domain'};
  156:     my %host_servers = &Apache::loncommon::get_library_servers($defdom);
  157:     my $course_home = '<select name="course_home" size="1">'."\n";
  158:     foreach my $server (sort(keys(%host_servers))) {
  159:         $course_home .= qq{<option value="$server"};
  160:         if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
  161:             $course_home .= " selected ";
  162:         }
  163:         $course_home .= qq{>$server $host_servers{$server}</option>};
  164:     }
  165:     $course_home .= "\n</select>\n";
  166:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  167:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  168:     my $helplink=&Apache::loncommon::help_open_topic('Create_Course','Help on Creating Courses');
  169:     $r->print(<<ENDDOCUMENT);
  170: <html>
  171: <script language="JavaScript" type="text/javascript">
  172: var editbrowser = null;
  173: function openbrowser(formname,elementname) {
  174:     var url = '/res/?';
  175:     if (editbrowser == null) {
  176:         url += 'launch=1&';
  177:     }
  178:     url += 'catalogmode=interactive&';
  179:     url += 'mode=edit&';
  180:     url += 'form=' + formname + '&';
  181:     url += 'element=' + elementname + '&';
  182:     url += 'only=sequence' + '';
  183:     var title = 'Browser';
  184:     var options = 'scrollbars=1,resizable=1,menubar=0';
  185:     options += ',width=700,height=600';
  186:     editbrowser = open(url,title,options,'1');
  187:     editbrowser.focus();
  188: }
  189: </script>
  190: <head>
  191: <title>The LearningOnline Network with CAPA</title>
  192: </head>
  193: $bodytag
  194: $helplink
  195: <form action="/adm/createcourse" method="post" name="ccrs">
  196: <h2>Course Information</h2>
  197: <p>
  198: <b>Course Title:</b>
  199: <input type="text" size="50" name="title">
  200: </p><p>
  201: <b>Course Home Server:</b>$course_home
  202: </p><p>
  203: <b>Course ID/Number (optional)</b>
  204: <input type="text" size="30" name="crsid">
  205: </p>
  206: <h2>Course Content</h2>
  207: <p>
  208: <b>Map:</b>
  209: <input type="text" size="50" name="topmap">
  210: <a href="javascript:openbrowser('ccrs','topmap')">Select Map</a>
  211: </p><p>
  212: <b>Do NOT generate as standard course</b> 
  213: (only check if you know what you are doing):
  214: <input type="checkbox" name="nonstandard">
  215: </p>
  216: <p>
  217: <b>First Resource</b> (standard courses only):
  218: <input type="radio" name="firstres" value="blank">Blank
  219: &nbsp;
  220: <input type="radio" name="firstres" value="syl" checked>Syllabus
  221: &nbsp;
  222: <input type="radio" name="firstres" value="nav">Navigate
  223: </p>
  224: 
  225: <h2>Assessment Parameters</h2>
  226: <p>
  227: <b>Open all assessments: </b>
  228: <input type="checkbox" name="openall" checked>
  229: </p>
  230: <h2>Messaging</h2>
  231: <p>
  232: <b>Set course policy feedback to Course Coordinator: </b>
  233: <input type="checkbox" name="setpolicy" checked>
  234: </p><p>
  235: <b>Set content feedback to Course Coordinator: </b>
  236: <input type="checkbox" name="setcontent" checked>
  237: </p>
  238: <h2>Communication</h2>
  239: <p>
  240: <b>Disable student resource discussion: </b>
  241: <input type="checkbox" name="disresdis" /> <br />
  242: <b>Disable student use of chatrooms: </b>
  243: <input type="checkbox" name="disablechat" />
  244: </p>
  245: <h2>Access Control</h2>
  246: <p>
  247: <b>Students need access key to enter course: </b>
  248: <input type="checkbox" name="setkeys" />
  249: </p>
  250: <h2>Course Coordinator</h2>
  251: <p>
  252: <b>Username:</b> <input type="text" size="15" name="ccuname" />
  253: </p><p>
  254: <b>Domain:</b> $domform
  255: </p><p>
  256: <b>Immediately expire own role as Course Coordinator:</b>
  257: <input type="checkbox" name="expireown" checked>
  258: </p><p>
  259: <input type="hidden" name="phase" value="two" />
  260: <input type="submit" value="Open Course">
  261: </p>
  262: </form>
  263: </body>
  264: </html>
  265: ENDDOCUMENT
  266: }
  267: 
  268: # ====================================================== Phase two: make course
  269: 
  270: sub create_course {
  271:     my $r=shift;
  272:     my $topurl='/res/'.&Apache::lonnet::declutter($ENV{'form.topmap'});
  273:     my $ccuname=$ENV{'form.ccuname'};
  274:     my $ccdomain=$ENV{'form.ccdomain'};
  275:     $ccuname=~s/\W//g;
  276:     $ccdomain=~s/\W//g;
  277:     my $cdescr=$ENV{'form.title'};
  278:     my $curl=$ENV{'form.topmap'};
  279:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  280:     $r->print(<<ENDENHEAD);
  281: <html>
  282: <head>
  283: <title>The LearningOnline Network with CAPA</title>
  284: </head>
  285: $bodytag
  286: ENDENHEAD
  287:     #
  288:     # Verify data
  289:     #
  290:     # Check the veracity of the course coordinator
  291:     if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
  292:         $r->print('No such user '.$ccuname.' at '.$ccdomain.'</body></html>');
  293: 	return;
  294:     }
  295:     # Check the proposed home server for the course
  296:     my %host_servers = &Apache::loncommon::get_library_servers
  297:         ($ENV{'request.role.domain'});
  298:     if (! exists($host_servers{$ENV{'form.course_home'}})) {
  299:         $r->print('Invalid home server for course: '.
  300:                   $ENV{'form.course_home'}.'</body></html>');
  301:         return;
  302:     }
  303: #
  304: # Open course
  305: #
  306:     my $courseid=&Apache::lonnet::createcourse($ENV{'request.role.domain'},
  307:                                                $cdescr,$curl,
  308:                                                $ENV{'form.course_home'},
  309:                                                $ENV{'form.nonstandard'});
  310: 
  311:     # Note: The testing routines depend on this being output; see 
  312:     # Utils::Course. This needs to at least be output as a comment
  313:     # if anyone ever decides to not show this, and Utils::Course::new
  314:     # will need to be suitably modified.
  315:     $r->print('New LON-CAPA Course ID: '.$courseid.'<br>');
  316: #
  317: # Check if created correctly
  318: #
  319:     my ($crsudom,$crsunum)=($courseid=~/^\/(\w+)\/(\w+)$/);
  320:     my $crsuhome=&Apache::lonnet::homeserver($crsunum,$crsudom);
  321:     $r->print('Created on: '.$crsuhome.'<br>');
  322: #
  323: # Set environment
  324: #
  325:     my %cenv=();
  326:     my $envflag=0;
  327:     if ($ENV{'form.crsid'}) {
  328: 	$envflag=1;
  329:         $cenv{'courseid'}=$ENV{'form.crsid'};
  330:     }
  331:     if (($ccdomain) && ($ccuname)) {
  332:        if ($ENV{'form.setpolicy'}) {
  333: 	   $envflag=1;
  334:            $cenv{'policy.email'}=$ccuname.':'.$ccdomain;
  335:        }
  336:        if ($ENV{'form.setcontent'}) {
  337:            $envflag=1;
  338:            $cenv{'question.email'}=$ccuname.':'.$ccdomain;
  339:        }
  340:     }
  341:     if ($ENV{'form.setkeys'}) {
  342:        $envflag=1;
  343:        $cenv{'keyaccess'}='yes';
  344:     }
  345:     if ($ENV{'form.disresdis'}) {
  346: 	$envflag=1;
  347:         $cenv{'pch.roles.denied'}='st';
  348:     }
  349:     if ($ENV{'form.disablechat'}) {
  350: 	$envflag=1;
  351:         $cenv{'plc.roles.denied'}='st';
  352:     }
  353: 
  354:     # Record we've not yet viewed the Course Initialization Helper for this course
  355:     $cenv{'course.helper.not.run'} = 1;
  356:     #
  357:     # Use new Randomseed
  358:     #
  359:     $envflag=1;
  360:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
  361:     #
  362:     # By default, use standard grading
  363:     $cenv{'grading'} = 'standard';
  364: 
  365:     if ($envflag) {
  366:        $r->print('Setting environment: '.                 
  367:           &Apache::lonnet::put('environment',\%cenv,$crsudom,$crsunum).'<br>');
  368:    }
  369: #
  370: # Open all assignments
  371: #
  372:     if ($ENV{'form.openall'}) {
  373:        my $storeunder=$crsudom.'_'.$crsunum.'.0.opendate';
  374:        my %storecontent = ($storeunder        => time,
  375:                            $storeunder.'type' => 'date_start');
  376:        
  377:        $r->print('Opening all assignments: '.&Apache::lonnet::cput
  378:                  ('resourcedata',\%storecontent,$crsudom,$crsunum).'<br>');
  379:    }
  380: #
  381: # Set first page
  382: #
  383:     unless (($ENV{'form.nonstandard'}) || ($ENV{'form.firstres'} eq 'blank')) {
  384: 	$r->print('Setting first resource: ');
  385:         my ($errtext,$fatal)=
  386:            &Apache::londocs::mapread($crsunum,$crsudom,'default.sequence');
  387:         $r->print(($fatal?$errtext:'read ok').' - ');
  388:         my $title; my $url;
  389:         if ($ENV{'form.firstres'} eq 'syl') {
  390: 	    $title='Syllabus';
  391:             $url='/public/'.$crsudom.'/'.$crsunum.'/syllabus';
  392:         } else {
  393:             $title='Navigate Contents';
  394:             $url='/adm/navmaps';
  395:         }
  396:         $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
  397:         ($errtext,$fatal)=
  398:            &Apache::londocs::storemap($crsunum,$crsudom,'default.sequence');
  399:         $r->print(($fatal?$errtext:'write ok').'<br>');
  400:   }
  401: #
  402: # Make current user course adminstrator
  403: #
  404:     my $end=undef;
  405:     my $addition='';
  406:     if ($ENV{'form.expireown'}) { $end=time+5; $addition='expired'; }
  407:     $r->print('Assigning '.$addition.' role of course coordinator to self: '.
  408:     &Apache::lonnet::assignrole(
  409:      $ENV{'user.domain'},$ENV{'user.name'},$courseid,'cc',$end).'<br>');
  410: #
  411: # Make additional user course administrator
  412: #
  413:    if (($ccdomain) && ($ccuname)) {
  414:     $r->print('Assigning role of course coordinator to '.
  415:                $ccuname.' at '.$ccdomain.': '.
  416:     &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
  417:    }
  418:     if ($ENV{'form.setkeys'}) {
  419: 	$r->print(
  420:  '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">Manage Access Keys</a></p>');
  421:     }
  422:     $r->print('<p>Roles will be active at next login.</p></body></html>');
  423: }
  424: 
  425: # ===================================================================== Handler
  426: sub handler {
  427:     my $r = shift;
  428: 
  429:     if ($r->header_only) {
  430:        $r->content_type('text/html');
  431:        $r->send_http_header;
  432:        return OK;
  433:     }
  434: 
  435:     if (&Apache::lonnet::allowed('ccc',$ENV{'request.role.domain'})) {
  436:        $r->content_type('text/html');
  437:        $r->send_http_header;
  438: 
  439:        if ($ENV{'form.phase'} eq 'two') {
  440:            &create_course($r);
  441:        } else {
  442: 	   &print_course_creation_page($r);
  443:        }
  444:    } else {
  445:       $ENV{'user.error.msg'}=
  446:         "/adm/createcourse:ccc:0:0:Cannot create courses";
  447:       return HTTP_NOT_ACCEPTABLE; 
  448:    }
  449:    return OK;
  450: } 
  451: 
  452: 1;
  453: __END__

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