News:

Support the VirtueMart project and become a member

Main Menu

VM1 to VM2: Porting of Product Reviews missing

Started by mbarry, January 16, 2014, 06:33:42 AM

Previous topic - Next topic

mbarry

VM 2.0.26a/d
Joomla 2.5.17

Scenario:
      Run the VM2 Migration tool with "everything" selected".
         
Issue:
      The product reviews and ratings gathered in the VM1 shop are not ported to VM2.
      For large VM1 shops, these tables could hold a significant amount of data and would be very time consuming/error prone to recreate in VM2.
     
Solution:
      A new function portVm1Ratings() has been created and included in helpers/migrator.php
     
      Call function from migrateAllInOne() after line 208 as $result = $this->portVm1Ratings()

      install.sql is also modified to include new entry " `ratings` longblob, "  in Table `#__virtuemart_migration_oldtonew_ids`
     
      function saveRating() in models/ratings.php was modified to support vm1 to vm2 migration process where the $data['lastip'] should already exist
      line 266 change
        From
          $data['lastip'] = $_SERVER['REMOTE_ADDR'];
        To
          if(empty($data['lastip'])) {
                $data['lastip'] = $_SERVER['REMOTE_ADDR'];
              }       

Affected files:
      administrator/components/com_virtuemart/helpers/migrator.php  line 208 and 2167
      administrator/components/com_virturemart/install/install.sql  line 424 
      administrator/components/com_virturemart/models/ratings.php   line 266

code snip for portVm1Ratings()

   /**
    *
    * Port VM1 Product reviews
    * Assumes userId is maintained between VM1 and VM2 which they will be if JUpgrade is used
    *
    * @author M. Barry
    */
   public function portVm1Ratings() {
   
      if($this->_stop || (microtime(true)-$this->starttime) >= ($this->maxScriptTime)){
         return;
      }
      vmInfo('Migration: processing product ratings ..... ');
      
      $alreadyKnownIds = $this->getMigrationProgress('ratings');
      $alreadyProcessed = count($alreadyKnownIds);
      $completionStatus = false;
      $i = 0;

      $q = 'SELECT `pr`.`review_id`, `pr`.`product_id`, `pr`.`userid`, `pr`.`user_rating`, `pr`.`comment`, `pr`.`time`, `pr`.`published`, `pv`.`lastip` FROM `#__vm_product_reviews` AS pr
      JOIN  `#__vm_product_votes` AS pv
      WHERE `pr`.`product_id` = `pv`.`product_id`';
         
      $this->_db->setQuery($q);
      $reviews = $this->_db->loadAssocList();
      $maxItems = count($reviews);
      
      if (empty($reviews)) {
         vmWarn('portVm1Ratings : No VM1 product reviews exist');
         return;
      }
   
      foreach ($reviews as $review) {
         
         if(array_key_exists($review['review_id'],$alreadyKnownIds)) {
            continue;   
         }
         
         $productIdMapping = $this->getMigrationProgress('products');
         $parentProductId = $productIdMapping[$review['product_id']];
         
         if(empty($parentProductId)) {
            vmError('portVm1Ratings : product could not be found ID '.$review['product_id']);
            continue;
         }
   
         // Migration process should ensure VM2 userId is identical to VM1 userId if JUpgrade is used to migrate the VM1 tables to VM2         
         $q = 'SELECT `virtuemart_user_id` FROM `#__virtuemart_vmusers` WHERE `#__virtuemart_vmusers`.`virtuemart_user_id`= "'.$review['userid'].'"';
         $this->_db->setQuery($q);
         $userId = $this->_db->loadResult();
         
         if(empty($userId)) {
            vmError('portVm1Ratings : User does not exist ID '.$userId);
            continue;
         }
   
         $data['virtuemart_product_id'] = $parentProductId;
         $data['created_by'] = $userId;
         $data['created_on'] = $this->_changeToStamp($review['time']);
         $data['vote'] = $review['user_rating'];
         $data['lastip'] = $review['lastip'];
         $data['comment'] = $review['comment'];
         $data['published'] = $review['published'];
   
         $model = VmModel::getModel ('ratings');
         $model->saveRating($data);
         $errors = $model->getErrors ();
         if(!empty($error)) {
            vmError ('portVm1Rating : '.$error);
         }
         
         $alreadyKnownIds[$review['review_id']] = (int) $parentProductId;
         
         $i++;
         if((microtime(true)-$this->starttime) >= ($this->maxScriptTime)){
            break;
         }
      }
      $this->storeMigrationProgress('ratings',$alreadyKnownIds);
      vmInfo('Migration: '.$i.' product reviews processed this run, already processed '.$alreadyProcessed.' of '.$maxItems);
      if(($alreadyProcessed+$i) == $maxItems) $completionStatus = true;
      return $completionStatus;
   }