@Override
  public OperationResult run(Connection conn) throws SQLException {
    // First, find the user
    User user = Queries.getUser(conn, spec.getUsername(), this);
    if (user == null) {
      return new OperationResult(false, "Unknown user " + spec.getUsername());
    }

    // Create the CourseRegistration
    CourseRegistration reg = new CourseRegistration();
    reg.setUserId(user.getId());
    reg.setCourseId(spec.getCourseId());
    reg.setRegistrationType(spec.getRegistrationType());
    reg.setSection(spec.getSection());

    // Insert the CourseRegistration
    DBUtil.storeModelObject(conn, reg);

    Course course = null;
    try {
      course = DBUtil.loadModelObjectForId(conn, Course.SCHEMA, spec.getCourseId());
    } catch (NoSuchUniqueIdException e) {
      logger.error("Could not find course for id={}", spec.getCourseId());
    }

    return new OperationResult(
        true,
        "Added user "
            + spec.getUsername()
            + " to course"
            + (course != null ? " " + course.getNameAndTitle() : ""));
  }
    @Override
    public void load(final Runnable onSuccess, final ICallback<Pair<String, Throwable>> onFailure) {
      final Integer userId =
          pageParams.getInt(PageObjectParamNameMap.getInstance().get(UserSelection.class));
      if (userId == null) {
        onFailure.call(new Pair<String, Throwable>("No user id specified", null));
        return;
      }

      // Special case: if the requested user id is the logged-in user's id,
      // then we succeed trivially.  (A user can always access his/her
      // own information, such as a submission history.)
      User loggedInUser = session.get(User.class);
      if (loggedInUser.getId() == userId.intValue()) {
        GWT.log("User self-selection");
        UserSelection userSelection = new UserSelection();
        userSelection.setUser(loggedInUser);
        session.add(userSelection);
        onSuccess.run();
        return;
      }

      // General case: a user is being selected among all users in the course.

      // Users registered in the course should already have been loaded
      User[] regUserList = session.get(User[].class);

      // Check to see if the user is registered in the course
      for (User userInCourse : regUserList) {
        if (userInCourse.getId() == userId.intValue()) {
          // Huzzah!
          UserSelection userSelection = new UserSelection();
          userSelection.setUser(userInCourse);
          session.add(userSelection);
          onSuccess.run();
          return;
        }
      }

      // User is not registered in course
      onFailure.call(
          new Pair<String, Throwable>("User " + userId + " is not registered in the course", null));
    }
  @Override
  public void submit(int problemId, String programText)
      throws CloudCoderAuthenticationException, SubmissionException {
    // Make sure that client is authenticated and has permission to edit the given problem
    User user = ServletUtil.checkClientIsAuthenticated(getThreadLocalRequest());

    HttpSession session = getThreadLocalRequest().getSession();

    // The Problem should be stored in the user's session
    Problem problem = (Problem) session.getAttribute(SessionAttributeKeys.PROBLEM_KEY);
    if (problem == null || problem.getProblemId() != problemId) {
      throw new CloudCoderAuthenticationException();
    }

    // Insert a full-text change into the database.
    Change fullTextChange =
        new Change(
            ChangeType.FULL_TEXT,
            0,
            0,
            0,
            0,
            System.currentTimeMillis(),
            user.getId(),
            problem.getProblemId(),
            programText);
    Database.getInstance().storeChanges(new Change[] {fullTextChange});

    // Get test cases.  (TODO: cache them?)
    List<TestCase> testCaseList = Database.getInstance().getTestCasesForProblem(problemId);

    ISubmitService submitService = DefaultSubmitService.getInstance();

    logger.info("Passing submission to submit service...");
    IFutureSubmissionResult future = submitService.submitAsync(problem, testCaseList, programText);

    // Put the full-text Change and IFutureSubmissionResult in the user's session.
    addSessionObjects(session, fullTextChange, future);
  }
Exemple #4
0
  @Override
  public Boolean run(Connection conn) throws SQLException {
    PreparedStatement stmt =
        prepareStatement(
            conn,
            "update cc_quizzes as q"
                + "  join cc_course_registrations as cr on  cr.course_id = q.course_id"
                + "                                     and cr.section = q.section"
                + "                                     and cr.user_id = ?"
                + "                                     and q.problem_id = ?"
                + "                                     and q.section = ?"
                + "                                     and q.course_id = ?"
                + " set end_time = ?");
    stmt.setInt(1, user.getId());
    stmt.setInt(2, quiz.getProblemId());
    stmt.setInt(3, quiz.getSection());
    stmt.setInt(4, quiz.getCourseId());
    long currentTime = System.currentTimeMillis();
    stmt.setLong(5, currentTime);

    int updateCount = stmt.executeUpdate();
    return updateCount > 0;
  }