protected function run_tests($code, $testcases) { global $CFG; Twig_Autoloader::register(); $loader = new Twig_Loader_String(); $this->twig = new Twig_Environment($loader, array('debug' => true, 'autoescape' => false, 'optimizations' => 0)); $twigcore = $this->twig->getExtension('core'); $twigcore->setEscaper('py', 'python_escaper'); $twigcore->setEscaper('python', 'python_escaper'); $twigcore->setEscaper('c', 'java_escaper'); $twigcore->setEscaper('java', 'java_escaper'); $twigcore->setEscaper('ml', 'matlab_escaper'); $twigcore->setEscaper('matlab', 'matlab_escaper'); $this->setup_sandbox(); $this->setup_grader(); $this->allruns = array(); if (isset($this->sandboxparams)) { $sandboxparams = json_decode($this->sandboxparams, true); } else { $sandboxparams = array(); } if ($this->prototypetype != 0) { $files = array(); // We're running a prototype question ?! } else { // Load any files from the prototype $context = qtype_coderunner::question_context($this); $prototype = qtype_coderunner::get_prototype($this->coderunnertype, $context); $files = $this->get_data_files($prototype, $prototype->questionid); } $files += $this->get_data_files($this, $this->id); // Add in files for this question if (isset($this->cputimelimitsecs)) { $sandboxparams['cputime'] = intval($this->cputimelimitsecs); } if (isset($this->memlimitmb)) { $sandboxparams['memorylimit'] = intval($this->memlimitmb); } if (isset($this->templateparams) && $this->templateparams != '') { $this->parameters = json_decode($this->templateparams); } $outcome = $this->run_with_combinator($code, $testcases, $files, $sandboxparams); // If that failed for any reason (e.g. no combinator template or timeout // or signal) run the tests individually. Any compilation // errors or abnormal terminations (not including signals) in individual // tests bomb the whole test process, but otherwise we should finish // with a TestingOutcome object containing a test result for each test // case. if ($outcome == null) { $outcome = $this->run_tests_singly($code, $testcases, $files, $sandboxparams); } $this->sandboxinstance->close(); if ($this->showsource) { $outcome->sourcecodelist = $this->allruns; } return $outcome; }
* @subpackage coderunner * @copyright 2015 Richard Lobb, University of Canterbury * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('AJAX_SCRIPT', true); if (strpos(__DIR__, 'local/CodeRunner/type/coderunner') !== false) { require_once '../../../../config.php'; // Symbolically linked rather than copied } else { require_once '../../../config.php'; // "Normal" case of a copy of the code } require_once $CFG->dirroot . '/question/engine/lib.php'; require_once $CFG->dirroot . '/question/type/coderunner/questiontype.php'; require_login(); require_sesskey(); $qtype = required_param('qtype', PARAM_ALPHANUMEXT); $courseid = required_param('courseid', PARAM_INT); header('Content-type: application/json; charset=utf-8'); try { $coursecontext = context_course::instance($courseid); $questiontype = qtype_coderunner::get_prototype($qtype, $coursecontext); $questiontype->success = true; $questiontype->error = ''; } catch (moodle_exception $e) { $questiontype = new stdClass(); $questiontype->success = false; $questiontype->error = "Error fetching prototype. " . $e->getMessage(); } echo json_encode($questiontype); die;