Skip to content

Commit 93a2e6b

Browse files
authored
[help-editor] add help form for the instrument (#9821)
Add help form for the instrument. Previously, it was impossible to add help text for a new instrument because there was no way to add a new entry.
1 parent 4e0d0e5 commit 93a2e6b

File tree

11 files changed

+153
-82
lines changed

11 files changed

+153
-82
lines changed

htdocs/index.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,30 @@
3939
// @phan-file-suppress PhanRedefineFunctionInternal
4040

4141
// phpcs:ignore
42-
function array_any(array $array, callable $callback): bool
43-
{
44-
foreach ($array as $key => $value) {
45-
if ($callback($value, $key)) {
46-
return true;
42+
if (!function_exists('array_any')) {
43+
// phpcs:ignore
44+
function array_any(array $array, callable $callback): bool
45+
{
46+
foreach ($array as $key => $value) {
47+
if ($callback($value, $key)) {
48+
return true;
49+
}
4750
}
51+
return false;
4852
}
49-
50-
return false;
5153
}
52-
5354
// phpcs:ignore
54-
function array_find(array $array, callable $callback)
55-
{
56-
foreach ($array as $key => $value) {
57-
if ($callback($value, $key)) {
58-
return $value;
55+
if (!function_exists('array_find')) {
56+
// phpcs:ignore
57+
function array_find(array $array, callable $callback)
58+
{
59+
foreach ($array as $key => $value) {
60+
if ($callback($value, $key)) {
61+
return $value;
62+
}
5963
}
64+
return null;
6065
}
61-
62-
return null;
6366
}
6467
}
6568

modules/help_editor/ajax/help.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
$factory = \NDB_Factory::singleton();
1717
$user = $factory->user();
1818
$editable = $user->hasPermission('context_help');
19-
2019
try {
20+
2121
$moduleName = $_REQUEST['testName'] ?? null;
2222
$subpageName = $_REQUEST['subtest'] ?? null;
2323
$m = $loris->getModule($moduleName);
@@ -33,6 +33,7 @@
3333
if (ob_get_level() > 0) {
3434
ob_end_flush();
3535
}
36+
3637
exit(0);
3738
} catch (Exception $e) {
3839

@@ -41,11 +42,12 @@
4142

4243
if (!empty($moduleName)) {
4344
try {
44-
$helpID = \LORIS\help_editor\HelpFile::hashToID(
45+
$helpID = \LORIS\help_editor\HelpFile::hashToID(
4546
md5($subpageName ?? $moduleName)
4647
);
47-
$help_file = \LORIS\help_editor\HelpFile::factory($helpID);
48-
$data = $help_file->toArray();
48+
49+
$help_file = \LORIS\help_editor\HelpFile::factory($helpID);
50+
$data = $help_file->toArray();
4951
} catch (\NotFound $e) {
5052
// Send data with empty strings so that the content can be edited
5153
$data = [

modules/help_editor/ajax/process.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,21 @@
7878
),
7979
]
8080
);
81+
82+
}
83+
84+
if ($_POST['new'] == true) {
85+
// insert the help file
86+
HelpFile::insert(
87+
[
88+
'hash' => md5($_POST['instrument']),
89+
'topic' => $_POST['title'],
90+
'content' => $_POST['content'],
91+
'created' => date(
92+
'Y-m-d h:i:s',
93+
time()
94+
),
95+
]
96+
);
97+
echo "done";
8198
}

modules/help_editor/jsx/helpEditorForm.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Markdown from 'jsx/Markdown';
55
import Help from 'jsx/Help';
66
import swal from 'sweetalert2';
77
import {TextboxElement, TextareaElement} from 'jsx/Form';
8+
import {SelectElement} from 'jsx/Form';
89

910
/**
1011
* Help Editor Form Page.
@@ -21,6 +22,7 @@ import {TextboxElement, TextareaElement} from 'jsx/Form';
2122
const HelpEditorForm = (props) => {
2223
const [title, setTitle] = useState(props.title ?? '');
2324
const [content, setContent] = useState(props.content ?? '');
25+
const [instrument, setInstrument] = useState(props.instrument ?? '');
2426
const helpPreview = [];
2527
const helpContainers = document.getElementsByClassName('help-container');
2628

@@ -46,6 +48,9 @@ const HelpEditorForm = (props) => {
4648
case 'content':
4749
setContent(value);
4850
break;
51+
case 'instrument':
52+
setInstrument(value);
53+
break;
4954
}
5055
};
5156

@@ -59,12 +64,24 @@ const HelpEditorForm = (props) => {
5964
formData.append('section', props.section ?? '');
6065
formData.append('subsection', props.subsection ?? '');
6166
formData.append('helpID', props.helpid ?? '');
62-
67+
if (!props.helpid) {
68+
formData.append('new', 'true');
69+
formData.append('instrument', instrument ?? '');
70+
}
6371
fetch(loris.BaseURL + '/help_editor/ajax/process.php', {
6472
method: 'POST',
6573
body: formData,
6674
}).then((response) => {
75+
console.log(response);
76+
6777
if (response.status !== 200) {
78+
swal.fire({
79+
title: 'Content update unsuccessful.',
80+
text: 'Help content cannot be added to an instrument '
81+
+ 'that has already been registered.',
82+
type: 'error',
83+
confirmButtonText: 'Try again',
84+
});
6885
console.error(response.status);
6986
return;
7087
}
@@ -94,6 +111,16 @@ const HelpEditorForm = (props) => {
94111
<div className="panel-body">
95112
<div className="row">
96113
<div className="col-sm-9">
114+
115+
<SelectElement
116+
name='instrument'
117+
label='Instrument'
118+
emptyOption={true}
119+
options={props.instrumentslist}
120+
onUserInput={onUserInput}
121+
value={instrument}
122+
disabled={props.helpid !== null}
123+
/>
97124
<TextboxElement
98125
label='Title'
99126
name='title'
@@ -135,6 +162,8 @@ HelpEditorForm.propTypes = {
135162
subsection: PropTypes.string,
136163
helpid: PropTypes.string,
137164
url: PropTypes.string,
165+
instrument: PropTypes.string,
166+
instrumentslist: PropTypes.objectOf(PropTypes.string),
138167
};
139168

140169
window.RHelpEditorForm = React.createFactory(HelpEditorForm);

modules/help_editor/jsx/help_editor.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ class HelpEditor extends React.Component {
9898
}
9999

100100
const {data} = this.state;
101-
/**
102-
* XXX: Currently, the order of these fields MUST match the order of the
103-
* queried columns in _setupVariables() in media.class.inc
104-
*/
101+
const addHelp = () => {
102+
window.location.replace(
103+
loris.BaseURL+'/help_editor/edit_help_content?helpID=null'
104+
);
105+
};
106+
const actions = [
107+
{label: 'Adding help content for a specific instrument', action: addHelp},
108+
];
105109
const fields = [
106110
{label: 'Help ID', show: false},
107111
{label: 'Topic', show: true, filter: {
@@ -112,13 +116,15 @@ class HelpEditor extends React.Component {
112116
name: 'content',
113117
type: 'text',
114118
}},
119+
{label: 'Instrument', show: true},
115120
];
116121

117122
return (
118123
<FilterableDataTable
119124
name="help_filter"
120125
data={data}
121126
fields={fields}
127+
actions={actions}
122128
getFormattedCell={this.formatColumn}
123129
/>
124130
);

modules/help_editor/php/edit_help_content.class.inc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Edit_Help_Content extends \NDB_Form
107107
'UTF-8',
108108
false
109109
)
110-
: null,
110+
: null,
111111
];
112112

113113
if (!empty($data['section'])) {
@@ -125,9 +125,23 @@ class Edit_Help_Content extends \NDB_Form
125125
}
126126

127127
if ($data['helpid']) {
128-
$help_data = HelpFile::factory($data['helpid'])->toArray();
129-
$data['title'] = $help_data['topic'];
130-
$data['content'] = htmlspecialchars_decode(trim($help_data['content']));
128+
$help_data = HelpFile::factory($data['helpid'])->toArray();
129+
$data['title'] = $help_data['topic'];
130+
$data['content'] = htmlspecialchars_decode(
131+
trim(
132+
$help_data['content']
133+
)
134+
);
135+
$data['instrument'] = $help_data['instrument'];
136+
$data['instrumentslist'] = [];
137+
$instrumentList = \NDB_BVL_Instrument::getInstrumentNamesList(
138+
$request->getAttribute("loris")
139+
);
140+
$data['instrumentslist'] = $instrumentList;
141+
142+
if ($data['helpid'] =='null') {
143+
$data['instrumentslist'] = $instrumentList;
144+
}
131145
}
132146

133147
// case where no help content exists

modules/help_editor/php/helpfile.class.inc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @link https://github.com/aces/Loris
1414
*/
1515
namespace LORIS\help_editor;
16+
require __DIR__ . '/../../../tools/generic_includes.php';
1617

1718
/**
1819
* Base class for files containing help text information
@@ -67,6 +68,18 @@ class HelpFile
6768
created, updated FROM help WHERE helpID = :HID",
6869
['HID' => $helpID]
6970
);
71+
72+
// get instrument test_name
73+
$instruments = $DB->pselect("SELECT * FROM test_names", []);
74+
foreach ($instruments as $item) {
75+
if ($result !== null && md5($item['Test_name']) === $result['hash']) {
76+
77+
$result['instrument_full_name'] = $item['Full_name'];
78+
$result['instrument'] = $item['Test_name'];
79+
break;
80+
}
81+
}
82+
7083
// set the help file
7184
$obj->data = $result ?? [];
7285

@@ -115,9 +128,12 @@ class HelpFile
115128
*/
116129
function toArray()
117130
{
118-
$data = $this->data;
119-
$data['topic'] = stripslashes($this->data['topic']);
120-
$data['content'] = stripslashes($this->data['content']);
131+
$data = $this->data;
132+
$data['topic'] = stripslashes($this->data['topic'] ?? '');
133+
134+
$data['content'] = stripslashes($this->data['content'] ?? '');
135+
136+
$data['instrument'] = stripslashes($this->data['instrument'] ?? '');
121137

122138
return $data;
123139
}

modules/help_editor/php/helprowprovisioner.class.inc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ class HelpRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
4242
{
4343
parent::__construct(
4444
$loris,
45-
"SELECT help.helpID as helpID,
46-
help.topic as Topic,
47-
help.content as Content
48-
FROM help
49-
WHERE help.hash IS NOT NULL
50-
AND help.topic IS NOT NULL
51-
ORDER BY help.helpID",
45+
"SELECT h.helpID as helpID,
46+
h.topic as Topic,
47+
h.content as Content,
48+
t.Full_name
49+
FROM help h
50+
JOIN test_names t
51+
ON h.hash = MD5(t.Test_name)
52+
WHERE h.hash IS NOT NULL
53+
AND h.topic IS NOT NULL
54+
ORDER BY h.helpID",
5255
[]
5356
);
5457
}

modules/help_editor/templates/form_edit_help_content.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
content: `{$content}`,
99
section: "{$section}",
1010
subsection: "{$subsection}",
11+
instrument: "{$instrument}",
12+
instrumentslist: {$instrumentslist|@json_encode nofilter},
1113
helpid: {$helpid},
1214
})
1315
);

modules/help_editor/test/HelpEditorTestIntegrationTest.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ class HelpEditorTestIntegrationTest extends LorisIntegrationTest
3535
function setUp(): void
3636
{
3737
parent::setUp();
38-
$md5String = md5("TestTestTest");
39-
$this->DB->insert(
40-
"help",
41-
[
42-
'helpID' => '999999',
43-
'hash' => $md5String,
44-
'topic' => 'Test Topic',
45-
'content' => 'This is a test content.',
46-
'created' => '2013-04-05 00:00:00',
47-
]
48-
);
4938
}
5039

5140
/**
@@ -56,7 +45,6 @@ function setUp(): void
5645
function tearDown(): void
5746
{
5847
parent::tearDown();
59-
$this->DB->delete("help", ['helpID' => '999999']);
6048
}
6149

6250
/**
@@ -153,18 +141,12 @@ function testHelpEditorSearchByTopic()
153141
$this->safeGet($this->url . "/help_editor/");
154142
$this->safeFindElement(
155143
WebDriverBy::Name("topic")
156-
)->sendKeys("Test Topic");
157-
//click the [show data] button
158-
$this->safeClick(
159-
WebDriverBy::cssSelector(
160-
"#dynamictable>tbody:nth-child(2)>".
161-
"tr:nth-child(1)>td:nth-child(2)>a:nth-child(1)"
162-
)
163-
);
144+
)->sendKeys("bmi");
145+
164146
$bodyText = $this->safeFindElement(
165147
WebDriverBy::cssSelector('.panel-body')
166148
)->getText();
167-
$this->assertStringContainsString("test", $bodyText);
149+
$this->assertStringContainsString("bmi", $bodyText);
168150
}
169151

170152
/**
@@ -177,17 +159,11 @@ function testHelpEditorSearchByKeyword()
177159
$this->safeGet($this->url . "/help_editor/");
178160
$this->safeFindElement(
179161
WebDriverBy::Name("content")
180-
)->sendKeys("test");
181-
$this->safeClick(
182-
WebDriverBy::cssSelector(
183-
"#dynamictable>tbody:nth-child(2)>".
184-
"tr:nth-child(1)>td:nth-child(2)>a:nth-child(1)"
185-
)
186-
);
162+
)->sendKeys("BMI");
187163
$bodyText = $this->safeFindElement(
188164
WebDriverBy::cssSelector('.panel-body')
189165
)->getText();
190-
$this->assertStringContainsString("test", $bodyText);
166+
$this->assertStringContainsString("BMI", $bodyText);
191167
}
192168
}
193169

0 commit comments

Comments
 (0)