ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
pgt-file.php
Go to the documentation of this file.
1 <?php
2 
18 class PGTStorageFile extends PGTStorage
19 {
31  var $_path;
32 
41  function getPath()
42  {
43  return $this->_path;
44  }
45 
52  var $_format;
53 
61  function getFormat()
62  {
63  return $this->_format;
64  }
65 
66  // ########################################################################
67  // DEBUGGING
68  // ########################################################################
69 
77  function getStorageType()
78  {
79  return "file";
80  }
81 
89  function getStorageInfo()
90  {
91  return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
92  }
93 
94  // ########################################################################
95  // CONSTRUCTOR
96  // ########################################################################
97 
107  function PGTStorageFile($cas_parent,$format,$path)
108  {
110  // call the ancestor's constructor
111  $this->PGTStorage($cas_parent);
112 
113  if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
115 
116  // check that the path is an absolute path
117  if ( $path[0] != '/' ) {
118  phpCAS::error('an absolute path is needed for PGT storage to file');
119  }
120 
121  // store the path (with a leading and trailing '/')
122  $path = preg_replace('|[/]*$|','/',$path);
123  $path = preg_replace('|^[/]*|','/',$path);
124  $this->_path = $path;
125 
126  // check the format and store it
127  switch ($format) {
130  $this->_format = $format;
131  break;
132  default:
133  phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
134  }
135  phpCAS::traceEnd();
136  }
137 
138  // ########################################################################
139  // INITIALIZATION
140  // ########################################################################
141 
147  function init()
148  {
150  // if the storage has already been initialized, return immediatly
151  if ( $this->isInitialized() )
152  return;
153  // call the ancestor's method (mark as initialized)
154  parent::init();
155  phpCAS::traceEnd();
156  }
157 
158  // ########################################################################
159  // PGT I/O
160  // ########################################################################
161 
170  function getPGTIouFilename($pgt_iou)
171  {
173  return $this->getPath().$pgt_iou.'.'.$this->getFormat();
174  phpCAS::traceEnd();
175  }
176 
186  function write($pgt,$pgt_iou)
187  {
189  $fname = $this->getPGTIouFilename($pgt_iou);
190  if ( $f=fopen($fname,"w") ) {
191  if ( fputs($f,$pgt) === FALSE ) {
192  phpCAS::error('could not write PGT to `'.$fname.'\'');
193  }
194  fclose($f);
195  } else {
196  phpCAS::error('could not open `'.$fname.'\'');
197  }
198  phpCAS::traceEnd();
199  }
200 
211  function read($pgt_iou)
212  {
214  $pgt = FALSE;
215  $fname = $this->getPGTIouFilename($pgt_iou);
216  if ( !($f=fopen($fname,"r")) ) {
217  phpCAS::trace('could not open `'.$fname.'\'');
218  } else {
219  if ( ($pgt=fgets($f)) === FALSE ) {
220  phpCAS::trace('could not read PGT from `'.$fname.'\'');
221  }
222  fclose($f);
223  }
224 
225  // delete the PGT file
226  @unlink($fname);
227 
228  phpCAS::traceEnd($pgt);
229  return $pgt;
230  }
231 
234 }
235 
236 
237 ?>