Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_export_online_quiz_to_xml.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 
36 {
37 
38 
45  function Tool_Export_Online_Quiz_To_XML($assetid=0)
46  {
47  $this->Tool($assetid);
48 
49  }//end constructor
50 
51 
61  public static function paintTool(&$o, $type_code)
62  {
63 
64  // do a lock check, only allows one user at a time
65  $import_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('import_tools_manager');
66  $locks = $GLOBALS['SQ_SYSTEM']->am->getLockInfo($import_mgr->id, 'attributes', TRUE, TRUE);
67  $userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
68  $userHasLockId = '';
69  $hasLock = TRUE;
70 
71  foreach ($locks as $lock) {
72  if (empty($lock)) continue;
73  if ($lock['lock_type'] == 'attributes' && $lock['userid'] == $userid) {
74  $hasLock = TRUE;
75  } else if ($lock['lock_type'] == 'attributes' ) {
76  $hasLock = FALSE;
77  $userHasLockId = $lock['userid'];
78  }
79  }
80 
81  if (!$hasLock) {
82  $o->openRaw();
83  echo (translate('tool_export_online_quiz_to_xml_can_not_acquire_lock', $userHasLockId));
84  $o->closeRaw();
85  return FALSE;
86  }
87 
88  $show_download = FALSE;
89  if (isset($_REQUEST['show_download'])) {
90  $show_download = TRUE;
91  } else if (isset($_REQUEST['download'])) {
92 
93  $export_dir = SQ_DATA_PATH.'/private/export_assets';
94  if (!is_dir($export_dir)) {
95  if (!create_directory($export_dir)) return FALSE;
96  }
97 
98  if (!file_exists($export_dir.'/export.tgz')){
99  $command = "cd $export_dir ; tar -czf export.tgz * --remove-files";
100  exec($command, $output, $return);
101 
102  // exec populate $return with 0 if successful. If not successful, $return will be an exit status code and
103  // we don't have a usable compressed file for the user to download
104  if ($return !== 0) {
105  trigger_localised_error('EXP0001', E_USER_ERROR, $return);
106  return;
107  }
108  }
109 
110  // if we don't have a valid filesize, there isn't much point going fruther as the user will end up downloading an unusable file
111  $filesize = filesize($export_dir.'/export.tgz');
112  if (!$filesize) {
113  trigger_localised_error('EXP0002', E_USER_ERROR);
114  return;
115  }
116 
117  require_once SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
118  $ext = 'tgz';
119  $type = (empty($standards_lists_mime_types[$ext])) ? 'text/plain' : $standards_lists_mime_types[$ext];
120 
121  // We are about to get our download so let go of the lock
122  $GLOBALS['SQ_SYSTEM']->am->releaseLock($import_mgr->id, 'attributes');
123 
124  // Fix for IE caching
125  header("Pragma: public");
126  header("Expires: 0");
127  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
128  header("Cache-Control: private", FALSE);
129 
130  header('Content-Type: '.$type);
131  header('Content-Disposition: attachment; filename="export.tgz";');
132  header('Content-Length: '.$filesize);
133 
134  readfile($export_dir.'/export.tgz');
135  exit;
136  }
137 
138  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
139  $o->openSection($tool_info['name']);
140 
141  $o->openField(translate('tool_export_online_quiz_to_xml_root_node'));
142  asset_finder($type_code.'_root_node');
143  $o->note(translate('tool_export_online_quiz_to_xml_root_node_note'));
144  $o->closeField();
145 
146  $o->openField(translate('tool_export_online_quiz_to_xml_type_codes'));
147  $available_types = Array (
148  'page_standard' => 'Standard Page',
149  'page_online_quiz' => 'Online Quiz'
150  );
151  combo_box($type_code.'_type_codes', $available_types, TRUE);
152  $o->note(translate('tool_export_online_quiz_to_xml_type_codes_note'));
153  $o->closeField();
154 
155  if ($show_download) {
156  $o->openField(translate('download'));
157  $backend_url = $GLOBALS['SQ_SYSTEM']->backend->getBackendUrl();
158  $download_url = $backend_url.'&backend_section=tools&tool_type_code='.$type_code.'&download=1';
159  echo '<a href="'.$download_url.'" >'.translate('download').'</a>';
160  $o->closeField();
161  }
162 
163  $o->closeSection();
164 
166 
167  }//end paintTool()
168 
169 
179  public static function processTool(&$o, $type_code)
180  {
181 
182  $import_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('import_tools_manager');
183  if(!$GLOBALS['SQ_SYSTEM']->am->acquireLock($import_mgr->id, 'attributes')) {
184  trigger_localised_error('EXP0003', E_USER_ERROR);
185  return FALSE;
186  }
187 
188  // make export temp directory
189  $export_dir = SQ_DATA_PATH.'/private/export_assets';
190  if (!is_dir($export_dir)) {
191  if (!create_directory($export_dir)) return FALSE;
192  }
193 
194  // clean up before export
195  if (!clear_directory($export_dir)) return FALSE;
196 
197  $root_node = (!empty($_POST[$type_code.'_root_node']['assetid'])) ? $_POST[$type_code.'_root_node']['assetid'] : 0;
198  $type_codes = array_get_index($_POST, $type_code.'_type_codes', Array());
199 
200  if (empty($root_node)) {
201  trigger_error ('There is no root node specified', E_USER_NOTICE);
202  return FALSE;
203  }
204 
205  if (empty($type_codes)) {
206  trigger_error ('There is no type codes specified', E_USER_NOTICE);
207  return FALSE;
208  }
209 
210  // set HIPO running_vars
211  $vars['export_dir'] = $export_dir;
212  $vars['root_node'] = $root_node;
213  $vars['type_codes'] = $type_codes;
214  $vars['export_settings']['page_standard'] = Tool_Export_Online_Quiz_To_XML::processStandardPageSettings($o, $type_code);
215 
216  // run HIPO job
217  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
218  $hh->queueHipo('hipo_job_export_online_quiz_to_xml', $vars, '');
219  $backend_url = $GLOBALS['SQ_SYSTEM']->backend->getBackendUrl();
220  // run the hipo job and redirect back to the tool and show download link
221  $url = $hh->runQueuedJobs($backend_url.'&backend_section=tools&tool_type_code='.$type_code.'&show_download=1');
222 
223  if (!empty($url)) $o->setRedirect($url);
224  return TRUE;
225 
226  }//end processTool()
227 
228 
238  public static function paintStandardPageSettings(&$o, $type_code)
239  {
240  $o->openSection(translate('tool_export_online_quiz_to_xml_standard_page_settings'));
241 
242  $o->openField(translate('tool_export_online_quiz_to_xml_paint_method'));
243  radio_button($type_code.'_paint_method', 'raw', TRUE);
244  label('Raw (no Paint Layout)', $type_code.'_page_standard_paint_method_raw');
245  echo '<br / >';
246  radio_button($type_code.'_paint_method', 'default_layout');
247  label('Using default Paint Layout (based on lookup rules)', $type_code.'_page_standard_paint_method_default_layout');
248  $o->closeField();
249 
250  $o->openField(translate('tool_export_online_quiz_to_xml_start_tag'));
251  text_box($type_code.'_start_tag', '<!-- START TAG -->');
252  $o->closeField();
253 
254  $o->openField(translate('tool_export_online_quiz_to_xml_end_tag'));
255  text_box($type_code.'_end_tag', '<!-- END TAG -->');
256  $o->closeField();
257 
258  $o->sectionNote(translate('tool_export_online_quiz_to_xml_standard_page_settings_note'));
259 
260  $o->closeSection();
261 
262  }//end paintStandardPageSettings()
263 
264 
274  public static function processStandardPageSettings(&$o, $type_code)
275  {
276  $settings['start_tag'] = array_get_index($_POST, $type_code.'_start_tag', '');
277  $settings['end_tag'] = array_get_index($_POST, $type_code.'_end_tag', '');
278  $settings['paint_method'] = array_get_index($_POST, $type_code.'_paint_method', 'raw');
279 
280  return $settings;
281 
282  }//end processStandardPageSettings()
283 
284 
285 }//end class
286 
287 
288 ?>