# So swirl does not repeat execution of plot commands
AUTO_DETECT_NEWVAR <- FALSE

# Returns TRUE if the user has created a specified glm model
# with a specified name. 
creates_glm_model <- function(correctExpr){
  e <- get("e", parent.frame())
  # Recreate what the user has done
  eUsr <- cleanEnv(e$snapshot)
  mdlUsr <- eval(e$expr, eUsr)
  # Append the result, as a list to e$delta for progress restoration
  e$delta <- c(e$delta, as.list(eUsr))
  # Recreate what the user should have done
  eSw <- cleanEnv(e$snapshot)
  mdlSw <- eval(parse(text=correctExpr), eSw)
  # Check whether the model's name is correct
  if(length(ls(eSw)) > 0){
    nameGood <- sum(ls(eUsr) %in% ls(eSw)) & sum(ls(eSw) %in% ls(eUsr))
    # If not, highlight the misspelling
    if(!nameGood){
      swirl_out(paste0("You seem to have misspelled the model's name.  I was expecting ", ls(eSw), 
                       " but you apparenlty typed ", ls(eUsr), ".")) 
      return(FALSE)
    }
  }
  # Check for effective equality of the models
  isTRUE(all.equal(as.vector(mdlUsr$coefficients), as.vector(mdlSw$coefficients))) &
    identical(mdlUsr$family$family, mdlSw$family$family) &
    isTRUE(all.equal(mdlUsr$fitted.values, mdlSw$fitted.values))
}

# Returns TRUE if e$expr matches any of the expressions given
# (as characters) in the argument. 
ANY_of_exprs <- function(...){
  e <- get("e", parent.frame())
  any(sapply(c(...), function(expr)omnitest(expr)))
}

# Get the swirl state
getState <- function(){
  # Whenever swirl is running, its callback is at the top of its call stack. 
  # Swirl's state, named e, is stored in the environment of the callback. 
  environment(sys.function(1))$e
}

# Get the value which a user either entered directly or was computed
# by the command he or she entered. 
getVal <- function(){
  getState()$val
}

# Get the last expression which the user entered at the R console. 
getExpr <- function(){
  getState()$expr
}

coursera_on_demand <- function(){
  selection <- getState()$val
  if(selection == "Yes"){
    email <- readline("What is your email address? ") 
    token <- readline("What is your assignment token? ") 
    
    payload <- sprintf('{  
      "assignmentKey": "B7GXWK8zEeWDwQ7PSvPrHw",
      "submitterEmail": "%s",  
      "secret": "%s",  
      "parts": {  
        "msvTh": {  
          "output": "correct"  
        }  
      }  
    }', email, token)
    url <- 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1'
  
    respone <- httr::POST(url, body = payload)
    if(respone$status_code >= 200 && respone$status_code < 300){
      message("Grade submission succeeded!") 
    } else {
      message("Grade submission failed.") 
      message("Press ESC if you want to exit this lesson and you")
      message("want to try to submit your grade at a later time.") 
      return(FALSE)
    }
  }
  TRUE
}