|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 function removedoublecr($filename) { 00030 // This function will adjust a file in roughly Aiken style by replacing extra newlines with <br/> tags 00031 // so that instructors can have newlines wherever they like as long as the overall format is in Aiken 00032 00033 $filearray = file($filename); 00035 if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) { 00036 $outfile = explode("\r", $filearray[0]); 00037 } else { 00038 $outfile = $filearray; 00039 } 00040 00041 $outarray = array(); 00042 00043 foreach ($outfile as $line) { 00044 // remove leading and trailing whitespace 00045 trim($line); 00046 // check it's length, if 0 do not output... if it is > 0 output 00047 if ($line[0] == "\n" OR strlen($line)==0 ) { 00048 if (count($outarray) ) { 00049 // get the last item in the outarray 00050 $cur_pos = (count($outarray) - 1); 00051 $outarray[$cur_pos] = trim($outarray[$cur_pos])."<br/>\n"; 00052 } 00053 } 00054 else { 00055 $length=strlen($line); 00056 if ($length==0) { 00057 // don't do anything 00058 } 00059 else { 00060 if ($line[$length-1] == "\n") { 00061 $outarray[] = $line; 00062 } 00063 else { 00064 $outarray[] = $line."\n"; 00065 } 00066 } 00067 } 00068 } 00069 // output modified file to original 00070 if ( is_writable($filename) ) { 00071 00072 if (! $handle =fopen ($filename ,'w' )) { 00073 echo "Cannot open file ($filename)" ; 00074 exit; 00075 } 00076 foreach ($outarray as $outline) { 00077 fwrite($handle, $outline); 00078 } 00079 fclose($handle); 00080 } 00081 else { 00082 // file not writeable 00083 } 00084 } 00085 00086 // jjg7:8/9/2004 00087 function importmodifiedaikenstyle($filename) { 00088 // This function converts from Brusca style to Aiken 00089 $lines = file($filename); 00090 $answer_found = 0; 00091 $responses = 0; 00092 $outlines = array(); 00093 foreach ($lines as $line) { 00094 // strip leading and trailing whitespace 00095 $line = trim($line); 00096 // add a space at the end, quick hack to make sure words from different lines don't run together 00097 $line = $line. ' '; 00098 00099 // ignore lines less than 2 characters 00100 if (strlen($line) < 2) { 00101 continue; 00102 } 00103 00104 00105 // see if we have the answer line 00106 if ($line[0] =='*') { 00107 if ($line[0] == '*') { 00108 $answer_found = 1; 00109 $line[0]="\t"; 00110 $line = ltrim($line); 00111 $answer = $line[0]; 00112 } 00113 } 00114 00115 $leadin = substr($line, 0,2); 00116 if (strpos(".A)B)C)D)E)F)G)H)I)J)a)b)c)d)e)f)g)h)i)j)A.B.C.D.E.F.G.H.I.J.a.b.c.d.e.f.g.h.i.j.", $leadin)>0) { 00117 00118 // re-add newline to indicate end of previous question/response 00119 if (count($outlines)) { 00120 $cur_pos = (count($outlines) - 1); 00121 $outlines[$cur_pos] = $outlines[$cur_pos]."\n"; 00122 } 00123 00124 00125 $responses = 1; 00126 // make character uppercase 00127 $line[0]=strtoupper($line[0]); 00128 00129 // make entry followed by '.' 00130 $line[1]='.'; 00131 } 00132 elseif ( ($responses AND $answer_found) OR (count($outlines)<=1) ) { 00133 // we have found responses and an answer and the current line is not an answer 00134 switch ($line[0]) { 00135 case 1: 00136 case 2: 00137 case 3: 00138 case 4: 00139 case 5: 00140 case 6: 00141 case 7: 00142 case 8: 00143 case 9: 00144 00145 // re-add newline to indicate end of previous question/response 00146 if (count($outlines)) { 00147 $cur_pos = (count($outlines) - 1); 00148 $outlines[$cur_pos] = $outlines[$cur_pos]."\n"; 00149 } 00150 00151 // this next ugly block is to strip out the numbers at the beginning 00152 $np = 0; 00153 // this probably could be done cleaner... it escapes me at the moment 00154 while ($line[$np] == '0' OR $line[$np] == '1' OR $line[$np] == '2' 00155 OR $line[$np] == '3' OR $line[$np] == '4' OR $line[$np] == '5' 00156 OR $line[$np] == '6' OR $line[$np] == '7' OR $line[$np] == '8' 00157 OR $line[$np] == '9' ) { 00158 $np++; 00159 } 00160 // grab everything after '###.' 00161 $line = substr($line, $np+1, strlen($line)); 00162 00163 if ($responses AND $answer_found) { 00164 $responses = 0; 00165 $answer_found = 0; 00166 $answer = strtoupper($answer); 00167 $outlines[] = "ANSWER: $answer\n\n"; 00168 } 00169 break; 00170 } 00171 } 00172 if (substr($line, 0, 14) == 'ANSWER CHOICES') { 00173 // don't output this line 00174 } 00175 else { 00176 $outlines[]=$line; 00177 } 00178 } // close for each line 00179 00180 // re-add newline to indicate end of previous question/response 00181 if (count($outlines)) { 00182 $cur_pos = (count($outlines) - 1); 00183 $outlines[$cur_pos] = $outlines[$cur_pos]."\n"; 00184 } 00185 00186 // output the last answer 00187 $answer = strtoupper($answer); 00188 $outlines[] = "ANSWER: $answer\n\n"; 00189 00190 // output modified file to original 00191 if ( is_writable($filename) ) { 00192 if (! $handle =fopen ($filename ,'w' )) { 00193 echo "Cannot open file ($filename)" ; 00194 exit; 00195 } 00196 foreach ($outlines as $outline) { 00197 fwrite($handle, $outline); 00198 } 00199 fclose($handle); 00200 return true; 00201 } 00202 else { 00203 return false; 00204 } 00205 } 00206