first commit
This commit is contained in:
65
presetcode/add.php
Normal file
65
presetcode/add.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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/presetcode/add.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 presetcode_form();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)));
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
unset($data->id);
|
||||
$data->programmingid = $programming->id;
|
||||
|
||||
if ($data->choosename == '1') $data->name = '<prepend>';
|
||||
if ($data->choosename == '2') $data->name = '<postpend>';
|
||||
|
||||
$data->sequence = $DB->count_records('programming_presetcode', array('programmingid' => $programming->id), 'MAX(sequence)') + 1;
|
||||
$DB->insert_record('programming_presetcode', $data);
|
||||
programming_presetcode_adjust_sequence($programming->id);
|
||||
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)), get_string('presetcodeadded', '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', 'presetcode', $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('addpresetcode', 'programming').$OUTPUT->help_icon('presetcode', 'programming'));
|
||||
|
||||
$mform->display();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
}
|
||||
31
presetcode/delete.php
Normal file
31
presetcode/delete.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // programming ID
|
||||
$code_id = required_param('code', PARAM_INT);
|
||||
$params = array('id' => $id, 'code' => $code_id);
|
||||
$PAGE->set_url('/mod/programming/presetcode/delete.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);
|
||||
|
||||
$DB->delete_records('programming_presetcode', array('id' => $code_id));
|
||||
programming_presetcode_adjust_sequence($programming->id);
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)), get_string('presetcodedeleted', 'programming'));
|
||||
|
||||
?>
|
||||
67
presetcode/edit.php
Normal file
67
presetcode/edit.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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
|
||||
$code_id = required_param('code', PARAM_INT);
|
||||
$params = array('id' => $id, 'code' => $code_id);
|
||||
$PAGE->set_url('/mod/programming/presetcode/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 presetcode_form();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)));
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
$data->id = $data->code;
|
||||
|
||||
if ($data->choosename == '1') $data->name = '<prepend>';
|
||||
if ($data->choosename == '2') $data->name = '<postpend>';
|
||||
|
||||
$DB->update_record('programming_presetcode', $data);
|
||||
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)), get_string('presetcodemodified', 'programming'));
|
||||
|
||||
} else {
|
||||
$data = $DB->get_record('programming_presetcode', array('id' => $code_id));
|
||||
$data->code = $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', 'presetcode', $course, $programming, $cm);
|
||||
echo $renderer->render_navtab($tabs);
|
||||
|
||||
echo html_writer::tag('h2', $programming->name);
|
||||
echo html_writer::tag('h3', get_string('editpresetcode', 'programming').$OUTPUT->help_icon('datafile', 'programming'));
|
||||
|
||||
/// Print page content
|
||||
$mform->display();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
}
|
||||
106
presetcode/form.php
Normal file
106
presetcode/form.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
require_once ($CFG->libdir.'/formslib.php');
|
||||
|
||||
class presetcode_form extends moodleform {
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
function definition() {
|
||||
global $CFG, $COURSE, $cm, $programming;
|
||||
$mform =& $this->_form;
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
$mform->addElement('hidden', 'id', $cm->id);
|
||||
$mform->addElement('hidden', 'code');
|
||||
|
||||
$places = array();
|
||||
$places[] = $mform->createElement('radio', 'choosename', null, get_string('prepend', 'programming'), 1);
|
||||
$places[] = $mform->createElement('radio', 'choosename', null, get_string('postpend', 'programming'), 2);
|
||||
$places[] = $mform->createElement('radio', 'choosename', null, get_string('customfile', 'programming'), 0);
|
||||
$mform->addGroup($places, 'places', get_string('place', 'programming'), ' ', false);
|
||||
|
||||
$mform->addElement('text', 'name', get_string('filename', 'programming'));
|
||||
$mform->disabledIf('name', 'choosename', 'not eq', 0);
|
||||
|
||||
$mform->addElement('select', 'languageid', get_string('language', 'programming'), programming_get_language_options($programming));
|
||||
|
||||
$mform->addElement('textarea', 'presetcode', get_string('codeforuser', 'programming'), 'rows="5" cols="50"');
|
||||
|
||||
$mform->addElement('checkbox', 'usepresetcodeforcheck', get_string('usepresetcodeforcheck', 'programming'));
|
||||
$mform->addElement('textarea', 'presetcodeforcheck', get_string('codeforcheck', 'programming'), 'rows="5" cols="50"');
|
||||
$mform->disabledIf('presetcodeforcheck', 'usepresetcodeforcheck');
|
||||
|
||||
// buttons
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
|
||||
function set_data($data) {
|
||||
if (empty($data->name) || $data->name == '<prepend>') {
|
||||
$data->choosename = 1;
|
||||
$data->name = '';
|
||||
} else if ($data->name == '<postpend>') {
|
||||
$data->choosename = 2;
|
||||
$data->name = '';
|
||||
} else {
|
||||
$data->choosename = 0;
|
||||
}
|
||||
|
||||
if (!empty($data->presetcodeforcheck)) {
|
||||
$data->usepresetcodeforcheck = true;
|
||||
}
|
||||
|
||||
if (empty($data->presetcode)) {
|
||||
$data->usepresetcodeforcheck = false;
|
||||
}
|
||||
parent::set_data($data);
|
||||
}
|
||||
|
||||
function validation($data, $files) {
|
||||
global $DB;
|
||||
|
||||
$errors = array();
|
||||
if ($data['choosename'] == 0) {
|
||||
/// filename should not be empty
|
||||
if (empty($data['name'])) {
|
||||
$errors['name'] = get_string('required');
|
||||
}
|
||||
|
||||
/// filename should only contain alpha, digit and underlins
|
||||
if (empty($errors['name']) && !preg_match('/^[a-zA-Z0-9_\-\.]+$/', $data['name'])) {
|
||||
$errors['name'] = get_string('filenamechars', 'programming');
|
||||
}
|
||||
|
||||
/// file extension must be correct
|
||||
$lang = $DB->get_record('programming_languages', array('id' => $data['languageid']));
|
||||
$allowedext = array_merge(explode(' ', $lang->headerext), explode(' ', $lang->sourceext));
|
||||
$ext = substr($data['name'], strrpos($data['name'], '.'));
|
||||
if (empty($errors['name']) && !in_array($ext, $allowedext)) {
|
||||
$errors['name'] = get_string('extmustbe', 'programming', implode(', ', $allowedext));
|
||||
}
|
||||
|
||||
/// file name should not duplicate
|
||||
if (empty($errors['name']) && empty($data['id']) && count_records_select('programming_presetcode', "programmingid={$data['programmingid']} AND name='{$data['name']}'")) {
|
||||
$errors['name'] = get_string('filenamedupliate', 'programming');
|
||||
}
|
||||
|
||||
} else if (empty($data['id']) && $data['choosename'] == 1 && count_records_select('programming_presetcode', "programmingid={$data['programmingid']} AND name='<prepend>'")) {
|
||||
$errors['places'] = get_string('prependcodeexists', 'programming');
|
||||
|
||||
} else if (empty($data['id']) && $data['choosename'] == 2 && count_records_select('programming_presetcode', "programmingid={$data['programmingid']} AND name='<postpend>'")) {
|
||||
$errors['places'] = get_string('postpendcodeexists', 'programming');
|
||||
}
|
||||
|
||||
if (empty($data['presetcode'])) {
|
||||
$errors['presetcode'] = get_string('required');
|
||||
}
|
||||
|
||||
if (!empty($data['usepresetcodeforcheck']) && empty($data['presetcodeforcheck'])) {
|
||||
$errors['presetcodeforcheck'] = get_string('required');
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
}
|
||||
111
presetcode/list.php
Normal file
111
presetcode/list.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?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/presetcode/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', 'presetcode', $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('presetcode', 'programming').$OUTPUT->help_icon('presetcode', 'programming'));
|
||||
print_presetcode_table();
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer($course);
|
||||
|
||||
function print_presetcode_table() {
|
||||
global $CFG, $DB, $OUTPUT, $cm, $page, $perpage, $programming, $course, $language, $groupid;
|
||||
|
||||
$table = new html_table();
|
||||
$table->head = array(
|
||||
get_string('sequence', 'programming'),
|
||||
get_string('name', 'programming'),
|
||||
get_string('language'),
|
||||
get_string('codeforuser', 'programming'),
|
||||
get_string('codeforcheck', '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');
|
||||
$table->setup();*/
|
||||
|
||||
$codes = $DB->get_records('programming_presetcode', array('programmingid' => $programming->id), 'sequence');
|
||||
if (is_array($codes)) {
|
||||
$langs = $DB->get_records('programming_languages');
|
||||
$codes_count = count($codes)-1;
|
||||
$i = 0;
|
||||
|
||||
$stredit = get_string('edit');
|
||||
$strdelete = get_string('delete');
|
||||
$strmoveup = get_string('moveup');
|
||||
$strmovedown = get_string('movedown');
|
||||
|
||||
foreach ($codes as $code) {
|
||||
$data = array();
|
||||
$data[] = $code->sequence;
|
||||
$data[] = htmlentities($code->name);
|
||||
$data[] = $langs[$code->languageid]->name;
|
||||
$data[] = $code->presetcode ? 'Yes' : '';
|
||||
$data[] = $code->presetcodeforcheck ? 'Yes' : '';
|
||||
$url = new moodle_url('/mod/programming/presetcode/edit.php', array('id' => $cm->id, 'code' => $code->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/presetcode/delete.php', array('id' => $cm->id, 'code' => $code->id));
|
||||
$act = new confirm_action(get_string('presetcodedeleted', '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/presetcode/move.php', array('id' => $cm->id, 'code' => $code->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 < $codes_count) {
|
||||
$url = new moodle_url('/mod/programming/presetcode/move.php', array('id' => $cm->id, 'code' => $code->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('nopresetcode', 'programming'));
|
||||
}
|
||||
echo html_writer::tag('p', $OUTPUT->action_link(new moodle_url('/mod/programming/presetcode/add.php', array('id' => $cm->id)), get_string('addpresetcode', 'programming')));
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
33
presetcode/move.php
Normal file
33
presetcode/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
|
||||
$code_id = required_param('code', PARAM_INT);
|
||||
$direction = required_param('direction', PARAM_INT);
|
||||
$params = array('id' => $id, 'code' => $code_id, 'direction' => $direction);
|
||||
$PAGE->set_url('/mod/programming/presetcode/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_presetcode_adjust_sequence($programming->id, $code_id, $direction);
|
||||
redirect(new moodle_url('/mod/programming/presetcode/list.php', array('id' => $cm->id)), get_string('presetcodemoved', 'programming'), 1);
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user