Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_tickbox_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  }//end constructor
70 
71 
80  function _upgrade($current_version)
81  {
82  // first do things as normal
83  $res = parent::_upgrade($current_version);
84 
85  if (version_compare($current_version, '0.6', '<')) {
86  echo('UPGRADING FORM QUESTION TYPE TICKBOX LIST FROM '.$current_version."\n");
87  // Upgrade the option list
88  // The option list in form_questions that use the option list (option_list, select and tickbox_list)
89  // have been changed from a string delimiter by newline characters to a serialised array
90  // So here we should manually upgrade questions to reflect this change
91 
92  $count = 0;
93 
94  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
95  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
96 
97  pre_echo('Updating Tickbox List Question Types...');
98 
99  // get all question assets
100  $form_ids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form', FALSE, TRUE);
101  $question_assets = Array();
102  foreach ($form_ids as $form_id => $type) {
103  $form = $GLOBALS['SQ_SYSTEM']->am->getAsset($form_id, $type['type_code']);
104  $question_assets += $form->getChildren($form->id, 'form_question_type_tickbox_list', TRUE);
105  }
106  $section_ids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form_section', FALSE, TRUE);
107  foreach ($section_ids as $section_id => $type) {
108  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($section_id, $type['type_code']);
109  $question_assets += $section->getChildren($section->id, 'form_question_type_tickbox_list', TRUE);
110  }
111 
112  // update them
113  foreach ($question_assets as $id => $type) {
114  $q = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
115  $options = $q->attr('options');
116 
117  // Verify that the seralised options are not in array format before upgradinga
118  // Don't report PHP Notice errors for the next block in case unserialize() fails
119  $orig_error_reporting = error_reporting();
120  error_reporting($orig_error_reporting ^ E_NOTICE);
121  $options_array_format = is_array(unserialize($options));
122  error_reporting($orig_error_reporting);
123 
124  if (!$options_array_format) {
125  $options = str_replace("\r", '', $options);
126  $opts = explode("\n", $options);
127  $q->setAttrValue('options', serialize($opts));
128 
129  // Save the question
130  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
131 
132  if (!$q->saveAttributes()) {
133  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
134  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
135 
136  pre_echo('UPGRADE FAILED - Could not save attributes');
137  return FALSE;
138  }
139 
140  $count++;
141 
142  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
143 
144  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($q);
145  unset($q);
146  }//end if
147  }//end foreach
148 
149  pre_echo('Updated Tickbox List Question Types - '.$count.' question(s)...');
150 
151  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
152  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
153 
154  pre_echo('TICKBOX LIST QUESTION TYPES SUCCESSFULLY UPGRADED TO VERSION 0.5');
155  }//end if
156  return TRUE;
157 
158  }//end _upgrade()
159 
160 
161 }//end class
162 ?>