Skip to main content

Overview

The Assignment model represents a programming assignment or contest in Wecode. It manages assignment configuration, participant access, submission deadlines, late penalties, and relationships to problems and classes.

Fillable Fields

name
string
required
The name/title of the assignment
total_submits
integer
Maximum number of submissions allowed per problem (0 for unlimited)
open
boolean
default:"false"
Whether the assignment is open for submissions
score_board
boolean
default:"false"
Whether to display the scoreboard for this assignment
javaexceptions
boolean
default:"false"
Whether to enable Java exception handling
start_time
datetime
required
When the assignment starts and students can begin submittingCast: datetime
finish_time
datetime
required
When the assignment ends (before extra_time is applied)Cast: datetime
extra_time
integer
default:"0"
Additional time in seconds after finish_time (deadline = finish_time + extra_time)
late_rule
string
Expression language formula for calculating late submission coefficientUses Symfony ExpressionLanguage with variables: delay, extra_timeExample: 100 - (delay / extra_time) * 100
participants
string
Comma-separated list or description of participants
description
text
Assignment description and instructions (HTML supported)
user_id
integer
required
ID of the user (instructor) who created this assignment
language_ids
string
Comma-separated list of allowed programming language IDs

Relationships

problems()

problems
BelongsToMany<Problem>
Problems attached to this assignmentPivot fields:
  • score - Points awarded for this problem
  • ordering - Display order in assignment
  • problem_name - Custom name for the problem in this assignment
Timestamps: Yes
$assignment->problems()->withPivot('score', 'ordering', 'problem_name')

user()

user
BelongsTo<User>
The instructor who created this assignment
$assignment->user // Returns User model

submissions()

submissions
HasMany<Submission>
All submissions made to this assignment

lops()

lops
BelongsToMany<Lop>
Classes/groups that have access to this assignment
$assignment->lops // Returns collection of Lop (class) models

scoreboard()

scoreboard
HasOne<Scoreboard>
The cached scoreboard for this assignment

Public Methods

assignment_info()

Static method to retrieve assignment information by ID.
public static function assignment_info($assignment_id)
Parameters:
  • $assignment_id (integer) - Assignment ID to retrieve
Returns: Assignment model or default array if assignment ID is 0 (practice assignment) Example:
$info = Assignment::assignment_info(5);
// Returns Assignment model with id=5

$practice = Assignment::assignment_info(0);
// Returns: ['id' => 0, 'name' => 'instructors\' submit', ...]

can_submit()

Checks if a user can submit to this assignment.
public function can_submit(User $user, Problem $problem = null)
Parameters:
  • $user (User) - User attempting to submit
  • $problem (Problem, optional) - Specific problem to check for practice assignments
Returns: Object with properties:
  • can_submit (boolean) - Whether submission is allowed
  • error_message (string) - Error message if submission is not allowed
Validation checks:
  1. Guest users cannot submit
  2. Trial accounts must not be expired
  3. Practice problems require permission
  4. Assignment must be open (for students)
  5. Assignment must have started (for students)
  6. Assignment must not be finished
  7. User must be a participant
Example:
$result = $assignment->can_submit($user, $problem);
if ($result->can_submit) {
    // Allow submission
} else {
    echo $result->error_message;
}

is_participant()

Checks if a user is registered as a participant.
public function is_participant($user)
Parameters:
  • $user (User) - User to check
Returns: boolean Logic:
  • Practice assignments (id=0): Always returns true
  • Admin users: Always returns true
  • Other users: Checks if user belongs to any of the assignment’s lops (classes)

started()

Checks if the assignment has started.
public function started()
Returns: boolean - True if current time >= start_time

is_finished()

Checks if the assignment deadline has passed.
public function is_finished()
Returns: boolean - True if current time > finish_time + extra_time

eval_coefficient()

Calculates the current late submission coefficient based on current time.
public function eval_coefficient()
Returns: float|string - Coefficient value (0-10000) or “error” if calculation fails Example:
$coefficient = $assignment->eval_coefficient();
// Returns 100.0 if submitted on time
// Returns 50.0 if late_rule applies 50% penalty

update_submissions_coefficient()

Recalculates and updates coefficients for all submissions in this assignment.
public function update_submissions_coefficient()
Use case: Called after updating the assignment’s late_rule formula

cannot_edit()

Checks if a user has permission to edit this assignment.
public function cannot_edit(User $actor)
Parameters:
  • $actor (User) - User attempting to edit
Returns: false if editing is allowed, or string error message if not Permission rules:
  • Admin: Can edit any assignment
  • Head instructor: Can edit own assignments or assignments in their classes
  • Others: Cannot edit

reset_final_submission_choices()

Recalculates which submissions are marked as final for scoring.
public function reset_final_submission_choices()
Logic:
  • For each user-problem pair, selects the best submission
  • Prioritizes submissions with pre_score = 10000 (full marks)
  • Among full-score submissions, chooses highest final score (considering coefficient)
  • Updates is_final flag on all submissions

Expression Language Provider

The late_rule field uses Symfony ExpressionLanguage with custom math functions:

Available Functions

class my_expression_language_provider implements ExpressionFunctionProviderInterface
Available functions: abs, acos, acosh, asin, asinh, atan, atan2, atanh, base_convert, bindec, ceil, cos, cosh, decbin, dechex, decoct, deg2rad, exp, expm1, fdiv, floor, fmod, hexdec, hypot, intdiv, is_finite, is_infinite, is_nan, log, log10, log1p, max, min, octdec, pi, pow, rad2deg, round, sin, sinh, sqrt, tan, tanh

Available Variables

  • delay (integer) - Seconds between finish_time and submission time (negative if early)
  • extra_time (integer) - The assignment’s extra_time field

Example Late Rules

// No penalty
'100'

// Linear penalty: 10% per hour late (max 10 hours)
'max(0, 100 - (delay / 3600) * 10)'

// Using extra_time: full credit within extra_time, 0 after
'delay <= extra_time ? 100 : 0'

// Exponential decay
'100 * exp(-delay / extra_time)'

Example Usage

// Create a new assignment
$assignment = Assignment::create([
    'name' => 'Midterm Exam',
    'total_submits' => 50,
    'open' => true,
    'score_board' => true,
    'start_time' => now(),
    'finish_time' => now()->addHours(3),
    'extra_time' => 1800, // 30 minutes grace period
    'late_rule' => '100 - min(100, (delay / extra_time) * 50)',
    'user_id' => auth()->id(),
]);

// Attach problems
$assignment->problems()->attach($problem->id, [
    'score' => 100,
    'ordering' => 1,
    'problem_name' => 'Two Sum'
]);

// Assign to classes
$assignment->lops()->attach([1, 2, 3]);

// Check if user can submit
$result = $assignment->can_submit($user);
if (!$result->can_submit) {
    return response()->json(['error' => $result->error_message], 403);
}

// Get current penalty coefficient
$coefficient = $assignment->eval_coefficient();
echo "Current submission will receive {$coefficient}% of full score";

Build docs developers (and LLMs) love