Created
July 21, 2015 13:08
-
-
Save VovanZver/90508c63b628782a48fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sony_import_upload(){ | |
$form = array(); | |
$form['desc'] = array( | |
'#markup' => '<table><tbody> | |
<tr><td>Username</td><td>Category</td><td>Points</td><td>Date</td></tr> | |
<tr><td>aa01</td><td>SonyMobileOnTour</td><td>100</td><td>01 Jan 2012</td></tr> | |
<tr><td>Kemmy</td><td>OverAndAbove</td><td>200</td><td>01 Mar 2012</td></tr> | |
</tbody> | |
</table> | |
<p> The categories allowed are: General, Bonus, QuizEntry, QuizCorrect, MysteryShop, SonyMobileOnTour, OverAndAbove, TrainingSession, SonyEvent, SocialMediaChallenge.</p>' | |
); | |
$form['import']['file_upload'] = array( | |
'#type' => 'file', | |
'#title' => t('CSV File'), | |
'#size' => 40, | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => 'Import', | |
); | |
// set the form encoding type | |
$form['#attributes']['enctype'] = "multipart/form-data"; | |
return $form; | |
} | |
function sony_import_upload_submit($form, &$form_state) { | |
$dest_dir = file_default_scheme() . '://'; | |
$validators = array('file_validate_extensions' => array('csv')); | |
if ($file = file_save_upload('file_upload', $validators, $dest_dir)) { | |
$url = file_create_url($file->uri); | |
sony_import_points ($url); | |
} else { | |
form_set_error('Error',''); | |
} | |
} | |
function sony_import_points ($file){ | |
ini_set('auto_detect_line_endings', true); | |
$handle = fopen($file, "r"); | |
$cur_time = time(); | |
$badUsers = array(); | |
while ( ( $data = fgetcsv($handle, 0, ",")) !== FALSE){ | |
$query = db_select('users','u'); | |
$query->fields('u',array('uid','created')); | |
$query->condition('name',$data[0]); | |
$user = $query->execute()->fetchAssoc(); | |
if (!$user) { | |
$badUsers[] = $data[0]; | |
} | |
if ($user && is_numeric($data[2])) { | |
$date_approve = strtotime('+3 month',$user['created']); | |
$timestamp = strtotime($data[3]); | |
// echo '<pre>'; | |
// var_dump($data[3]); | |
// echo '</pre>'; | |
// exit; | |
if ($cur_time < $date_approve) { | |
$moderate = 1; | |
} else { | |
$moderate = 0; | |
} | |
$params = array( | |
'points' => $data[2], | |
'uid' => $user['uid'], | |
'operation' => 'admin', | |
'description' => $data[1], | |
'reference' => '', | |
'time_stamp' => $timestamp, | |
'approver_uid' => 1, | |
'moderate' => $moderate, | |
); | |
userpoints_userpointsapi($params); | |
} | |
} | |
drupal_set_message('Import has been completed!','status'); | |
if (!empty($badUsers)) { | |
drupal_set_message('Users do not exist: ' . implode(', ', $badUsers),'warning'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment