示例#1
0
 /**
  * Request a page of responses from the API
  *
  * A page of responses will be requested if it appears that that page has not
  * yet been loaded (tested by checking if the initial element of the page has
  * been initialized in the $data array).
  *
  * @param int $pageNumber Page number to request
  * @param bool $forceRefresh (Optional) Force a refresh of backing data, even
  *		if cached (defaults to `FALSE`)
  *
  * @return bool `TRUE` if the page is requested, `FALSE` if it is already cached
  *		(and therefore not requested)
  **/
 private function requestPageNumber($pageNumber, $forceRefresh = false)
 {
     if (!isset($this->data[$this->pageNumberToKey($pageNumber)]) || $forceRefresh && isset($this->api)) {
         // assume one page if no pagination (and already loaded)
         if (isset($this->pagination[CanvasPageLink::CURRENT])) {
             $params = $this->pagination[CanvasPageLink::CURRENT]->getParams();
             $params[CanvasPageLink::PARAM_PAGE_NUMBER] = $pageNumber;
             $page = $this->api->get($this->pagination[CanvasPageLink::CURRENT]->getEndpoint(), $params);
             $this->data = array_replace($this->data, $page->data);
             return true;
         }
     }
     return false;
 }
    /**
     * Obtain a Canvas API token, if needed.
     *
     * @param scalar $step optional Where are we in the API token negotiation workflow? (defaults to API_DECISION_NEEDED_STEP -- the beginning)
     * @param boolean $skip optional Skip this step (defaults to FALSE)
     *
     * @throws CanvasAPIviaLTI_Installer_Exception If $step is not a pre-defined *_STEP constant
     **/
    public static function acquireAPIToken($step = self::API_DECISION_NEEDED_STEP, $skip = false)
    {
        global $secrets;
        // FIXME grown-ups don't program like this
        global $metadata;
        // FIXME grown-ups don't program like this
        global $smarty;
        // FIXME grown-ups don't program like this
        if ($skip) {
            if (isset($metadata['CANVAS_API_TOKEN']) || isset($metadata['CANVAS_API_USER'])) {
                $api = new CanvasPest("{$metadata['CANVAS_INSTANCE_URL']}/login/oauth2", $metadata['CANVAS_API_TOKEN']);
                $api->delete('token');
                unset($metadata['CANVAS_API_TOKEN']);
                unset($metadata['CANVAS_API_USER']);
                $smarty->addMessage('Existing admin Canvas API token information expunged', 'There was already an administrative access token stored in your
					 application metadata, and it has now been expunged.');
            } else {
                $smarty->addMessage('No admin Canvas API token acquired', 'An administrative API token has not been acquired. Users will be asked to
					 acquire their own API tokens on their first use of the LTI.');
            }
        } else {
            switch ($step) {
                case self::API_DECISION_NEEDED_STEP:
                    $smarty->assign('content', '
						<form action="' . $metadata['APP_URL'] . '/admin/oauth.php" method="post">
							<label for="url"> Canvas Instance URL <input type="text" name="url" id="url" placeholder="' . $metadata['CANVAS_INSTANCE_URL_PLACEHOLDER'] . '" value="' . (isset($metadata['CANVAS_INSTANCE_URL']) ? $metadata['CANVAS_INSTANCE_URL'] : '') . '" /></label>
							<label for="token"> API Access Token <input type="text" name="token" id="token" placeholder="Leave blank to acquire a token interactively" /></label>
							<input type="hidden" name="skip" value="0" />
							<input type="hidden" name="step" value="' . self::API_DECISION_ENTERED_STEP . '" />
							<input type="submit" value="Use administrative token" />
						</form>
						or
						<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
							<input type="hidden" name="skip" value="1" />
							<input type="hidden" name="step" value="' . self::API_DECISION_ENTERED_STEP . '" />
							<input type="submit" value="Require users to acquire individual tokens" />
						</form>
					');
                    $smarty->display();
                    exit;
                case self::API_DECISION_ENTERED_STEP:
                    $oauth = new OAuthNegotiator();
                    if ($oauth->isAPIToken()) {
                        $metadata['CANVAS_API_TOKEN'] = $oauth->getToken();
                        $smarty->addMessage('Admin Canvas API token acquired', 'An administrative API access token has been acquired and stored in your application metadata.', NotificationMessage::GOOD);
                    }
                    /* clear the processed step */
                    unset($_REQUEST['step']);
                    break;
                case self::API_TOKEN_PROVIDED_STEP:
                    $smarty->addMessage('Admin Canvas API token provided', 'You provided an API access token and it has been stored in your application metadata.');
                    break;
                default:
                    throw new CanvasAPIviaLTI_Installer_Exception("Unknown step ({$step}) in obtaining API token.", CanvasAPIviaLTI_Installer_Exception::API_STEP_MISMATCH);
            }
        }
    }