BxDataMigrationPoll.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. require_once 'BxDataMigrationData.php';
  3. class BxDataMigrationPoll extends BxDataMigrationData
  4. {
  5. var $iTransffered = 0;
  6. /**
  7. * Class constructor;
  8. *
  9. * @param : $oMigrationModule (object) - object instance of migration class;
  10. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  11. * @param : $oDolModule (object);
  12. * @return : void;
  13. */
  14. function BxDataMigrationPoll(&$oMigrationModule, &$rOldDb, $oDolModule = '')
  15. {
  16. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  17. }
  18. /**
  19. * Function migrate polls data;
  20. *
  21. * @return : (integer) operation result;
  22. */
  23. function getMigration()
  24. {
  25. if(!$this -> oDolModule) {
  26. $this -> setResultStatus('System error: object instance is not received');
  27. return MIGRATION_FAILED;
  28. }
  29. // set new status;
  30. $this -> setResultStatus('Polls transfer now');
  31. mysql_query('SET NAMES utf8', $this->rOldDb);
  32. // transfer all profiles polls;
  33. $sError = $this -> transferredProfilePolls();
  34. if(!$sError) {
  35. // transfer all admins polls;
  36. $sError = $this -> transferredAdminPolls();
  37. if($sError) {
  38. $this -> setResultStatus($sError);
  39. return MIGRATION_FAILED;
  40. }
  41. }
  42. else {
  43. $this -> setResultStatus($sError);
  44. return MIGRATION_FAILED;
  45. }
  46. // set as finished;
  47. $this -> setResultStatus('All polls were transferred (' . $this -> iTransffered . ' items)');
  48. return MIGRATION_SUCCESSFUL;
  49. }
  50. /**
  51. * Function will trasnfer all admin's polls;
  52. *
  53. * @return : (string) - error message or empty;
  54. */
  55. function transferredAdminPolls()
  56. {
  57. // set charset;
  58. /*$sQuery = "ALTER TABLE `polls_q` CHANGE `Question` `Question` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL";
  59. mysql_query($sQuery, $this->rOldDb);
  60. // set charset;
  61. $sQuery = "ALTER TABLE `polls_a` CHANGE `Answer` `Answer` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL";
  62. mysql_query($sQuery, $this->rOldDb);
  63. */
  64. $sQuery = "SELECT * FROM `polls_q`";
  65. $rResult = mysql_query($sQuery, $this -> rOldDb);
  66. while( $aRow = mysql_fetch_assoc($rResult) )
  67. {
  68. $sPollQuestion = $this -> oMigrationModule -> _oDb -> escape($aRow['Question']);
  69. $sPollResult = $sPollAnswers = '';
  70. $iPollTotalresult = 0;
  71. $sPollStatus = $aRow['Active'] == 'on' ? 'active' : '';
  72. // get poll's answer list;
  73. $sQuery = "SELECT * FROM `polls_a` WHERE `ID` = {$aRow['ID']}";
  74. $rResult2 = mysql_query($sQuery, $this -> rOldDb);
  75. // get answer list;
  76. while( $aRow2 = mysql_fetch_assoc($rResult2) )
  77. {
  78. $sPollAnswers .= $aRow2['Answer'] . '<delim>';
  79. $sPollResult .= $aRow2['Votes'] . ';';
  80. $iPollTotalresult += $aRow2['Votes'];
  81. }
  82. $sPollAnswers = $this -> oMigrationModule -> _oDb -> escape($sPollAnswers);
  83. $sQuery =
  84. "
  85. INSERT INTO
  86. `{$this -> oDolModule -> _oDb -> sTablePrefix}data`
  87. SET
  88. `poll_question` = '{$sPollQuestion}',
  89. `poll_answers` = '{$sPollAnswers}',
  90. `poll_results` = '{$sPollResult}',
  91. `poll_status` = '{$sPollStatus }',
  92. `poll_approval` = '1',
  93. `poll_date` = UNIX_TIMESTAMP(),
  94. `poll_total_votes` = '{$iPollTotalresult}',
  95. `id_profile` = 0
  96. ";
  97. $this -> oMigrationModule -> _oDb -> query($sQuery);
  98. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  99. if($iResult <= 0) {
  100. return 'Database error. Cannot insert new admin poll in the database.';
  101. }
  102. $this -> iTransffered++;
  103. }
  104. }
  105. /**
  106. * Function will transfer all profile's polls;
  107. *
  108. * @return : (string) - error message or empty;
  109. */
  110. function transferredProfilePolls()
  111. {
  112. // set charset;
  113. /* $sQuery = "ALTER TABLE `ProfilesPolls` CHANGE `poll_question` `poll_question` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL";
  114. mysql_query($sQuery, $this->rOldDb);
  115. // set charset;
  116. $sQuery = "ALTER TABLE `ProfilesPolls` CHANGE `poll_answers` `poll_answers` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL";
  117. mysql_query($sQuery, $this->rOldDb);
  118. */
  119. $sQuery = "SELECT * FROM `ProfilesPolls`";
  120. $rResult = mysql_query($sQuery, $this -> rOldDb);
  121. while($aRow = mysql_fetch_assoc($rResult))
  122. {
  123. if( !$this -> isPollExisting($aRow['id_poll']) ) {
  124. $sPollQuestion = $this -> oMigrationModule -> _oDb -> escape($aRow['poll_question']);
  125. $sPollAnswers = $this -> oMigrationModule -> _oDb -> escape($aRow['poll_answers']);
  126. // execute query;
  127. $sQuery =
  128. "
  129. INSERT INTO
  130. `{$this -> oDolModule -> _oDb -> sTablePrefix}data`
  131. SET
  132. `id_poll` = {$aRow['id_poll']},
  133. `poll_question` = '{$sPollQuestion}',
  134. `poll_answers` = '{$sPollAnswers}',
  135. `poll_results` = '{$aRow['poll_results']}',
  136. `poll_status` = '{$aRow['poll_status']}',
  137. `poll_approval` = '{$aRow['poll_approval']}',
  138. `poll_date` = UNIX_TIMESTAMP(),
  139. `poll_total_votes` = '{$aRow['poll_total_votes']}',
  140. `id_profile` = {$aRow['id_profile']}
  141. ";
  142. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  143. if($iResult <= 0) {
  144. return 'Database error. Cannot insert new profile poll in the database.';
  145. }
  146. $this -> iTransffered++;
  147. }
  148. else {
  149. return 'Duplicate data (in profiles poll).
  150. Polls with
  151. similar info already exists
  152. (remove all polls and start again)';
  153. }
  154. }
  155. }
  156. /**
  157. * Function will check existing poll in module;
  158. *
  159. * @param : $iPollId (integer) - poll's Id;
  160. * @return : true if exist;
  161. */
  162. function isPollExisting($iPollId)
  163. {
  164. $iPollId = (int) $iPollId;
  165. $sQuery = "SELECT COUNT(*) FROM `{$this -> oDolModule -> _oDb -> sTablePrefix}data` WHERE `id_poll` = '{$iPollId}'";
  166. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  167. }
  168. }