-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConceptCollection.inc.php
executable file
·260 lines (225 loc) · 7.21 KB
/
ConceptCollection.inc.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
require_once (MCL_ROOT . 'Concept.inc.php');
/**
* Stores a collection of concept objects.
*/
class ConceptCollection
{
/**
* Array of Concept objects of the format:
* [<dict_db>][<concept_id>]
*/
private $arr_concept = array();
/**
* Array of ConceptSearchResultsGroup objects.
*/
private $arr_results_group = array();
/**
* Constructor
*/
public function __construct() {
// do nothing
}
/***************************************************************************************************
* CONCEPT FUNCTIONS
**************************************************************************************************/
/**
* Add a Concept to the collection
*/
public function addConcept(Concept $c)
{
// Concepts are internally partitioned by their dictionary source
$dict_db = '';
if ($c->css_dict) $dict_db = strtolower($c->css_dict->dict_db);
// Add the concept
$this->arr_concept[$dict_db][$c->concept_id] = $c;
}
/**
* Get the specified concept.
* @param int $concept_id
* @param mixed $css_dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY or a string of the name of a dictionary database
* @return Concept
*/
public function getConcept($concept_id, $css_dict = '')
{
// Determine the dictionary
if ($css_dict instanceof ConceptSearchSource) {
$dict_db = strtolower($css_dict->dict_db);
} else {
$dict_db = $css_dict;
}
// Get the concept
$c = null;
if ( isset($this->arr_concept[ $dict_db ][ $concept_id ]) ) {
$c = $this->arr_concept[ $dict_db ][ $concept_id ];
}
return $c;
}
/**
* Get the number of concepts stored in this collection.
* @param mixed $css_dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY, a string of the name of a dictionary database, or null to indicate all dictionary sources.
* @return int
*/
public function getCount($css_dict = null)
{
// Determine the dictionary
if ($css_dict instanceof ConceptSearchSource) {
$dict_db = strtolower($css_dict->dict_db);
} else {
$dict_db = $css_dict;
}
// Count
$num = 0;
if (is_null($dict_db)) {
// Return concepts for all dictionaries if null
foreach (array_keys($this->arr_concept) as $key_dict) {
$num += count($this->arr_concept[$key_dict]);
}
} elseif (isset($this->arr_concept[$dict_db])) {
// Return concepts for only the specified dictionary if anything other null
$num = count($this->arr_concept[$dict_db]);
}
return $num;
}
/**
* Get the number of visible concepts stored in this collection. The collection
* may store a number of "invisible" concepts (i.e. concept sets or Q/A sets) that
* were not part of the base search query.
* @return int
*/
public function getVisibleCount()
{
$num = 0;
foreach (array_keys($this->arr_concept) as $key_dict) {
foreach (array_keys($this->arr_concept[$key_dict]) as $concept_id) {
if ($this->arr_concept[$key_dict][$concept_id]->display) $num++;
}
}
return $num;
}
/***************************************************************************************************
* CSRG FUNCTIONS
**************************************************************************************************/
/**
* Add ConceptSearchResultsGroup object to this ConceptCollection.
* NOTE: This does not add the group's concepts. This must be done indepedently.
*/
public function addGroup(ConceptSearchResultsGroup $csrg)
{
$this->arr_results_group[] = $csrg;
}
/**
* Returns an array with IDs of the ConceptSearchResultsGroup objects.
*/
public function getSearchResultsGroupIds()
{
return array_keys($this->arr_results_group);
}
/**
* Get the specified ConceptSearchResultsGroup
*/
public function getSearchResultsGroup($csrg_id)
{
$csrg = null;
if ( isset($this->arr_results_group[$csrg_id]) )
{
$csrg = $this->arr_results_group[$csrg_id];
}
return $csrg;
}
/**
* Get the number of ConceptSearchResultsGroup objects stored in this collection.
* @return int
*/
public function getSearchResultsGroupCount() {
return count($this->arr_results_group);
}
/***************************************************************************************************
* HELPER FUNCTIONS
**************************************************************************************************/
/**
* Get an arary of keys for dictionary sources used in this collection.
* @return array
*/
public function getDictionarySources()
{
return array_keys($this->arr_concept);
}
/**
* Get array of concept IDs contained in the search results group
* @param ConceptSearchSource $css_dict
* @return array
*/
public function getConceptIds(ConceptSearchSource $css_dict = null)
{
// Set the dictionary source
if ($css_dict) $dict_db = $css_dict->dict_db;
else $dict_db = '';
// Get the IDs
if (isset($this->arr_concept[$dict_db])) {
return array_keys($this->arr_concept[$dict_db]);
} else {
trigger_error('Unrecognized dictionary source: ' . $dict_db, E_USER_ERROR);
}
}
/**
* Returns a string of comma-separated IDs for all concepts that are members of the specified dictionary source.
* @param ConceptSearchSource $css_dict
* @return string
*/
public function getConceptIdCsv(ConceptSearchSource $css_dict = null)
{
return implode(',', $this->getConceptIds($css_dict));
}
/**
* Returns a string of comma-separated IDs of all visible concepts in this collection.
* @param ConceptSearchSource $css_dict
*/
public function getVisibleConceptIds(ConceptSearchSource $css_dict = null)
{
// Set the dictionary source
if ($css_dict) $dict_db = $css_dict->dict_db;
else $dict_db = '';
// Get the IDs
$arr_concept_id = array();
if ( isset($this->arr_concept[$dict_db]) )
{
$arr_concept_id = array();
foreach (array_keys($this->arr_concept[$dict_db]) as $concept_id) {
if ($this->arr_concept[$dict_db][$concept_id]->display) {
$arr_concept_id[$concept_id] = $concept_id;
}
}
}
return $arr_concept_id;
}
/**
* Returns a string of comma-separated IDs of all visible concepts in this collection.
* @param ConceptSearchSource $css_dict
*/
public function getVisibleConceptIdCsv(ConceptSearchSource $css_dict = null)
{
return implode(',', $this->getVisibleConceptIds($css_dict));
}
/**
* Returns a string containing comma-separated concept ids for all questions
* and answers referred to by the concepts in this collection.
*/
public function getQAIdCsv(ConceptSearchSource $css_dict = null)
{
// Set the dictionary source
if ($css_dict) $dict_db = $css_dict->dict_db;
else $dict_db = '';
// Build the CSV
$arr_qa_id = array();
if ( !isset($this->arr_concept[ $dict_db ]) ) return '';
foreach (array_keys($this->arr_concept[$dict_db]) as $_concept_id)
{
$c = $this->arr_concept[ $dict_db ][ $_concept_id ];
foreach ($c->getAnswerIds () as $_id) $arr_qa_id[$_id] = $_id;
foreach ($c->getQuestionIds() as $_id) $arr_qa_id[$_id] = $_id;
}
return implode(',', $arr_qa_id);
}
}
?>