50 define (
'SQ_IN_IMPORT', 1);
52 error_reporting(E_ALL);
53 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
55 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
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";
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";
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";
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";
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';
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";
90 $import_link = Array(
'asset' => &$root_node,
'link_type' => SQ_LINK_TYPE_1);
92 # restore error reporting
93 error_reporting(E_ALL);
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);
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);
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";
111 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
112 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_OPEN);
113 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
115 # Sets up some place holding variables
116 $current_option =
'';
119 $questions_created = 0;
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'])) {
128 # Create a new Question Group
130 $pool_name = get_attribute_value($xml_elem,
'name');
134 $stripped_tag_name = strip_tags($pool_name);
135 $trimmed_tag_name = trim($stripped_tag_name);
137 $new_group->setAttrValue(
'name', $trimmed_tag_name);
139 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
140 $status = $new_group->create($import_link);
141 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
144 $group_link = Array(
'asset' => &$new_group,
'link_type' => SQ_LINK_TYPE_1);
146 # Resets question count and array
148 if(isset($question)) unset($question);
154 # Creates a new question
156 # Create arrays to store new questions
157 $question = array(
'name' =>
'Question '.$question_count,
'response_form' => array());
162 # Sets question text with html code conversion
163 case 'questiontext' :
164 $question[
'question_text'] = html_entity_decode(get_node_value($xml_elem));
167 # Sets value of current option
169 $options[$current_option][
'text'] = get_node_value($xml_elem);
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));
178 # checks to see if node is <Option_x>
179 if (strpos(strtolower($xml_elem[
'tag']),
'option_')>=0) {
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'),
187 'response_supplement' =>
'',
193 } elseif (in_array($xml_elem[
'type'], array(
'close'))) {
195 switch (strtolower($xml_elem[
'tag'])) {
196 # Create a new Question Group
200 if(isset($question) && !empty($group_link)) {
201 # if qustion already exists save it to matrix
202 $question[
'response_form'] = $options;
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);
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();
216 # question not needed anymore so can forget it
217 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($new_question);
219 $questions_created++;
227 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
228 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
229 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
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";
244 function get_node_value($data) {
246 if (isset($data[
'value'])) {
247 return $data[
'value'];
261 function get_attribute_value($data, $attr =
'') {
263 if (isset($data[
'attributes']) && count($data[
'attributes'])) {
264 foreach ($data[
'attributes'] as $n => $v) {
265 if(strtolower($n) == strtolower($attr)) {