/mod/lesson/reformat.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 206 lines · 133 code · 24 blank · 49 comment · 42 complexity · 417543a2b3408ca45e9dbbd72ad550a3 MD5 · raw file

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