forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat.php
204 lines (181 loc) · 6.59 KB
/
reformat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* jjg7:8/9/2004
*
* @package mod
* @subpackage lesson
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
**/
defined('MOODLE_INTERNAL') || die();
function removedoublecr($filename) {
// This function will adjust a file in roughly Aiken style by replacing extra newlines with <br/> tags
// so that instructors can have newlines wherever they like as long as the overall format is in Aiken
$filearray = file($filename);
/// Check for Macintosh OS line returns (ie file on one line), and fix
if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) {
$outfile = explode("\r", $filearray[0]);
} else {
$outfile = $filearray;
}
foreach ($outfile as $line) {
// remove leading and trailing whitespace
trim($line);
// check it's length, if 0 do not output... if it is > 0 output
if ($line[0] == "\n" OR strlen($line)==0 ) {
if (count($outarray) ) {
// get the last item in the outarray
$cur_pos = (count($outarray) - 1);
$outarray[$cur_pos] = trim($outarray[$cur_pos])."<br/>\n";
}
}
else {
$length=strlen($line);
if ($length==0) {
// don't do anything
}
else {
if ($line[$length-1] == "\n") {
$outarray[] = $line;
}
else {
$outarray[] = $line."\n";
}
}
}
}
// output modified file to original
if ( is_writable($filename) ) {
if (! $handle =fopen ($filename ,'w' )) {
echo "Cannot open file ($filename)" ;
exit;
}
foreach ($outarray as $outline) {
fwrite($handle, $outline);
}
fclose($handle);
}
else {
// file not writeable
}
}
// jjg7:8/9/2004
function importmodifiedaikenstyle($filename) {
// This function converts from Brusca style to Aiken
$lines = file($filename);
$answer_found = 0;
$responses = 0;
$outlines = array();
foreach ($lines as $line) {
// strip leading and trailing whitespace
$line = trim($line);
// add a space at the end, quick hack to make sure words from different lines don't run together
$line = $line. ' ';
// ignore lines less than 2 characters
if (strlen($line) < 2) {
continue;
}
// see if we have the answer line
if ($line[0] =='*') {
if ($line[0] == '*') {
$answer_found = 1;
$line[0]="\t";
$line = ltrim($line);
$answer = $line[0];
}
}
$leadin = substr($line, 0,2);
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) {
// re-add newline to indicate end of previous question/response
if (count($outlines)) {
$cur_pos = (count($outlines) - 1);
$outlines[$cur_pos] = $outlines[$cur_pos]."\n";
}
$responses = 1;
// make character uppercase
$line[0]=strtoupper($line[0]);
// make entry followed by '.'
$line[1]='.';
}
elseif ( ($responses AND $answer_found) OR (count($outlines)<=1) ) {
// we have found responses and an answer and the current line is not an answer
switch ($line[0]) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
// re-add newline to indicate end of previous question/response
if (count($outlines)) {
$cur_pos = (count($outlines) - 1);
$outlines[$cur_pos] = $outlines[$cur_pos]."\n";
}
// this next ugly block is to strip out the numbers at the beginning
$np = 0;
// this probably could be done cleaner... it escapes me at the moment
while ($line[$np] == '0' OR $line[$np] == '1' OR $line[$np] == '2'
OR $line[$np] == '3' OR $line[$np] == '4' OR $line[$np] == '5'
OR $line[$np] == '6' OR $line[$np] == '7' OR $line[$np] == '8'
OR $line[$np] == '9' ) {
$np++;
}
// grab everything after '###.'
$line = substr($line, $np+1, strlen($line));
if ($responses AND $answer_found) {
$responses = 0;
$answer_found = 0;
$answer = strtoupper($answer);
$outlines[] = "ANSWER: $answer\n\n";
}
break;
}
}
if (substr($line, 0, 14) == 'ANSWER CHOICES') {
// don't output this line
}
else {
$outlines[]=$line;
}
} // close for each line
// re-add newline to indicate end of previous question/response
if (count($outlines)) {
$cur_pos = (count($outlines) - 1);
$outlines[$cur_pos] = $outlines[$cur_pos]."\n";
}
// output the last answer
$answer = strtoupper($answer);
$outlines[] = "ANSWER: $answer\n\n";
// output modified file to original
if ( is_writable($filename) ) {
if (! $handle =fopen ($filename ,'w' )) {
echo "Cannot open file ($filename)" ;
exit;
}
foreach ($outlines as $outline) {
fwrite($handle, $outline);
}
fclose($handle);
return true;
}
else {
return false;
}
}