Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
import_quiz_from_xml.php
1 <?php
50 define ('SQ_IN_IMPORT', 1);
51 
52 error_reporting(E_ALL);
53 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
54 
55 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
56 
57 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
58 if (empty($SYSTEM_ROOT)) {
59  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
60  exit();
61 }
62 
63 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
64  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
65  exit();
66 }
67 
68 $root_node_id = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '';
69 if (empty($root_node_id) || !is_numeric($root_node_id)) {
70  echo "ERROR: You need to supply root node under which the assets are to be imported as fourth argument\n";
71  exit();
72 }
73 
74 $import_file = (isset($_SERVER['argv'][3])) ? $_SERVER['argv'][3] : '';
75 if (empty($import_file) || !is_file($import_file)) {
76  echo "ERROR: You need to supply the path to the import file as the second argument\n";
77  exit();
78 }
79 
80 require_once $SYSTEM_ROOT.'/core/include/init.inc';
81 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_online_quiz/online_quiz_question_group/online_quiz_question_group.inc';
82 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_online_quiz/online_quiz_questions/online_quiz_question_multichoice/online_quiz_question_multichoice.inc';
83 
84 $root_node = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_node_id);
85 if (is_null($root_node)) {
86  echo "\nProvided assetid is not valid for given system, Script will stop execution\n";
87  exit;
88 }
89 
90 $import_link = Array('asset' => &$root_node, 'link_type' => SQ_LINK_TYPE_1);
91 
92 # restore error reporting
93 error_reporting(E_ALL);
94 
95 # Creates XML Parser
96 $p = xml_parser_create();
97 xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
98 xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
99 
100 # Reads in file and parses for precessing.
101 $xml_file = file_get_contents($import_file);
102 xml_parse_into_struct($p, $xml_file, $xml_import_vals, $index);
103 
104 # print an error if one occured
105 if ($error_code = xml_get_error_code($p)) {
106  echo 'XML Error: '.xml_error_string($error_code).' Line:'.xml_get_current_line_number($p).' Col:'.xml_get_current_column_number($p)."\n";
107  exit();
108 }
109 
110 
111 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
112 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_OPEN);
113 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
114 
115 # Sets up some place holding variables
116 $current_option = '';
117 $question_count = 1;
118 $groups_created = 0;
119 $questions_created = 0;
120 
121 
122 # START PROCESSING XML
123 foreach ($xml_import_vals as $xml_elem) {
124  # ignores closing tags
125  if(in_array($xml_elem['type'], array('open','complete'))) {
126  switch (strtolower($xml_elem['tag'])) {
127 
128  # Create a new Question Group
129  case 'pool':
130  $pool_name = get_attribute_value($xml_elem, 'name');
131  if($pool_name) {
132  $new_group = new Online_Quiz_Question_Group();
133 
134  $stripped_tag_name = strip_tags($pool_name);
135  $trimmed_tag_name = trim($stripped_tag_name);
136 
137  $new_group->setAttrValue('name', $trimmed_tag_name);
138 
139  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
140  $status = $new_group->create($import_link);
141  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
142 
143  # builds the link
144  $group_link = Array('asset' => &$new_group, 'link_type' => SQ_LINK_TYPE_1);
145 
146  # Resets question count and array
147  $question_count = 1;
148  if(isset($question)) unset($question);
149 
150  $groups_created++;
151  }
152  break;
153 
154  # Creates a new question
155  case 'question':
156  # Create arrays to store new questions
157  $question = array('name' => 'Question '.$question_count, 'response_form' => array());
158  $options = array();
159  $question_count++;
160  break;
161 
162  # Sets question text with html code conversion
163  case 'questiontext' :
164  $question['question_text'] = html_entity_decode(get_node_value($xml_elem));
165  break;
166 
167  # Sets value of current option
168  case 'option_text' :
169  $options[$current_option]['text'] = get_node_value($xml_elem);
170  break;
171 
172  # Sets value of current option response with html code conversion
173  case 'response_supplement' :
174  $options[$current_option]['response_supplement'] = html_entity_decode(get_node_value($xml_elem));
175  break;
176 
177  default :
178  # checks to see if node is <Option_x>
179  if (strpos(strtolower($xml_elem['tag']), 'option_')>=0) {
180  # parse last char
181  # sets current option placeholder
182  # and construct option array
183  $current_option = substr(strtolower($xml_elem['tag']), strlen('option_'), 1);
184  $options[$current_option] = array(
185  'points' => (int) get_attribute_value($xml_elem, 'points'),
186  'text' => '',
187  'response_supplement' => '',
188  );
189  }
190  break;
191  }
192 
193  } elseif (in_array($xml_elem['type'], array('close'))) {
194 
195  switch (strtolower($xml_elem['tag'])) {
196  # Create a new Question Group
197 
198  case 'question':
199 
200  if(isset($question) && !empty($group_link)) {
201  # if qustion already exists save it to matrix
202  $question['response_form'] = $options;
203 
204  $new_question = new Online_Quiz_Question_Multichoice();
205 
206  # Sets question attributes
207  $new_question->setAttrValue('name', $question['name']);
208  $new_question->setAttrValue('question_text', $question['question_text']);
209  $new_question->setAttrValue('response_form', $options);
210 
211  # Create asset and set Question Text to use bodycopy
212  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
213  $status = $new_question->create($group_link);
214  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
215 
216  # question not needed anymore so can forget it
217  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($new_question);
218 
219  $questions_created++;
220 
221  }
222  break;
223  }
224  }
225 }
226 
227 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
228 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
229 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
230 
231 echo "*** Import Complete *** \n";
232 echo " Questions Created : \t $questions_created \n";
233 echo " Groups Created : \t $groups_created \n";
234 echo "*** Import Complete *** \n";
235 
244 function get_node_value($data) {
245 
246  if (isset($data['value'])) {
247  return $data['value'];
248  }
249  return '';
250 }
251 
261 function get_attribute_value($data, $attr = '') {
262 
263  if (isset($data['attributes']) && count($data['attributes'])) {
264  foreach ($data['attributes'] as $n => $v) {
265  if(strtolower($n) == strtolower($attr)) {
266  return $v;
267  }
268  }
269  }
270  return '';
271 }
272 
273 ?>