MDL-37801 mod_glossary - encode / decode links to 'showentry' pages during backup...
[moodle.git] / mod / lesson / reformat.php
blob7712341cbd34c4ed97c50c5245e9a50fef1e8812
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * jjg7:8/9/2004
21 * @package mod
22 * @subpackage lesson
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
25 **/
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]);
37 } else {
38 $outfile = $filearray;
41 $outarray = array();
43 foreach ($outfile as $line) {
44 // remove leading and trailing whitespace
45 trim($line);
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";
54 else {
55 $length=strlen($line);
56 if ($length==0) {
57 // don't do anything
59 else {
60 if ($line[$length-1] == "\n") {
61 $outarray[] = $line;
63 else {
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)" ;
74 exit;
76 foreach ($outarray as $outline) {
77 fwrite($handle, $outline);
79 fclose($handle);
81 else {
82 // file not writeable
86 // jjg7:8/9/2004
87 function importmodifiedaikenstyle($filename) {
88 // This function converts from Brusca style to Aiken
89 $lines = file($filename);
90 $answer_found = 0;
91 $responses = 0;
92 $outlines = array();
93 foreach ($lines as $line) {
94 // strip leading and trailing whitespace
95 $line = trim($line);
96 // add a space at the end, quick hack to make sure words from different lines don't run together
97 $line = $line. ' ';
99 // ignore lines less than 2 characters
100 if (strlen($line) < 2) {
101 continue;
105 // see if we have the answer line
106 if ($line[0] =='*') {
107 if ($line[0] == '*') {
108 $answer_found = 1;
109 $line[0]="\t";
110 $line = ltrim($line);
111 $answer = $line[0];
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";
125 $responses = 1;
126 // make character uppercase
127 $line[0]=strtoupper($line[0]);
129 // make entry followed by '.'
130 $line[1]='.';
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
134 switch ($line[0]) {
135 case 1:
136 case 2:
137 case 3:
138 case 4:
139 case 5:
140 case 6:
141 case 7:
142 case 8:
143 case 9:
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
152 $np = 0;
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' ) {
158 $np++;
160 // grab everything after '###.'
161 $line = substr($line, $np+1, strlen($line));
163 if ($responses AND $answer_found) {
164 $responses = 0;
165 $answer_found = 0;
166 $answer = strtoupper($answer);
167 $outlines[] = "ANSWER: $answer\n\n";
169 break;
172 if (substr($line, 0, 14) == 'ANSWER CHOICES') {
173 // don't output this line
175 else {
176 $outlines[]=$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)" ;
194 exit;
196 foreach ($outlines as $outline) {
197 fwrite($handle, $outline);
199 fclose($handle);
200 return true;
202 else {
203 return false;