Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_select_management.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
19 
20 define('SQ_DELIMITER_UNIX', "\n");
21 define('SQ_DELIMITER_WINDOWS', "\r\n");
22 define('SQ_DELIMITER_MAC', "\n");
23 
35 {
36 
37 
43  {
44  $this->Asset_Management($pm);
45 
46  $this->vars = Array(
47  'height' => Array(
48  'added' => '0.0.1',
49  'type' => 'text',
50  'default' => '1',
51  ),
52  'default' => Array(
53  'added' => '0.0.1',
54  'type' => 'serialise',
55  'default' => Array(),
56  ),
57  'multiple' => Array(
58  'added' => '0.0.1',
59  'type' => 'boolean',
60  'default' => FALSE,
61  'parameters' => Array(
62  'allow_empty' => FALSE,
63  ),
64  ),
65  'extra' => Array(
66  'added' => '0.0.1',
67  'type' => 'text',
68  'default' => '',
69  ),
70  'options' => Array(
71  'added' => '0.5',
72  'type' => 'serialise',
73  'default' => Array(),
74  ),
75  'allow_empty' => Array(
76  'added' => '0.3',
77  'type' => 'boolean',
78  'default' => FALSE,
79  'parameters' => Array(
80  'allow_empty' => FALSE,
81  ),
82  ),
83  'empty_key' => Array(
84  'added' => '0.5',
85  'type' => 'text',
86  'default' => 'EMPTY',
87  ),
88  'empty_text' => Array(
89  'added' => '0.3',
90  'type' => 'text',
91  'default' => '-- Leave Empty --',
92  ),
93  );
94 
95  }//end constructor
96 
97 
106  function _upgrade($current_version)
107  {
108  if (!parent::_upgrade($current_version)) return FALSE;
109 
110  $asset_name = strtoupper($this->_pm->getTypeInfo($this->getAssetType(), 'name'));
111 
112  if (version_compare($current_version, '0.5', '<')) {
113  pre_echo('STARTING UPGRADE OF '.$asset_name.' FROM VERSION '.$current_version.' TO VERSION 0.5');
114 
115  $count = 0;
116 
117  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
118  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
119 
120  pre_echo('Updating Select Question Types...');
121 
122  // Get a list of questions
123  $form_email = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form_email', FALSE, TRUE);
124  $form_email += $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form_section', FALSE, TRUE);
125  $form_email += $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('simple_form', FALSE, TRUE);
126  foreach ($form_email as $fe_id => $fe_blank) {
127  $fe = $GLOBALS['SQ_SYSTEM']->am->getAsset($fe_id, $fe_blank['type_code']);
128 
129  $q_value = $fe->attr('questions');
130 
131  // Cycle thru the questions
132  foreach ($q_value as $q_place_id => $q_id) {
133  if (isset($q_id['type_code'])) {
134  $type_code_index = 'type_code';
135  } else if (isset($q_id['question_type_code'])) {
136  $type_code_index = 'question_type_code';
137  } else {
138  continue;
139  }//end else
140  if ($q_id[$type_code_index] == 'form_question_type_select') {
141  $options = $q_id['attributes']['options'];
142 
143  // Check the format stored in, and grab the options as an array
144  $format_check = strpos($options, "\r");
145  if ($format_check === FALSE) {
146  $opt = explode(SQ_DELIMITER_UNIX, $options);
147  } else {
148  $opt = explode(SQ_DELIMITER_WINDOWS, $options);
149  }
150 
151  // Convert the array into a format we want
152  $processed_attribute = Array();
153  foreach ($opt as $ol) {
154  $processed_attribute[$ol] = $ol;
155  }
156 
157  // Serialize the array
158  $processed_attribute = serialize($processed_attribute);
159 
160  // Save back to the questions
161  $q_value[$q_place_id]['attributes']['options'] = $processed_attribute;
162 
163  // Increase the count
164  $count++;
165 
166  }
167  }
168 
169  // Save the attribute
170  $fe->setAttrValue('questions', $q_value);
171 
172  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
173 
174  if (!$fe->saveAttributes()) {
175  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
176  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
177 
178  pre_echo($asset_name.' UPGRADE FAILED - Could not save attributes');
179  return FALSE;
180  }
181 
182  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
183 
184  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($fe);
185  unset($fe);
186 
187  }//end foreach
188 
189  pre_echo('Updated Select Question Types - '.$count.' question(s)...');
190 
191  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
192  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
193 
194  pre_echo($asset_name.' SUCCESSFULLY UPGRADED TO VERSION 0.5');
195 
196  }//end if
197 
198  return TRUE;
199 
200  }//end _upgrade()
201 
202 
203 }//end class
204 ?>