Skip to content

Fix frankenstyle naming violations for Moodle plugin database approval #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions FRANKENSTYLE_CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Frankenstyle Naming Compliance Changes

This document summarizes the changes made to comply with Moodle's frankenstyle naming conventions.

## Changes Made

### 1. Exception Class
- **Old**: `onlinejudge_exception` (in `exceptions.php`)
- **New**: `\local_onlinejudge\exception` (in `classes/exception.php`)

### 2. Judge Base Class
- **Old**: `judge_base` (in `judgelib.php`)
- **New**: `\local_onlinejudge\judge\base` (in `classes/judge/base.php`)

### 3. Judge Sandbox Class
- **Old**: `judge_sandbox` (in `judge/sandbox/lib.php`)
- **New**: `\local_onlinejudge\judge\sandbox` (in `classes/judge/sandbox.php`)

### 4. Judge Sphere Engine Class
- **Old**: `judge_sphere_engine` (in `judge/sphere_engine/lib.php`)
- **New**: `\local_onlinejudge\judge\sphere_engine` (in `classes/judge/sphere_engine.php`)

### 5. Event Class
- **Old**: `onlinejudge_task_judged` (in `classes/event/onlinejudge_task_judged.php`)
- **New**: `task_judged` (in `classes/event/task_judged.php`)
- **Namespace**: Changed from `mod_onlinejudge\event` to `local_onlinejudge\event`

## Backward Compatibility

To ensure existing code continues to work, backward compatibility aliases have been added in `judgelib.php`:

```php
// Backward compatibility aliases
if (!class_exists('onlinejudge_exception')) {
class_alias('\\local_onlinejudge\\exception', 'onlinejudge_exception');
}
if (!class_exists('judge_base')) {
class_alias('\\local_onlinejudge\\judge\\base', 'judge_base');
}
if (!class_exists('judge_sandbox')) {
class_alias('\\local_onlinejudge\\judge\\sandbox', 'judge_sandbox');
}
if (!class_exists('judge_sphere_engine')) {
class_alias('\\local_onlinejudge\\judge\\sphere_engine', 'judge_sphere_engine');
}
```

## Files Structure

The new frankenstyle-compliant structure is:

```
classes/
├── exception.php # \local_onlinejudge\exception
├── judge/
│ ├── base.php # \local_onlinejudge\judge\base
│ ├── sandbox.php # \local_onlinejudge\judge\sandbox
│ └── sphere_engine.php # \local_onlinejudge\judge\sphere_engine
└── event/
└── task_judged.php # \local_onlinejudge\event\task_judged
```

## Migration Guide

### For New Code
Use the new namespaced classes:
```php
// Use this
use local_onlinejudge\exception;
use local_onlinejudge\judge\base;
use local_onlinejudge\judge\sandbox;
use local_onlinejudge\judge\sphere_engine;
use local_onlinejudge\event\task_judged;
```

### For Existing Code
No changes required - old class names will continue to work due to backward compatibility aliases.

## Language Strings

Added new language strings for the renamed event:
- `event_task_judged` - "Online Judge Task Judged"
- `event_task_judged_description` - "The event is fired when an online judge task has been judged."

## Benefits

1. **Compliance**: Meets Moodle's frankenstyle naming requirements for plugin database approval
2. **Autoloading**: Classes can be autoloaded using Moodle's class loader
3. **Namespace**: Proper namespace organization prevents naming conflicts
4. **Maintainability**: Clear class organization in the `classes/` directory
5. **Compatibility**: Existing code continues to work without modification

## Notes

- The old files in `judge/` directory are still present for backward compatibility
- All functions in `judgelib.php` retain their original names (they follow correct naming)
- The plugin continues to work exactly as before with no functional changes
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package mod_onlinejudge
* @package local_onlinejudge
* @author Andrew Naguib <andrewnajeeb at fci dot helwan dot edu dot eg>
* @copyright 2018 Andrew Naguib
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_onlinejudge\event;
namespace local_onlinejudge\event;

defined('MOODLE_INTERNAL') || die();

class onlinejudge_task_judged extends \core\event\base {
class task_judged extends \core\event\base {

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_onlinejudge_task_judged', 'local_onlinejudge');
return get_string('event_task_judged', 'local_onlinejudge');
}

/**
Expand All @@ -42,7 +42,7 @@ public static function get_name() {
* @return string
*/
public function get_description() {
return get_string('event_onlinejudge_task_judged_description');
return get_string('event_task_judged_description', 'local_onlinejudge');
}

/**
Expand Down
56 changes: 56 additions & 0 deletions classes/exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - https://moodle.org
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* NOTICE OF COPYRIGHT
*
* Online Judge for Moodle
* https://github.com/hit-moodle/moodle-local_onlinejudge
*
* Copyright (C) 2009 onwards
* Sun Zhigang http://sunner.cn
* Andrew Naguib <andrew at fci helwan edu eg>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*/

/**
* Online judge related exceptions
*
* @package local_onlinejudge
* @copyright 2011 Sun Zhigang (http://sunner.cn)
* @author Sun Zhigang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_onlinejudge;

defined('MOODLE_INTERNAL') || die();

class exception extends \moodle_exception {
function __construct($errorcode, $a = NULL, $debuginfo = NULL) {
parent::__construct($errorcode, 'local_onlinejudge', '', $a, $debuginfo);
}
}
193 changes: 193 additions & 0 deletions classes/judge/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
// This file is part of Moodle - https://moodle.org
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* NOTICE OF COPYRIGHT
*
* Online Judge for Moodle
* https://github.com/hit-moodle/moodle-local_onlinejudge
*
* Copyright (C) 2009 onwards
* Sun Zhigang http://sunner.cn
* Andrew Naguib <andrew at fci helwan edu eg>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*/

/**
* Judge base class
*
* @package local_onlinejudge
* @copyright 2011 Sun Zhigang (http://sunner.cn)
* @author Sun Zhigang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_onlinejudge\judge;

defined('MOODLE_INTERNAL') || die();

class base {

// object of the task
protected $task;

// language id without judge id
protected $language;

function __construct($task) {
$this->task = $task;
$this->language = substr($this->task->language, 0, strpos($this->task->language, '-'));
}

/**
* Return an array of programming languages supported by this judge
*
* The array key must be the language's ID, such as c_sandbox, python_ideone.
* The array value must be a human-readable name of the language, such as 'C (local)', 'Python (ideone.com)'
*/
static function get_languages() {
return array();
}

/**
* Put options into task
*
* @param object options
* @return throw exceptions on error
*/
static function parse_options($options, & $task) {
$options = (array)$options;
// only common options are parsed here.
// special options should be parsed by childclass
foreach ($options as $key => $value) {
if ($key == 'memlimit' and $value > 1024 * 1024 * get_config('local_onlinejudge', 'maxmemlimit')) {
$value = 1024 * 1024 * get_config('local_onlinejudge', 'maxmemlimit');
}
if ($key == 'cpulimit' and $value > get_config('local_onlinejudge', 'maxcpulimit')) {
$value = get_config('local_onlinejudge', 'maxcpulimit');
}
$task->$key = $value;
}
}

/**
* Return the infomation of the compiler of specified language
*
* @param string $language ID of the language
* @return compiler information or null
*/
static function get_compiler_info($language) {
return array();
}

/**
* Whether the judge is avaliable
*
* @return true for yes, false for no
*/
static function is_available() {
return false;
}

/**
* Judge the current task
*
* @return bool [updated task or false]
*/
function judge() {
return false;
}

/**
* Compare the stdout of program and the output of testcase
*/
protected function diff() {
$task = &$this->task;

// convert data into UTF-8 charset if possible
$task->stdout = $this->convert_to_utf8($task->stdout);
$task->stderr = $this->convert_to_utf8($task->stderr);
$task->output = $this->convert_to_utf8($task->output);

// trim tailing return chars which are meaning less
$task->output = rtrim($task->output, "\r\n");
$task->stdout = rtrim($task->stdout, "\r\n");

if (strcmp($task->output, $task->stdout) == 0) return ONLINEJUDGE_STATUS_ACCEPTED; else {
$tokens = array();
$tok = strtok($task->output, " \n\r\t");
while ($tok !== false) {
$tokens[] = $tok;
$tok = strtok(" \n\r\t");
}

$tok = strtok($task->stdout, " \n\r\t");
foreach ($tokens as $anstok) {
if ($tok === false || $tok !== $anstok) return ONLINEJUDGE_STATUS_WRONG_ANSWER;
$tok = strtok(" \n\r\t");
}
if ($tok !== false) {
return ONLINEJUDGE_STATUS_WRONG_ANSWER;
}
return ONLINEJUDGE_STATUS_PRESENTATION_ERROR;
}
}

/**
* If string is not encoded in UTF-8, convert it into utf-8 charset
*/
protected function convert_to_utf8($string) {
$localwincharset = get_string('localewincharset', 'langconfig');
if (!empty($localwincharset) and !mb_check_encoding($string, 'UTF-8') and mb_check_encoding($string, $localwincharset)) {
return core_text::convert($string, $localwincharset);
} else {
return $string;
}
}

/**
* Save files of current task to a temp directory
*
* @return array of the full path of saved files
*/
protected function create_temp_files() {
$dstfiles = array();

$fs = get_file_storage();
$files = $fs->get_area_files(\context_system::instance()->id, 'local_onlinejudge', 'tasks', $this->task->id, 'sortorder', false);
foreach ($files as $file) {
$path = onlinejudge_get_temp_dir() . $file->get_filepath();
$fullpath = $path . $file->get_filename();
if (!check_dir_exists($path)) {
throw new \moodle_exception('errorcreatingdirectory', '', '', $path);
}
$file->copy_content_to($fullpath);
$dstfiles[] = $fullpath;
}

return $dstfiles;
}
}
Loading