<?php

// no direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_SITE . "/swg/swg.php";
require_once __DIR__ . '/helper.php';
JHTML::script("modules/mod_swg_userstats/script/userstats.js", true);
$stats = ModSWG_UserStatsHelper::getStats(JFactory::getUser(), UnitConvert::Mile);
require JModuleHelper::getLayoutPath('mod_swg_userstats');
 public function attend()
 {
     // Record this user's attendance or non-attendance
     $db = JFactory::getDBO();
     $query = $db->getQuery(true);
     $user = JFactory::getUser();
     $values = array("eventtype = " . JRequest::getInt("evttype"), "eventid = " . JRequest::getInt("evtid"), "user = "******"count(1)");
     $query->from("eventattendance");
     $query->where($values);
     $db->setQuery($query);
     $been = $db->loadResult();
     // Set up delete/insert query
     if ($been && !JRequest::getBool("set")) {
         // Unset attendance
         $query = $db->getQuery(true);
         $query->delete("eventattendance");
         $query->where($values);
         $db->setQuery($query);
         $db->query();
     } else {
         if (!$been && JRequest::getBool("set")) {
             // Set attendance
             $query = $db->getQuery(true);
             $query->insert("eventattendance");
             $query->set($values);
             $db->setQuery($query);
             $db->query();
         }
     }
     // Return to the page, showing the current event
     switch (JRequest::getInt("evttype")) {
         case Event::TypeWalk:
             $anchor = "walk";
             break;
         case Event::TypeSocial:
             $anchor = "social";
             break;
         case Event::TypeWeekend:
             $anchor = "weekend";
             break;
         default:
             $anchor = "event";
             break;
     }
     // Redirect to display the event, unless this is an AJAX request
     // TODO: Should use format=json
     // 		if (strtolower(JRequest::getString("format")) == "json")
     if (JRequest::getBool("json")) {
         $result = array('status' => JRequest::getBool("set"));
         if (JRequest::getBool("stats")) {
             // TODO: Uncouple
             include_once JPATH_SITE . "/modules/mod_swg_userstats/helper.php";
             $result['stats'] = ModSWG_UserStatsHelper::getStats($user, UnitConvert::Mile);
         }
         echo json_encode($result);
         exit;
     } else {
         $anchor .= "_" . JRequest::getInt("evtid");
         $target = JURI::current() . "#" . $anchor;
         JFactory::getApplication()->redirect($target);
     }
     return false;
 }