3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
27 defined('MOODLE_INTERNAL') ||
die();
29 function removedoublecr($filename) {
30 // This function will adjust a file in roughly Aiken style by replacing extra newlines with <br/> tags
31 // so that instructors can have newlines wherever they like as long as the overall format is in Aiken
33 $filearray = file($filename);
34 /// Check for Macintosh OS line returns (ie file on one line), and fix
35 if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) {
36 $outfile = explode("\r", $filearray[0]);
38 $outfile = $filearray;
43 foreach ($outfile as $line) {
44 // remove leading and trailing whitespace
46 // check it's length, if 0 do not output... if it is > 0 output
47 if ($line[0] == "\n" OR strlen($line)==0 ) {
48 if (count($outarray) ) {
49 // get the last item in the outarray
50 $cur_pos = (count($outarray) - 1);
51 $outarray[$cur_pos] = trim($outarray[$cur_pos])."<br/>\n";
55 $length=strlen($line);
60 if ($line[$length-1] == "\n") {
64 $outarray[] = $line."\n";
69 // output modified file to original
70 if ( is_writable($filename) ) {
72 if (! $handle =fopen ($filename ,'w' )) {
73 echo "Cannot open file ($filename)" ;
76 foreach ($outarray as $outline) {
77 fwrite($handle, $outline);
87 function importmodifiedaikenstyle($filename) {
88 // This function converts from Brusca style to Aiken
89 $lines = file($filename);
93 foreach ($lines as $line) {
94 // strip leading and trailing whitespace
96 // add a space at the end, quick hack to make sure words from different lines don't run together
99 // ignore lines less than 2 characters
100 if (strlen($line) < 2) {
105 // see if we have the answer line
106 if ($line[0] =='*') {
107 if ($line[0] == '*') {
110 $line = ltrim($line);
115 $leadin = substr($line, 0,2);
116 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) {
118 // re-add newline to indicate end of previous question/response
119 if (count($outlines)) {
120 $cur_pos = (count($outlines) - 1);
121 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
126 // make character uppercase
127 $line[0]=strtoupper($line[0]);
129 // make entry followed by '.'
132 elseif ( ($responses AND $answer_found) OR (count($outlines)<=1) ) {
133 // we have found responses and an answer and the current line is not an answer
145 // re-add newline to indicate end of previous question/response
146 if (count($outlines)) {
147 $cur_pos = (count($outlines) - 1);
148 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
151 // this next ugly block is to strip out the numbers at the beginning
153 // this probably could be done cleaner... it escapes me at the moment
154 while ($line[$np] == '0' OR $line[$np] == '1' OR $line[$np] == '2'
155 OR $line[$np] == '3' OR $line[$np] == '4' OR $line[$np] == '5'
156 OR $line[$np] == '6' OR $line[$np] == '7' OR $line[$np] == '8'
157 OR $line[$np] == '9' ) {
160 // grab everything after '###.'
161 $line = substr($line, $np+
1, strlen($line));
163 if ($responses AND $answer_found) {
166 $answer = strtoupper($answer);
167 $outlines[] = "ANSWER: $answer\n\n";
172 if (substr($line, 0, 14) == 'ANSWER CHOICES') {
173 // don't output this line
178 } // close for each line
180 // re-add newline to indicate end of previous question/response
181 if (count($outlines)) {
182 $cur_pos = (count($outlines) - 1);
183 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
186 // output the last answer
187 $answer = strtoupper($answer);
188 $outlines[] = "ANSWER: $answer\n\n";
190 // output modified file to original
191 if ( is_writable($filename) ) {
192 if (! $handle =fopen ($filename ,'w' )) {
193 echo "Cannot open file ($filename)" ;
196 foreach ($outlines as $outline) {
197 fwrite($handle, $outline);