Overview

Namespaces

  • None
  • swag

Classes

  • H5pSwagifact
  • H5pUtil
  • SettingsPageController
  • ShortcodeUtil
  • swag\ArrayUtil
  • swag\Singleton
  • swag\WpUtil
  • SwagMapController
  • SwagPageController
  • Swagpath
  • SwagpathController
  • SwagpathSyncer
  • SwagPlugin
  • SwagPostItem
  • SwagTgmpaController
  • SwagTrack
  • SwagTrackController
  • SwagUser
  • Template
  • Xapi
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 
<?php

/**
 * Per user swag.
 */
class SwagUser {

    private static $swagUserById=array();
    private $user;
    private $email;

    /**
     * Construct.
     */
    private function __construct($user) {
        if ($user && $user->ID) {
            $this->user=$user;
            SwagUser::$swagUserById[$user->ID]=$this;
        }

        $this->xapi=SwagPlugin::instance()->getXapi();

        $this->completedSwagFetched=NULL;
        $this->completedSwag=NULL;
    }

    /**
     * Set email.
     */
    private function setEmail($email) {
        if ($this->user)
            throw new Exception("This is a real user, can't set email");

        $this->email=$email;
    }

    /**
     * Get user id.
     */
    public function getId() {
        if (!$this->user)
            throw new Exception("User is not logged in.");

        return $this->user->ID;
    }

    /**
     * Get user.
     */
    public function getUser() {
        if (!$this->user)
            throw new Exception("User is not logged in.");

        return $this->user;
    }

    /**
     * Get display name.
     */
    public function getDisplayName() {
        if ($this->user)
            return $this->user->display_name;

        return "Temporary User";
    }

    /**
     * Get email.
     */
    public function getEmail() {
        if ($this->user) {
            if (!$this->user->user_email)
                throw new Exception("We have a user, but no email");

            return $this->user->user_email;
        }

        if (!$this->email)
            throw new Exception("User email not set");

        return $this->email;
    }

    /**
     * Get completed for top level track.
     */
    public function getCompletedByTopLevelTrack($trackSlug) {
        $swagpaths=$this->getCompletedSwagpaths();
        $res=array();

        foreach ($swagpaths as $swagpath) {
            if ($swagpath->getTopLevelTrack()==$trackSlug)
                $res[]=$swagpath;
        }

        return $res;
    }

    /**
     * Clear fetched statements.
     */
    public function clearFetchedStatements() {
        $this->completedSwagFetched=NULL;
        $this->completedSwag=NULL;
    }

    /**
     * Get collected swag.
     */
    public function getCompletedSwagpaths() {
        if ($this->completedSwagFetched)
            return $this->completedSwag;

        if ($this->xapi) {
            $statements=$this->xapi->getStatements(array(
                "agentEmail"=>$this->getEmail(),
                "activity"=>"http://swag.tunapanda.org/",
                "verb"=>"http://adlnet.gov/expapi/verbs/completed",
                "related_activities"=>"true"
            ));
        }

        else {
            $statements=array();
        }

        $this->completedSwag=array();
        foreach ($statements as $statement) {
            $slug=str_replace("http://swag.tunapanda.org/","",$statement["object"]["id"]);
            $swagpath=Swagpath::getBySlug($slug);

            if ($swagpath)
                $this->completedSwag[]=$swagpath;
        }

        $this->completedSwagFetched=TRUE;

        return $this->completedSwag;
    }

    /**
     * Is this swag completed by the user?
     */
    public function isSwagpathCompleted($swagpath) {
        $completed=$this->getCompletedSwagpaths();
        foreach ($completed as $c)
            if ($c->getXapiObjectId()==$swagpath->getXapiObjectId())
                return TRUE;

        return FALSE;
    }

    /**
     * Logged in?
     */
    public function isLoggedIn() {
        if ($this->user && $this->user->ID)
            return TRUE;

        return FALSE;
    }

    /**
     * Get current SwagUser.
     */
    public static function getCurrent() {
        static $current;

        if (!$current) {
            $u=wp_get_current_user();

            if ($u && $u->ID) {
                $current=new SwagUser($u);
            }

            else {
                if (!session_id())
                    session_start();

                $sessionId=session_id();
                //error_log("creating pseudo user: ".$sessionId);

                $current=new SwagUser(NULL);
                $current->setEmail($sessionId."@example.com");
            }
        }

        return $current;
    }

    /**
     * Get by id.
     */
    public static function getById($id) {
        if (isset(SwagUser::$swagUserById[$id]))
            return SwagUser::$swagUserById[$id];

        $user=get_user_by("ID",$id);
        if (!$user)
            return NULL;

        return new SwagUser($user);
    }

    /**
     * Get by email.
     */
    public static function getByEmail($email) {
        $email=str_replace("mailto:","",$email);

        $current=SwagUser::getCurrent();
        if ($current->getEmail()==$email)
            return $current;

        $user=get_user_by("email",$email);
        if (!$user || !$user->ID)
            throw new Exception("user not found: ".$email);

        return new SwagUser($user);
    }
}
Swag API documentation generated by ApiGen