/**
  * Award a badge to a user and record some activity
  *
  * @param int $BadgeID
  * @param int $UserID This is the user that should get the award
  * @param int $InsertUserID This is the user that gave the award
  * @param string $Reason This is the reason the giver gave with the award
  */
 public function Award($BadgeID, $UserID, $InsertUserID = NULL, $Reason = '')
 {
     $Badge = Yaga::BadgeModel()->GetByID($BadgeID);
     if (!empty($Badge)) {
         if (!$this->Exists($UserID, $BadgeID)) {
             $this->SQL->Insert('BadgeAward', array('BadgeID' => $BadgeID, 'UserID' => $UserID, 'InsertUserID' => $InsertUserID, 'Reason' => $Reason, 'DateInserted' => date(DATE_ISO8601)));
             // Record the points for this badge
             UserModel::GivePoints($UserID, $Badge->AwardValue, 'Badge');
             // Increment the user's badge count
             $this->SQL->Update('User')->Set('CountBadges', 'CountBadges + 1', FALSE)->Where('UserID', $UserID)->Put();
             if (is_null($InsertUserID)) {
                 $InsertUserID = Gdn::Session()->UserID;
             }
             // Record some activity
             $ActivityModel = new ActivityModel();
             $Activity = array('ActivityType' => 'BadgeAward', 'ActivityUserID' => $UserID, 'RegardingUserID' => $InsertUserID, 'Photo' => $Badge->Photo, 'RecordType' => 'Badge', 'RecordID' => $BadgeID, 'Route' => '/badges/detail/' . $Badge->BadgeID . '/' . Gdn_Format::Url($Badge->Name), 'HeadlineFormat' => T('Yaga.Badge.EarnedHeadlineFormat'), 'Data' => array('Name' => $Badge->Name), 'Story' => $Badge->Description);
             // Create a public record
             $ActivityModel->Queue($Activity, FALSE);
             // TODO: enable the grouped notifications after issue #1776 is resolved , array('GroupBy' => 'Route'));
             // Notify the user of the award
             $Activity['NotifyUserID'] = $UserID;
             $ActivityModel->Queue($Activity, 'BadgeAward', array('Force' => TRUE));
             // Actually save the activity
             $ActivityModel->SaveQueue();
             $this->EventArguments['UserID'] = $UserID;
             $this->FireEvent('AfterBadgeAward');
         }
     }
 }
 /**
  * Creates a transport file for easily transferring Yaga configurations across
  * installs
  *
  * @param array An array containing the config areas to transfer
  * @param string Where to save the transport file
  * @return mixed False on failure, the path to the transport file on success
  */
 protected function _ExportData($Include = array(), $Path = NULL)
 {
     $StartTime = microtime(TRUE);
     $Info = new stdClass();
     $Info->Version = C('Yaga.Version', '?.?');
     $Info->StartDate = date('Y-m-d H:i:s');
     if (is_null($Path)) {
         $Path = PATH_UPLOADS . DS . 'export' . date('Y-m-d-His') . '.yaga.zip';
     }
     $FH = new ZipArchive();
     $Images = array();
     $Hashes = array();
     if ($FH->open($Path, ZipArchive::CREATE) !== TRUE) {
         $this->Form->AddError(sprintf(T('Yaga.Error.ArchiveCreate'), $FH->getStatusString()));
         return FALSE;
     }
     // Add configuration items
     $Info->Config = 'configs.yaga';
     $Configs = Gdn::Config('Yaga', array());
     unset($Configs['Version']);
     $ConfigData = serialize($Configs);
     $FH->addFromString('configs.yaga', $ConfigData);
     $Hashes[] = md5($ConfigData);
     // Add actions
     if ($Include['Action']) {
         $Info->Action = 'actions.yaga';
         $Actions = Yaga::ActionModel()->Get('Sort', 'asc');
         $this->SetData('ActionCount', count($Actions));
         $ActionData = serialize($Actions);
         $FH->addFromString('actions.yaga', $ActionData);
         $Hashes[] = md5($ActionData);
     }
     // Add ranks and associated image
     if ($Include['Rank']) {
         $Info->Rank = 'ranks.yaga';
         $Ranks = Yaga::RankModel()->Get('Level', 'asc');
         $this->SetData('RankCount', count($Ranks));
         $RankData = serialize($Ranks);
         $FH->addFromString('ranks.yaga', $RankData);
         array_push($Images, C('Yaga.Ranks.Photo'), NULL);
         $Hashes[] = md5($RankData);
     }
     // Add badges and associated images
     if ($Include['Badge']) {
         $Info->Badge = 'badges.yaga';
         $Badges = Yaga::BadgeModel()->Get();
         $this->SetData('BadgeCount', count($Badges));
         $BadgeData = serialize($Badges);
         $FH->addFromString('badges.yaga', $BadgeData);
         $Hashes[] = md5($BadgeData);
         foreach ($Badges as $Badge) {
             array_push($Images, $Badge->Photo);
         }
     }
     // Add in any images
     $FilteredImages = array_filter($Images);
     $ImageCount = count($FilteredImages);
     $this->SetData('ImageCount', $ImageCount);
     if ($ImageCount > 0) {
         $FH->addEmptyDir('images');
     }
     foreach ($FilteredImages as $Image) {
         if ($FH->addFile('.' . $Image, 'images/' . $Image) === FALSE) {
             $this->Form->AddError(sprintf(T('Yaga.Error.AddFile'), $FH->getStatusString()));
             //return FALSE;
         }
         $Hashes[] = md5_file('.' . $Image);
     }
     // Save all the hashes
     sort($Hashes);
     $Info->MD5 = md5(implode(',', $Hashes));
     $Info->EndDate = date('Y-m-d H:i:s');
     $EndTime = microtime(TRUE);
     $TotalTime = $EndTime - $StartTime;
     $m = floor($TotalTime / 60);
     $s = $TotalTime - $m * 60;
     $Info->ElapsedTime = sprintf('%02d:%02.2f', $m, $s);
     $FH->setArchiveComment(serialize($Info));
     if ($FH->close()) {
         return $Path;
     } else {
         $this->Form->AddError(sprintf(T('Yaga.Error.ArchiveSave'), $FH->getStatusString()));
         return FALSE;
     }
 }