-
Notifications
You must be signed in to change notification settings - Fork 123
/
ajax.php
109 lines (102 loc) · 4.44 KB
/
ajax.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
<?php
// This file is part of CodeRunner - http://coderunner.org.nz/
//
// CodeRunner 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.
//
// CodeRunner 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 CodeRunner. If not, see <http://www.gnu.org/licenses/>.
/**
* AJAX script to return a JSON-encoded row of the options for the specified
* question type by looking up the prototype in the question_coderunner_options
* table. Fields 'success' and 'error' are added for validation checking by
* the caller.
*
* Alternatively, if called with a parameter uiplugin rather than qtype, returns
* a list describing the UI plugin parameters and their descriptions.
*
* @group qtype_coderunner
* Assumed to be run after python questions have been tested, so focuses
* only on C-specific aspects.
*
* @package qtype_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);
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/type/coderunner/questiontype.php');
require_login();
require_sesskey();
$courseid = required_param('courseid', PARAM_INT);
$qtype = optional_param('qtype', '', PARAM_RAW_TRIMMED);
$uiplugin = strtolower(optional_param('uiplugin', '', PARAM_RAW_TRIMMED));
header('Content-type: application/json; charset=utf-8');
$coursecontext = context_course::instance($courseid);
if ($qtype) {
$questiontype = qtype_coderunner::get_prototype($qtype, $coursecontext);
if ($questiontype === null || is_array($questiontype)) {
$questionprototype = $questiontype;
$questiontype = new stdClass();
$questiontype->success = false;
if ($questiontype === null) {
$questiontype->error = json_encode(["error" => "missingprototype",
"alert" => "prototype_missing_alert", "extras" => ""]);
} else {
$extras = "";
foreach ($questionprototype as $component) {
$extras .= get_string(
'listprototypeduplicates',
'qtype_coderunner',
['id' => $component->id, 'name' => $component->name,
'category' => $component->category]
);
}
$questiontype->error = json_encode(["error" => "duplicateprototype",
"alert" => "prototype_duplicate_alert", "extras" => $extras]);
}
} else {
$questiontype->success = true;
$questiontype->error = '';
}
echo json_encode($questiontype);
} else if ($uiplugin) {
$uiplugins = qtype_coderunner_ui_plugins::get_instance();
$allnames = $uiplugins->all_names();
$uiparamstable = [];
$columnheaders = [];
if (!in_array($uiplugin, $allnames)) {
$uiheader = get_string('unknownuiplugin', 'qtype_coderunner', ['pluginname' => $uiplugin]);
} else {
$uiparams = $uiplugins->parameters($uiplugin);
if ($uiparams->length() === 0) {
$uiheader = get_string('nouiparameters', 'qtype_coderunner', ['uiname' => $uiplugin]);
} else {
$csv = implode(', ', $uiparams->all_names_starred());
$uiheader = get_string(
'uiparametertablehead',
'qtype_coderunner',
['uiname' => $uiplugin]
) . $csv . '.';
$uiparamstable = $uiparams->table();
$namehdr = get_string('uiparamname', 'qtype_coderunner');
$descrhdr = get_string('uiparamdesc', 'qtype_coderunner');
$defaulthdr = get_string('uiparamdefault', 'qtype_coderunner');
$columnheaders = [$namehdr, $descrhdr, $defaulthdr];
}
}
echo json_encode(['header' => $uiheader,
'uiparamstable' => $uiparamstable,
'columnheaders' => $columnheaders,
'showdetails' => get_string('showdetails', 'qtype_coderunner'),
'hidedetails' => get_string('hidedetails', 'qtype_coderunner')]);
}
die();