Annotation of loncom/interface/loncreatecourse.pm, revision 1.30

1.1       www         1: # The LearningOnline Network
                      2: # Create a course
1.5       albertel    3: #
1.30    ! www         4: # $Id: loncreatecourse.pm,v 1.29 2003/08/30 18:49:10 www Exp $
1.5       albertel    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: #
1.1       www        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: #
1.4       www        40: # 2/14,2/16,2/17,7/6 Gerd Kortemeyer
1.1       www        41: #
                     42: package Apache::loncreatecourse;
                     43: 
                     44: use strict;
                     45: use Apache::Constants qw(:common :http);
                     46: use Apache::lonnet;
1.12      www        47: use Apache::loncommon;
1.13      www        48: use Apache::lonratedt;
                     49: use Apache::londocs;
1.28      www        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;
1.29      www        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 {
1.30    ! www        93:     (my $courseid, my $which,$ENV{'form.output'})=@_;
1.29      www        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));
1.30    ! www       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:     }
1.28      www       141: }
1.13      www       142: 
1.2       www       143: # ===================================================== Phase one: fill-in form
                    144: 
1.10      matthew   145: sub print_course_creation_page {
1.2       www       146:     my $r=shift;
1.10      matthew   147:     my $defdom=$ENV{'request.role.domain'};
                    148:     my %host_servers = &Apache::loncommon::get_library_servers($defdom);
                    149:     my $course_home = '<select name="course_home" size="1">'."\n";
                    150:     foreach my $server (sort(keys(%host_servers))) {
1.14      matthew   151:         $course_home .= qq{<option value="$server"};
                    152:         if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
                    153:             $course_home .= " selected ";
                    154:         }
                    155:         $course_home .= qq{>$server $host_servers{$server}</option>};
1.10      matthew   156:     }
                    157:     $course_home .= "\n</select>\n";
1.9       matthew   158:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
1.12      www       159:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.17      www       160:     my $helplink=&Apache::loncommon::help_open_topic('Create_Course','Help on Creating Courses');
1.2       www       161:     $r->print(<<ENDDOCUMENT);
                    162: <html>
1.6       matthew   163: <script language="JavaScript" type="text/javascript">
                    164: var editbrowser = null;
                    165: function openbrowser(formname,elementname) {
                    166:     var url = '/res/?';
                    167:     if (editbrowser == null) {
                    168:         url += 'launch=1&';
                    169:     }
                    170:     url += 'catalogmode=interactive&';
                    171:     url += 'mode=edit&';
                    172:     url += 'form=' + formname + '&';
1.7       matthew   173:     url += 'element=' + elementname + '&';
                    174:     url += 'only=sequence' + '';
1.6       matthew   175:     var title = 'Browser';
                    176:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    177:     options += ',width=700,height=600';
                    178:     editbrowser = open(url,title,options,'1');
                    179:     editbrowser.focus();
                    180: }
                    181: </script>
1.2       www       182: <head>
                    183: <title>The LearningOnline Network with CAPA</title>
                    184: </head>
1.12      www       185: $bodytag
1.17      www       186: $helplink
1.6       matthew   187: <form action="/adm/createcourse" method="post" name="ccrs">
1.10      matthew   188: <h2>Course Information</h2>
                    189: <p>
                    190: <b>Course Title:</b>
1.6       matthew   191: <input type="text" size="50" name="title">
1.10      matthew   192: </p><p>
1.13      www       193: <b>Course Home Server:</b>$course_home
                    194: </p><p>
                    195: <b>Course ID/Number (optional)</b>
                    196: <input type="text" size="30" name="crsid">
                    197: </p>
                    198: <h2>Course Content</h2>
                    199: <p>
1.11      www       200: <b>Map:</b>
1.6       matthew   201: <input type="text" size="50" name="topmap">
1.24      www       202: <a href="javascript:openbrowser('ccrs','topmap')">Select Map</a>
1.10      matthew   203: </p><p>
1.11      www       204: <b>Do NOT generate as standard course</b> 
                    205: (only check if you know what you are doing):
                    206: <input type="checkbox" name="nonstandard">
1.13      www       207: </p>
                    208: <p>
                    209: <b>First Resource</b> (standard courses only):
1.17      www       210: <input type="radio" name="firstres" value="blank">Blank
1.13      www       211: &nbsp;
1.17      www       212: <input type="radio" name="firstres" value="syl" checked>Syllabus
1.13      www       213: &nbsp;
                    214: <input type="radio" name="firstres" value="nav">Navigate
                    215: </p>
                    216: 
                    217: <h2>Assessment Parameters</h2>
                    218: <p>
1.11      www       219: <b>Open all assessments: </b>
                    220: <input type="checkbox" name="openall" checked>
1.13      www       221: </p>
                    222: <h2>Messaging</h2>
                    223: <p>
1.11      www       224: <b>Set course policy feedback to Course Coordinator: </b>
                    225: <input type="checkbox" name="setpolicy" checked>
                    226: </p><p>
                    227: <b>Set content feedback to Course Coordinator: </b>
                    228: <input type="checkbox" name="setcontent" checked>
                    229: </p>
1.16      www       230: <h2>Communication</h2>
                    231: <p>
                    232: <b>Disable student resource discussion: </b>
1.26      matthew   233: <input type="checkbox" name="disresdis" /> <br />
                    234: <b>Disable student use of chatrooms: </b>
                    235: <input type="checkbox" name="disablechat" />
1.16      www       236: </p>
1.18      www       237: <h2>Access Control</h2>
                    238: <p>
                    239: <b>Students need access key to enter course: </b>
                    240: <input type="checkbox" name="setkeys" />
                    241: </p>
1.10      matthew   242: <h2>Course Coordinator</h2>
                    243: <p>
1.11      www       244: <b>Username:</b> <input type="text" size="15" name="ccuname" />
                    245: </p><p>
                    246: <b>Domain:</b> $domform
1.10      matthew   247: </p><p>
1.11      www       248: <b>Immediately expire own role as Course Coordinator:</b>
                    249: <input type="checkbox" name="expireown" checked>
1.10      matthew   250: </p><p>
                    251: <input type="hidden" name="phase" value="two" />
1.6       matthew   252: <input type="submit" value="Open Course">
1.10      matthew   253: </p>
1.2       www       254: </form>
                    255: </body>
                    256: </html>
                    257: ENDDOCUMENT
                    258: }
                    259: 
                    260: # ====================================================== Phase two: make course
                    261: 
1.10      matthew   262: sub create_course {
1.2       www       263:     my $r=shift;
                    264:     my $topurl='/res/'.&Apache::lonnet::declutter($ENV{'form.topmap'});
                    265:     my $ccuname=$ENV{'form.ccuname'};
                    266:     my $ccdomain=$ENV{'form.ccdomain'};
                    267:     $ccuname=~s/\W//g;
                    268:     $ccdomain=~s/\W//g;
                    269:     my $cdescr=$ENV{'form.title'};
                    270:     my $curl=$ENV{'form.topmap'};
1.12      www       271:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.2       www       272:     $r->print(<<ENDENHEAD);
                    273: <html>
                    274: <head>
                    275: <title>The LearningOnline Network with CAPA</title>
                    276: </head>
1.12      www       277: $bodytag
1.2       www       278: ENDENHEAD
1.10      matthew   279:     #
                    280:     # Verify data
                    281:     #
                    282:     # Check the veracity of the course coordinator
1.2       www       283:     if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
1.3       www       284:         $r->print('No such user '.$ccuname.' at '.$ccdomain.'</body></html>');
1.2       www       285: 	return;
                    286:     }
1.10      matthew   287:     # Check the proposed home server for the course
                    288:     my %host_servers = &Apache::loncommon::get_library_servers
                    289:         ($ENV{'request.role.domain'});
                    290:     if (! exists($host_servers{$ENV{'form.course_home'}})) {
                    291:         $r->print('Invalid home server for course: '.
                    292:                   $ENV{'form.course_home'}.'</body></html>');
                    293:         return;
                    294:     }
1.2       www       295: #
                    296: # Open course
                    297: #
1.10      matthew   298:     my $courseid=&Apache::lonnet::createcourse($ENV{'request.role.domain'},
                    299:                                                $cdescr,$curl,
1.11      www       300:                                                $ENV{'form.course_home'},
                    301:                                                $ENV{'form.nonstandard'});
1.2       www       302: 
1.27      bowersj2  303:     # Note: The testing routines depend on this being output; see 
                    304:     # Utils::Course. This needs to at least be output as a comment
                    305:     # if anyone ever decides to not show this, and Utils::Course::new
                    306:     # will need to be suitably modified.
1.4       www       307:     $r->print('New LON-CAPA Course ID: '.$courseid.'<br>');
                    308: #
1.12      www       309: # Check if created correctly
1.4       www       310: #
                    311:     my ($crsudom,$crsunum)=($courseid=~/^\/(\w+)\/(\w+)$/);
                    312:     my $crsuhome=&Apache::lonnet::homeserver($crsunum,$crsudom);
                    313:     $r->print('Created on: '.$crsuhome.'<br>');
1.12      www       314: #
                    315: # Set environment
                    316: #
                    317:     my %cenv=();
                    318:     my $envflag=0;
1.4       www       319:     if ($ENV{'form.crsid'}) {
1.12      www       320: 	$envflag=1;
                    321:         $cenv{'courseid'}=$ENV{'form.crsid'};
                    322:     }
                    323:     if (($ccdomain) && ($ccuname)) {
                    324:        if ($ENV{'form.setpolicy'}) {
                    325: 	   $envflag=1;
                    326:            $cenv{'policy.email'}=$ccuname.':'.$ccdomain;
                    327:        }
                    328:        if ($ENV{'form.setcontent'}) {
1.18      www       329:            $envflag=1;
                    330:            $cenv{'question.email'}=$ccuname.':'.$ccdomain;
                    331:        }
                    332:     }
                    333:     if ($ENV{'form.setkeys'}) {
                    334:        $envflag=1;
                    335:        $cenv{'keyaccess'}='yes';
1.16      www       336:     }
                    337:     if ($ENV{'form.disresdis'}) {
                    338: 	$envflag=1;
                    339:         $cenv{'pch.roles.denied'}='st';
1.26      matthew   340:     }
                    341:     if ($ENV{'form.disablechat'}) {
                    342: 	$envflag=1;
                    343:         $cenv{'plc.roles.denied'}='st';
1.21      albertel  344:     }
1.23      bowersj2  345: 
                    346:     # Record we've not yet viewed the Course Initialization Helper for this course
                    347:     $cenv{'course.helper.not.run'} = 1;
1.21      albertel  348:     #
                    349:     # Use new Randomseed
                    350:     #
1.22      albertel  351:     $envflag=1;
                    352:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
1.25      matthew   353:     #
                    354:     # By default, use standard grading
                    355:     $cenv{'grading'} = 'standard';
1.22      albertel  356: 
1.12      www       357:     if ($envflag) {
                    358:        $r->print('Setting environment: '.                 
                    359:           &Apache::lonnet::put('environment',\%cenv,$crsudom,$crsunum).'<br>');
                    360:    }
                    361: #
                    362: # Open all assignments
                    363: #
                    364:     if ($ENV{'form.openall'}) {
                    365:        my $storeunder=$crsudom.'_'.$crsunum.'.0.opendate';
                    366:        my %storecontent = ($storeunder        => time,
                    367:                            $storeunder.'type' => 'date_start');
                    368:        
                    369:        $r->print('Opening all assignments: '.&Apache::lonnet::cput
                    370:                  ('resourcedata',\%storecontent,$crsudom,$crsunum).'<br>');
                    371:    }
1.13      www       372: #
                    373: # Set first page
                    374: #
                    375:     unless (($ENV{'form.nonstandard'}) || ($ENV{'form.firstres'} eq 'blank')) {
                    376: 	$r->print('Setting first resource: ');
                    377:         my ($errtext,$fatal)=
                    378:            &Apache::londocs::mapread($crsunum,$crsudom,'default.sequence');
                    379:         $r->print(($fatal?$errtext:'read ok').' - ');
                    380:         my $title; my $url;
                    381:         if ($ENV{'form.firstres'} eq 'syl') {
                    382: 	    $title='Syllabus';
                    383:             $url='/public/'.$crsudom.'/'.$crsunum.'/syllabus';
                    384:         } else {
                    385:             $title='Navigate Contents';
                    386:             $url='/adm/navmaps';
                    387:         }
                    388:         $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
1.15      albertel  389:         ($errtext,$fatal)=
1.13      www       390:            &Apache::londocs::storemap($crsunum,$crsudom,'default.sequence');
                    391:         $r->print(($fatal?$errtext:'write ok').'<br>');
                    392:   }
1.2       www       393: #
                    394: # Make current user course adminstrator
                    395: #
1.12      www       396:     my $end=undef;
                    397:     my $addition='';
                    398:     if ($ENV{'form.expireown'}) { $end=time+5; $addition='expired'; }
                    399:     $r->print('Assigning '.$addition.' role of course coordinator to self: '.
1.2       www       400:     &Apache::lonnet::assignrole(
1.12      www       401:      $ENV{'user.domain'},$ENV{'user.name'},$courseid,'cc',$end).'<br>');
1.2       www       402: #
                    403: # Make additional user course administrator
                    404: #
1.12      www       405:    if (($ccdomain) && ($ccuname)) {
1.2       www       406:     $r->print('Assigning role of course coordinator to '.
                    407:                $ccuname.' at '.$ccdomain.': '.
1.3       www       408:     &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
1.12      www       409:    }
1.20      www       410:     if ($ENV{'form.setkeys'}) {
                    411: 	$r->print(
                    412:  '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">Manage Access Keys</a></p>');
                    413:     }
                    414:     $r->print('<p>Roles will be active at next login.</p></body></html>');
1.2       www       415: }
                    416: 
                    417: # ===================================================================== Handler
1.1       www       418: sub handler {
                    419:     my $r = shift;
                    420: 
                    421:     if ($r->header_only) {
                    422:        $r->content_type('text/html');
                    423:        $r->send_http_header;
                    424:        return OK;
                    425:     }
                    426: 
1.10      matthew   427:     if (&Apache::lonnet::allowed('ccc',$ENV{'request.role.domain'})) {
1.1       www       428:        $r->content_type('text/html');
                    429:        $r->send_http_header;
                    430: 
1.2       www       431:        if ($ENV{'form.phase'} eq 'two') {
1.10      matthew   432:            &create_course($r);
1.2       www       433:        } else {
1.10      matthew   434: 	   &print_course_creation_page($r);
1.2       www       435:        }
1.1       www       436:    } else {
                    437:       $ENV{'user.error.msg'}=
                    438:         "/adm/createcourse:ccc:0:0:Cannot create courses";
                    439:       return HTTP_NOT_ACCEPTABLE; 
                    440:    }
                    441:    return OK;
                    442: } 
                    443: 
                    444: 1;
                    445: __END__

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