Skip to main content
Sign in
Snippets Groups Projects
Commit dc7c3176 authored by Tim Hunt's avatar Tim Hunt
Browse files

Add a settings.php file for configuration.

Also, make test.php more Moodle-y.

Some coding style clean-up in cliconnector.class.php.
parent e557fc41
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,19 @@ $string['pluginnameadding'] = 'Adding a stack question';
$string['pluginnameediting'] = 'Editing a stack question';
$string['pluginnamesummary'] = 'Stack questions use a computer algebra system to mark the students work. ...';
// admin settings
$string['settingcasdebugging'] = 'CAS debugging';
$string['settingcasdebugging_desc'] = 'Whether to store debugging information about the CAS connection.';
$string['settingcasmaximaversion'] = 'Maxima version';
$string['settingcasmaximaversion_desc'] = 'The version of Maxima being used.';
$string['settingcastimeout'] = 'CAS connection timeout';
$string['settingcastimeout_desc'] = 'The timout to use when trying to connect to Maxima.';
$string['settingplatformtype'] = 'Platform type';
$string['settingplatformtype_desc'] = 'Stack needs to know what sort of operating system it is running on.';
$string['settingplatformtypeunix'] = 'Linux';
$string['settingplatformtypeserver'] = 'Server mode';
$string['settingplatformtypewin'] = 'Windows';
// casstring.class.php
$string['stackCas_spaces'] = 'Spaces found in expression: ';
$string['stackCas_percent'] = '% found in expression: ';
......@@ -47,6 +60,7 @@ $string['stackCas_forbiddenWord'] = 'Forbidden Word: ';
// cassession.class.php
$string['stackCas_CASError'] = 'The CAS returned the following error(s):';
$string['stackCas_allFailed'] = 'CAS failed to return any evaluated expressions. Please check your connection with the CAS.';
$string['stackCas_failedReturn'] = 'CAS failed to return any data.';
// castext.class.php
$string['stackCas_tooLong'] = 'CASText statement is too long.';
......
......
<?php
// This file is part of Stack - http://stack.bham.ac.uk//
//
// Stack 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.
//
// Stack 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 Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information for the Stack question type.
*
* @package qtype_stack
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$settings->add(new admin_setting_configselect('qtype_stack/platform',
get_string('settingplatformtype', 'qtype_stack'),
get_string('settingplatformtype_desc', 'qtype_stack'), 'linux', array(
'unix' => get_string('settingplatformtypeunix', 'qtype_stack'),
'win' => get_string('settingplatformtypewin', 'qtype_stack'),
'server' => get_string('settingplatformtypeserver', 'qtype_stack'))));
$settings->add(new admin_setting_configselect('qtype_stack/maximaversion',
get_string('settingcasmaximaversion', 'qtype_stack'),
get_string('settingcasmaximaversion_desc', 'qtype_stack'), '5.20.2',
array('5.19.2' => '5.19.2', '5.20.2' => '5.20.2')));
$settings->add(new admin_setting_configtext('qtype_stack/castimeout',
get_string('settingcastimeout', 'qtype_stack'),
get_string('settingcastimeout_desc', 'qtype_stack'), 5, PARAM_INT, 3));
$settings->add(new admin_setting_configcheckbox('qtype_stack/casdebugging',
get_string('settingcasdebugging', 'qtype_stack'),
get_string('settingcasdebugging_desc', 'qtype_stack'), 0));
......@@ -15,6 +15,16 @@
// You should have received a copy of the GNU General Public License
// along with Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the maxima connection class.
*
* @copyright 2012 University of Birmingham
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('answer.class.php');
require_once(dirname(__FILE__) . '/../legacy.class.php');
/**
......@@ -23,110 +33,78 @@
* takes in an array of CAS commands, builds a string suitable for maxima
* sends this string to maxima, then extracts the results from the returned string
* Also handles errors from maxima.
*
* @copyright 2012 University of Birmingham
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('answer.class.php');
require_once(dirname(__FILE__) . '/../legacy.class.php');
class STACK_CAS_Maxima_CLIConnector {
/**
*
*
* @param int
* @access private
*/
private $seed;
/**
*
*
* @param error
* @access private
*/
private $errorLog;
/**
*
*
* @param string
* @access private
*/
private $display;
/**
*
*
* @param STACK_options
* @access private
*/
private $options;
/**
*
*
* @param string
* @access private
*/
private $rawResult;
/**
*
*
* @param array
* @access private
*/
private $casCommands;
/**
*
*
* @param string
* @access private
*/
private $security;
/**
*
*
* @param array
* @access private
*/
private $csNames;
/**
*
*
* @param array
* @access private
*/
private $csVars;
/**
*
*
* @param array
* @access private
*/
private $csCmds;
/**
* The logger for this class
*
* @param STACK_Logger
* @access private
*/
private $logger;
// /**
// * The logger for this class
// *
// * @param STACK_Logger
// */
// private $logger;
/**
* @param array cas commands
* @param string display type (mathml/latex)
*/
private $maximaAnsArray;
function __construct($options, $seed, $security='s')
{
function __construct($options, $seed, $security = 's') {
$this->options = $options;
$this->seed = $seed;
$this->security = $security;
......@@ -137,20 +115,30 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
$this->rawcommand = '';
$this->config = $this->load_config();
}
protected function load_config() {
global $CFG;
static $settings = null;
$path = 'C:\xampp\data\moodledata\stack';
$initCommand = "load(\"".$path."\maximalocal.mac\");";
$initCommand = str_replace("\\", "/", $initCommand);
$initCommand .= "\n";
if (is_null($settings)) {
$settings = get_config('qtype_stack');
}
$this->config['platform'] = 'win';
$this->config['logs'] = $path;
$this->config['CASCommand'] = $path.'\maxima.bat';
$this->config['CASInitCommand'] = $initCommand;
$this->config['CASTimeout'] = 5;
$this->config['CASDebug'] = false;
$this->config['CASVersion'] = '5.21.1';
make_upload_directory('stack');
$path = $CFG->dataroot . '/stack';
$initCommand = 'load("' . $path . '/maximalocal.mac");' . "\n";
return array(
'platform' => $settings->platform,
'logs' => $path,
'CASCommand' => $path . '/maxima.bat',
'CASInitCommand' => $initCommand,
'CASTimeout' => $settings->castimeout,
'CASDebug' => $settings->casdebugging,
'CASVersion' => $settings->maximaversion,
);
}
/**
......@@ -158,9 +146,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*
* @return array|bool return results from cas, or false if error.
*/
function sendCASCommands($casCommands)
{
function sendCASCommands($casCommands) {
$this->casCommands = $casCommands;
$cas_options=$this->options->getcascommands();
......@@ -168,26 +154,22 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
$this->csVars .= $cas_options['commands'];
$this->constructMaximaCommand($casCommands);
if(!$this->sendToMaxima())
{
if(!$this->sendToMaxima()) {
//$this->errorLog->addError('Could not send to Maxima. ');
//$this->logger->error('Could not send command to maxima. ');
}
else
{
if($this->parseAns())
{
} else {
if($this->parseAns()) {
//$this->errorLog->addError('Parsing answer failed. ');
}
if(!$this->validateAns())
{
//$this->errorLog->addError('Validation failed, '.$this->validateAns());
if(!$this->validateAns()) {
//$this->errorLog->addError('Validation failed, '.$this->validateAns());
}
return $this->returnResults();
}
}
/**
* Sends a answertest to the cas
*
......@@ -196,46 +178,35 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
* @param string $anstest
* @access protected
* @return array
*
*/
public function sendAnsTest($student, $teacher, $anstest)
{
public function sendAnsTest($student, $teacher, $anstest) {
$this->buildVariables($student, $teacher, $anstest);
$this->constructMaximaAnstest($student, $teacher, $anstest);
//echo $this->rawcommand;
if(!$this->sendToMaxima())
{
if(!$this->sendToMaxima()) {
//$this->errorLog->addError('Could not send to Maxima. ');
return 'Could not send to maxima. ';
}
else
{
} else {
//echo 'parsedAnsTest: '.$this->parseAnsTest();
//var_dump($this->parseAnsTest());
return $this->parseAnsTest();
}
}
/**
* wraps up the cas commands in a string suitable for maxima
*
* @param array $casCommands
* @return string
*/
function constructMaximaCommand($casCommands)
{
function constructMaximaCommand($casCommands) {
$i = 0;
foreach($casCommands as $label => $cmd)
{
foreach($casCommands as $label => $cmd) {
$cmd = str_replace('?', 'qmchar', $cmd); // replace any ?'s that slipped through
$this->csNames .= ", $label";
$this->csCmds .= ", print(\"$i=[ error= [\"), cte(\"$label\",errcatch($label:$cmd)) ";
$i++;
}
$cs ='cab:block([ RANDOM_SEED';
......@@ -259,9 +230,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
* @param string $ansTest
* @return string the command
*/
protected function constructMaximaAnstest($exp1, $exp2, $ansTest)
{
protected function constructMaximaAnstest($exp1, $exp2, $ansTest) {
$cs = "cab:block([ STACK_SA,STACK_TA,str{$this->csNames}] {$this->csVars}, ";
$cs .= " print(\"[ Timestamp = [ $this->seed ], Ans= [ error = [\"), STACK_SA:cte(\"STACK_SA\",errcatch($exp1)),";
$cs .= " print(\" TAAns= [ error = [\"), STACK_TA:cte(\"STACK_TA\",errcatch(STACK_TA:$exp2)),";
......@@ -270,15 +239,12 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
return $cs;
}
/**
* Sends string to maxima
*
* @return string
*/
function sendToMaxima()
{
function sendToMaxima() {
$platform = $this->config['platform'];
if ($platform == 'win')
......@@ -303,30 +269,27 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
return $result;
}
/**
* Removes the junk characters maxima adds to each variable.
*
* @param string $var The string to clean
* @return string The cleaned string
*/
protected function cleanUpVar($var)
{
protected function cleanUpVar($var) {
$var = str_replace('[', '',$var);
$var = str_replace(']', '',$var);
$var = str_replace(',', '',$var);
$var = trim($var);
return $var;
}
//"
/**
* Maxima-specific function used to parse CAS output into an array.
*
* @param array $strin Raw CAS output
* @return array
*/
function CASParsePreparse($strin)
{
function CASParsePreparse($strin) {
// Take the raw string from the CAS, and unpack this into an array.
$offset = 0;
$strin_len = strlen($strin);
......@@ -369,8 +332,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
* @param string $instr The raw CAS output
* @return array Has field ['questionVarsInst'] containing the array of instantiated local variables
*/
function CASParseCASOutput($instr)
{
function CASParseCASOutput($instr) {
$errors = '';
$locals = array();
......@@ -413,8 +375,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*
* @return bool whether variables are missing. An explaination is added to the error variable of each object.
*/
function parseAns()
{
function parseAns() {
$trimmedResult = '';
$foundErrors = false;
//check we have a timestamp & remove everything before it.
......@@ -469,8 +430,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*/
private function TidyMaximaErrors($errstr) {
if (FALSE===strpos($errstr,'0 to a negative exponent')) {
} else {
if (strpos($errstr,'0 to a negative exponent') !== false) {
$errstr = stack_string('Maxima_DivisionZero');
}
......@@ -554,8 +514,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*
* @return array of answers
*/
function parseAnsTest()
{
function parseAnsTest() {
if(($this->rawResult == '') ||($this->rawResult == NULL))
{
$this->returnErrors = 'Error. No CAS Output for answer test';
......@@ -625,17 +584,13 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
return $toReturn;
}
/**
* Returns an array containing the results from maxima.
* in the form [label] => display
*
* @return array
*/
function returnResults()
{
function returnResults() {
//of the format [label] -> display
//let everything have an error by default
......@@ -671,15 +626,13 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*
* @return array
*/
function returnValues()
{
function returnValues() {
//of the format [label] -> display
$noObjects = count($this->maximaAnsArray);
$returnArray = array();
for($i =0; $i <$noObjects; $i++)
{
for($i =0; $i <$noObjects; $i++) {
$obj = $this->maximaAnsArray[$i];
$label = $obj->getLabel();
$val = $obj->getValue();
......@@ -696,8 +649,7 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
*
* @return array in the form [label] => error
*/
function returnErrors()
{
function returnErrors() {
//of the format [label] -> error
$returnArray = array();
......@@ -733,9 +685,8 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
* @return string
* @access public
*/
private function sendWin($strin)
{
$ret = FALSE;
private function sendWin($strin) {
$ret = false;
$descriptors = array(
0 => array('pipe', 'r'),
......@@ -908,6 +859,4 @@ require_once(dirname(__FILE__) . '/../legacy.class.php');
return $ret;
}
}
......@@ -10,28 +10,34 @@ require_once('stringutil.class.php');
require_once('options.class.php');
require_once('cas/castext.class.php');
if ($_POST && isset($_POST['cas'])) {
require_login();
$string = $_POST['cas'];
}
$context = context_system::instance();
require_capability('moodle/site:config', $context);
$PAGE->set_context($context);
$PAGE->set_url('/question/type/stack/stack/test.php');
$start = microtime(true);
$string = optional_param('cas', '', PARAM_RAW);
if (null != $string) {
if ($string) {
$ct = new STACK_CAS_CasText($string);
$displayText = $ct->Get_display_castext();
$errs = $ct->Get_errors();
}
echo $displayText;
echo $errs;
?>
echo $OUTPUT->header();
if ($string) {
echo '<p>', $displayText, '</p>';
echo $errs;
}
?>
<form action="test.php" method="POST">
<!--<textarea cols="80" rows="5" name="cas">@ diff(x^5, x) @ \[ @diff(x^3, x)@ \]</textarea><br /><br /> -->
<textarea cols="80" rows="5" name="cas"><?php echo $string;?></textarea><br /><br />
<input type="submit" value="chat" />
<input type="submit" value="Chat" />
</form>
<?php
echo $OUTPUT->footer();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment