forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caschat.php
169 lines (141 loc) · 5.87 KB
/
caschat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
// This file is part of Stack - http://stack.maths.ed.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/>.
/**
* This script lets the user send commands to the Maxima, and see the response.
* This can be useful for learning about the CAS syntax, and also for testing
* that maxima is working correctly.
*
* @copyright 2012 University of Birmingham
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
require_once(__DIR__ . '/locallib.php');
require_once(__DIR__ . '/stack/utils.class.php');
require_once(__DIR__ . '/stack/options.class.php');
require_once(__DIR__ . '/stack/cas/castext.class.php');
require_once(__DIR__ . '/stack/cas/casstring.class.php');
require_once(__DIR__ . '/stack/cas/cassession.class.php');
require_once(__DIR__ . '/stack/cas/keyval.class.php');
// Get the parameters from the URL.
$questionid = optional_param('questionid', null, PARAM_INT);
if (!$questionid) {
require_login();
$context = context_system::instance();
require_capability('qtype/stack:usediagnostictools', $context);
$urlparams = array();
} else {
// Load the necessary data.
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST);
$question = question_bank::load_question($questionid);
// Process any other URL parameters, and do require_login.
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question);
// Check permissions.
question_require_capability_on($questiondata, 'view');
}
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url('/question/type/stack/caschat.php', $urlparams);
$title = stack_string('chattitle');
$PAGE->set_title($title);
$debuginfo = '';
$errs = '';
$varerrs = '';
$vars = optional_param('vars', '', PARAM_RAW);
$string = optional_param('cas', '', PARAM_RAW);
$simp = optional_param('simp', '', PARAM_RAW);
// Always fix dollars in this script.
// Very useful for converting existing text for use elswhere in Moodle, such as in pages of text.
$string = stack_maths::replace_dollars($string);
// Sort out simplification.
if ('on' == $simp) {
$simp = true;
} else {
$simp = false;
}
// Initially simplification should be on.
if (!$vars and !$string) {
$simp = true;
}
// Set a value for the "insert stars" flag. Useful for testing, but should be set to zero normally.
$stars = 0;
if ($string) {
$options = new stack_options();
$options->set_site_defaults();
$options->set_option('simplify', $simp);
$session = new stack_cas_session(null, $options);
if ($vars) {
$keyvals = new stack_cas_keyval($vars, $options, 0, 't', true, $stars);
$session = $keyvals->get_session();
$varerrs = $keyvals->get_errors();
}
if (!$varerrs) {
$ct = new stack_cas_text($string, $session, 0, 't', true, $stars);
$displaytext = $ct->get_display_castext();
$errs = $ct->get_errors();
$debuginfo = $ct->get_debuginfo();
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
echo html_writer::tag('p', stack_string('chatintro'));
// If we are editing the General Feedback from a question it is very helpful to see the question text.
if ($questionid) {
echo $OUTPUT->heading(stack_string('questiontext'), 3);
echo html_writer::tag('pre', $question->questiontext, array('class' => 'questiontext'));
}
if (!$varerrs) {
if ($string) {
echo $OUTPUT->box(stack_ouput_castext($displaytext));
}
}
if ($simp) {
$simp = stack_string('autosimplify').' '.
html_writer::empty_tag('input', array('type' => 'checkbox', 'checked' => $simp, 'name' => 'simp'));
} else {
$simp = stack_string('autosimplify').' '.html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'simp'));
}
$varlen = substr_count($vars, "\n") + 3;
$stringlen = max(substr_count($string, "\n") + 3, 8);
echo html_writer::tag('form',
html_writer::tag('h2', stack_string('questionvariables')) .
html_writer::tag('p', $varerrs) .
html_writer::tag('p', html_writer::tag('textarea', $vars,
array('cols' => 100, 'rows' => $varlen, 'name' => 'vars'))) .
html_writer::tag('p', $simp) .
html_writer::tag('h2', stack_string('castext')) .
html_writer::tag('p', $errs) .
html_writer::tag('p', html_writer::tag('textarea', $string,
array('cols' => 100, 'rows' => $stringlen, 'name' => 'cas'))) .
html_writer::tag('p', html_writer::empty_tag('input',
array('type' => 'submit', 'value' => stack_string('chat')))),
array('action' => $PAGE->url, 'method' => 'post'));
if ($string) {
// Display the question variables.
echo $OUTPUT->heading(stack_string('questionvariablevalues'), 3);
echo html_writer::start_tag('div', array('class' => 'questionvariables'));
$variables = $session->get_keyval_representation();
$variables = implode(";\n", explode('; ', $variables));
if (trim($variables) == '') {
$variables .= get_string('none');
}
echo html_writer::tag('pre', $variables);
echo html_writer::end_tag('div');
}
if ('' != trim($debuginfo)) {
echo $OUTPUT->box($debuginfo);
}
echo $OUTPUT->footer();