Skip to main content

Overview

The moss_controller integrates with Stanford’s MOSS (Measure of Software Similarity) system to detect code plagiarism in assignment submissions. It provides interfaces for configuring MOSS, submitting code for analysis, and viewing results.

Constructor

public function __construct()
Applies the following middleware:
  • auth - Requires user authentication

Methods

index()

public function index($assignment_id)
Displays the MOSS interface for an assignment with current analysis status.
assignment_id
integer
required
Assignment ID to view MOSS results for
Access Control:
  • Admin and head_instructor only
  • Aborts with 403 for other roles
Returns: View with:
  • All assignments list
  • Current MOSS user ID from settings
  • Selected assignment information
  • Last MOSS update timestamp
  • MOSS results for each problem:
    • Problem data
    • MOSS report URL (if available)
    • “submission submitted” message (if processing)
    • NULL (if not yet analyzed)
Route: /moss/{id?} (GET) Example from controller:
$assignment = Assignment::find($assignment_id);
if ($assignment == null)
    abort(404, 'Can not find this assignment!');

$moss_userid = Setting::get('moss_userid');
if ($moss_userid == null) 
    $moss_userid = -1;

$moss_problems = array();
foreach ($assignment->problems as $problem) {
    $moss_problems[$problem->id]['problem'] = $problem;
    $path = Submission::get_path('', $assignment_id, $problem->id) . '/';
    
    if (file_exists($path . "moss_link.txt") && file_get_contents($path . "moss_link.txt") != '') {
        $moss_problems[$problem->id]['moss'] = shell_exec("tail -n1 $path/moss_link.txt");
        shell_exec("rm $path/moss_running");
    } else if (file_exists($path . "moss_running")) {
        $moss_problems[$problem->id]['moss'] = "submission submitted to moss, awaiting response, please be patient";
    } else {
        $moss_problems[$problem->id]['moss'] = NULL;
    }
}

update()

public function update(Request $request, $assignment_id = FALSE)
Updates the MOSS user ID configuration and prepares the MOSS script.
assignment_id
integer
required
Assignment ID (for redirect after update)
moss_userid
string
required
MOSS user ID obtained from Stanford
Access Control:
  • Admin and head_instructor only (implied from route)
Process:
  1. Saves MOSS user ID to settings
  2. Reads original MOSS script template
  3. Replaces placeholder with actual user ID
  4. Writes configured script to tester path
  5. Makes script executable
Returns: Redirect to MOSS index for the assignment Route: /moss/{id} (POST) Example from controller:
$userid = $request->moss_userid;
Setting::set('moss_userid', $userid);

$moss_original = trim(file_get_contents(
    rtrim(Setting::get('tester_path'), '/') . '/moss_original'
));
$moss_path = rtrim(Setting::get('tester_path'), '/') . '/moss';

file_put_contents(
    $moss_path, 
    str_replace('MOSS_USER_ID', $userid, $moss_original)
);

shell_exec("chmod +x {$moss_path}");

detect()

public function detect(Request $request, $assignment_id = FALSE)
Initiates plagiarism detection by submitting final submissions to MOSS.
assignment_id
integer
required
Assignment ID to analyze
detect
string
required
Trigger parameter (must be present)
Validation Rules:
  • detect: required
Access Control:
  • Admin and head_instructor only (implied from form submission)
Returns: Redirect to MOSS index for the assignment Route: /moss/detect/{id} (POST)

Private Methods

_detect()

private function _detect($assignment_id = FALSE)
Performs the actual MOSS plagiarism detection process.
assignment_id
integer
required
Assignment ID to analyze
Process:
  1. Get Final Submissions
    • Fetches all final submissions for the assignment
    • Groups submissions by problem ID
  2. For Each Problem:
    • Builds list of submission file paths
    • Submits files to MOSS via command line
    • Redirects MOSS output to moss_link.txt
    • Creates moss_running flag file
  3. Update Timestamp
    • Records MOSS update time in assignment record
File Structure:
assignments_root/assignment_{id}/problem_{id}/
├── {username1}/
│   └── {filename}.{ext}
├── {username2}/
│   └── {filename}.{ext}
├── moss_link.txt        # MOSS result URL
└── moss_running         # Flag indicating processing
Example from controller:
$items = Submission::get_final_submissions($assignment_id);
$groups = array();

// Group by problem
foreach ($items as $item) {
    if (!isset($groups[$item->problem_id]))
        $groups[$item->problem_id] = array($item);
    else
        array_push($groups[$item->problem_id], $item);
}

// Process each problem
foreach ($groups as $problem_id => $group) {
    $list = '';
    $assignment_path = $assignments_path . "/assignment_{$assignment_id}";
    
    foreach ($group as $item) {
        $list .= "problem_{$problem_id}/{$item->username}/{$item->file_name}." .
                 (string)Language::find($item->language_id)->extension . " ";
    }
    
    shell_exec(
        "list='$list'; cd $assignment_path; $tester_path/moss \$list > problem_{$problem_id}/moss_link.txt 2>&1 &"
    );
    shell_exec("cd $assignment_path/problem_{$problem_id}; touch moss_running");
}

Assignment::where('id', $assignment_id)->update(['moss_update' => date('Y-m-d H:i:s')]);

MOSS Integration Workflow

1. Initial Setup

  1. Obtain MOSS Account
    • Register at Stanford MOSS website
    • Receive MOSS user ID via email
  2. Configure Wecode
    • Navigate to MOSS page for any assignment
    • Enter MOSS user ID in configuration form
    • Submit to save and configure MOSS script

2. Running Detection

  1. Select Assignment
    • Navigate to /moss/{assignment_id}
    • View current MOSS status for all problems
  2. Trigger Detection
    • Click “Detect” button
    • System collects all final submissions
    • Submissions grouped by problem
    • Files submitted to MOSS in background
  3. Processing States
    • Not Analyzed: No MOSS results yet (NULL)
    • Processing: “submission submitted to moss, awaiting response”
    • Complete: MOSS report URL displayed

3. Viewing Results

  1. Check Status
    • Refresh MOSS page to check for completion
    • Once complete, MOSS URL appears
  2. Access Report
    • Click on MOSS URL
    • Opens Stanford MOSS report in new tab
    • Report shows similarity percentages and matches

MOSS Script Configuration

Template Structure

The system uses a template file moss_original with placeholder:
#!/usr/bin/perl
# MOSS configuration
$userid = MOSS_USER_ID;
# ... rest of MOSS script

Configuration Process

  1. Read moss_original template from tester path
  2. Replace MOSS_USER_ID with actual user ID
  3. Write to moss file in tester path
  4. Make executable with chmod +x

Paths

  • Template: {tester_path}/moss_original
  • Configured Script: {tester_path}/moss
  • Results: {assignments_root}/assignment_{id}/problem_{id}/moss_link.txt

MOSS Result Files

Contains MOSS output including the report URL:
... MOSS processing output ...
http://moss.stanford.edu/results/1234567890
The last line (retrieved with tail -n1) contains the report URL.

moss_running

Flag file indicating MOSS submission is in progress:
  • Created when MOSS detection starts
  • Deleted when results are retrieved
  • Presence indicates “awaiting response” state

Settings

moss_userid

Type: String Description: MOSS user ID obtained from Stanford registration Storage: System settings table Usage: Injected into MOSS script for authentication

Notes

Supported Languages

MOSS supports various programming languages. The script automatically detects language based on file extension:
  • C/C++ (.c, .cpp, .cc, .h)
  • Java (.java)
  • Python (.py, .py2, .py3)
  • Pascal (.pas)
  • And many more

Background Processing

MOSS detection runs in background using shell redirection:
moss $list > moss_link.txt 2>&1 &
The & allows the web request to complete while MOSS processes asynchronously.

Result Persistence

MOSS results are stored in the filesystem per problem, not in the database. This allows:
  • Independent analysis per problem
  • Easy re-analysis without data loss
  • Direct file access for debugging

  • Assignment: Assignment being analyzed
  • Submission: Student submissions to compare
  • Language: Programming language detection
  • Setting: MOSS configuration storage

Build docs developers (and LLMs) love