示例#1
0
            // Success callback
            function(data) {
              lastSuccessfulSubmissionData = thisSubmissionData;
              $lastSaved.text(I18n.t('saved_at', 'Quiz saved at %{t}', { t: $.friendlyDatetime(new Date()) }));
              quizSubmission.currentlyBackingUp = false;
              quizSubmission.inBackground = false;
              if(repeat) {
                setTimeout(function() {quizSubmission.updateSubmission(true, true) }, 30000);
              }
              if(data && data.end_at) {
                var endAtFromServer     = Date.parse(data.end_at),
                    submissionEndAt     = Date.parse(endAt.text()),
                    serverEndAtTime     = endAtFromServer.getTime(),
                    submissionEndAtTime = submissionEndAt.getTime();

                quizSubmission.timeLeft = data.time_left * 1000;

                // if the new endAt from the server is different than our current endAt, then notify
                // the user that their time limit's changed and let updateTime do the rest.
                if (serverEndAtTime !== submissionEndAtTime) {
                    if(serverEndAtTime > submissionEndAtTime) {
                      $.flashMessage(I18n.t('You have been given extra time on this attempt'))
                    } else {
                      $.flashMessage(I18n.t('Your time for this quiz has been reduced.'));
                    }

                  quizSubmission.endAt.text(data.end_at);
                  quizSubmission.endAtParsed = endAtFromServer;
                }
              }
            },
  render = _.memoize(() => {
    let datetime = this.props.dateTime
    if (!datetime) {
      return (<time />)
    }
    if (!_.isDate(datetime)) {
      datetime = tz.parse(datetime)
    }
    const fudged = $.fudgeDateForProfileTimezone(datetime)
    const friendly = this.props.format ? tz.format(datetime, this.props.format) : $.friendlyDatetime(fudged)

    const timeProps = Object.assign({}, this.props, {
      title: $.datetimeString(datetime),
      dateTime: datetime.toISOString(),
    })

    let fixedPrefix = this.props.prefix
    if (fixedPrefix && !fixedPrefix.endsWith(' ')) {
      fixedPrefix += ' '
    }

    return (
      <span>
        <ScreenReaderContent>
          {fixedPrefix + friendly}
        </ScreenReaderContent>

        <time {...timeProps} ref={(c) => { this.time = c }} aria-hidden="true">
          <span className="visible-desktop">
            {/* something like: Mar 6, 2014 */}
            {fixedPrefix + friendly}
          </span>
          <span className="hidden-desktop">
            {/* something like: 3/3/2014 */}
            {fudged.toLocaleDateString()}
          </span>
        </time>
      </span>
    )
  }, () => this.props.dateTime)
示例#3
0
        (function(submissionData) {
          // Need a shallow clone of the data here because $.ajaxJSON modifies in place
          var thisSubmissionData = _.clone(submissionData);
          // If this is a timeout-based submission and the data is the same as last time,
          // palliate the server by skipping the data submission
          if (!quizSubmission.inBackground && repeat && _.isEqual(submissionData, lastSuccessfulSubmissionData)) {
            $lastSaved.text(I18n.t('saving_not_needed', "No new data to save. Last checked at %{t}", { t: $.friendlyDatetime(new Date()) }));

            quizSubmission.currentlyBackingUp = false;

            setTimeout(function() { quizSubmission.updateSubmission(true, true) }, 30000);
            return;
          }
          $.ajaxJSON(url, 'PUT', submissionData,
            // Success callback
            function(data) {
              lastSuccessfulSubmissionData = thisSubmissionData;
              $lastSaved.text(I18n.t('saved_at', 'Quiz saved at %{t}', { t: $.friendlyDatetime(new Date()) }));
              quizSubmission.currentlyBackingUp = false;
              quizSubmission.inBackground = false;
              if(repeat) {
                setTimeout(function() {quizSubmission.updateSubmission(true, true) }, 30000);
              }
              if(data && data.end_at) {
                var endAtFromServer     = Date.parse(data.end_at),
                    submissionEndAt     = Date.parse(endAt.text()),
                    serverEndAtTime     = endAtFromServer.getTime(),
                    submissionEndAtTime = submissionEndAt.getTime();

                quizSubmission.timeLeft = data.time_left * 1000;

                // if the new endAt from the server is different than our current endAt, then notify
                // the user that their time limit's changed and let updateTime do the rest.
                if (serverEndAtTime !== submissionEndAtTime) {
                    if(serverEndAtTime > submissionEndAtTime) {
                      $.flashMessage(I18n.t('You have been given extra time on this attempt'))
                    } else {
                      $.flashMessage(I18n.t('Your time for this quiz has been reduced.'));
                    }

                  quizSubmission.endAt.text(data.end_at);
                  quizSubmission.endAtParsed = endAtFromServer;
                }
              }
            },
            // Error callback
            function(resp, ec) {
              quizSubmission.currentlyBackingUp = false;

              // has the user logged out?
              // TODO: support this redirect in LDB, by getting out of high security mode.
              if (ec.status === 401 || resp['status'] == 'unauthorized') {
                showDeauthorizedDialog();
                // since we popped up our own "not logged in" modal, skip the default error handler
                // see jquery.ajaxJSON.js defaultAjaxError
                if ($.inArray(ec, $.ajaxJSON.ignoredXHRs) === -1) {
                  $.ajaxJSON.ignoredXHRs.push(ec)
                }
              }
              else {
                // Connectivity lost?
                var current_user_id = window.ENV.current_user_id || "none";
                $.ajaxJSON(
                  location.protocol + '//' + location.host + "/simple_response.json?user_id=" + current_user_id + "&rnd=" + Math.round(Math.random() * 9999999),
                  'GET', {},
                  function() {},
                  function() {
                    $.flashError(I18n.t('errors.connection_lost', "Connection to %{host} was lost.  Please make sure you're connected to the Internet before continuing.", {'host': location.host}));
                  }
                );
              }

              if(repeat) {
                setTimeout(function() {quizSubmission.updateSubmission(true) }, 30000);
              }
            },
            {
              timeout: 15000
            }
          );
        })(data);