first commit

This commit is contained in:
2026-02-07 09:46:32 +08:00
commit 5fcd5dc646
443 changed files with 89466 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?php
class backup_base64_element extends backup_final_element implements processable {
public function get_value() {
return base64_encode(parent::get_value());
}
}

View File

@@ -0,0 +1,69 @@
<?php
// 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
// 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/>.
/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/programming/backup/moodle2/backup_programming_stepslib.php'); // Because it exists (must)
/**
* programming backup task that provides all the settings and steps to perform one
* complete backup of the activity
*/
class backup_programming_activity_task extends backup_activity_task {
/**
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity
}
/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// Choice only has one structure step
$this->add_step(new backup_programming_activity_structure_step('programming_structure', 'programming.xml'));
}
/**
* Code the transformations to perform in the activity in
* order to get transportable (encoded) links
*/
static public function encode_content_links($content) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
// Link to the list of programmings
$search="/(".$base."\/mod\/programming\/index.php\?id\=)([0-9]+)/";
$content= preg_replace($search, '$@PROGRAMMINGINDEX*$2@$', $content);
// Link to programming view by moduleid
$search="/(".$base."\/mod\/programming\/view.php\?id\=)([0-9]+)/";
$content= preg_replace($search, '$@PROGRAMMINGVIEWBYID*$2@$', $content);
return $content;
}
}

View File

@@ -0,0 +1,90 @@
<?php
include_once('backup_base64_element.class.php');
/**
* Define all the backup steps that will be used by the backup_programming_activity_task
*/
/**
* Define the complete programming structure for backup, with file and id annotations
*/
class backup_programming_activity_structure_step extends backup_activity_structure_step {
protected function define_structure() {
// To know if we are including userinfo
$userinfo = $this->get_setting_value('userinfo');
// Define each element separated
$programming = new backup_nested_element('programming', array('id'), array(
'name', 'intro', 'introformat', 'grade',
'globalid', 'timeopen', 'timeclose', 'timelimit', 'memlimit',
'nproc', 'timediscount', 'discount', 'allowlate', 'attempts',
'keeplatestonly', 'inputfile', 'outputfile', 'presetcode',
'generator', 'validator', 'generatortype', 'validatortype',
'validatorlang', 'showmode', 'timemodified'));
$languages = new backup_nested_element('langlimits');
$language = new backup_nested_element('langlimit', array('id'), array('languageid'));
$testcases = new backup_nested_element('testcases');
$testcase = new backup_nested_element('testcase', array('id'), array('seq',
new backup_base64_element('input'), new backup_base64_element('gzinput'),
new backup_base64_element('output'), new backup_base64_element('gzoutput'),
'cmdargs', 'timelimit', 'memlimit', 'nproc', 'pub', 'weight', 'memo',
'timemodified'));
$datafiles = new backup_nested_element('datafiles');
$datafile = new backup_nested_element('datafile', array('id'), array(
'seq', 'filename', 'isbinary', 'datasize', new backup_base64_element('data'),
'checkdatasize', new backup_base64_element('checkdata'), 'memo', 'timemodified'));
$presetcodes = new backup_nested_element('presetcodes');
$presetcode = new backup_nested_element('presetcode', array('id'), array(
'languageid', 'name', 'sequence', 'presetcode', 'presetcodeforcheck'));
$submits = new backup_nested_element('submits');
$submit = new backup_nested_element('submit', array('id'), array(
'userid', 'timemodified', 'language', 'code',
'codelines', 'codesize', 'status', 'compilemessage',
'timeused', 'memused', 'judgeresult', 'passed'));
$programming->add_child($languages);
$languages->add_child($language);
$programming->add_child($testcases);
$testcases->add_child($testcase);
$programming->add_child($datafiles);
$datafiles->add_child($datafile);
$programming->add_child($presetcodes);
$presetcodes->add_child($presetcode);
$programming->add_child($submits);
$submits->add_child($submit);
// Define sources
$programming->set_source_table('programming', array('id' => backup::VAR_ACTIVITYID));
$language->set_source_table('programming_langlimit', array('programmingid' => backup::VAR_PARENTID));
$testcase->set_source_table('programming_tests', array('programmingid' => backup::VAR_PARENTID));
$datafile->set_source_table('programming_datafile', array('programmingid' => backup::VAR_PARENTID));
$presetcode->set_source_table('programming_presetcode', array('programmingid' => backup::VAR_PARENTID));
// All the rest of elements only happen if we are including user info
if ($userinfo) {
$submit->set_source_table('programming_submits', array('programmingid' => backup::VAR_PARENTID));
}
// Define id annotations
$programming->annotate_ids('scale', 'grade');
$submit->annotate_ids('user', 'userid');
// Define file annotations
$programming->annotate_files('mod_programming', 'intro', null); // This file area hasn't itemid
// Return the root element (programming), wrapped into standard activity structure
return $this->prepare_activity_structure($programming);
}
}

View File

@@ -0,0 +1,112 @@
<?php
// 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
// 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/>.
/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/programming/backup/moodle2/restore_programming_stepslib.php'); // Because it exists (must)
/**
* programming restore task that provides all the settings and steps to perform one
* complete restore of the activity
*/
class restore_programming_activity_task extends restore_activity_task {
/**
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity
}
/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// Choice only has one structure step
$this->add_step(new restore_programming_activity_structure_step('programming_structure', 'programming.xml'));
}
/**
* Define the contents in the activity that must be
* processed by the link decoder
*/
static public function define_decode_contents() {
$contents = array();
$contents[] = new restore_decode_content('programming', array('intro'), 'programming');
return $contents;
}
/**
* Define the decoding rules for links belonging
* to the activity to be executed by the link decoder
*/
static public function define_decode_rules() {
$rules = array();
$rules[] = new restore_decode_rule('PROGRAMMINGVIEWBYID', '/mod/programming/view.php?id=$1', 'course_module');
$rules[] = new restore_decode_rule('PROGRAMMINGINDEX', '/mod/programming/index.php?id=$1', 'course');
return $rules;
}
/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* programming logs. It must return one array
* of {@link restore_log_rule} objects
*/
static public function define_restore_log_rules() {
$rules = array();
$rules[] = new restore_log_rule('programming', 'add', 'view.php?id={course_module}', '{programming}');
$rules[] = new restore_log_rule('programming', 'update', 'view.php?id={course_module}', '{programming}');
$rules[] = new restore_log_rule('programming', 'view', 'view.php?id={course_module}', '{programming}');
$rules[] = new restore_log_rule('programming', 'upload', 'view.php?a={programming}', '{programming}');
$rules[] = new restore_log_rule('programming', 'view submission', 'submissions.php.php?id={course_module}', '{programming}');
$rules[] = new restore_log_rule('programming', 'update grades', 'submissions.php.php?id={course_module}&user={user}', '{user}');
return $rules;
}
/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* course logs. It must return one array
* of {@link restore_log_rule} objects
*
* Note this rules are applied when restoring course logs
* by the restore final task, but are defined here at
* activity level. All them are rules not linked to any module instance (cmid = 0)
*/
static public function define_restore_log_rules_for_course() {
$rules = array();
$rules[] = new restore_log_rule('programming', 'view all', 'index.php?id={course}', null);
return $rules;
}
}

View File

@@ -0,0 +1,146 @@
<?php
// 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
// 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/>.
/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Define all the restore steps that will be used by the restore_programming_activity_task
*/
/**
* Structure step to restore one programming activity
*/
class restore_programming_activity_structure_step extends restore_activity_structure_step {
protected function define_structure() {
$paths = array();
$userinfo = $this->get_setting_value('userinfo');
$programming = new restore_path_element('programming', '/activity/programming');
$paths[] = $programming;
$paths[] = new restore_path_element('programming_langlimit', '/activity/programming/langlimits/langlimit');
$paths[] = new restore_path_element('programming_presetcode', '/activity/programming/presetcodes/presetcode');
$paths[] = new restore_path_element('programming_datefile', '/activity/programming/datefiles/datefile');
$paths[] = new restore_path_element('programming_testcase', '/activity/programming/testcases/testcase');
if ($userinfo) {
$paths[] = new restore_path_element('programming_submit', '/activity/programming/submits/submit');
}
// Return the paths wrapped into standard activity structure
return $this->prepare_activity_structure($paths);
}
protected function process_programming($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->course = $this->get_courseid();
$data->timeopen = $this->apply_date_offset($data->timeopen);
$data->timeclose = $this->apply_date_offset($data->timeclose);
$data->timemodified = $this->apply_date_offset($data->timemodified);
// insert the programming record
$newitemid = $DB->insert_record('programming', $data);
// immediately after inserting "activity" record, call this
$this->apply_activity_instance($newitemid);
}
protected function process_programming_submit($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->programmingid = $this->get_new_parentid('programming');
$data->userid = $this->get_mappingid('user', $data->userid);
$newitemid = $DB->insert_record('programming_submits', $data);
$this->set_mapping('programming_submit', $oldid, $newitemid, true); // Going to have files
// $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submission'), $oldid, $newitemid);
}
protected function process_programming_langlimit($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->programmingid = $this->get_new_parentid('programming');
$newitemid = $DB->insert_record('programming_langlimit', $data);
$this->set_mapping('programming_langlimit', $oldid, $newitemid);
}
protected function process_programming_presetcode($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->programmingid = $this->get_new_parentid('programming');
$newitemid = $DB->insert_record('programming_presetcode', $data);
$this->set_mapping('programming_presetcode', $oldid, $newitemid);
}
protected function process_programming_datefile($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->programmingid = $this->get_new_parentid('programming');
$data->data = base64_decode($data->data);
$data->checkdata = base64_decode($data->checkdata);
$newitemid = $DB->insert_record('programming_datefile', $data);
$this->set_mapping('programming_datefile', $oldid, $newitemid);
}
protected function process_programming_testcase($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->programmingid = $this->get_new_parentid('programming');
$data->input = base64_decode($data->input);
$data->gzinput = base64_decode($data->gzinput);
$data->output = base64_decode($data->output);
$data->gzoutput = base64_decode($data->gzoutput);
$newitemid = $DB->insert_record('programming_tests', $data);
$this->set_mapping('programming_testcase', $oldid, $newitemid);
}
protected function after_execute() {
// Add programming related files, no need to match by itemname (just internally handled context)
$this->add_related_files('mod_programming', 'intro', null);
// Add programming submission files, matching by programming_submission itemname
}
}