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: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 
<?php

require_once __DIR__."/../utils/H5pUtil.php";
require_once __DIR__."/../swagifact/H5pSwagifact.php";

/**
 * An item in a swag post.
 */
class SwagPostItem {

    protected $index;
    protected $swagPost;
    protected $type;
    protected $parameters;

    /**
     * Constructor.
     */
    public function __construct($type, $parameters) {
        $this->type=$type;
        $this->parameters=$parameters;
    }

    /**
     * Notification that this item is about to be shown.
     */
    public function preShow() {
        switch ($this->type) {
            case 'h5p':
                $id=H5pUtil::getH5pIdByShortcodeArgs($this->parameters);
                $h5p=H5pUtil::getH5pById($id,array("parameters"));
                $h5pParameters=json_decode($h5p["parameters"],TRUE);
                if ($h5pParameters["timeline"]) {
                    $this->saveCompletedStatement(SwagUser::getCurrent());
                    $this->swagPost->saveProvidedSwagIfCompleted(SwagUser::getCurrent());
                }
                break;
            
            default:
                break;
        }
    }

    /**
     * Save a completed statement to xapi.
     * This is normally done by other components, but sometimes
     * it can be useful to override this...
     */
    public function saveCompletedStatement($swagUser) {
        $xapi=SwagPlugin::instance()->getXapi();
        if (!$xapi)
            return array();

        $user=$swagUser->getUser();
        if (!$user || !$user->ID)
            return;

        $pageUrl=get_permalink($this->swagPost->getPost()->ID);

        $statement=array(
            "actor"=>array(
                "mbox"=>"mailto:".$user->user_email,
                "name"=>$user->display_name
            ),

            "object"=>array(
                "objectType"=>"Activity",
                "id"=>$this->getObjectUrl()
            ),

            "verb"=>array(
                "id"=>"http://adlnet.gov/expapi/verbs/completed"
            ),

            "context"=>array(
                "contextActivities"=>array(
                    "grouping"=>array(
                        array(
                            "objectType"=>"Activity",
                            "id"=>$pageUrl,
                            "definition"=>array(
                                "type"=>"http://activitystrea.ms/schema/1.0/page"
                            )
                        )
                    )
                )
            ),
        );

        $xapi->putStatement($statement);
    }

    /**
     * Set index.
     */
    public function setSwagPost($swagPost) {
        $this->swagPost=$swagPost;
    }

    /**
     * Set index.
     */
    public function setIndex($index) {
        $this->index=$index;
    }

    /**
     * Is this the selected index? Determines this by checking
     * the $_REQUEST["tab"] value.
     */
    public function isSelected() {
        return $_REQUEST["tab"]==$this->index;
    }

    /**
     * Get direct url.
     */
    public function getUrl() {
        return $this->swagPost->getPost()->post_permalink."?tab=".$this->index;
    }

    /**
     * Is this part completed?
     */
    public function isCompleted($swagUser) {
        $objectUrl=$this->getObjectUrl();

        foreach ($this->swagPost->getRelatedStatements($swagUser) as $statement) {
            if ($statement["object"]["id"]==$objectUrl)
                return TRUE;
        }

        return FALSE;
    }

    /**
     * Get xAPI object for checking completion.
     */
    public function getObjectUrl() {
        if (!$this->isTypeAvailable())
            return NULL;

        if (!$this->objectUrl) {
            switch ($this->type) {
                case "h5p":
                    $id=H5pUtil::getH5pIdByShortcodeArgs($this->parameters);
                    $this->objectUrl=
                        get_site_url().
                        "/wp-admin/admin-ajax.php?action=h5p_embed&id=".$id;
                    break;

                case "deliverable":
                    $slug=$this->parameters["slug"];
                    $this->objectUrl=
                        get_site_url().
                        "/wp-content/plugins/wp-deliverable/deliverable.php/".$slug;
                    break;
            }
        }

        return $this->objectUrl;
    }

    /**
     * Get the title.
     */
    public function getTitle() {
        global $wpdb;

        if (!$this->isTypeAvailable())
            return "";

        switch ($this->type) {
            case "h5p":
                $id=H5pUtil::getH5pIdByShortcodeArgs($this->parameters);
                return H5pUtil::getH5pTitleById($id);
                break;

            case "deliverable":
                $slug=$this->parameters["slug"];
                $q=$wpdb->prepare("SELECT title FROM {$wpdb->prefix}deliverable WHERE slug=%s",$slug);
                $title=$wpdb->get_var($q);

                if ($wpdb->last_error)
                    throw new Exception($wpdb->last_error);

                return $title;
                break;
        }
    }

    /**
     * Is the type for this item available?
     */
    public function isTypeAvailable() {
        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
        switch ($this->type) {
            case "h5p":
                return is_plugin_active("h5p/h5p.php");
                break;

            case "deliverable":
                return is_plugin_active("wp-deliverable/wp-deliverable.php");
                break;
        }

        return FALSE;
    }

    /**
     * Get content.
     */
    public function getContent() {
        if (!$this->isTypeAvailable())
            return "dependencies missing for ".$this->type;

        switch ($this->type) {
            case "h5p":
                $id=H5pUtil::getH5pIdByShortcodeArgs($this->parameters);
                return do_shortcode("[h5p id='$id']");
                break;

            case "deliverable":
                $slug=$this->parameters["slug"];
                return do_shortcode("[deliverable slug='$slug']");
                break;
        }
    }

    /**
     * Create.
     */
    public static function create($type, $slug) {
        switch ($type) {
            case "h5p":
            case "h5p-course-item":
                return new H5pSwagifact("h5p",array(
                    "slug"=>$slug
                ));
                break;

            case "deliverable":
            case "deliverable-course-item":
                return new SwagPostItem("deliverable",array(
                    "slug"=>$slug
                ));
                break;
        }

        return $item;
    }
}
Swag API documentation generated by ApiGen