[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/ -> reformat.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1