Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_option_list_management.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
19 
20 
32 {
33 
34 
40  {
41  $this->Asset_Management($pm);
42 
43  $this->vars = Array(
44  'options' => Array(
45  'added' => '0.0.1',
46  'type' => 'option_list',
47  'default' => '',
48  ),
49  'default' => Array(
50  'added' => '0.0.1',
51  'type' => 'serialise',
52  'default' => Array(),
53  ),
54  'extra' => Array(
55  'added' => '0.0.1',
56  'type' => 'text',
57  'default' => '',
58  ),
59  'required' => Array(
60  'added' => '0.0.1',
61  'type' => 'boolean',
62  'default' => FALSE,
63  'parameters' => Array(
64  'allow_empty' => FALSE,
65  ),
66  'description' => 'DEPRECATED AS OF v0.3',
67  ),
68  );
69 
70  }//end constructor
71 
72 
81  function _upgrade($current_version)
82  {
83  // first do things as normal
84  $res = parent::_upgrade($current_version);
85 
86  if (version_compare($current_version, '0.5', '<')) {
87  echo('UPGRADING FORM QUESTION TYPE OPTION LIST FROM '.$current_version."\n");
88  // Upgrade the option list
89  // The option list in form_questions that use the option list (option_list, select and tickbox_list)
90  // have been changed from a string delimiter by newline characters to a serialised array
91  // So here we should manually upgrade questions to reflect this change
92 
93  $count = 0;
94 
95  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
96  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
97 
98  pre_echo('Updating Option List Question Types...');
99 
100  // get all question assets
101  $form_ids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form', FALSE, TRUE);
102  $question_assets = Array();
103  foreach ($form_ids as $form_id => $type) {
104  $form = $GLOBALS['SQ_SYSTEM']->am->getAsset($form_id, $type['type_code']);
105  $question_assets += $form->getChildren($form->id, 'form_question_type_option_list', TRUE);
106  }
107  $section_ids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form_section', FALSE, TRUE);
108  foreach ($section_ids as $section_id => $type) {
109  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($section_id, $type['type_code']);
110  $question_assets += $section->getChildren($section->id, 'form_question_type_option_list', TRUE);
111  }
112 
113  // update them
114  foreach ($question_assets as $id => $type) {
115  $q = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
116  $options = $q->attr('options');
117 
118  // Verify that the seralised options are not in array format before upgradinga
119  // Don't report PHP Notice errors for the next block in case unserialize() fails
120  $orig_error_reporting = error_reporting();
121  error_reporting($orig_error_reporting ^ E_NOTICE);
122  $options_array_format = is_array(unserialize($options));
123  error_reporting($orig_error_reporting);
124 
125  if (!$options_array_format) {
126  $options = str_replace("\r", '', $options);
127  $opts = explode("\n", $options);
128  $q->setAttrValue('options', serialize($opts));
129 
130  // Save the question
131  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
132 
133  if (!$q->saveAttributes()) {
134  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
135  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
136 
137  pre_echo('UPGRADE FAILED - Could not save attributes');
138  return FALSE;
139  }
140 
141  $count++;
142 
143  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
144 
145  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($q);
146  unset($q);
147  }//end if
148  }//end foreach
149 
150  pre_echo('Updated Option List Question Types - '.$count.' question(s)...');
151 
152  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
153  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
154 
155  pre_echo('OPTION LIST QUESTION TYPES SUCCESSFULLY UPGRADED TO VERSION 0.4');
156  }//end if
157  return TRUE;
158 
159  }//end _upgrade()
160 
161 
162 }//end class
163 ?>