@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); }
/** @param args */ private static void registerStudents() throws Exception { Scanner keyboard = new Scanner(System.in); Class.forName("com.mysql.jdbc.Driver"); Properties config = DBUtil.getConfigProperties(); Connection conn = DBUtil.connectToDatabase(config, "cloudcoder.db"); List<Course> courses = DBUtil.getAllModelObjects( conn, Course.SCHEMA, new IFactory<Course>() { @Override public Course create() { return new Course(); } }); Course c = ConfigurationUtil.choose( keyboard, "For which course would you like to register students?", courses); // TODO: look up the term for each course String filename = ConfigurationUtil.ask( keyboard, "Enter the name of the file containing a tab-separated list student registration entries in this format: \n" + "username\tfirstname\tlastname\temail\tpassword\tsection\n" + "Usernames in the datbase will be re-used, but the names/email/password will not be updated," + "and users will not be registered for a course if they are already registered"); int num = ConfigurationUtil.registerStudentsForCourseId( new FileInputStream(filename), c.getId(), conn); System.out.println("Registered " + num + " students for " + c.getName()); }
@Override public ProblemAndTestCaseList importExercise(Course course, String exerciseHash) throws CloudCoderAuthenticationException { if (course == null || exerciseHash == null) { throw new IllegalArgumentException(); } // Make sure a user is authenticated User user = ServletUtil.checkClientIsAuthenticated( getThreadLocalRequest(), GetCoursesAndProblemsServiceImpl.class); // Find user's registration in the course: if user is not instructor, // import is not allowed CourseRegistrationList reg = Database.getInstance().findCourseRegistrations(user, course); if (!reg.isInstructor()) { throw new CloudCoderAuthenticationException( "Only an instructor can import a problem in a course"); } // Attempt to load the problem from the exercise repository. ConfigurationSetting repoUrlSetting = Database.getInstance().getConfigurationSetting(ConfigurationSettingName.PUB_REPOSITORY_URL); if (repoUrlSetting == null) { logger.error("Repository URL configuration setting is not set"); return null; } // GET the exercise from the repository HttpGet get = new HttpGet(repoUrlSetting.getValue() + "/exercisedata/" + exerciseHash); ProblemAndTestCaseList exercise = null; HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); ContentType contentType = ContentType.getOrDefault(entity); Reader reader = new InputStreamReader(entity.getContent(), contentType.getCharset()); exercise = new ProblemAndTestCaseList(); exercise.setTestCaseList(new TestCase[0]); JSONConversion.readProblemAndTestCaseData( exercise, ReflectionFactory.forClass(Problem.class), ReflectionFactory.forClass(TestCase.class), reader); // Set the course id exercise.getProblem().setCourseId(course.getId()); } catch (IOException e) { logger.error("Error importing exercise from repository", e); return null; } finally { client.getConnectionManager().shutdown(); } // Set "when assigned" and "when due" to reasonable default values long now = System.currentTimeMillis(); exercise.getProblem().setWhenAssigned(now); exercise.getProblem().setWhenDue(now + 48L * 60L * 60L * 1000L); // Set problem authorship as IMPORTED exercise.getProblem().setProblemAuthorship(ProblemAuthorship.IMPORTED); // For IMPORTED problems, parent_hash is actually the hash of the problem // itself. If the problem is modified (and the authorship changed // to IMPORTED_AND_MODIFIED), then the (unchanged) parent_hash value // really does reflect the "parent" problem. exercise.getProblem().setParentHash(exerciseHash); // Store the exercise in the database exercise = Database.getInstance().storeProblemAndTestCaseList(exercise, course, user); return exercise; }