Annotation of loncom/publisher/testbankimport.pm, revision 1.4

1.3       albertel    1: # Handler for parsing text upload problem descriptions into .problems
1.4     ! raeburn     2: # $Id: testbankimport.pm,v 1.3 2004/02/20 18:18:27 albertel Exp $
1.3       albertel    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: 
1.1       raeburn    27: package Apache::testbankimport;
                     28: 
1.3       albertel   29: use strict;
                     30: use Apache::Constants qw(:common :http :methods);
                     31: use Apache::loncacc;
                     32: use Apache::loncommon();
                     33: use Apache::Log();
                     34: use Apache::lonnet;
                     35: use HTML::Entities();
                     36: use Apache::lonlocal;
                     37: use Apache::lonupload;
                     38: use File::Basename();
1.1       raeburn    39: 
                     40: # ---------------------------------------------------------------- Display Control
                     41: sub display_control {
                     42: # figure out what page we're on and where we're heading.
                     43:     my $page = $ENV{'form.page'};
                     44:     my $command = $ENV{'form.go'};
                     45:     my $current_page = &calculate_page($page,$command);
                     46:     return $current_page;
                     47: }
                     48: 
                     49: # CALCULATE THE CURRENT PAGE
                     50: sub calculate_page($$) {
                     51:     my ($prev,$dir) = @_;
                     52:     return 0 if $prev eq '';    # start with first page
                     53:     return $prev + 1 if $dir eq 'NextPage';
                     54:     return $prev - 1 if $dir eq 'PreviousPage';
                     55:     return $prev     if $dir eq 'ExitPage';
                     56:     return 0 if $dir eq 'BackToStart';
                     57: }
                     58: 
                     59: # ---------------------------------------------------------------- Jscript One
                     60: 
                     61: sub jscript_one {
                     62:     my $jsref = shift;
                     63:     $$jsref = <<"END_SCRIPT";
                     64: function verify() {
                     65:     if ((document.forms.display.blocks.value == "") || (!document.forms.display.blocks.value) || (document.forms.display.blocks.value == "0")) {
                     66:         alert("You must enter the number of blocks of questions of a given question type.  This number must be 1 or more.")
                     67:         return false
                     68:     }
                     69:     if (document.forms.display.qnumformat.options[document.forms.display.qnumformat.selectedIndex].value == "-1") {
                     70:         alert("You must select the format used for the question number, e.g., (1), 1., (1, or 1).")
                     71:         return false
                     72:     }
                     73:     return true
                     74: }
                     75: function nextPage() {
                     76:     if (verify()) {
                     77:         document.forms.display.go.value="NextPage"
                     78:         document.forms.display.submit()
                     79:     }
                     80: }
                     81: function backPage() {
                     82:     document.forms.display.go.value="PreviousPage"
                     83:     document.forms.display.submit()
                     84: }
                     85: function setElements() {
                     86:     var iter = 0
                     87:     var selParam = 0
                     88: END_SCRIPT
                     89:     if (exists($ENV{'form.blocks'}) ) {
                     90:         $$jsref .= qq|
                     91:     document.forms.display.blocks.value = $ENV{'form.blocks'}\n|;
                     92:     } elsif (exists($ENV{'form.qnumformat'}) ) {
                     93:         $$jsref .= <<"TO_HERE";
                     94:     for (iter=0; iter<document.forms.display.qnumformat.length; iter++) {
                     95:         if(document.forms.display.qnumformat.options[iter].value == "$ENV{'form.qnumformat'}") {
                     96:             selParam = iter
                     97:         }
                     98:     }
                     99:     document.forms.display.qnumformat.selectedIndex = selParam
                    100: TO_HERE
                    101:     }
                    102:     $$jsref .= qq|
                    103: }
                    104:     |;
                    105: }
                    106: 
                    107: # ---------------------------------------------------------------- Jscript Two
                    108: sub jscript_two {
                    109:     my ($jsref,$qcount) = @_;
                    110:     my $blocks = 0;
                    111:     if ( exists( $ENV{'form.blocks'}) ) {
                    112:         $blocks = $ENV{'form.blocks'};
                    113:     }
                    114:     $$jsref = <<"END_SCRIPT";
                    115: function verify() {
                    116:     var poolForm = document.forms.display
                    117:     var curmax = 0
                    118:     var curmin = 0
                    119:     for (var i=0; i<$blocks; i++) {
                    120:         var iter = i+1
                    121:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "MC") {
                    122:             if (poolForm.elements[5*i+4].selectedIndex == 0) {
                    123:                 alert ("You must choose the foil labelling format in Multiple Choice questions")
                    124:                 return false
                    125:             }
                    126:         }
                    127:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "MA") {
                    128:             if (poolForm.elements[5*i+4].selectedIndex == 0) {
                    129:                 alert ("You must choose the foil labelling format in Multiple Answer questions")
                    130:                 return false
                    131:             }
                    132:             if (poolForm.elements[5*i+5].selectedIndex == 0) {
                    133:                 alert ("You must choose the answer format in Multiple Answer questions") 
                    134:                 return false
                    135:             }
                    136:         }
                    137:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "FIB") {
                    138:             if (poolForm.elements[5*i+5].selectedIndex == 0) {
                    139:                 alert ("You must choose the answer format in Fill-in-the-blank questions") 
                    140:                 return false
                    141:             }
                    142:         }
                    143:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "TF") {
                    144:             if (poolForm.elements[5*i+5].selectedIndex == 0) {
                    145:                 alert ("You must choose the answer format in True/False questions") 
                    146:                 return false
                    147:             }
                    148:         }
                    149:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "Ord") {
                    150:             if (poolForm.elements[5*i+4].selectedIndex == 0) {
                    151:                 alert ("You must choose the foil labelling format in Ranking/ordering questions")
                    152:                 return false
                    153:             }
                    154:             if (poolForm.elements[5*i+5].selectedIndex == 0) {
                    155:                 alert ("You must choose the answer format in Ranking/ordering questions")
                    156:                 return false
                    157:             }
                    158:         }
                    159:         if (poolForm.elements[5*i+3].options[poolForm.elements[5*i+3].selectedIndex].value == "-1") {
                    160:             alert ("You must choose the question type for block "+iter)
                    161:             return false
                    162:         }
                    163:         if ((poolForm.elements[5*i+1].value == "") || !(poolForm.elements[5*i+1].value)) {
                    164:             alert ("You must choose the start number for block "+iter)
                    165:             return false
                    166:         }
                    167:         if ((poolForm.elements[5*i+2].value == "") || !(poolForm.elements[5*i+2].value)) {
                    168:             alert ("You must choose the end number for block "+iter)
                    169:             return false
                    170:         }
                    171:         if (poolForm.elements[5*i+2].value - poolForm.elements[5*i+1].value < 0) {
                    172:             alert ("In block: "+iter+" the end number must be the same or greater than the start number")
                    173:             return false
                    174:         }
                    175:         if (i == 0) {
                    176:             curmin = parseInt(poolForm.elements[5*i+1].value)
                    177:             curmax = parseInt(poolForm.elements[5*i+2].value)
                    178:         }
                    179:         else {
                    180:             if (parseInt(poolForm.elements[5*i+1].value) < curmin) {
                    181:                 if (parseInt(poolForm.elements[5*i+2].value) >= curmin ) {
                    182:                     alert("The question number range for block "+iter+" overlaps with the question number range for one of the previous blocks - this is not permitted.")
                    183:                     return false
                    184:                 }
                    185:             }
                    186:             else {
                    187:                 if (parseInt(poolForm.elements[5*i+1].value) <= curmax) {
                    188:                     for (var j=parseInt(poolForm.elements[5*i+1].value); j<=parseInt(poolForm.elements[5*i+2].value); j++) {
                    189:                         for (var k=0; k<i; k++) {
                    190:                             if ((j >= parseInt(poolForm.elements[5*k+1].value)) && (j <= parseInt(poolForm.elements[5*k+2].value))) {
                    191:                                 var overlap = k+1
                    192:                                 alert("The question number range for block "+iter+" overlaps with the question number range for block "+overlap+" - this is not permitted.")
                    193:                                 return false
                    194:                             }
                    195:                         }
                    196:                     }
                    197:                 }
                    198:             }
                    199:             if (parseInt(poolForm.elements[5*i+1].value) < curmin) {
                    200:                 curmin = parseInt(poolForm.elements[5*i+1].value)
                    201:             }
                    202:             if (parseInt(poolForm.elements[5*i+2].value) > curmax) {
                    203:                 curmax = parseInt(poolForm.elements[5*i+2].value)
                    204:             }
                    205:         }
                    206:     }
                    207:     if (curmax >$qcount+curmin) {
                    208:         alert("The last # for one or more of the blocks is too large -  the last number of the last block can not be greater than $qcount: the total number of questions in the uploaded file.")
                    209:         return false
                    210:     }
                    211:     var endpt = $qcount + curmin
                    212:     for (var n=curmin; n<endpt; n++) {
                    213:         var warnFlag = true
                    214:         for (var m=0; m<$blocks; m++) {
                    215:             if ((n >= parseInt(poolForm.elements[5*m+1].value)) && (n <= parseInt(poolForm.elements[5*m+2].value))) {
                    216:                 warnFlag = false
                    217:             }
                    218:         }
                    219:         if (warnFlag) {
                    220:             alert("The question type for question "+n+" could not be identified because it does not fall within the number ranges you have provided for any of the $blocks block(s)")
                    221:             return false
                    222:         }
                    223:     } 
                    224:     return true 
                    225: }
                    226:  
                    227: function nextPage() {
                    228:     if (verify()) {
                    229:         document.forms.display.go.value="NextPage"
                    230:         document.forms.display.submit()
                    231:     }
                    232: }
                    233: function backPage() {
                    234:     document.forms.display.go.value="PreviousPage"
                    235:     document.forms.display.submit()
                    236: }
                    237: function colSet(caller) {
                    238:     var poolForm = document.forms.display
                    239:     var curVal = poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value  
                    240:     poolForm.elements[caller*5+4].length = 0
                    241:     if (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "-1") {
                    242:         poolForm.elements[caller*5+4].options[0] = new Option("<--- Set type ","-1",true,true)
                    243:     }
                    244:     else {
                    245:         if ((poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "MC") || (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "MA") || (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "Ord")) {
                    246:             poolForm.elements[caller*5+4].options[0] = new Option("Please Select","-1",true,true)
                    247:             poolForm.elements[caller*5+4].options[1] = new Option("a.","lcperiod",false,false)
                    248:             poolForm.elements[caller*5+4].options[2] = new Option("A.","ucperiod",false,false)
                    249:             poolForm.elements[caller*5+4].options[3] = new Option("(a)","lcparen",false,false)
                    250:             poolForm.elements[caller*5+4].options[4] = new Option("(A)","ucparen",false,false)
                    251:             poolForm.elements[caller*5+4].options[5] = new Option("(i)","romparen",false,false)
                    252:             poolForm.elements[caller*5+4].options[6] = new Option("i.","romperiod",false,false)
                    253:             poolForm.elements[caller*5+4].selectedIndex = 0
                    254:         }
                    255:         else {
                    256:             poolForm.elements[caller*5+4].options[0] = new Option("Not required","0",true,true)
                    257:         }
                    258:     }
                    259:     poolForm.elements[caller*5+5].length = 0
                    260:     if (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "-1") {
                    261:         poolForm.elements[caller*5+5].options[0] = new Option("<--- Set type ","-1",true,true)
                    262:     }
                    263:     else {
                    264:         if ((poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "MA") || (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "FIB"))  {
                    265:             poolForm.elements[caller*5+5].options[0] = new Option("Please Select","-1",true,true)
                    266:             poolForm.elements[caller*5+5].options[1] = new Option("single answer","single",false,false)
                    267:             poolForm.elements[caller*5+5].options[2] = new Option("comma","comma",false,false)
                    268:             poolForm.elements[caller*5+5].options[3] = new Option("space","space",false,false)
                    269:             poolForm.elements[caller*5+5].options[4] = new Option("new line","line",false,false)
                    270:             poolForm.elements[caller*5+5].options[5] = new Option("tab","tab",false,false)
                    271:         }
                    272:         else {
                    273:             if (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "Ord") {
                    274:                 poolForm.elements[caller*5+5].options[0] = new Option("Please Select","-1",true,true)
                    275:                 poolForm.elements[caller*5+5].options[1] = new Option("comma","comma",false,false)
                    276:                 poolForm.elements[caller*5+5].options[2] = new Option("space","space",false,false)
                    277:                 poolForm.elements[caller*5+5].options[3] = new Option("new line","line",false,false)
                    278:                 poolForm.elements[caller*5+5].options[4] = new Option("tab","tab",false,false)
                    279:             }
                    280:             else { 
                    281:                 if (poolForm.elements[caller*5+3].options[poolForm.elements[caller*5+3].selectedIndex].value == "TF") {
                    282:                     poolForm.elements[caller*5+5].options[0] = new Option("Please Select","-1",true,true)
                    283:                     poolForm.elements[caller*5+5].options[1] = new Option("True or False","word",false,false)
                    284:                     poolForm.elements[caller*5+5].options[2] = new Option("T or F","lett",false,false)
                    285:                 }
                    286:                 else {
                    287:                     poolForm.elements[caller*5+5].options[0] = new Option("Not required","0",true,true)
                    288:                 }
                    289:             }
                    290:         }
                    291:     }
                    292: }
                    293: 
                    294: function setElements() {
                    295:     var iter = 0
                    296:     var selParam = 0
                    297: END_SCRIPT
                    298:     my @names = ("start_","end_","qtype_","foilformat_","ansr_");
                    299:     for (my $x=0; $x<$blocks; $x++) {
                    300:         foreach my $name (@names) {
                    301:             my $parname = $name.$x;
                    302:             my $value = $ENV{"form.$parname"};
                    303:             if ($value ne "") {
                    304:                 if (($name eq "start_")  || ($name eq "end_")) {
                    305:                     $$jsref .= qq|
                    306:     document.forms.display.$parname.value = $value\n|;
                    307:                 } elsif ($name eq "qtype_") {
                    308:                     $$jsref .= qq|
                    309:     for (iter=0; iter<document.forms.display.$parname.length; iter++) {
                    310:         if (document.forms.display.$parname.options[iter].value == "$value") {
                    311:             selParam = iter
                    312:         }
                    313:     }
                    314:     document.forms.display.$parname.selectedIndex = selParam
                    315:     colSet($x)
                    316:                     |;
                    317:                 } elsif (($name eq "foilformat_") || ($name eq "ansr_")) {
                    318:                     $$jsref .= <<"TO_HERE";
                    319:     for (iter=0; iter<document.forms.display.$parname.length; iter++) {
                    320:         if (document.forms.display.$parname.options[iter].value == "$value") {
                    321:             selParam = iter
                    322:         }
                    323:     }
                    324:     document.forms.display.$parname.selectedIndex = selParam
                    325: TO_HERE
                    326:                 } 
                    327:             }
                    328:         }
                    329:     }
                    330:     $$jsref .= qq|
                    331: }
                    332:     |;
                    333: } 
                    334: # ---------------------------------------------------------------- Jscript Three
                    335: 
                    336: sub jscript_three {
                    337:     my ($fullpath,$jsref) = @_;
                    338:     my $source = '';
                    339:     if (exists($ENV{'form.go'}) ) {
                    340:         $source = $ENV{'form.go'};
                    341:     }
                    342:     $$jsref = <<"END_OF_ONE";
                    343: function verify() {
                    344:     if ((document.forms.dataForm.newdir.value == '')  || (!document.forms.dataForm.newdir.value)) {
                    345:         alert("Step 4: You must choose a destination directory for the import") 
                    346:         return false
                    347:     }
                    348:     return true
                    349: } 
                    350: 
                    351: function nextPage() {
                    352:     if (verify()) {
                    353:         document.forms.dataForm.go.value="NextPage"
                    354:         document.forms.dataForm.submit()
                    355:     }
                    356: }
                    357: function backPage() {
                    358:     document.forms.dataForm.go.value="PreviousPage"
                    359:     document.forms.dataForm.submit()
                    360: }
                    361: 
                    362: function createWin() {
                    363:     document.dataForm.newdir.value = "";
                    364:     newWindow = window.open("","CreateDir","HEIGHT=400,WIDTH=750,scrollbars=yes")
                    365:     newWindow.document.open()
                    366:     newWindow.document.write("<html><head><title>Create Testbank directory</title><meta http-equiv='pragma' content='no-cache'>\\n")
                    367:     newWindow.document.write("</head><body bgcolor='#CCFFDD' topmargin='0' leftmargin='0' marginheight='0'marginwidth='0' rightmargin='0'>\\n")
                    368:     newWindow.document.write("<img border='0' src='/adm/lonInterFace/author.jpg' alt='[Author Header]'>\\n")
                    369:     newWindow.document.write("<table border='0' cellspacing='0' cellpadding='0' width='430' bgcolor='#CCFFDD'>\\n")
                    370:     newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
                    371:     newWindow.document.write("<td><h3>Location: <tt>$fullpath</tt></h3><h3>New Directory</h3></td></tr>\\n")
                    372:     newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
                    373:     newWindow.document.write("<td><form name='fileaction' action='/adm/cfile' method='post'>\\n")
                    374:     newWindow.document.write("<font face='arial,helvetica,sans-serif'>Enter the name of the new directory where you will store the converted testbank questions<br /><br />")
                    375:     newWindow.document.write("<input type='hidden' name='filename' value='$fullpath'>")   
                    376:     newWindow.document.write("<input type='hidden' name='action' value='newdir'>")
                    377:     newWindow.document.write("<input type='hidden' name='callingmode' value='testbank'>")
                    378:     newWindow.document.write("$fullpath<input type='text' name='newfilename' value=''/>")
                    379:     newWindow.document.write("<input type='button' value='Go' onClick='document.fileaction.submit();' />")
                    380:     newWindow.document.write("</td></tr>\\n")
                    381:     newWindow.document.write("</table></body></html>")
                    382:     newWindow.document.close()
                    383:     newWindow.focus()
                    384: }
                    385: END_OF_ONE
                    386:     if ($source eq "PreviousPage") { 
                    387:         $$jsref .= qq|  
                    388: function setElements() {
                    389:     var iter = 0
                    390:     var selParam = 0
                    391:         |;
                    392:         foreach my $item (keys %ENV) {
                    393:             if ($item =~ m/^form\.(\w+)$/) {
                    394:                 my $name = $1; 
                    395:                 my $value = $ENV{"form.$name"};
                    396:                 unless ($value eq "") {
                    397:                     if ($name eq "newdir") {
                    398:              	        $$jsref .= qq(    document.forms.dataForm.$name.value = "$value"\n);
                    399:                     }
                    400:                 }
                    401:             }
                    402:         }
                    403:         $$jsref .= "}";
                    404:     }
                    405: }
                    406: 
                    407: 
                    408: # ---------------------------------------------------------------- Jscript Four
                    409: sub jscript_four {
                    410:     my ($jsref,$fullpath) = @_;
                    411:     $$jsref = qq|   
                    412: function backtoStart() {
                    413:     document.location.href="$fullpath"
                    414: }
                    415: function backpage() {
                    416:     document.forms.verify.go.value="PreviousPage"
                    417:     document.forms.verify.submit()
                    418: }
                    419:     |;
                    420: }
                    421: 
                    422: # ---------------------------------------------------------------- Display Zero
                    423: sub display_zero {
                    424:     my ($r,$uname,$fn,$page,$fullpath) = @_;
                    425:     $r->print(qq|
                    426: <table border='0' cellspacing='0' cellpadding='0' width='100%'>
                    427:        <tr>
                    428:         <td>&nbsp;</td>
                    429:         <td colspan='2'><font face='arial,helvetica,sans-serif'> 
                    430: The <b>Testbank Upload</b> utility can be used by LON-CAPA authors to convert <i>multiple choice</i>, <i>multiple answer correct</i>, <i>fill-in-the-blank</i>, <i>ordering/ranking</i>, <i>true/false</i> and <i>essay</i> questions from a plain text testbank file to LON-CAPA problem files.  Five requirements must be met to ensure that you will succeed in converting your plain text file of testbank questions to functioning LON-CAPA problems.
                    431:         <ol>
                    432:          <li>The questions and answers you upload must be in plain text format.  Any header lines should occur before the text containing the questions and answers.</li>
                    433:          <li>All questions (including question text and all foils) must occur before any of the answers.  Each question should begin on a new line, and should start with the question number. Questions should be numbered sequentially using a number followed immediately by a space, a period, or enclosed in parentheses, i.e., 1 , 1., (1), 1), or (1 .</li>
                    434:          <li><i>Multiple choice</i> and <i>multiple answer correct</i> questions should consist of: (i) the question number followed by (ii) the question text beginning on the same line and (iii) two or more foils, with each foil beginning on a new line and prefixed by a unique letter, or Roman numeral, listed in alphabetic or numeric order, beginning at a (alphabetic) or i (Roman numeral), followed by a period, or enclosed in parentheses, i.e., a., (a), i., or (i).</li>
                    435:          <li>One or more correct answers should be provided for all questions (although blank answers may be provided for <i>essay</i> questions).  Answers should be numbered sequentially, using the same scheme as used for the questions, and must occur after <b>all</b> the questions.        
                    436:          <li>If <i>fill-in-the-blank</i> or <i>multiple answer</i> questions have more than one correct answer, each answer should appear in a comma-, tab-, space-, or new line-delimited list. For a <i>ranking/ordering</i> question, the "answer" should contain the foil identifiers correctly ordered in a similarly delimited list.</li> 
                    437:         </ol>      
                    438: Five steps are involved in the import process.
                    439:         <ol>
                    440:          <li>Upload your text file to the server.|);
                    441: 
                    442:     if ($fn eq '') {
                    443:         $r->print("<b>Incomplete</b>. Please return to the <a href='$fullpath'>construction space menu<a> to upload a file");
                    444:     } else {
                    445:         $r->print(" <b>Completed</b> - successful upload of <i>$fn</i>");
                    446:     } 
                    447:     $r->print(qq|</li>
                    448:          <li>Provide information about the question format - i.e.,  question numbering style, and the number of blocks of questions of each question type.</li>
                    449:          <li>Provide information about the questions in each block, including question type, start and end question numbers for each block, and foil labelling style and answer format where required.</li>
                    450:          <li>Create a new directory where you will store the converted testbank questions.</li> 
                    451:          <li>Complete the import of questions to the selected pool.</li>
                    452:         </ol>
                    453:         </font>
                    454:         </td>
                    455:        </tr>
                    456:        </table>
                    457:        <br />
                    458:        <br />
                    459:        <form name="info" method="post">
                    460:        <input type="hidden" name="uploaduname" value="$uname">
                    461:        <input type="hidden" name="filename" value="$fn">
                    462:        <input type="hidden" name="page" value="$page">
                    463:        <input type="hidden" name="phase" value="three">
                    464:        <input type="hidden" name="go" value="NextPage">
                    465:        <table border="0" width="100%">
                    466:         <tr>
                    467:          <td align='left'>
                    468:           <input type="button" name="goback" value="Exit Now" onClick="javascript:location.href='$fullpath'">
                    469:          </td>
                    470:          <td align='right'>
                    471:           <input type="button" name="nextpage" value="Continue to step 2" onClick="javascript:submit()">
                    472:          </td>
                    473:         </tr>
                    474:        </table>
                    475:        </form> 
                    476:     |);  
                    477: }
                    478: 
                    479: 
                    480: # ---------------------------------------------------------------- Display One
                    481: 
                    482: sub display_one {
                    483:     my ($r,$uname,$fn,$page,$textref) = @_;
                    484:     $r->print(qq|
                    485:    <form method='post' name='display'>
                    486:    <table border='0' cellspacing='0' cellpadding='3' width='100%'>
                    487:     <tr bgcolor='#ccddaa'>
                    488:      <td>&nbsp;</td> 
                    489:      <td align='left'>
                    490:        <h3><font face='arial,helvetica,sans-serif'>Step 2: Identification of blocks of questions</b>&nbsp;</font></h3>
                    491:      </td>
                    492:     </tr>
                    493:     <tr>
                    494:      <td colspan='2'>&nbsp;</td>
                    495:     </tr>
                    496:     <tr>
                    497:      <td colspan='2'>
                    498:       <table border="0" cellspacing="0" cellpadding="2">
                    499:        <tr>
                    500:         <td>&nbsp;</td>
                    501:         <td colspan='2'>
                    502:          <font face='arial,helvetica,sans-serif'><b>Testbank data uploaded to the server:</b></font>
                    503:        </td>
                    504:       </tr>
                    505:       <tr>
                    506:        <td colspan='3'>&nbsp;</td>
                    507:       </tr>
                    508:       <tr>
                    509:        <td>&nbsp;</td>
                    510:        <td colspan='2'>
                    511:          <textarea name="rawdata" cols="70" rows="6" wrap="virtual" align="center">
                    512:     |);
                    513:     foreach my $line (@{$textref}) {
                    514:         $line =~ s/\n//g;
                    515:         $r->print("$line\n");
                    516:     }
                    517:     $r->print(qq|
                    518:        </textarea>
                    519:       </td>
                    520:      </tr>
                    521:      <tr>
                    522:       <td colspan='3'>&nbsp;</td>
                    523:      </tr>
                    524:      <tr>
                    525:      <tr>
                    526:       <td colspan='3'>&nbsp;</td>
                    527:      </tr>
                    528:      <tr bgcolor='#ccddaa'>
                    529:       <td>&nbsp;</td>
                    530:       <td align='left' valign='middle'><img src="/res/adm/pages/bl_step1.gif">&nbsp;&nbsp;
                    531:       </td>
                    532:       <td>
                    533:        <font face='arial,helvetica,sans-serif'><b>
                    534: Select the format of the question number</b> [e.g., 1,  1., 1), (1 or (1)].&nbsp;
                    535:                  <select name="qnumformat">
                    536:                   <option value = "-1" selected>Please Select
                    537:                   <option value="number">1
                    538:                   <option value="period">1.
                    539:                   <option value="paren">(1)
                    540:                   <option value="leadparen">(1
                    541:                   <option value="trailparen">1)
                    542:                  </select>
                    543:        </font>
                    544:       </td>
                    545:      </tr>
                    546:      <tr>
                    547:       <td colspan='3'>&nbsp;</td>
                    548:      </tr>
                    549:      <tr>
                    550:       <td>&nbsp;</td>
                    551:       <td colspan='2'><font face='arial,helvetica,sans-serif'>
                    552: A number in the specified format should appear at the start of each question. For multiple choice questions, the question number must begin the line that contains the question text; foils (starting (a), (i) etc.) should occur on subsequent lines. Correct answers should be numbered in the same way as the questions and should appear after <b>all</b> the questions (including question text and possible foils for all questions). Each numbered question must have a corresponding numbered answer, although the answer itself may be blank for essay questions.</td>
                    553:      </tr>
                    554:      <tr>
                    555:       <td colspan="3">&nbsp;</td>
                    556:      </tr>
                    557:      <tr>
                    558:       <td>&nbsp;</td>
                    559:       <td colspan='2'>
                    560:        <font face='arial,helvetica,sans-serif'>
                    561:         For example, you would select <b>1.</b> if your text file contained the following questions:<br><br>
                    562:  1. The capital of the USA is ..<br />
                    563:  &nbsp;&nbsp;(a) Washington D.C.<br />
                    564:  &nbsp;&nbsp;(b) New York<br />
                    565:  &nbsp;&nbsp;(c) Los Angeles<br />
                    566:  <br />
                    567:  2. The capital of Canada is ..<br />
                    568:  &nbsp;&nbsp;(a) Toronto<br />
                    569:  &nbsp;&nbsp;(b) Vancouver<br />
                    570:  &nbsp;&nbsp;(c) Ottawa<br />
                    571: <br />
                    572:  3. Describe an experiment you could conduct to measure c, the speed of light in a vacuum. <br /><br />
                    573:  1. (a)<br />
                    574:  2. (c)<br />
                    575:  3. <br />
                    576:       </td>
                    577:      <tr>
                    578:       <td colspan="3">&nbsp;</td>
                    579:      </tr>
                    580:      <tr bgcolor='#ccddaa'>
                    581:       <td>&nbsp;</td>
                    582:       <td><img src='/res/adm/pages/bl_step2.gif' align='left' valign='middle'>&nbsp;&nbsp;
                    583:       </td>
                    584:       <td>
                    585:        <font face='arial,helvetica,sans-serif'><b>
                    586: Please indicate the number of blocks of different question types in the text file.</b>&nbsp;&nbsp;
                    587:          <input type="text" name="blocks" value="" size="5">
                    588:       </td>
                    589:      </tr>
                    590:      <tr>
                    591:       <td colspan="3">&nbsp;</td>
                    592:      </tr>
                    593:      <tr>
                    594:       <td>&nbsp;</td>
                    595:       <td colspan='2'>
                    596:        <font face='arial,helvetica,sans-serif'>
                    597:         For example, you would enter <b>6</b> if your text file contained the following sequence of questions:<br><br>
                    598:  10 multiple choice questions<br>
                    599:   5 essay questions<br>
                    600:   5 fill-in-the-blank questions<br>
                    601:   5 multiple answer questions<br>
                    602:   4 multiple choice questions<br>
                    603:   3 essay questions<br>
                    604:       </font>
                    605:       </td>
                    606:      </tr>
                    607:      <tr>
                    608:       <td colspan='3'>&nbsp;</td>
                    609:      </tr>
                    610:      <tr>
                    611:        <td>&nbsp;</td>
                    612:        <td colspan='2'><font face='arial,helvetica,sans-serif'>You will indicate the question type and the question number range for each of the blocks on the next page.</font></td>
                    613:      </tr>
                    614:      <tr>
                    615:       <td colspan='3'>&nbsp;</td>
                    616:      </tr>
                    617:      <tr>
                    618:       <td colspan='3'>
                    619:        <table border='0' width="100%" cellspacing='0' cellpadding='2'>
                    620:         <tr>
                    621:          <td align='left'>
                    622:           <input type="button" name="backpage" value="Go back to step 1" onClick="javascript:backPage()">
                    623:          </td>
                    624:          <td align='right'>
                    625:           <input type="button" name="nextpage" value="Continue to step 3" onClick="javascript:nextPage()">
                    626:          </td>
                    627:         </tr>
                    628:        </table>
                    629:        <input type="hidden" name="page" value ="$page">
                    630:        <input type="hidden" name="go" value="">
                    631:        <input type="hidden" name="uploaduname" value="$uname">
                    632:        <input type="hidden" name="filename" value="$fn">
                    633:        <input type="hidden" name="phase" value="three">
                    634:       </td>
                    635:      </tr>
                    636:     </table>
                    637:    </td>
                    638:   </tr>
                    639:  </table>
                    640:  </form>
                    641:     |);
                    642: }
                    643: 
                    644: # ---------------------------------------------------------------- Display Two
                    645: 
                    646: sub display_two {
                    647:     my ($r,$uname,$fn,$page,$textref,$qcount) = @_;
                    648:     my $blocks = $ENV{'form.blocks'};
                    649:     my $qnumformat = $ENV{'form.qnumformat'};
                    650:     my @types = ("MC","MA","TF","Ess","FIB","Ord");
                    651:     my %typenames = (
                    652:              MC => "Multiple Choice",
                    653:              TF => "True/False",
                    654:              MA => "Multiple Answer",
                    655:              Ess => "Essay",
                    656:              FIB => "Fill-in-the-blank",
                    657:              Ord => "Ranking/ordering",
                    658:              );
                    659:     my %qnumtypes = (
                    660:              number => "1",
                    661:              period => "1.",
                    662:              paren => "(1)",
                    663:              leadparen => "(1",
                    664:              trailparen => "1)",
                    665:              );
                    666:     my @bgcolors = ('#ffffff','#eeeeee');
                    667:     my $bl1st = '';
                    668:     my $bl1end = '';
                    669:     if ($blocks == 1) {
                    670:         $bl1st = '1';
                    671:         $bl1end = $qcount;
                    672:     }
                    673:     $r->print(<<"END_OF_FUNC");
                    674:  <h3><font face='arial,helvetica,sans-serif'>Step 3: Classification of blocks</b>&nbsp;</font></h3>
                    675: <form method='post' name='display'>
                    676:    <table border='0' cellspacing='0' cellpadding ='0'>
                    677:     <tr>
                    678:      <td colspan='2'>&nbsp;</td>
                    679:     </tr>
                    680:     <tr>
                    681:      <td colspan='2'>
                    682:       <table border="0" cellspacing="0" cellpadding="0">
                    683:        <tr>
                    684:         <td>&nbsp;</td>
                    685:         <td><font face='arial,helvetica,sans-serif'>
                    686:           You indicated that <b>all</b> questions (and the corresponding answer(s) for each question) begin with a number in the following format:  <b>$qnumtypes{$qnumformat}</b>.<br><br>A total of <b>$qcount</b> questions and <b>$qcount</b> corresponding answers were found in the file you uploaded. If this questions total does not match the number you expect, please examine your original text file to verify that each question <i>and</i> each answer begins with a number in the specified format. If necessary use a text editor to edit your text file of questions, and click "Return to step 2" on this page and the "Return to Step 1" on the preceding page, so you can upload your text file again.<br><br>
                    687:           You also indicated that the <b>$qcount</b> questions can be divided into <b>$blocks</b> blocks of questions of a particular question type.</font>
                    688:         </td>
                    689:        </tr>
                    690:        <tr>
                    691:         <td colpsan='2'>&nbsp;</td>
                    692:        </tr>
                    693:        <tr>
                    694:         <td>&nbsp;</td>
                    695:         <td><font face='arial,helvetica,sans-serif'>
                    696:           Please provide additional information below ,about the types of questions you have uploaded, and, if applicable, the format of answers and &quot;foils&quot; for specific types of questions.
                    697:         </td>
                    698:        </tr>
                    699:        <tr>
                    700:         <td colpsan='2'>&nbsp;</td>
                    701:        </tr>
                    702:        <tr>
                    703:         <td>&nbsp;</td>
                    704:         <td>
                    705: <font face='arial,helvetica,sans-serif'>The following data were uploaded to the server</font><br>
                    706: <textarea name="rawdata" cols="70" rows="6" wrap="virtual" align="center">
                    707: END_OF_FUNC
                    708:     foreach my $line (@{$textref}) {
                    709:         $line =~ s/\n//g;
                    710:         $r->print("$line\n");
                    711:     }
                    712:     $r->print(qq| 
                    713: </textarea>
                    714:       </td>
                    715:      </tr>
                    716:      <tr>
                    717:       <td colspan='2'>&nbsp;</td>
                    718:      </tr>
                    719:      <tr bgcolor='#ccddaa'>
                    720:       <td><img src='/res/adm/pages/bl_step3.gif' align='left' valign='middle'>
                    721:       </td>
                    722:       <td width='100%' align='left'>&nbsp;&nbsp;
                    723:        <font size='+1' face='arial,helvetica,sans-serif'><b>Information about question types and formats in each block.</b></font>
                    724:       </td>
                    725:      </tr>
                    726:      <tr>
                    727:       <td colspan='2'>&nbsp;</td>
                    728:      </tr>
                    729:      <tr>
                    730:       <td>&nbsp;</td>
                    731:       <td><font face='arial,helvetica,sans-serif'>For <i>each</i> of the <b>$blocks</b> question blocks, please specify the question numbers of the first and last questions in the block (e.g., 1 and 10), and the question type of the questions in the block. Please provide additional information about foil formats and answer formats if required for the question type you selected.</font>
                    732:       </td>
                    733:      </tr>
                    734:      <tr>
                    735:       <td colspan='2'>&nbsp;</td>
                    736:      </tr>
                    737:      <tr>
                    738:       <td>&nbsp;</td>
                    739:       <td>
                    740:        <table border="0" cellspacing="0" cellpadding="0" bgcolor="#000000" align="left">
                    741:         <tr>
                    742:          <td>
                    743:           <table border="1" valign="top" align="center" width='100%'>
                    744:            <tr bgcolor="#eef6f6" align="left">
                    745:             <td>
                    746:              <table border='0' cellspacing='1' cellpadding='1' align='left' width='100%'>
                    747:               <tr bgcolor='#CCDDAA' align='center' valign='middle'>
                    748:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>
                    749:                 &nbsp;<b>Block</b>&nbsp;</font>
                    750:                </td>
                    751:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>&nbsp;
                    752:                 &nbsp;<b>First&nbsp;number</b>&nbsp;</font>
                    753:                </td>
                    754:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>&nbsp;
                    755:                 &nbsp;<b>Last&nbsp;number</b>&nbsp;</font>
                    756:                </td>
                    757:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>&nbsp;
                    758:                 &nbsp;<b>Question&nbsp;type</b>&nbsp;</font>
                    759:                </td>
                    760:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>&nbsp;
                    761:                 &nbsp;<b>Foil&nbsp;format</b>&nbsp;</font>
                    762:                </td>
                    763:                <td align="center" valign="middle" height='10'><font face='arial,helvetica,sans-serif'>&nbsp;
                    764:                 &nbsp;<b>Answer&nbsp;format</b>&nbsp;</font>
                    765:                </td>
                    766:               </tr>
                    767:     |);
                    768:     for (my $i=0; $i<$blocks; $i++) {
                    769:         my $iter = $i+1;
                    770:         my $rowcol = $i%2;
                    771:         $r->print(qq|
                    772:  <tr bgcolor="$bgcolors[$rowcol]">
                    773:   <td align="left">
                    774:    <font face='arial,helvetica,sans-serif'>&nbsp;$iter.</font>
                    775:   </td>
                    776:   <td align="left">&nbsp;
                    777:    <input type="text" name="start_$i" value="$bl1st" size="5">&nbsp;
                    778:   </td>
                    779:   <td align="left">&nbsp;
                    780:    <input type="text" name="end_$i" value="$bl1end" size="5">&nbsp;
                    781:   </td>
                    782:   <td align="left">
                    783:    <font face='arial,helvetica,sans-serif'>
                    784:    <select name="qtype_$i" onChange="colSet($i)">
                    785:     <option value= "-1" selected>Please Select
                    786:         |);
                    787:         foreach my $qtype (@types) {
                    788:             $r->print(qq|<option value="$qtype">$typenames{$qtype}|);
                    789:         }
                    790:         $r->print(qq|
                    791:    </select>
                    792:    </font>
                    793:   </td>
                    794:   <td align="left">&nbsp;
                    795:     <select name="foilformat_$i">
                    796:      <option value="-1">&lt;--- Set type&nbsp; 
                    797:     </select>&nbsp;
                    798:   </td>
                    799:   <td align="left">&nbsp;
                    800:     <select name="ansr_$i">
                    801:      <option value="-1">&lt;--- Set type&nbsp;
                    802:     </select>
                    803:   </td>
                    804:  </tr>
                    805:         |);
                    806:     }
                    807:     $r->print(qq|
                    808:        </table>
                    809:       </td>
                    810:      </tr>
                    811:     </table>
                    812:    </td>
                    813:   </tr>
                    814:  </table>
                    815: </td>
                    816: </tr>
                    817: <tr>
                    818:  <td colspan="2">&nbsp;</td>
                    819: </tr>
                    820: <tr>
                    821:  <td>&nbsp;</td>
                    822:  <td>
                    823: <font face='arial,helvetica,sans-serif'>For <i>multiple choice</i>, <i>multiple correct answer</i> and <i>ranking</i> type questions, you must use the <b>Foil format</b> column to choose the format of the identifier used for each of the possible answers (e.g., (a), a, a., i, (i) etc.) provided for a given question stem. For <i>multiple correct answer</i> and <i>fill-in-the-blank</i> questions with more than one correct answer you must use the <b>Answer format</b> column to choose the separator used between the answers, e.g., if the correct answers for question 28. were listed as: 28. (a),(d),(e) you would choose &quot;comma&quot;, or if they were listed as:</font><br><table border='0'><tr><td><font face='arial,helvetica,sans-serif'>28.&nbsp</font></td><td><font face='arial,helvetica,sans-serif'>(a)</font></td></tr><tr><td>&nbsp;</td><td><font face='arial,helvetica,sans-serif'>(d)</font></td></tr><tr><td>&nbsp;</td><td><font face='arial,helvetica,sans-serif'>(e)</font></td></tr></table>
                    824: <font face='arial,helvetica,sans-serif'>you would choose &quot;new line&quot;. For <i>true/false</i> questions you must use the <b>Answer format</b> column to choose how the correct answer - True or False, is displayed in the text file (e.g., T or F, true or false etc.). For <i>ranking</i> questions you must use the <b>Answer format</b> column to choose the separator used between the (ranked) answers.</font><br><br>
                    825:       </td>
                    826:      </tr>
                    827:      <tr>
                    828:       <td colspan='2'>&nbsp;</td>
                    829:      </tr>
                    830:      <tr>
                    831:       <td colspan='2'>
                    832: <input type="hidden" name="blocks" value="$blocks">
                    833: <input type="hidden" name="qnumformat" value="$qnumformat">
                    834:    <table border='0' width="100%" cellspacing='0' cellpadding='2'>
                    835:     <tr>
                    836:      <td align='left'>
                    837:       <input type="button" name="backpage" value="Go back to step 2" onClick="javascript:backPage()">
                    838:      </td>
                    839:      <td align='right'>
                    840:       <input type="button" name="nextpage" value="Continue to step 4" onClick="javascript:nextPage()">
                    841:      </td>
                    842:     </tr>
                    843:    </table>
                    844:    <input type="hidden" name="page" value ="$page">
                    845:    <input type="hidden" name="go" value="">
                    846:    <input type="hidden" name="uploaduname" value="$uname">
                    847:    <input type="hidden" name="filename" value="$fn">
                    848:    <input type="hidden" name="phase" value="three">
                    849:    </form>
                    850:   </td>
                    851:  </tr>
                    852: </table>
                    853: </td>
                    854: </tr>
                    855: </table>
                    856:     |);
                    857: } 
                    858: # ---------------------------------------------------------------- Display Three
                    859: sub display_three { 
                    860:     my ($r,$uname,$fn,$page,$textref,$qcount) = @_;
                    861:     my $qnumformat = $ENV{'form.qnumformat'};
                    862:     my $filename = $ENV{'form.filename'};
                    863:     my $source = $ENV{'form.go'};
                    864:     my $blocks = $ENV{'form.blocks'};
                    865:     my @items = ();
                    866:     my @bgcolors = ('#ffffff','#eeeeee');
                    867:     my @types = ("MC","MA","TF","Ess","FIB","Ord");
                    868:     my @alphabet = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
                    869:     my @romans = ("i","ii","iii","iv","v","vi","vii","viii","ix","x","xi","xii","xiii","xiv","xv","xvi","xvii","xviii","xix","xx","xxi","xxii","xxiii","xxiv","xxv","xxvi");
                    870:     my @start = ();
                    871:     my @end = ();
                    872:     my @nums = ();
                    873:     my @qtype = ();
                    874:     my @foilformats = ();
                    875:     my @ansrtypes = ();
                    876:     my %multparts = ();
                    877:     my $numitems = 0;
                    878:     for (my $i=0; $i<$blocks; $i++) {
                    879:         if (($ENV{"form.start_$i"} ne '') && ($ENV{"form.end_$i"} ne '')) {
                    880:             $start[$i] = $ENV{"form.start_$i"};
                    881:             $end[$i] = $ENV{"form.end_$i"};
                    882:             $nums[$i] = $end[$i]-$start[$i] +1;
                    883:             $qtype[$i] = $ENV{"form.qtype_$i"};
                    884:             if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "Ord")) {
                    885:                 $foilformats[$i] = $ENV{"form.foilformat_$i"};
                    886:             } else {
                    887:                 $foilformats[$i] = '';
                    888:             } 
                    889:             if (($qtype[$i] eq "MA") || ($qtype[$i] eq "FIB") || ($qtype[$i] eq "TF") || ($qtype[$i] eq "Ord")) {
                    890:                 $ansrtypes[$i] = $ENV{"form.ansr_$i"};
                    891:             } else {
                    892:                 $ansrtypes[$i] = '';
                    893:             }  
                    894:         } else { 
                    895:             $nums[$i] = 0;
                    896:         }
                    897:         $numitems += $nums[$i];
                    898:     }
                    899: 
                    900:     my $import = join//,@{$textref};
                    901:     @items = &file_split(\@start,\@end,\@nums,$qnumformat,\@foilformats,$textref,\%multparts,$numitems,\@qtype,$blocks);
                    902:     $r->print(<<"END_OF_ONE");
                    903:  <h3><font face='arial,helvetica,sans-serif'>Step 4: Review and selection of destination directory</b>&nbsp;</font></h3>
                    904: <form name="dataForm" method="post">
                    905: <table border='0' bgcolor='#CCFFDD' cellspacing='0' cellpadding ='0'>
                    906:       <tr>
                    907:        <td colspan='2'>
                    908:         <table border='0' cellspacing='0' cellpadding='0'>
                    909:        <tr>
                    910:         <td colspan='2'>&nbsp;</td>
                    911:        </tr>
                    912:        <tr>
                    913:         <td>&nbsp;</td>
                    914:         <td><font face='arial,helvetica,sans-serif'><b>
                    915: Based on your previous responses your data have been split into a total of $numitems questions.</b> 
                    916:         </td>
                    917:       </tr>
                    918:       <tr>
                    919:         <td colspan='2'>&nbsp;</td>
                    920:       </tr>
                    921:       <tr>
                    922:        <td>&nbsp;</td>
                    923:      <td width="80%" bgcolor="#000000" align="left">
                    924:      <table width="100%" border="0" cellpadding="1" cellspacing="0">
                    925:       <tr>
                    926:        <td width="100%" bgcolor="#000000">
                    927:         <table border="0" cellspacing="0" cellpadding="1" width="100%">
                    928:          <tr>
                    929:           <td width="100%" bgcolor="#000000">
                    930:            <table border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff" width="100%">
                    931:             <tr>
                    932:              <td bgcolor="#CCFFDD" width="100%">
                    933:               <table border='0' cellspacing='1' cellpadding='2' align='left' width= '100%'>
                    934:                <tr><td bgcolor="#CCDDAA" align="center" width='3%'><font face='arial,helvetica,sans-serif'><b>#</b></font></td><td bgcolor="#CCDDAA" align="center" width='5%'><font face='arial,helvetica,sans-serif'><b>Type</b></font></td><td bgcolor="#CCDDAA" align="center" width='60%'><font face='arial,helvetica,sans-serif'><b>Question</b></font></td><td bgcolor="#CCDDAA" align="center" width='32%'><font face='arial,helvetica,sans-serif'><b>Answer</b></font></td></tr>
                    935: END_OF_ONE
                    936:     for (my $j=0; $j<$numitems; $j++) {
                    937:         my $qnum = $j+1;
                    938:         my $rowcol = $j%2;
                    939:         $rowcol = @bgcolors[$rowcol];
                    940:         for (my $i=0; $i<$blocks; $i++) {
                    941:             if ($nums[$i] > 0) {
                    942:                 if (($j+1 >= $start[$i]) && ($j+1 <= $end[$i])) { 
                    943:                     if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA")) { 
                    944:                         for (my $k=0; $k<@{$multparts{$j}}; $k++) {
                    945:                             if ($k == 0) {
                    946:                                 $r->print(qq|<tr><td bgcolor="$rowcol" valign='top'><font face='arial,helvetica,sans-serif'>$qnum.</font></td><td bgcolor="$rowcol" valign='top'><font face='arial,helvetica,sans-serif'><b>$qtype[$i]</b></font></td><td bgcolor="$rowcol" valign='top'><font face='arial,helvetica,sans-serif'>$multparts{$j}[$k]<br><br>\n|);
                    947:                             } else { 
                    948:                                 my $foiltag = '';
                    949:                                 if ($foilformats[$i] eq "lcperiod") {
                    950:                                     $foiltag = $alphabet[$k-1].'.'; 
                    951:                                 } elsif ($foilformats[$i] eq "lcparen") {
                    952:                                     $foiltag = '('.$alphabet[$k-1].')';
                    953:                                 } elsif ($foilformats[$i] eq "ucperiod") {
                    954:                                     $foiltag = $alphabet[$k-1].'.';
                    955:                                     $foiltag =~ tr/a-z/A-Z/;
                    956:                                 } elsif ($foilformats[$i] eq "ucparen") {
                    957:                                     $foiltag = '('.$alphabet[$k-1].')';
                    958:                                     $foiltag =~ tr/a-z/A-Z/;
                    959:                                 } elsif ($foilformats[$i] eq "romperiod") {
                    960:                                     $foiltag = $romans[$k-1].'.';
                    961:                                 } elsif ($foilformats[$i] eq "romparen") {
                    962:                                     $foiltag = '('.$romans[$k-1].')';
                    963:                                 }
                    964:                                 $r->print(qq|$foiltag $multparts{$j}[$k]<br>\n|);
                    965:                             }
                    966:                         }
                    967:                         $r->print(qq|<br></font></td><td bgcolor="$rowcol" valign='top'><font face='arial,helvetica,sans-serif'>$items[$j+$numitems]</font></td></tr>|);
                    968:                     } else {
                    969:                         $r->print(qq|<tr><td bgcolor="$rowcol" valign="top"><font face='arial,helvetica,sans-serif'>$qnum.</font></td><td bgcolor="$rowcol" valign="top"><font face='arial,helvetica,sans-serif'><b>$qtype[$i]</b></font></td><td bgcolor="$rowcol" valign="top"><font face='arial,helvetica,sans-serif'>$items[$j]</font></td><td bgcolor="$rowcol" valign="top"><font face='arial,helvetica,sans-serif'>$items[$j+$numitems]</font></td></tr>|);
                    970:                     }
                    971:                     last;
                    972:                 }
                    973:             }
                    974:         }
                    975:     }
                    976:     $r->print(qq|
                    977:               </table>
                    978:               </td>
                    979:               </tr>
                    980:               </table>
                    981:              </td>
                    982:             </tr>
                    983:            </table>
                    984:           </td>
                    985:          </tr>
                    986:         </table>
                    987:        </td>
                    988:       </tr>
                    989:       <tr>
                    990:        <td colspan='2'>&nbsp;</td>
                    991:       </tr>
                    992:       <tr bgcolor='#ccddaa'>
                    993:        <td width='30' align='top'><img src='/res/adm/pages/bl_step4.gif'>
                    994:        </td>
                    995:        <td width='100%' align='left'>&nbsp;&nbsp;
                    996:         <font size='+1' face='arial,helvetica,sans-serif'><b>Create a directory to store your testbank questions.</b></font>
                    997:        </td>
                    998:       </tr>
                    999:       <tr>
                   1000:        <td colspan='2'>&nbsp;</td>
                   1001:       </tr>
                   1002:       <tr>
                   1003:        <td>&nbsp;</td>
                   1004:        <td>
                   1005:         <font face='Arial,Helvetica,sans-serif'>
                   1006: Please choose a destination LON-CAPA directory in which to store your uploaded questions.&nbsp;&nbsp;
                   1007:        <input type="button" name="createdir" value="Create Directory" onClick="javascript:createWin()"><input type="hidden" name="newdir" value=""></font></td>
                   1008:       </tr>
                   1009:       <tr>
                   1010:        <td colspan='2'>&nbsp;</td>
                   1011:       </tr>
                   1012:       <tr>
                   1013:        <td>&nbsp;</td>
                   1014:        <td><font face='arial,helvetica,sans-serif'>If you are satisfied with the questions and answers extracted from your uploaded text file, as shown above, and you have created a destination directory click the "Continue to step 5" button to convert the questions in your testbank to LON-CAPA problem files.</font></td>
                   1015:       </tr>
                   1016:       <tr>
                   1017:        <td colspan='2'>
                   1018:           <input type='hidden' name="go" value="">
                   1019:           <input type='hidden' name="qnumformat" value="$qnumformat">
                   1020:           <input type='hidden' name="blocks" value="$blocks">
                   1021:           <input type="hidden" name="uploaduname" value="$uname">
                   1022:           <input type="hidden" name="filename" value="$fn">
                   1023:           <input type='hidden' name="page" value="$page">
                   1024:           <input type="hidden" name="phase" value="three">
                   1025:     |);
                   1026:     for (my $i=0; $i<$blocks; $i++) {
                   1027:         $r->print(qq|
                   1028:           <input type='hidden' name="start_$i" value="$start[$i]">
                   1029:           <input type='hidden' name="end_$i" value="$end[$i]">
                   1030:           <input type='hidden' name="qtype_$i" value="$qtype[$i]">
                   1031:         |);
                   1032:         if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "Ord")) {
                   1033:             $r->print(qq|
                   1034:           <input type='hidden' name="foilformat_$i" value="$foilformats[$i]">
                   1035:             |);
                   1036:         }
                   1037:         if (($qtype[$i] eq "MA") || ($qtype[$i] eq "FIB") || ($qtype[$i] eq "TF") || ($qtype[$i] eq "Ord")) {
                   1038:             $r->print(qq|
                   1039:           <input type='hidden' name="ansr_$i" value="$ansrtypes[$i]">
                   1040:             |);
                   1041:         }
                   1042:     }
                   1043:     $r->print(qq|
                   1044:        </td>
                   1045:       </tr>
                   1046:       <tr>
                   1047:        <td colspan='2'>&nbsp;<br /><br /></td>
                   1048:       </tr>
                   1049:       <tr>
                   1050:        <td colspan='2'>
                   1051:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                   1052:          <tr>
                   1053:           <td align='left'>
                   1054:            <input type="button" name="backpage" value="Go back to step 3" onClick="javascript:backPage()">
                   1055:           </td>
                   1056:           <td align='right'>
                   1057:            <input type="button" name="nextpage" value="Continue to step 5" onClick="javascript:nextPage()">
                   1058:           </td>
                   1059:          </tr>
                   1060:         </table>
                   1061:        </td>
                   1062:       </tr>
                   1063:      </table>
                   1064:     </td>
                   1065:    </tr>
                   1066:   </table>
                   1067: </form>
                   1068:     |);
                   1069: }
                   1070: 
                   1071: # ---------------------------------------------------------------- Final Display
                   1072: sub final_display {
                   1073:     my ($r,$uname,$fn,$page,$textref) = @_;
                   1074:     my $qnumformat = $ENV{'form.qnumformat'};
                   1075:     my $blocks = $ENV{'form.blocks'};
                   1076:     my $newdir = $ENV{'form.newdir'};
                   1077:     my $linkdir = $newdir;
                   1078:     if ($linkdir =~ m#^/home/$uname/public_html/(.+)$#) {
                   1079:         $linkdir = '/priv/'.$uname.'/'.$1;
                   1080:     }
                   1081:     my $question_id = '';
                   1082:     my @question_title = ();
                   1083:     my @question_status  = ();
                   1084:     my @qtype = ();
                   1085:     my @start = ();
                   1086:     my @nums = ();
                   1087:     my @end = ();
                   1088:     my @foilformats = ();
                   1089:     my @ansrtypes = ();
                   1090:     my %multparts = ();
                   1091:     my $numitems = 0;
                   1092:     for (my $i=0; $i<$blocks; $i++) {
                   1093:         $start[$i] = $ENV{"form.start_$i"};
                   1094:         $end[$i] = $ENV{"form.end_$i"};
                   1095:         if (($end[$i] - $start[$i]) >= 0) {
                   1096:             $nums[$i] = $end[$i] - $start[$i]+1;
                   1097:         } else {
                   1098:             $nums[$i] = 0;
                   1099:         }
                   1100:         $qtype[$i] = $ENV{"form.qtype_$i"};
                   1101:         if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "Ord")) {
                   1102:             $foilformats[$i] = $ENV{"form.foilformat_$i"};
                   1103:         } else {
                   1104:             $foilformats[$i] = '';
                   1105:         }
                   1106:         if (($qtype[$i] eq "MA") || ($qtype[$i] eq "FIB") || ($qtype[$i] eq "TF") || ($qtype[$i] eq "Ord")) {
                   1107:             $ansrtypes[$i] = $ENV{"form.ansr_$i"};
                   1108:         }
                   1109:         $numitems += $nums[$i];
                   1110:     }
                   1111: 
                   1112:     my @bgcolors = ('#ffffff','#eeeeee');
                   1113: 
                   1114:     my $import = join/'\s'/,@{$textref};
                   1115:     my %answers = ();
                   1116:     my @items = &file_split(\@start,\@end,\@nums,$qnumformat,\@foilformats,$textref,\%multparts,$numitems,\@qtype,$blocks);
                   1117: 
                   1118: # Converting MC and MA answer to number, and splitting answers for FIB, and ordering for Ord.
                   1119:   my @alphabet = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
                   1120:     my @romans = ("i","ii","iii","iv","v","vi","vii","viii","ix","x","xi","xii","xiii","xiv","xv","xvi","xvii","xviii","xix","xx","xxi","xxii","xxiii","xxiv","xxv","xxvi");
                   1121:     my %patterns = (
                   1122:          comma => ',',
                   1123:          space => '\s+',
                   1124:          line => '[\r\n\f]+',
                   1125:          tab => '\t+',
                   1126:        );
                   1127:     for (my $i=0; $i<$blocks; $i++) {
                   1128:         if ($nums[$i] > 0) {
                   1129:             if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "FIB") || ($qtype[$i] eq "Ord")) {
                   1130:                 for (my $k=$numitems+$start[$i]-1; $k<$numitems+$end[$i]; $k++) {
                   1131:                     @{$answers{$k}} = ();
                   1132:                     if ($qtype[$i] eq "MC") {
1.4     ! raeburn  1133:                         $items[$k] =~ tr/A-Z/a-z/;
1.1       raeburn  1134:                         $items[$k] =~ s/\W//g;
                   1135:                         if ($foilformats[$i] eq "lcperiod" || $foilformats[$i] eq "lcparen" || $foilformats[$i] eq "ucparen" || $foilformats[$i] eq "ucperiod") {
                   1136:                             for (my $j=0; $j<@alphabet; $j++) {
                   1137:                                 if ($alphabet[$j] eq $items[$k]) {
                   1138:                                     push @{$answers{$k}}, $j;
                   1139:                                     last;
                   1140:                                 }
                   1141:                             }
                   1142:                         } elsif (($foilformats[$i] eq "romparen") || ($foilformats[$i] eq "romperiod")) {
                   1143:                             for (my $j=0; $j<@romans; $j++) {
                   1144:                                 if ($romans[$j] eq $items[$k]) {
                   1145:                                     push @{$answers{$k}}, $j;
                   1146:                                     last;
                   1147:                                 }
                   1148:                             }
                   1149:                         }
                   1150:                     } elsif (($qtype[$i] eq "MA") || ($qtype[$i] eq "Ord")) {
1.4     ! raeburn  1151:                         $items[$k] =~ tr/A-Z/a-z/;
1.1       raeburn  1152:                         my @corrects = split/$patterns{$ansrtypes[$i]}/,$items[$k];
                   1153:                         foreach my $correct (@corrects) {
                   1154:                             $correct =~s/\W//g;
                   1155:                             if ($foilformats[$i] eq "lcperiod" || $foilformats[$i] eq "lcparen" || $foilformats[$i] eq "ucparen" || $foilformats[$i] eq "ucperiod") {
                   1156:                                 for (my $j=0; $j<@alphabet; $j++) {
                   1157:                                     if ($alphabet[$j] eq $correct) {
                   1158:                                         push @{$answers{$k}}, $j;
                   1159:                                         last;
                   1160:                                     }
                   1161:                                 }
                   1162:                             } elsif (($foilformats[$i] eq "romparen") || ($foilformats[$i] eq "romperiod")) {
                   1163:                                 for (my $j=0; $j<@romans; $j++) {
                   1164:                                     if ($romans[$j] eq $correct) {
                   1165:                                         push @{$answers{$k}}, $j;
                   1166:                                         last;
                   1167:                                     }
                   1168:                                 }
                   1169:                             }
                   1170:                         }
                   1171:                     } elsif ($qtype[$i] eq "FIB") {
                   1172:                         @{$answers{$k}} = split/$patterns{$ansrtypes[$i]}/,$items[$k];
                   1173:                         for (my $j=0; $j<@{$answers{$k}}; $j++) {
                   1174:                             $answers{$k}[$j] =~ s/^\s+//;
                   1175:                             $answers{$k}[$j] =~ s/\s+$//;
                   1176:                         }
                   1177:                     }
                   1178:                 }
                   1179:             }
                   1180:         }
                   1181:     }
                   1182:     my $pooltarget = '';
                   1183:     my $pooldesc = '';
                   1184:     my @newquestions = ();
                   1185:     my $numquestions = 0;
                   1186:     my %qtype = ();
                   1187:     my %qtext = ();
                   1188:     my %qflag = ();
                   1189: 
                   1190:     $r->print(<<"END_OF_BLOCK");
                   1191:         <form name="verify" method="post">
                   1192:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                   1193:          <tr>
                   1194:           <td colspan='2'  align='left'>&nbsp;
                   1195:           </td>
                   1196:          </tr>
                   1197:          <tr bgcolor='#ccddaa'>
                   1198:           <td align='top'>&nbsp;
                   1199:           </td>
                   1200:           <td valign='middle'><img src='/res/adm/pages/bl_step5.gif'>&nbsp;&nbsp;
                   1201:            <font size='+1' face='arial,helvetica,sans-serif'>&nbsp;<b>Result of conversion of tesbank questions to LON-CAPA problems.</b></font>
                   1202:           </td>
                   1203:          </tr>
                   1204:          <tr>
                   1205:           <td colspan='2'>&nbsp;</td>
                   1206:          </tr>
                   1207: END_OF_BLOCK
                   1208:     if ($newdir ne "") {
                   1209:         my @qn_file = ();
                   1210:         my $qcount = 0;
                   1211:         for (my $i=0; $i<$blocks; $i++) {
                   1212:             if ($nums[$i] > 0) {
                   1213:                 if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "FIB") || ($qtype[$i] eq "Ord")) {
                   1214:                     for (my $j=$start[$i]-1; $j<$end[$i]; $j++) {
                   1215:                         my $answer = $j + $numitems;
                   1216:                         my $numans = scalar(@{$answers{$answer}});
                   1217:                         my $foilcount = 0;
                   1218:                         if (($qtype[$i] eq "MC") || ($qtype[$i] eq "MA") || ($qtype[$i] eq "Ord")) { 
                   1219:                             $foilcount = @{$multparts{$j}};
                   1220:                             $foilcount --;
                   1221:                         }
                   1222:                         $qn_file[$qcount] = &create_mcq($newdir,\@{$multparts{$j}},\@{$answers{$answer}},$qtype[$i],$j);
                   1223:                         $qcount ++;
                   1224:                         push @newquestions, $question_id;
                   1225:                     }
                   1226:                 } elsif ($qtype[$i] eq "TF") {
                   1227:                     for (my $j=$start[$i]-1; $j<$end[$i]; $j++) {
                   1228:                         my $answer = $j + $numitems;
                   1229:                         $items[$answer] =~ s/^\s+//;
                   1230:                         $items[$answer] =~ s/\s+$//;
                   1231:                         $items[$answer] =~ s/\W//g;
                   1232:                         $items[$answer] =~ tr/A-Z/a-z/;
                   1233:                         my $answer_id = '';
                   1234:                         if ($ansrtypes[$i] eq 'word' ) {
                   1235:                             if ($items[$answer] =~ m/true/) {
                   1236:                                 $answer_id = 0;
                   1237:                             } else {
                   1238:                                 $answer_id = 1;
                   1239:                             }
                   1240:                         } elsif ($ansrtypes[$i] eq 'lett') {
                   1241:                             if ($items[$answer] =~ m/^t/) {
                   1242:                                 $answer_id = 0;
                   1243:                             } else {
                   1244:                                 $answer_id = 1;
                   1245:                             }
                   1246:                         }
                   1247:                         $qn_file[$qcount] = create_ess($newdir,$answer_id,$items[$j],$items[$answer],$qtype[$i],$j);
                   1248:                         push @newquestions, $question_id;
                   1249:                         $qcount ++;
                   1250:                     }
                   1251:                 } elsif ($qtype[$i] eq "Ess") {
                   1252:                     for (my $j=$start[$i]-1; $j<$end[$i]; $j++) {
                   1253:                         my $answer = $j + $numitems;
                   1254:                         my $answer_id = '';
                   1255:                         $qn_file[$qcount] = create_ess($newdir,$answer_id,$items[$j],$items[$answer],$qtype[$i],$j);
                   1256:                         push @newquestions, $question_id;
                   1257:                         $qcount ++;
                   1258:                     }
                   1259:                 }
                   1260:             }
                   1261:         }
                   1262:         $r->print(qq|<tr><tr><td>&nbsp;</td><td><font face='arial,helvetica,sans-serif'>Individual problem files have been created from the problems included in the textbank file:
                   1263:        <ul>|);
                   1264:         for (my $i=0; $i<@qn_file; $i++) {
                   1265:             my $display = $i+1;
                   1266:             $r->print(qq|
                   1267:        <li><b><a href="$linkdir/$qn_file[$i]">Problem $display file</a></b></li>
                   1268:             |);
                   1269:         }
                   1270:         $r->print(qq|
                   1271:        </ul></font></td></tr>
                   1272:        <tr><td>&nbsp;</td>
                   1273:            <td><font face='arial,helvetica,sans-serif'>The problems must be published before they can be used in a course.</font></td>
                   1274:         |);
                   1275:     } else {
                   1276:         $r->print(qq|
                   1277:          <tr>
                   1278:           <td>&nbsp;</td>
                   1279:           <td><font face='arial,helvetica,sans-serif'>No destination file was selected or created, so import of your questions could not proceed.  
                   1280:           Please return to the previous page and select a valid file into which to import the questions. </font>
                   1281:            <input type="hidden" name="go" value="">
                   1282:            <input type="hidden" name="page" value="$page">
                   1283:            <input type="hidden" name="uploaduname" value="$uname">
                   1284:            <input type="hidden" name="filename" value="$fn">
                   1285:            <input type='hidden' name="page" value="$page">
                   1286:            <input type="hidden" name="phase" value="three">
                   1287:            <input type="hidden" name="qnumformat" value="$qnumformat">
                   1288:            <input type="hidden" name="newdir" value="$newdir">
                   1289:         |);
                   1290:         for (my $i=0; $i<$blocks; $i++) {
                   1291:            $r->print(qq|
                   1292:            <input type="hidden" name="start_$i" value="$start[$i]">
                   1293:            <input type="hidden" name="end_$i" value="$end[$i]">
                   1294:            <input type="hidden" name="qtype_$i" value="$qtype[$i]">
                   1295:            <input type="hidden" name="foilformat_$i" value="$foilformats[$i]">
                   1296:            <input type="hidden" name="ansr_$i" value="$ansrtypes[$i]">
                   1297:             |);
                   1298:         }
                   1299:         $r->print(<<"END_OF_FAIL");
                   1300:           </td>
                   1301:          </tr>
                   1302:          <tr>
                   1303:           <td colspan='2'>
                   1304:            <table border='0' width='100%'>
                   1305:             <tr>
                   1306:              <td align='right'>
                   1307:               <input type="button" name="backpage" value="Return to step 3" onClick="javascript:backPage()">
                   1308:              </td>
                   1309:             </tr>
                   1310:            </table>
                   1311:           </td>
                   1312:          </tr>
                   1313:         </table>
                   1314:        </td>
                   1315:       </tr>
                   1316:      </table>
                   1317:     </td>
                   1318:    </tr>
                   1319:   </table>
                   1320:  </form>
                   1321: </body>
                   1322: </html>
                   1323: END_OF_FAIL
                   1324:     return;
                   1325:   }
                   1326:   $r->print(<<"END_OF_BODY");
                   1327:              </table>
                   1328:             </td>
                   1329:            </tr>
                   1330:           </table>
                   1331:         </td>
                   1332:        </tr>
                   1333:        <tr>
                   1334:         <td colspan='2'>&nbsp;
                   1335:          <input type="hidden" name="go" value="">
                   1336:          <input type="hidden" name="page" value="$page">
                   1337:          <input type="hidden" name="uploaduname" value="$uname">
                   1338:          <input type="hidden" name="filename" value="$fn">
                   1339:          <input type='hidden' name="page" value="$page">
                   1340:          <input type="hidden" name="phase" value="one">
                   1341:          <input type="hidden" name="qnumformat" value="$qnumformat"> 
                   1342:          <input type="hidden" name="newdir" value="$newdir">
                   1343:         </td>
                   1344:        </tr>
                   1345:        <tr>
                   1346:         <td colspan='2'>
                   1347:          <table border='0' width='100%'>
                   1348:           <tr>
                   1349:            <td align='left'>
                   1350:              <input type='button' name='backtostart' value='Back to start page' onClick='javascript:backtoStart()'>
                   1351:            </td>
                   1352:           </tr>
                   1353:          </table>
                   1354:         </td>
                   1355:        </tr>
                   1356:       </table>
                   1357:      </td>
                   1358:     </tr>
                   1359:    </table>
                   1360:   </td>
                   1361:  </tr>
                   1362: </table>
                   1363: </form>
                   1364: END_OF_BODY
                   1365: }
                   1366: 
                   1367: sub question_count {
                   1368:     my ($qnumformat,$textref) = @_;
                   1369:     my $text_in = join "\n", @{$textref};
                   1370:     $text_in = "\n ".$text_in;
                   1371:     my $qpattern ='';
                   1372:     if ($qnumformat eq "period") {
                   1373:         $qpattern = '\d{1,}\.';
                   1374:     } elsif ($qnumformat eq "paren") {
                   1375:         $qpattern = '\(\d{1,}\)';
                   1376:     } elsif ($qnumformat eq "number") {
                   1377:         $qpattern = '\d{1,}';
                   1378:     } elsif ($qnumformat eq "leadparen") {
                   1379:         $qpattern = '\(\d{1,}';
                   1380:     } elsif ($qnumformat eq "trailparen") {
                   1381:         $qpattern = '\d{1,}\)';
                   1382:     }
                   1383:     my @questions = split/[\r\n\f]+\s?$qpattern\s?/,$text_in;
                   1384:     my $qcount = scalar(@questions);
                   1385:     $qcount = $qcount/2;
                   1386:     $qcount = int($qcount);
                   1387:     return $qcount;
                   1388: }
                   1389: 
                   1390: sub file_split {
                   1391:     my ($startsref,$endsref,$numsref,$qnumformat,$foilsref,$textref,$multpartsref,$numitems,$qtyperef,$blocks) = @_;
                   1392:     my $text_in = join "\n", @{$textref};
                   1393:     $text_in = "\n ".$text_in;
                   1394:     my $dignum = length($numitems);
                   1395:     my $numpat;
                   1396:     if ($dignum > 1) {
                   1397:         $numpat = ','.$dignum.'}';
                   1398:     } else {
                   1399:         $numpat = '}';
                   1400:     }
                   1401:     my $qpattern ='';
                   1402:     if ($qnumformat eq "period") {
                   1403:         $qpattern = '\d{1'.$numpat.'\.'; 
                   1404:     } elsif ($qnumformat eq "paren") {
                   1405:         $qpattern = '\(\d{1'.$numpat.'\)';
                   1406:     } elsif ($qnumformat eq "number") {
                   1407:         $qpattern = '\d{1'.$numpat;
                   1408:     } elsif ($qnumformat eq "leadparen") {
                   1409:         $qpattern = '\(\d{1'.$numpat;
                   1410:     } elsif ($qnumformat eq "trailparen") {
                   1411:         $qpattern = '\d{1'.$numpat.'\)';
                   1412:     }
                   1413:     my @questions = split/[\r\n\f]+\s?$qpattern\s?/,$text_in;
                   1414: # my @questions = split/\n\s\d{1,3}\.\s/,$text_in;
                   1415:     shift @questions;
                   1416:     my %multparts = ();
                   1417:     for (my $i=0; $i<$blocks; $i++) {
                   1418:         if (${$numsref}[$i] > 0) {
                   1419:             if ((${$qtyperef}[$i] eq "MC") || (${$qtyperef}[$i] eq "MA")) {
                   1420:                 my $splitstr = '';
                   1421:                 if (${$foilsref}[$i] eq "lcperiod") {
                   1422:                     $splitstr = '[a-z]\.';
                   1423:                 } elsif (${$foilsref}[$i] eq "lcparen") {
                   1424:                     $splitstr = '\([a-z]\)';
                   1425:                 } elsif (${$foilsref}[$i] eq "ucperiod") {
                   1426:                     $splitstr = '[A-Z]\.';
                   1427:                 } elsif (${$foilsref}[$i] eq "ucparen") {
                   1428:                     $splitstr = '\([A-Z]\)';
                   1429:                 } elsif (${$foilsref}[$i] eq "romperiod") {
                   1430:                     $splitstr = '[ivx]+\.';
                   1431:                 } elsif (${$foilsref}[$i] eq "romparen") {
                   1432:                     $splitstr = '\([ivx]+\)';
                   1433:                 }
                   1434:                 for (my $j=${$startsref}[$i]-1; $j<${$endsref}[$i]; $j++) {
                   1435:                     @{$multparts{$j}} = split/[\r\n\f]+\s?$splitstr\s?/,$questions[$j];
                   1436:                     chomp(@{$multparts{$j}});
                   1437:                     foreach my $foil (@{$multparts{$j}}) {
                   1438:                     } 
                   1439:                 }
                   1440:             } elsif (${$qtyperef}[$i] eq "FIB") { 
                   1441:                 for (my $j=${$startsref}[$i]-1; $j<${$endsref}[$i]; $j++) {
                   1442:                     @{$multparts{$j}} = ("$questions[$j]");
                   1443:                 }
                   1444:             }
                   1445:         }
                   1446:     }    
                   1447:     %{$multpartsref} = %multparts;
                   1448:     return @questions;
                   1449: }
                   1450:  
                   1451: # create_mcq builds an MC, MA, Ord or FIB question
                   1452: 
                   1453: sub create_mcq {
                   1454:     my ($newdir,$qstnref,$answerref,$qtype,$qnum) = @_;
                   1455:     $qnum ++;
                   1456:     if (length($qnum) == 1) {
                   1457:         $qnum = "00".$qnum;
                   1458:     } elsif (length($qnum) == 2) {
                   1459:         $qnum = "0".$qnum;
                   1460:     }
                   1461:     my $qstn = ${$qstnref}[0];
                   1462:     my $numfoils = scalar(@{$qstnref}) - 1; 
                   1463:     my $datestamp = localtime;
                   1464:     my $timestamp = time;
                   1465:     my $libfile = 'question_'.$qnum;
                   1466:     $libfile .= '.problem';
                   1467:     my $numansrs = scalar(@{$answerref});
                   1468:     my $output = qq|<problem>
                   1469:  <startouttext />$qstn<endouttext />
                   1470:     |;
                   1471:   
                   1472:     if ($qtype eq "MA") {
                   1473:         $output .= qq|
                   1474:    <optionresponse max="$numfoils" randomize="yes">
                   1475:     <foilgroup options="('True','False')">
                   1476:         |;
                   1477:         for (my $k=0; $k<@{$qstnref}-1; $k++) {
                   1478:             $output .= "   <foil name=\"foil".$k."\" value=\"";
                   1479:             if (grep/^$k$/,@{$answerref}) {
                   1480:                 $output .= "True\" location=\"random\"";
                   1481:             } else {
                   1482:                 $output .= "False\" location=\"random\"";
                   1483:             }
                   1484:             $output .= "\><startouttext />".${$qstnref}[$k+1]."<endouttext /></foil>\n";
                   1485:         }
                   1486:         chomp($output);
                   1487:         $output .= qq|
                   1488:     </foilgroup>
                   1489:    </optionresponse>
                   1490:   </problem>
                   1491:         |;
                   1492:     }
                   1493:     if ($qtype eq "MC") {
                   1494:         $output .= qq|
                   1495:    <radiobuttonresponse max="$numfoils" randomize="yes">
                   1496:     <foilgroup>
                   1497:         |;
                   1498:         for (my $k=0; $k<@{$qstnref}-1; $k++) {
                   1499:             $output .= "   <foil name=\"foil".$k."\" value=\"";
                   1500:             if (grep/^$k$/,@{$answerref}) {
                   1501:                 $output .= "true\" location=\"";
                   1502:             } else {
                   1503:                 $output .= "false\" location=\"";
                   1504:             }
                   1505:             if (lc (${$qstnref}[$k+1]) =~ m/^\s?([Aa]ll)|([Nn]one)\sof\sthe\sabove\.?/) { 
                   1506:                 $output .= "bottom\"";
                   1507:             } else {
                   1508:                 $output .= "random\"";
                   1509:             }
                   1510:             $output .= "\><startouttext />".${$qstnref}[$k+1]."<endouttext /></foil>\n";
                   1511:         }
                   1512:         chomp($output);
                   1513:         $output .= qq|
                   1514:     </foilgroup>
                   1515:    </radiobuttonresponse>
                   1516:   </problem>
                   1517:         |;
                   1518:     }
                   1519: 
                   1520:     if ($qtype eq "Ord") {
                   1521:         $output .= qq|
                   1522:    <rankresponse max="$numfoils" randomize="yes">
                   1523:     <foilgroup>
                   1524:         |;
                   1525:         for (my $k=0; $k<@{$qstnref}-1; $k++) {
                   1526:             $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$answerref[$k]."\><startouttext />".${$qstnref}[$k+1]."<endouttext /></foil>\n";
                   1527:         }
                   1528:         chomp($output);
                   1529:         $output .= qq|
                   1530:     </foilgroup>
                   1531:    </rankresponse>
                   1532:   </problem>
                   1533:         |;
                   1534:     }
                   1535:    
                   1536:     if ($qtype eq "FIB") {
                   1537:         my $numerical = 1;
                   1538:         for (my $i=0; $i<@{$answerref}; $i++) {
                   1539:             if (${$answerref}[$i] =~ m/([^\d\.]|\.\.)/) {
                   1540:                 $numerical = 0;
                   1541:             }
                   1542:         }
                   1543:         if ($numerical) {
                   1544:             my $numans;
                   1545:             my $tol;
                   1546:             if (@{$answerref} == 1) {
                   1547:                 $tol = 5;
                   1548:                 $numans = $$answerref[0];
                   1549:             } else {
1.2       raeburn  1550:                 my $min = $$answerref[0];
                   1551:                 my $max = $$answerref[0];    
                   1552:                 for (my $i=1; $i<@{$answerref}; $i++) {
                   1553:                     if ($$answerref[$i]<=$min) {
1.1       raeburn  1554:                         $min = $$answerref[$i];
1.2       raeburn  1555:                     } elsif ($$answerref[$i] >= $max) {
1.1       raeburn  1556:                         $max = $$answerref[$i];
                   1557:                     }
                   1558:                 }
                   1559:                 $numans = ($max + $min)/2;
                   1560:                 $tol = 100*($max - $min)/($numans*2); 
                   1561:             }
                   1562:             $output .= qq|
                   1563: <numericalresponse answer="$numans">
                   1564: 	<responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
                   1565: 	<responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
                   1566: 	<textline />
                   1567: </numericalresponse>
                   1568: </problem>
                   1569: |;
                   1570:         } else {
                   1571:             if (@{$answerref} == 1) {
                   1572:                 $output .= qq|
                   1573: <stringresponse answer="$$answerref[0]" type="ci">
                   1574: <textline>
                   1575: </textline>
                   1576: </stringresponse>
                   1577: </problem>
                   1578: |;
                   1579:             } else {
                   1580:                 for (my $i=0; $i<@{$answerref}; $i++) {
                   1581:                     ${$answerref}[$i] =~ s/\|/\|/g;
                   1582:                 }
                   1583:                 my $regexpans = join('|',@{$answerref});
                   1584:                 $regexpans = '/('.$regexpans.')/'; 
                   1585:                 $output .= qq|
                   1586: <stringresponse answer="$regexpans" type="re">
                   1587: <textline>
                   1588: </textline>
                   1589: </stringresponse>
                   1590: </problem>
                   1591: |;
                   1592:             }
                   1593:         }
                   1594:     }
                   1595:     open(PROB,">$newdir/$libfile");
                   1596:     print PROB $output;
                   1597:     close PROB;
                   1598:     return $libfile;
                   1599: }
                   1600: 
                   1601: # create_ess builds an essay or True/False question
                   1602: 
                   1603: sub create_ess {
                   1604:     my ($newdir,$answer_id,$qstn,$answertxt,$qtype,$qnum) = @_;
                   1605:     $qnum ++;
                   1606:     if (length($qnum) == 1) {
                   1607:         $qnum = "00".$qnum;
                   1608:     } elsif (length($qnum) == 2) {
                   1609:         $qnum = "0".$qnum;
                   1610:     }
                   1611:     my $libfile = 'question_'.$qnum;
                   1612:     $libfile .= '.problem';
                   1613:     my $output = qq|<problem>
                   1614:  <startouttext />$qstn<endouttext />|;
                   1615: 
                   1616:     my $answer = '';
                   1617:     my $answerlog = '';
                   1618:     if ($qtype eq "Ess") {
                   1619:         $output .= qq|
                   1620:    <essayresponse>
                   1621:    <textfield></textfield>
                   1622:    </essayresponse>
                   1623:    <postanswerdate>
                   1624:    $answertxt
                   1625:    </postanswerdate>
                   1626:   </problem>|;
                   1627:     } elsif ($qtype eq "TF") {
                   1628:          $answer = $answer_id;
                   1629:          $output .= qq|
                   1630:    <radiobuttonresponse max="2" randomize="yes">
                   1631:     <foilgroup>
                   1632:          |;
                   1633:          $output .= "   <foil name=\"foil0\" value=\"true\" location=\"random\"><startouttext />";
                   1634:          if ($answer_id) {
                   1635:               $output .= "False";
                   1636:          } else {
                   1637:               $output .= "True";
                   1638:          }
                   1639:          $output .= "<endouttext /></foil>\n";
                   1640:          $output .= "   <foil name=\"foil1\" value=\"false\" location=\"random\"><startouttext />";
                   1641:          if ($answer_id) {
                   1642:               $output .= "True";
                   1643:          } else {
                   1644:               $output .= "False";
                   1645:          }
                   1646:          $output .= qq|<endouttext /></foil>
                   1647:     </foilgroup>
                   1648:    </radiobuttonresponse>
                   1649:   </problem>|;
                   1650:      }
                   1651:      open(PROB,">$newdir/$libfile");
                   1652:      print PROB $output;
                   1653:      close PROB;
                   1654:      return $libfile;
                   1655: }
                   1656: 
                   1657: sub file_error {
                   1658:     my ($r,$uname,$fn,$current_page);
                   1659:     $r->print("No data here");
                   1660: } 
                   1661: 
                   1662: # ---------------------------------------------------------------- Main Handler
                   1663: sub handler {
                   1664:     my $r=shift;
                   1665:     my $uname;
                   1666:     my $udom;
                   1667:     my $javascript = '';
                   1668:     my $page_name = '';
                   1669:     my $current_page = '';
                   1670:     my $loadentries = '';
                   1671:     my $qcount = '';
                   1672: #
                   1673: # phase two: re-attach user
                   1674: #
                   1675:     if ($ENV{'form.uploaduname'}) {
                   1676:         $ENV{'form.filename'}='/priv/'.$ENV{'form.uploaduname'}.'/'.
                   1677:             $ENV{'form.filename'};
                   1678:     }
                   1679:     ($uname,$udom)=
                   1680:         &Apache::loncacc::constructaccess($ENV{'form.filename'},
                   1681:                                           $r->dir_config('lonDefDomain'));
                   1682:     unless (($uname) && ($udom)) {
                   1683:         $r->log_reason($uname.' at '.$udom.
                   1684:                        ' trying to publish file '.$ENV{'form.filename'}.
                   1685:                        ' - not authorized',
                   1686:                        $r->filename);
                   1687:         return HTTP_NOT_ACCEPTABLE;
                   1688:     }
                   1689:                                                                              
                   1690:     my $fn;
                   1691:     my $badfile = 0;
                   1692:     if ($ENV{'form.filename'}) {
                   1693:         $fn=$ENV{'form.filename'};
                   1694:         $fn=~s/^http\:\/\/[^\/]+\///;
                   1695:         $fn=~s/^\///;
                   1696:         $fn=~s/(\~|priv\/)(\w+)//;
                   1697:         $fn=~s/\/+/\//g;
                   1698:     } else {
                   1699:         $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   1700:                        ' unspecified filename for upload', $r->filename);
                   1701:         return HTTP_NOT_FOUND;
                   1702:     }
                   1703:     my $pathname = &File::Basename::dirname($fn);
                   1704:     my $fullpath = '/priv/'.$uname.$pathname;
                   1705:     unless ($pathname eq '/') {
                   1706:         $fullpath .= '/';
                   1707:     }
                   1708: 
                   1709:     my $dirpath = '/home/'.$uname.'/public_html';
                   1710: 
                   1711:     my @text = ();
                   1712:     my $loadentries = '';
                   1713:     if ($ENV{'form.phase'} eq 'three') {    
                   1714:         if (-e "$dirpath$fn") {
                   1715:             open(TESTBANK,"<$dirpath$fn");
                   1716:             @text = <TESTBANK>;
                   1717:             close(TESTBANK);
                   1718:         } else {
                   1719:             $badfile = 1;  
                   1720:         }
                   1721:     }
                   1722:         
                   1723: # ----------------------------------------------------------- Start page output
                   1724:     &Apache::loncommon::content_type($r,'text/html');
                   1725:     $r->send_http_header;
                   1726: 
                   1727:     if ($ENV{'form.phase'} eq 'three') {
                   1728:         $current_page = &display_control();
                   1729:         my @PAGES = ('Welcome','Blocks','Format','Target','Confirmation');
                   1730:         $page_name = $PAGES[$current_page];
                   1731: 
                   1732:         if ($page_name eq 'Blocks') {
                   1733:             $loadentries = 'onLoad= "setElements()"';
                   1734:             &jscript_one(\$javascript);
                   1735:         } elsif ($page_name eq 'Format') { 
                   1736:             $qcount = question_count($ENV{'form.qnumformat'},\@text);
                   1737:  	    &jscript_two(\$javascript,$qcount);
                   1738:          } elsif ($page_name eq 'Target') {
                   1739:              if ($ENV{'form.go'} eq "PreviousPage") {
                   1740:                  $loadentries =  'onLoad = "setElements()"';
                   1741:  	     }
                   1742: 	     &jscript_three($fullpath,\$javascript);
                   1743:         } elsif ($page_name eq 'Confirmation') {
                   1744: 	    &jscript_four(\$javascript,$fullpath);
                   1745:         }
                   1746:     }                                                                                
                   1747: 
                   1748:     $r->print("<html><head><title>LON-CAPA Construction Space</title><script type=\"text/javascript\">\n//<!--\n$javascript\n// --></script>\n</head>");
                   1749:                                                                                   
                   1750:     $r->print(&Apache::loncommon::bodytag('Upload testbank questions to Construction Space',undef,$loadentries));
                   1751:                                                                                   
                   1752:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                   1753:         $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                   1754:                   &mt(' at ').$udom.'</font></h3>');
                   1755:     }
                   1756: 
                   1757:     if ($ENV{'form.phase'} eq 'three') {
                   1758:         if ($badfile) {
                   1759:             &file_error($r,$uname,$fn,$current_page);
                   1760:         } else {        
                   1761:             &display_zero ($r,$uname,$fn,$current_page,$fullpath) if $page_name eq 'Welcome';
                   1762:             &display_one ($r,$uname,$fn,$current_page,\@text) if $page_name eq 'Blocks';
                   1763:             &display_two ($r,$uname,$fn,$current_page,\@text,$qcount) if $page_name eq 'Format';
                   1764:             &display_three ($r,$uname,$fn,$current_page,\@text,$qcount) if $page_name eq 'Target';
                   1765:             &final_display ($r,$uname,$fn,$current_page,\@text) if $page_name eq 'Confirmation';
                   1766:         }
                   1767:     } elsif ($ENV{'form.phase'} eq 'two') {
                   1768:         my $flag = &Apache::lonupload::phasetwo($r,$fn,$uname,$udom,'testbank');
                   1769:         if ($flag eq 'ok') {
                   1770:             my $current_page = 0;
                   1771:             &display_zero($r,$uname,$fn,$current_page,$fullpath);
                   1772:         }
                   1773:     } else {
                   1774:         &Apache::lonupload::phaseone($r,$fn,$uname,$udom,'testbank');
                   1775:     }
                   1776:     $r->print('</body></html>');
                   1777:     return OK;
                   1778: }
                   1779: 1;
                   1780: __END__
                   1781: 

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