first commit
This commit is contained in:
73
datafile/add.php
Normal file
73
datafile/add.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once($CFG->libdir.'/weblib.php');
|
||||
require_once('../lib.php');
|
||||
require_once('form.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$params = array('id' => $id);
|
||||
$PAGE->set_url('/mod/programming/datafile/list.php', $params);
|
||||
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
print_error('invalidcoursemodule');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
print_error('coursemisconf');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
print_error('invalidprogrammingid', 'programming');
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
require_capability('mod/programming:edittestcase', $context);
|
||||
|
||||
$mform = new datafile_form();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect(new moodle_url('list.php', array('id' => $cm->id)));
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
unset($data->id);
|
||||
$data->programmingid = $programming->id;
|
||||
|
||||
$data->seq = $DB->count_records('programming_datafile', array('programmingid' => $programming->id), 'MAX(seq)') + 1;
|
||||
|
||||
$content = $mform->get_file_content('data');
|
||||
$data->data = bzcompress($content);
|
||||
$data->datasize = strlen($content);
|
||||
|
||||
if (!empty($data->usecheckdata)) {
|
||||
$content = $mform->get_file_content('checkdata');
|
||||
$data->checkdata = bzcompress($content);
|
||||
$data->checkdatasize = strlen($content);
|
||||
}
|
||||
|
||||
$data->timemodified = time();
|
||||
$DB->insert_record('programming_datafile', $data);
|
||||
programming_datafile_adjust_sequence($programming->id);
|
||||
|
||||
redirect(new moodle_url('list.php', array('id' => $cm->id)), get_string('datafileadded', 'programming'));
|
||||
|
||||
} else {
|
||||
/// Print the page header
|
||||
$PAGE->set_title($programming->name);
|
||||
$PAGE->set_heading(format_string($course->fullname));
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/// Print tabs
|
||||
$renderer = $PAGE->get_renderer('mod_programming');
|
||||
$tabs = programming_navtab('edittest', 'datafile', $course, $programming, $cm);
|
||||
echo $renderer->render_navtab($tabs);
|
||||
|
||||
/// Print page content
|
||||
echo html_writer::tag('h2', $programming->name);
|
||||
echo html_writer::tag('h3', get_string('adddatafile', 'programming').$OUTPUT->help_icon('datafile', 'programming'));
|
||||
$mform->display();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
}
|
||||
33
datafile/delete.php
Normal file
33
datafile/delete.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$datafile_id = required_param('datafile', PARAM_INT);
|
||||
$params = array('id' => $id, 'datafile' => $datafile_id);
|
||||
$PAGE->set_url('/mod/programming/datafile/delete.php', $params);
|
||||
|
||||
if ($id) {
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
error('Course Module ID was incorrect');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
error('Course is misconfigured');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
error('Course module is incorrect');
|
||||
}
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
require_capability('mod/programming:edittestcase', $context);
|
||||
|
||||
$DB->delete_records('programming_datafile', array('id' => $datafile_id));
|
||||
programming_datafile_adjust_sequence($programming->id);
|
||||
redirect(new moodle_url('/mod/programming/datafile/list.php', array('id' => $cm->id)), get_string('datafiledeleted', 'programming'), 1);
|
||||
|
||||
?>
|
||||
55
datafile/download.php
Normal file
55
datafile/download.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$datafile_id = required_param('datafile', PARAM_INT);
|
||||
$checkdata = optional_param('checkdata', 0, PARAM_INT);
|
||||
$params = array('id' => $id, 'datafile' => $datafile_id);
|
||||
if ($checkdata) {
|
||||
$params['checkdata'] = $checkdata;
|
||||
}
|
||||
$PAGE->set_url('/mod/programming/datafile/download.php', $params);
|
||||
|
||||
if ($id) {
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
error('Course Module ID was incorrect');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
error('Course is misconfigured');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
error('Course module is incorrect');
|
||||
}
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
if ($checkdata) {
|
||||
require_capability('mod/programming:viewhiddentestcase', $context);
|
||||
} else {
|
||||
require_capability('mod/programming:viewpubtestcase', $context);
|
||||
}
|
||||
|
||||
|
||||
$file = $DB->get_record('programming_datafile', array('id' => $datafile_id, 'programmingid' => $programming->id));
|
||||
if ($file) {
|
||||
$content = bzdecompress($checkdata ? $file->checkdata : $file->data);
|
||||
$size = $checkdata ? $file->checkdatasize : $file->datasize;
|
||||
if ($file->isbinary) {
|
||||
header('Content-Type: application/octec-stream');
|
||||
} else{
|
||||
header('Content-Type: plain/text');
|
||||
}
|
||||
header("Content-Disposition: attachment; filename=$file->filename");
|
||||
header("Content-Length: $size");
|
||||
print $content;
|
||||
} else {
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
echo 'Not Found';
|
||||
}
|
||||
?>
|
||||
82
datafile/edit.php
Normal file
82
datafile/edit.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once($CFG->libdir.'/weblib.php');
|
||||
require_once('../lib.php');
|
||||
require_once('form.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$datafile_id = required_param('datafile', PARAM_INT);
|
||||
$params = array('id' => $id, 'datafile' => $datafile_id);
|
||||
$PAGE->set_url('/mod/programming/datafile/edit.php', $params);
|
||||
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
print_error('invalidcoursemodule');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
print_error('coursemisconf');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
print_error('invalidprogrammingid', 'programming');
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
require_capability('mod/programming:edittestcase', $context);
|
||||
|
||||
$mform = new datafile_form();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect(new moodle_url('/mod/programming/datafile/list.php', array('id' => $cm->id)));
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
$data->id = $data->datafile;
|
||||
$content = $mform->get_file_content('data');
|
||||
if (!empty($content)) {
|
||||
$data->datasize = strlen($content);
|
||||
$data->data = bzcompress($content);
|
||||
} else {
|
||||
unset($data->datasize);
|
||||
unset($data->data);
|
||||
}
|
||||
if (!empty($data->usecheckdata)) {
|
||||
$content = $mform->get_file_content('checkdata');
|
||||
if (!empty($content)) {
|
||||
$data->checkdatasize = strlen($content);
|
||||
$data->checkdata = bzcompress($content);
|
||||
} else {
|
||||
unset($data->checkdatasize);
|
||||
unset($data->checkdata);
|
||||
}
|
||||
}
|
||||
|
||||
$data->timemodified = time();
|
||||
$DB->update_record('programming_datafile', $data);
|
||||
|
||||
redirect(new moodle_url('/mod/programming/datafile/list.php', array('id' => $cm->id)), get_string('datafilemodified', 'programming'));
|
||||
|
||||
} else {
|
||||
$data = $DB->get_record('programming_datafile', array('id' => $datafile_id));
|
||||
$data->datafile = $data->id;
|
||||
$data->id = $cm->id;
|
||||
$mform->set_data($data);
|
||||
|
||||
/// Print the page header
|
||||
$PAGE->set_title($programming->name);
|
||||
$PAGE->set_heading(format_string($course->fullname));
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/// Print tabs
|
||||
$renderer = $PAGE->get_renderer('mod_programming');
|
||||
$tabs = programming_navtab('edittest', 'datafile', $course, $programming, $cm);
|
||||
echo $renderer->render_navtab($tabs);
|
||||
|
||||
/// Print page content
|
||||
echo html_writer::tag('h2', $programming->name);
|
||||
echo html_writer::tag('h3', get_string('editdatafile', 'programming').$OUTPUT->help_icon('datafile', 'programming'));
|
||||
$mform->display();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
}
|
||||
59
datafile/form.php
Normal file
59
datafile/form.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
require_once ($CFG->libdir.'/formslib.php');
|
||||
|
||||
class datafile_form extends moodleform {
|
||||
|
||||
function definition() {
|
||||
global $CFG, $COURSE, $cm;
|
||||
$mform =& $this->_form;
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
$mform->addElement('hidden', 'id', $cm->id);
|
||||
$mform->addElement('hidden', 'datafile');
|
||||
|
||||
$places = array();
|
||||
$mform->addElement('text', 'filename', get_string('filename', 'programming'), 'maxlength="50"');
|
||||
$filetype = array();
|
||||
$filetype[] = $mform->createElement('radio', 'isbinary', null, get_string('textfile', 'programming'), 0);
|
||||
$filetype[] = $mform->createElement('radio', 'isbinary', null, get_string('binaryfile', 'programming'), 1);
|
||||
$mform->addGroup($filetype, 'filetype', get_string('filetype', 'programming'), ' ', false);
|
||||
$mform->addElement('filepicker', 'data', get_string('datafile', 'programming'));
|
||||
$mform->addElement('checkbox', 'usecheckdata', get_string('usecheckdata', 'programming'));
|
||||
$mform->addElement('filepicker', 'checkdata', get_string('datafileforcheck', 'programming'));
|
||||
$mform->disabledIf('checkdata', 'usecheckdata');
|
||||
|
||||
$mform->addElement('textarea', 'memo', get_string('memo', 'programming'), 'rows="5" cols="50"');
|
||||
|
||||
// buttons
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
|
||||
function validation($data, $files) {
|
||||
$errors = array();
|
||||
/// filename should not be empty
|
||||
if (empty($data['filename'])) {
|
||||
$errors['filename'] = get_string('required');
|
||||
} else {
|
||||
/// filename should only contain alpha, digit and underlins
|
||||
if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $data['filename'])) {
|
||||
$errors['filename'] = get_string('filenamechars', 'programming');
|
||||
}
|
||||
|
||||
/// file name should not duplicate
|
||||
if (empty($data['id']) && count_records_select('programming_datafile', "programmingid={$data['programmingid']} AND filename='{$data['filename']}'")) {
|
||||
$errors['filename'] = get_string('datafilenamedupliate', 'programming');
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($data['id']) && empty($files['data'])) {
|
||||
$errors['data'] = get_string('required');
|
||||
}
|
||||
|
||||
if (empty($data['id']) && !empty($data['usecheckdata']) && empty($files['checkdata'])) {
|
||||
$errors['checkdata'] = get_string('required');
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
}
|
||||
117
datafile/list.php
Normal file
117
datafile/list.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$params = array('id' => $id);
|
||||
$PAGE->set_url('/mod/programming/datafile/list.php', $params);
|
||||
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
print_error('invalidcoursemodule');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
print_error('coursemisconf');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
print_error('invalidprogrammingid', 'programming');
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
require_capability('mod/programming:viewhiddentestcase', $context);
|
||||
|
||||
/// Print the page header
|
||||
$PAGE->set_title($programming->name);
|
||||
$PAGE->set_heading(format_string($course->fullname));
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/// Print tabs
|
||||
$renderer = $PAGE->get_renderer('mod_programming');
|
||||
$tabs = programming_navtab('edittest', 'datafile', $course, $programming, $cm);
|
||||
echo $renderer->render_navtab($tabs);
|
||||
|
||||
/// Print page content
|
||||
echo html_writer::tag('h2', $programming->name);
|
||||
echo html_writer::tag('h3', get_string('datafiles', 'programming').$OUTPUT->help_icon('datafile', 'programming'));
|
||||
print_datafile_table();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
|
||||
function print_datafile_table() {
|
||||
global $CFG, $DB, $OUTPUT, $cm, $params, $page, $perpage, $programming, $course, $language, $groupid;
|
||||
|
||||
$table = new html_table();
|
||||
$table->head = array(
|
||||
get_string('sequence', 'programming'),
|
||||
get_string('filename', 'programming'),
|
||||
get_string('filetype', 'programming'),
|
||||
get_string('data', 'programming'),
|
||||
get_string('checkdata', 'programming'),
|
||||
get_string('action'),
|
||||
);
|
||||
$table->data = array();
|
||||
|
||||
/**$table->set_attribute('id', 'presetcode-table');
|
||||
$table->set_attribute('class', 'generaltable generalbox');
|
||||
$table->set_attribute('align', 'center');
|
||||
$table->set_attribute('cellpadding', '3');
|
||||
$table->set_attribute('cellspacing', '1');
|
||||
$table->no_sorting('code');*/
|
||||
|
||||
$strpresstodownload = get_string('presstodownload', 'programming');
|
||||
$strbinaryfile = get_string('binaryfile', 'programming');
|
||||
$strtextfile = get_string('textfile', 'programming');
|
||||
$stredit = get_string('edit');
|
||||
$strdelete = get_string('delete');
|
||||
$strmoveup = get_string('moveup');
|
||||
$strmovedown = get_string('movedown');
|
||||
$files = $DB->get_records('programming_datafile', array('programmingid' => $programming->id), 'seq');
|
||||
if (is_array($files)) {
|
||||
$files_count = count($files)-1;
|
||||
$i = 0;
|
||||
foreach ($files as $file) {
|
||||
$data = array();
|
||||
$data[] = $file->seq;
|
||||
$data[] = htmlentities($file->filename);
|
||||
$data[] = $file->isbinary ? $strbinaryfile : $strtextfile;
|
||||
$size = programming_format_codesize($file->datasize);
|
||||
$url = new moodle_url('/mod/programming/datafile/download.php', array('id' => $cm->id, 'datafile' => $file->id));
|
||||
$data[] = $OUTPUT->action_link($url, $size, null, array('title' => $strpresstodownload));
|
||||
if ($file->checkdatasize) {
|
||||
$size = programming_format_codesize($file->checkdatasize);
|
||||
$url->param('checkdata', 1);
|
||||
$data[] = $OUTPUT->action_link($url, $size, null, array('title' => $strpresstodownload));
|
||||
} else {
|
||||
$data[] = get_string('n/a', 'programming');
|
||||
}
|
||||
$url = new moodle_url('/mod/programming/datafile/edit.php', array('id' => $cm->id, 'datafile' => $file->id));
|
||||
$html = $OUTPUT->action_link($url, html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/edit'))), null, array('class' => 'icon edit', 'title' => $stredit));
|
||||
$url = new moodle_url('/mod/programming/datafile/delete.php', array('id' => $cm->id, 'datafile' => $file->id));
|
||||
$act = new confirm_action(get_string('deletedatafileconfirm', 'programming'));
|
||||
$html .= $OUTPUT->action_link($url, html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'))), $act, array('class' => 'icon delete', 'title' => $strdelete));
|
||||
if ($i > 0) {
|
||||
$url = new moodle_url('/mod/programming/datafile/move.php', array('id' => $cm->id, 'datafile' => $file->id, 'direction' => 1));
|
||||
$html .= $OUTPUT->action_link($url, html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/up'))), null, array('class' => 'icon up', 'title' => $strmoveup));
|
||||
}
|
||||
if ($i < $files_count) {
|
||||
$url = new moodle_url('/mod/programming/datafile/move.php', array('id' => $cm->id, 'datafile' => $file->id, 'direction' => 2));
|
||||
$html .= $OUTPUT->action_link($url, html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/down'))), null, array('class' => 'icon down', 'title' => $strmovedown));
|
||||
}
|
||||
$data[] = $html;
|
||||
$table->data[] = $data;
|
||||
$i++;
|
||||
}
|
||||
|
||||
echo html_writer::table($table);
|
||||
} else {
|
||||
echo html_writer::tag('p', get_string('nodatafile', 'programming'));
|
||||
}
|
||||
echo html_writer::tag('p', $OUTPUT->action_link(new moodle_url('/mod/programming/datafile/add.php', array('id' => $cm->id)), get_string('adddatafile', 'programming')));
|
||||
}
|
||||
|
||||
?>
|
||||
33
datafile/move.php
Normal file
33
datafile/move.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$datafile_id = required_param('datafile', PARAM_INT);
|
||||
$direction = required_param('direction', PARAM_INT);
|
||||
$params = array('id' => $id, 'datafile' => $datafile_id, 'direction' => $direction);
|
||||
$PAGE->set_url('/mod/programming/datafile/move.php', $params);
|
||||
|
||||
if ($id) {
|
||||
if (! $cm = get_coursemodule_from_id('programming', $id)) {
|
||||
error('Course Module ID was incorrect');
|
||||
}
|
||||
|
||||
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
|
||||
error('Course is misconfigured');
|
||||
}
|
||||
|
||||
if (! $programming = $DB->get_record('programming', array('id' => $cm->instance))) {
|
||||
error('Course module is incorrect');
|
||||
}
|
||||
}
|
||||
|
||||
require_login($course->id, true, $cm);
|
||||
$context = context_module::instance($cm->id);
|
||||
require_capability('mod/programming:edittestcase', $context);
|
||||
|
||||
programming_datafile_adjust_sequence($programming->id, $datafile_id, $direction);
|
||||
redirect(new moodle_url('/mod/programming/datafile/list.php', array('id' => $cm->id)), get_string('datafilemoved', 'programming'), 1);
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user