Exemplo n.º 1
0
  @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 startImportAllProblemsFromCourse(Course source, Course dest)
      throws CloudCoderAuthenticationException {
    User authenticatedUser =
        ServletUtil.checkClientIsAuthenticated(
            getThreadLocalRequest(), GetCoursesAndProblemsServiceImpl.class);

    // Make sure that the authenticated user is registered as an instructor for
    // both the source and destination courses.
    boolean sourceInstructor = false, destInstructor = false;
    List<? extends Object[]> courses = Database.getInstance().getCoursesForUser(authenticatedUser);
    for (Object[] triple : courses) {
      CourseRegistration reg = (CourseRegistration) triple[2];
      if (reg.getCourseId() == source.getId() && reg.getRegistrationType().isInstructor()) {
        sourceInstructor = true;
      }
      if (reg.getCourseId() == dest.getId() && reg.getRegistrationType().isInstructor()) {
        destInstructor = true;
      }
    }

    // Create a FutureImportCourseResult
    FutureImportCourseResult result = new FutureImportCourseResult();
    getThreadLocalRequest()
        .getSession()
        .setAttribute(SessionAttributeKeys.FUTURE_IMPORT_COURSE_RESULT_KEY, result);

    if (!sourceInstructor || !destInstructor) {
      result.set(new OperationResult(false, "Permission denied (not an instructor)"));
      return;
    }

    // Start the actual operation
    result.start(source, dest, authenticatedUser);
  }