<commit_before>import Vapor
import HTTP
import Reswifq

final class ProblemsController {
    
    let view: ViewRenderer
    
    init(_ view: ViewRenderer) {
        self.view = view
    }
    
    /// GET /events/:id/problems
    func problems(request: Request) throws -> ResponseRepresentable {
        let event = try request.parameters.next(Event.self)
        let problems = try event.eventProblems.sort("sequence", .ascending).all()
        
        return try render("event-problems", [
            "event": event,
            "problems": problems
            ], for: request, with: view)
    }
    
    /// GET /events/:id/submissions
    func submissions(request: Request) throws -> ResponseRepresentable {
        let event = try request.parameters.next(Event.self)
        let submissions = try Submission.makeQuery()
            .join(EventProblem.self, baseKey: "event_problem_id", joinedKey: "id")
            .filter(EventProblem.self, "event_id", event.id)
            .filter(Submission.self, "user_id", request.user!.id)
            .sort("created_at", .descending).limit(20).all()
        
        var shouldRefreshPageAutomatically = false
        
        var joinedSubmissions: [Node] = []
        for submission in submissions {
            var joinedSubmission = try submission.makeNode(in: nil)
            let user = try submission.user.get()!
            let problem = try submission.eventProblem.get()!.problem.get()!
            joinedSubmission["userName"] = user.name.makeNode(in: nil)
            joinedSubmission["problemName"] = problem.name.makeNode(in: nil)
            joinedSubmissions.append(joinedSubmission)
            
            if submission.state == .submitted || submission.state == .gradingInProgress {
                shouldRefreshPageAutomatically = true
            }
        }
        
        return try render("submissions", [
            "event": event,
            "submissions": joinedSubmissions,
            "shouldRefresh": shouldRefreshPageAutomatically
            ], for: request, with: view)<commit_msg>Submissions - teachers/admins can see all submissions but students only see theirs<commit_after>import Vapor
import HTTP
import Reswifq

final class ProblemsController {
    
    let view: ViewRenderer
    
    init(_ view: ViewRenderer) {
        self.view = view
    }
    
    /// GET /events/:id/problems
    func problems(request: Request) throws -> ResponseRepresentable {
        let event = try request.parameters.next(Event.self)
        let problems = try event.eventProblems.sort("sequence", .ascending).all()
        
        return try render("event-problems", [
            "event": event,
            "problems": problems
            ], for: request, with: view)
    }
    
    /// GET /events/:id/submissions
    func submissions(request: Request) throws -> ResponseRepresentable {
        guard let user = request.user else {
            throw Abort.unauthorized
        }
        
        let event = try request.parameters.next(Event.self)
        
        var query = try Submission.makeQuery()
            .join(EventProblem.self, baseKey: "event_problem_id", joinedKey: "id")
            .filter(EventProblem.self, "event_id", event.id).sort("created_at", .descending).limit(20)
        
        if user.role == .student {
            query = try query.filter(Submission.self, "user_id", request.user!.id)
        }
        
        let submissions = try query.all()
        
        var shouldRefreshPageAutomatically = false
        
        var joinedSubmissions: [Node] = []
        for submission in submissions {
            var joinedSubmission = try submission.makeNode(in: nil)
            let user = try submission.user.get()!
            let problem = try submission.eventProblem.get()!.problem.get()!
            joinedSubmission["userName"] = user.name.makeNode(in: nil)
            joinedSubmission["problemName"] = problem.name.makeNode(in: nil)
            joinedSubmissions.append(joinedSubmission)
            
            if submission.state == .submitted || submission.state == .gradingInProgress {
                shouldRefreshPageAutomatically = true
            }
        }
        
        return try render("submissions", [
            "event": event,
            "submissions": joinedSubmissions,
            "shouldRefresh": shouldRefreshPageAutomatically
            ], for: request, with: view)
{'fraction_non_alphanumeric': 0.10308110557317626, 'fraction_numerical': 0.0009062075215224287, 'mean_word_length': 2.1334279630943933, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883142', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1142, 'n_words': 268}