Skip to content

Commit

Permalink
closes (#96)-3; ref. assignment_submission to assign_submission
Browse files Browse the repository at this point in the history
The backup and restore functions referenced the old `assignment_submission`
table, which is replaced by the `assign_submission` table in MOODLE 3.x and 4.x.
There were also several fields previously added to the database and were never
included in the activity backup or restore.
  • Loading branch information
ndrwnaguib committed Aug 23, 2023
1 parent 1195b52 commit 6a51049
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// This file is part of Moodle - https://moodle.org

// This file is part of Moodle - http://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
Expand All @@ -19,6 +20,7 @@
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @copyright 2011 onwards Sun Zhigang (sunner) {@link http://sunner.cn}
* @copyright 2017 onwards Andrew Naguib (ndrwnaguib) {@link http://ndrwnaguib.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -31,7 +33,7 @@ class backup_assignfeedback_onlinejudge_subplugin extends backup_subplugin {
/**
* Returns the subplugin information to attach at assignment element
*/
protected function define_assignment_subplugin_structure() {
protected function define_grade_subplugin_structure() {

/**
* Any activity sublugins is always rooted by one backup_subplugin_element()
Expand All @@ -55,7 +57,7 @@ protected function define_assignment_subplugin_structure() {
* levels (get_recommended_name() and 'config' in the example below). That will make things
* on restore easier, as far as subplugin information will be clearly separated from module information.
*/
$subplugin = $this->get_subplugin_element(null, '/assignment/assignmenttype', 'onlinejudge');
$subplugin = $this->get_subplugin_element();

/**
* Here we define the real structure the subplugin is going to generate - see note above. Obviously the
Expand All @@ -71,7 +73,7 @@ protected function define_assignment_subplugin_structure() {
$assassoff = new backup_nested_element($this->get_recommended_name());

$onlinejudges = new backup_nested_element('onlinejudges');
$onlinejudge = new backup_nested_element('onlinejudge', array('id'), array('language', 'memlimit', 'cpulimit', 'compileonly', 'ratiope', 'ideoneuser', 'ideonepass'));
$onlinejudge = new backup_nested_element('onlinejudge', array('id'), array('language', 'memlimit', 'cpulimit', 'compileonly', 'ratiope', 'clientid', 'accesstoken', 'compile_lm_option', 'compile_warnings_option', 'compile_static_option'));
$testcases = new backup_nested_element('testcases');
$testcase = new backup_nested_element('testcase', array('id'), array('input', 'output', 'usefile', 'feedback', 'subgrade', 'sortorder'));

Expand All @@ -84,39 +86,36 @@ protected function define_assignment_subplugin_structure() {
$onlinejudge->set_source_table('assignment_oj', array('assignment' => backup::VAR_PARENTID));
$testcase->set_source_table('assignment_oj_testcases', array('assignment' => backup::VAR_PARENTID));

$testcase->annotate_files('assign', 'onlinejudge_input', 'id');
$testcase->annotate_files('assign', 'onlinejudge_output', 'id');

return $subplugin; // And we return the root subplugin element
}

/**
* Returns the subplugin information to attach at submission element
*/
protected function define_submission_subplugin_structure() {
$testcase->annotate_files('assignfeedback_onlinejudge', 'onlinejudge_input', 'id');
$testcase->annotate_files('assignfeedback_onlinejudge', 'onlinejudge_output', 'id');

// remember this has not XML representation
$subplugin = $this->get_subplugin_element(null, '/assignment/assignmenttype', 'onlinejudge');
// Returns the subplugin information to attach at submission element

// type of the subplugin, name of the subplugin and name of the connection point (recommended)
$asssuboff = new backup_nested_element($this->get_recommended_name());
// onlinejudge assignment type does not copy task details. So must backup from local onlinejudge
$onlinejudgesubmissions = new backup_nested_element('onlinejudgesubmissions');
$onlinejudgesubmission = new backup_nested_element('onlinejudgesubmission', array('id'), array('submission', 'testcase', 'task', 'latest'));
$onlinejudge_submissions = new backup_nested_element('onlinejudge_submissions');
$onlinejudge_submission = new backup_nested_element('onlinejudge_submission', array('id'), array('submission', 'testcase', 'task', 'latest'));
$tasks = new backup_nested_element('tasks');
$task = new backup_nested_element('task', array('id'), array('cmid', 'userid', 'language', 'memlimit', 'cpulimit', 'imput', 'output', 'compileonly', 'component', 'status', 'stdout', 'stderr', 'compileroutput', 'infoteacher', 'infostudent', 'cpuusage', 'memusage', 'submittime', 'judgetime', 'var1', 'var2', 'var3', 'var4', 'deleted'));

$subplugin->add_child($asssuboff);
$asssuboff->add_child($onlinejudgesubmissions);
$onlinejudgesubmissions->add_child($onlinejudgesubmission);
$onlinejudgesubmission->add_child($tasks);
$task = new backup_nested_element(
'task',
array('id'),
array(
'cmid', 'userid', 'language', 'memlimit', 'cpulimit', 'input', 'output',
'compileonly', 'compile_lm_option', 'compile_warnings_option', 'compile_static_option',
'component', 'status', 'stdout', 'stderr', 'compileroutput', 'infoteacher', 'infostudent',
'cpuusage', 'memusage', 'submittime', 'judgetime', 'var1', 'var2', 'var3', 'var4', 'deleted'
)
);

$assassoff->add_child($onlinejudge_submissions);
$onlinejudge_submissions->add_child($onlinejudge_submission);
$onlinejudge_submission->add_child($tasks);
$tasks->add_child($task);

$onlinejudgesubmission->set_source_table('assignment_oj_submissions', array('submission' => backup::VAR_PARENTID));
$onlinejudge_submission->set_source_table('assignment_oj_submissions', array('submission' => backup::VAR_PARENTID));
$task->set_source_table('onlinejudge_tasks', array('cmid' => backup::VAR_MODID, 'id' => '../../task'));

$task->annotate_ids('user', 'userid');

return $subplugin; // And we return the root subplugin element
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// This file is part of Moodle - https://moodle.org

// This file is part of Moodle - http://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
Expand All @@ -14,33 +15,12 @@
// 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
*/

/**
* @package local_onlinejudge
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @copyright 2011 onwards Sun Zhigang (sunner) {@link http://sunner.cn}
* @copyright 2017 onwards Andrew Naguib (ndrwnaguib) {@link http://ndrwnaguib.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -55,10 +35,38 @@
*/
class restore_assignfeedback_onlinejudge_subplugin extends restore_subplugin {

/**
* Returns the paths to be handled by the subplugin at assignment level
*/
protected function define_grade_subplugin_structure() {

$paths = array();

$elename = $this->get_namefor('onlinejudge');
$elepath = $this->get_pathfor('/onlinejudges/onlinejudge'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

$elename = $this->get_namefor('testcase');
$elepath = $this->get_pathfor('/testcases/testcase'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

// Returns the paths to be handled by the subplugin at submission level

$elename = $this->get_namefor('onlinejudge_submission');
$elepath = $this->get_pathfor('/onlinejudge_submissions/onlinejudge_submission'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

$elename = $this->get_namefor('task');
$elepath = $this->get_pathfor('/onlinejudge_submissions/onlinejudge_submission/tasks/task'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

return $paths; // And we return the interesting paths
}

/**
* This method processes the onlinejudge element inside one onlinejudge assignment (see onlinejudge subplugin backup)
*/
public function process_assignfeedback_onlinejudge_assign($data) {
public function process_assignfeedback_onlinejudge_onlinejudge($data) {
global $DB;

$data = (object)$data;
Expand All @@ -73,25 +81,25 @@ public function process_assignfeedback_onlinejudge_assign($data) {
/**
* This method processes the testcase element inside one onlinejudge assignment (see onlinejudge subplugin backup)
*/
public function process_assignment_onlinejudge_testcase($data) {
public function process_assignfeedback_onlinejudge_testcase($data) {
global $DB;

$data = (object)$data;
$oldid = $data->id;

$data->assignment = $this->get_new_parentid('assignment');
$data->assignment = $this->get_new_parentid('assign');

$newitemid = $DB->insert_record('assignment_oj_testcases', $data);
$this->set_mapping($this->get_namefor('testcase'), $oldid, $newitemid, true);

$this->add_related_files('assign', 'onlinejudge_input', $this->get_namefor('testcase'), null, $oldid);
$this->add_related_files('assign', 'onlinejudge_output', $this->get_namefor('testcase'), null, $oldid);
$this->add_related_files('assignfeedback_onlinejudge', 'onlinejudge_input', $this->get_namefor('testcase'), null, $oldid);
$this->add_related_files('assignfeedback_onlinejudge', 'onlinejudge_output', $this->get_namefor('testcase'), null, $oldid);
}

/**
* This method processes the task element inside one onlinejudge assignment (see onlinejudge subplugin backup)
*/
public function process_assignment_onlinejudge_task($data) {
public function process_assignfeedback_onlinejudge_task($data) {
global $DB;

$data = (object)$data;
Expand All @@ -102,9 +110,9 @@ public function process_assignment_onlinejudge_task($data) {

$newitemid = $DB->insert_record('onlinejudge_tasks', $data);

// Since process_assignment_onlinejudge_onlinejudge_submission() is called before this function,
// Since process_assignfeedback_onlinejudge_onlinejudge_submission() is called before this function,
// we must update assignment_oj_submissions table's task by this way
$DB->set_field('assignment_oj_submissions', 'task', $newitemid, array('task' => $oldid, 'submission' => $this->get_new_parentid('assignment_submission')));
$DB->set_field('assignment_oj_submissions', 'task', $newitemid, array('task' => $oldid, 'submission' => $this->get_new_parentid('assign_submission')));
}

/**
Expand All @@ -116,44 +124,8 @@ public function process_assignfeedback_onlinejudge_onlinejudge_submission($data)
$data = (object)$data;

$data->testcase = $this->get_mappingid($this->get_namefor('testcase'), $data->testcase);
$data->submission = $this->get_mappingid('assignment_submission', $data->submission);
$data->submission = $this->get_mappingid('submission', $data->submission);

$DB->insert_record('assignment_oj_submissions', $data);
}

/**
* Returns the paths to be handled by the subplugin at assignment level
*/
protected function define_assignment_subplugin_structure() {

$paths = array();

$elename = $this->get_namefor('onlinejudge');
$elepath = $this->get_pathfor('/onlinejudges/onlinejudge'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

$elename = $this->get_namefor('testcase');
$elepath = $this->get_pathfor('/testcases/testcase'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

return $paths; // And we return the interesting paths
}

/**
* Returns the paths to be handled by the subplugin at submission level
*/
protected function define_submission_subplugin_structure() {

$paths = array();

$elename = $this->get_namefor('onlinejudge_submission');
$elepath = $this->get_pathfor('/onlinejudge_submissions/onlinejudge_submission'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

$elename = $this->get_namefor('task');
$elepath = $this->get_pathfor('/onlinejudge_submissions/onlinejudge_submission/tasks/task'); // because we used get_recommended_name() in backup this works
$paths[] = new restore_path_element($elename, $elepath);

return $paths; // And we return the interesting paths
}
}
}
10 changes: 5 additions & 5 deletions clients/mod/assign/feedback/onlinejudge/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="assignment"/>
<KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assignment" REFFIELDS="id"
<KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assign" REFFIELDS="id"
COMMENT="assignment foreign key" PREVIOUS="primary"/>
</KEYS>
</TABLE>
Expand All @@ -60,8 +60,8 @@
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="submission"/>
<KEY NAME="submission" TYPE="foreign" FIELDS="submission" REFTABLE="assignment_submissions"
REFFIELDS="id" COMMENT="assignment_submissions foreign key" PREVIOUS="primary" NEXT="testcase"/>
<KEY NAME="submission" TYPE="foreign" FIELDS="submission" REFTABLE="assign_submission"
REFFIELDS="id" COMMENT="assign_submission foreign key" PREVIOUS="primary" NEXT="testcase"/>
<KEY NAME="testcase" TYPE="foreign" FIELDS="testcase" REFTABLE="assignment_oj_testcases" REFFIELDS="id"
PREVIOUS="submission"/>
</KEYS>
Expand Down Expand Up @@ -92,12 +92,12 @@
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="assignment"/>
<KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assignment" REFFIELDS="id"
<KEY NAME="assignment" TYPE="foreign" FIELDS="assignment" REFTABLE="assign" REFFIELDS="id"
COMMENT="assignment foreign key" PREVIOUS="primary"/>
</KEYS>
<INDEXES>
<INDEX NAME="sortorder" UNIQUE="false" FIELDS="sortorder"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
29 changes: 28 additions & 1 deletion clients/mod/assign/feedback/onlinejudge/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function xmldb_assignfeedback_onlinejudge_upgrade($oldversion = 0) {

// Adding keys to table assignment_oj_submissions
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('submission', XMLDB_KEY_FOREIGN, array('submission'), 'assignment_submissions', array('id'));
$table->add_key('submission', XMLDB_KEY_FOREIGN, array('submission'), 'assignment_submission', array('id'));
$table->add_key('testcase', XMLDB_KEY_FOREIGN, array('testcase'), 'assignment_oj_testcases', array('id'));
// Adding indexes to table assignment_oj_submissions
$table->add_index('latest', XMLDB_INDEX_NOTUNIQUE, array('latest'));
Expand Down Expand Up @@ -286,6 +286,33 @@ function xmldb_assignfeedback_onlinejudge_upgrade($oldversion = 0) {
}
}
}
// define table assignment_oj_submissions to be dropped
$table = new xmldb_table('assignment_oj_submissions');
// conditionally launch drop table for assignment_oj_submissions
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}

// Define table assignment_oj_submissions to be created
$table = new xmldb_table('assignment_oj_submissions');
// Adding fields to table assignment_oj_submissions
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('submission', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('testcase', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('task', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('latest', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');

// Adding keys to table assignment_oj_submissions
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('submission', XMLDB_KEY_FOREIGN, array('submission'), 'assign_submission', array('id'));
$table->add_key('testcase', XMLDB_KEY_FOREIGN, array('testcase'), 'assignment_oj_testcases', array('id'));
// Adding indexes to table assignment_oj_submissions
$table->add_index('latest', XMLDB_INDEX_NOTUNIQUE, array('latest'));
// Conditionally launch create table for assignment_oj_submissions
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

upgrade_plugin_savepoint(true, 2018061400, 'assignfeedback', 'onlinejudge');
}

Expand Down
4 changes: 2 additions & 2 deletions clients/mod/assign/feedback/onlinejudge/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function delete_instance() {
global $CFG, $DB;

// delete onlinejudge submissions
$submissions = $DB->get_records('assignment_submissions', array('assignment' => $this->assignment->get_instance()->id));
$submissions = $DB->get_records('assign_submission', array('assignment' => $this->assignment->get_instance()->id));
foreach ($submissions as $submission) {
if (!$DB->delete_records('assignment_oj_submissions', array('submission' => $submission->id))) return false;
}
Expand Down Expand Up @@ -566,4 +566,4 @@ public function view_page($action) {
return '';
}

}
}

0 comments on commit 6a51049

Please sign in to comment.