Updated the 19 build version
[moodle.git] / mod / lesson / reformat.php
blobe13bc76207858b7270d53f0a5c3dccf364181622
1 <?php // $Id$
2 /**
3 * jjg7:8/9/2004
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
10 function removedoublecr($filename) {
11 // This function will adjust a file in roughly Aiken style by replacing extra newlines with <br/> tags
12 // so that instructors can have newlines wherever they like as long as the overall format is in Aiken
14 $filearray = file($filename);
15 /// Check for Macintosh OS line returns (ie file on one line), and fix
16 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
17 $outfile = explode("\r", $filearray[0]);
18 } else {
19 $outfile = $filearray;
22 foreach ($outfile as $line) {
23 // remove leading and trailing whitespace
24 trim($line);
25 // check it's length, if 0 do not output... if it is > 0 output
26 if ($line[0] == "\n" OR strlen($line)==0 ) {
27 if (count($outarray) ) {
28 // get the last item in the outarray
29 $cur_pos = (count($outarray) - 1);
30 $outarray[$cur_pos] = trim($outarray[$cur_pos])."<br/>\n";
33 else {
34 $length=strlen($line);
35 if ($length==0) {
36 // don't do anything
38 else {
39 if ($line[$length-1] == "\n") {
40 $outarray[] = $line;
42 else {
43 $outarray[] = $line."\n";
48 // output modified file to original
49 if ( is_writable($filename) ) {
51 if (! $handle =fopen ($filename ,'w' )) {
52 echo "Cannot open file ($filename)" ;
53 exit;
55 foreach ($outarray as $outline) {
56 fwrite($handle, $outline);
58 fclose($handle);
60 else {
61 // file not writeable
65 // jjg7:8/9/2004
66 function importmodifiedaikenstyle($filename) {
67 // This function converts from Brusca style to Aiken
68 $lines = file($filename);
69 $answer_found = 0;
70 $responses = 0;
71 $outlines = array();
72 foreach ($lines as $line) {
73 // strip leading and trailing whitespace
74 $line = trim($line);
75 // add a space at the end, quick hack to make sure words from different lines don't run together
76 $line = $line. ' ';
78 // ignore lines less than 2 characters
79 if (strlen($line) < 2) {
80 continue;
84 // see if we have the answer line
85 if ($line[0] =='*') {
86 if ($line[0] == '*') {
87 $answer_found = 1;
88 $line[0]="\t";
89 $line = ltrim($line);
90 $answer = $line[0];
94 $leadin = substr($line, 0,2);
95 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) {
97 // re-add newline to indicate end of previous question/response
98 if (count($outlines)) {
99 $cur_pos = (count($outlines) - 1);
100 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
104 $responses = 1;
105 // make character uppercase
106 $line[0]=strtoupper($line[0]);
108 // make entry followed by '.'
109 $line[1]='.';
111 elseif ( ($responses AND $answer_found) OR (count($outlines)<=1) ) {
112 // we have found responses and an answer and the current line is not an answer
113 switch ($line[0]) {
114 case 1:
115 case 2:
116 case 3:
117 case 4:
118 case 5:
119 case 6:
120 case 7:
121 case 8:
122 case 9:
124 // re-add newline to indicate end of previous question/response
125 if (count($outlines)) {
126 $cur_pos = (count($outlines) - 1);
127 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
130 // this next ugly block is to strip out the numbers at the beginning
131 $np = 0;
132 // this probably could be done cleaner... it escapes me at the moment
133 while ($line[$np] == '0' OR $line[$np] == '1' OR $line[$np] == '2'
134 OR $line[$np] == '3' OR $line[$np] == '4' OR $line[$np] == '5'
135 OR $line[$np] == '6' OR $line[$np] == '7' OR $line[$np] == '8'
136 OR $line[$np] == '9' ) {
137 $np++;
139 // grab everything after '###.'
140 $line = substr($line, $np+1, strlen($line));
142 if ($responses AND $answer_found) {
143 $responses = 0;
144 $answer_found = 0;
145 $answer = strtoupper($answer);
146 $outlines[] = "ANSWER: $answer\n\n";
148 break;
151 if (substr($line, 0, 14) == 'ANSWER CHOICES') {
152 // don't output this line
154 else {
155 $outlines[]=$line;
157 } // close for each line
159 // re-add newline to indicate end of previous question/response
160 if (count($outlines)) {
161 $cur_pos = (count($outlines) - 1);
162 $outlines[$cur_pos] = $outlines[$cur_pos]."\n";
165 // output the last answer
166 $answer = strtoupper($answer);
167 $outlines[] = "ANSWER: $answer\n\n";
169 // output modified file to original
170 if ( is_writable($filename) ) {
171 if (! $handle =fopen ($filename ,'w' )) {
172 echo "Cannot open file ($filename)" ;
173 exit;
175 foreach ($outlines as $outline) {
176 fwrite($handle, $outline);
178 fclose($handle);
179 return true;
181 else {
182 return false;