Project

General

Profile

« Previous | Next » 

Revision 4288

refactor into reusable methods such that queries can be constructed from different form interfaces.

View differences:

lib/style/skins/first/search.js
1
function checkSearch(submitFormObj) {
2
	//alert("calling method");
1
/****************************************************************************
2
* Multiple Assessment download
3
* @param form containing the neceassary input items
4
* @return true/false for form submission
5
*****************************************************************************/
6
function multipleAssessmentSearch(submitFormObj) {
7

  
8
	//harvest the metadata fields we want to include
9
	var metadataObjs = new Array();
10
	/*
11
	var index = 0;
12
	for (var i=0; i < submitFormObj.length; i++) {
13
		var formElement = submitFormObj.elements[i];
14
		if (formElement.type == "checkbox" && formElement.checked) {
15
			//ignore certain other checkboxes, kind of a hack 
16
			if (formElement.name == "includeQuestions") {
17
				continue;
18
			}
19
			var metadataObj = new Object();
20
			metadataObj.name = formElement.name;
21
			metadataObj.value = formElement.value;
22
			metadataObjs[index] = metadataObj;
23
			index++;
24
		}
25
	}
26
	*/
3 27
	
4
    //var checkBox = document.getElementById("searchAll");
28
	//TODO option for all questions vs. just one
29
	var questionId = submitFormObj.questionId.value;
30
	//alert("questionId=" + questionId);
31
	var questionIds = new Array();
32
	questionIds[0] = questionId;
33
	
34
	var documentObjects = new Array();
35
	
36
	if (submitFormObj.docids.length > 1) {
37
		for (var i=0; i < submitFormObj.docids.length; i++) {
38
			var documentObject = new Object();
39
			documentObject.docid = submitFormObj.docids[i].value;
40
			documentObject.questionIds = questionIds;
41
			documentObjects[i] = documentObject;
42
		}
43
	}
44
	else {
45
		var documentObject = new Object();
46
		documentObject.docid = submitFormObj.docids.value;
47
		for (var z = 0; z < submitFormObj[documentObject.docid].length; z++) {
48
			questionIds[z] = submitFormObj[documentObject.docid][z].value;
49
		}
50
		documentObject.questionIds = questionIds;
51
		documentObjects[0] = documentObject;
52
	}
53
		
54
	var query = generateQuery(documentObjects, metadataObjs);
55
	
56
	submitFormObj.dataquery.value = query;
57
	
58
	return true;
59
		
60
}
61

  
62
/****************************************************************************
63
* Single Assessment download
64
* @param form containing the neceassary input items
65
* @return true/false for form submission
66
*****************************************************************************/
67
function assessmentSearch(submitFormObj) {
68

  
69
	//harvest the metadata fields we want to include
70
	var metadataObjs = new Array();
71
	var index = 0;
72
	for (var i=0; i < submitFormObj.length; i++) {
73
		var formElement = submitFormObj.elements[i];
74
		if (formElement.type == "checkbox" && formElement.checked) {
75
			//ignore certain other checkboxes, kind of a hack 
76
			if (formElement.name == "includeQuestions") {
77
				continue;
78
			}
79
			var metadataObj = new Object();
80
			metadataObj.name = formElement.name;
81
			metadataObj.value = formElement.value;
82
			metadataObjs[index] = metadataObj;
83
			index++;
84
		}
85
	}
86
	
87
	//var checkBox = document.getElementById("searchAll");
5 88
	var docId = submitFormObj.docid.value;
6 89
	//alert("docId=" + docId);
7 90
	
......
21 104
		}
22 105
	}
23 106
	
24
	//alert("questionIds=" + questionIds);
107
	//set up the list of objects to pass to the query assembler, just one document
108
	var documentObjects = new Array();
109
	var documentObject = new Object();
110
	documentObject.docid = docId;
111
	documentObject.questionIds = questionIds;
112
	documentObjects[0] = documentObject;
25 113
	
26
	//construct the assessment metadata query
114
	var query = generateQuery(documentObjects, metadataObjs);
115
	
116
	submitFormObj.dataquery.value = query;
117
	
118
	return true;
119
		
120
}
121

  
122
/****************************************************************************
123
* Query Generation function
124
* @param docObjs is an Array of Objects with "docid" (String) and "questionIds" (Array) properties
125
* @param metadataObjs is an Array of Objects with "name" and "value" properties (both String)
126
* @return generated query string
127
*****************************************************************************/
128
function generateQuery(docObjs, metadataObjs) {
129
	//alert("calling method");
130
	
131
	//construct the assessment metadata attribute selection snippet
27 132
	var metadataAttributes = "";
28 133
	var index = 0;
29
	for (var i=0; i < submitFormObj.length; i++) {
30
		var formElement = submitFormObj.elements[i];
31
		if (formElement.type == "checkbox" && formElement.checked) {
32
			//ignore certain other checkboxes, kind of a hack 
33
			if (formElement.name == "includeQuestions") {
34
				continue;
35
			}
134
	for (var j=0; j < metadataObjs.length; j++) {
135
		var metadataObj = metadataObjs[j];
36 136
		
37
			metadataAttributes += "<attribute index=\"";
38
			metadataAttributes += index;
39
			metadataAttributes += "\">";
137
		metadataAttributes += "<attribute index=\"";
138
		metadataAttributes += index;
139
		metadataAttributes += "\">";
140
		
141
		metadataAttributes += "<pathexpr label=\"";
142
		metadataAttributes += metadataObj.name;
143
		metadataAttributes += "\">";
144
		metadataAttributes += metadataObj.value;
145
		metadataAttributes += "</pathexpr>";
146
		
147
		metadataAttributes += "</attribute>";
148
		
149
		index++;
150
	}//metadataObjs loop
151
	
152
	
153
	//construct the begining of the query
154
	var tempQuery = 
155
        "<?xml version=\"1.0\"?>"
156
        + "<dataquery>"
157
			+ "<union>";
40 158
			
41
			metadataAttributes += "<pathexpr label=\"";
42
			metadataAttributes += formElement.name;
43
			metadataAttributes += "\">";
44
			metadataAttributes += formElement.value;
45
			metadataAttributes += "</pathexpr>";
46
			
47
			metadataAttributes += "</attribute>";
48
			
49
			index++;
50
		}
51
	}
159
	for (var i=0; i < docObjs.length; i++) {
160

  
161
		var docId = docObjs[i].docid;
162
		alert("docId=" + docId);
163
		
164
		//get the question ids for this question
165
		var questionIds = docObjs[i].questionIds;
52 166
	
53
	//assemble the assessment metadata
54
	metadataAttributes = 
167
		alert("questionIds=" + questionIds);
168
		
169
		//assemble the assessment metadata
170
		var metadataAttributeSelection = "";
171
		if (metadataAttributes.length > 0) {
172
			metadataAttributeSelection =
55 173
				"<datapackage id=\"" + docId + "\">"
56 174
					+ "<entity id=\"" + docId + "\">"
57 175
						+ metadataAttributes
58 176
					+ "</entity>"
59 177
				+ "</datapackage>";
178
		}
179
				
180
		//loop for each question item
181
		for (var k=0; k < questionIds.length; k++) {
182
			var questionId = questionIds[k];
183
		
184
			tempQuery +=
185
			"<query>"
186
			//select the data
187
			+ "<selection>"
188
				+ "<datapackage id=\"" + docId + "\">"
189
					+ "<entity index=\"0\">"
190
						+ "<attribute index=\"0\"/>"
191
						+ "<attribute index=\"1\"/>"
192
						//omit student id attribute
193
						+ "<attribute index=\"3\"/>"
194
						+ "<attribute index=\"4\"/>"
195
					+ "</entity>"
196
				+ "</datapackage>";
197
				
198
			//select the metadata
199
			tempQuery += metadataAttributeSelection;
60 200
	
61
	//construct the entire query
62
	var tempQuery = 
63
        "<?xml version=\"1.0\"?>"
64
        + "<dataquery>"
65
			+ "<union>";
201
			//select the question metadata						
202
			if (questionId.length > 0) {
203
				tempQuery +=
204
				"<datapackage id=\"" + questionId + "\">"
205
					+ "<entity id=\"" + questionId + "\">"
206
						+ "<attribute index=\"0\">"
207
							+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
208
						+ "</attribute>"
209
						+ "<attribute index=\"1\">"
210
							+ "<pathexpr label=\"qTitle\">//assessment/section/item/@title</pathexpr>"
211
						+ "</attribute>"
212
						+ "<attribute index=\"2\">"
213
							+ "<pathexpr label=\"qLabel\">//assessment/section/item/presentation/@label</pathexpr>"
214
						+ "</attribute>"
215
					+ "</entity>"
216
				+ "</datapackage>";
217
			}
218
				
219
			tempQuery += "</selection>";
66 220
			
67
			//loop for each question item
68
			for (var i=0; i < questionIds.length; i++) {
69
				var questionId = questionIds[i];
70
				
221
			//join to the quesion "table"
222
			if (questionId.length > 0) {
71 223
				tempQuery +=
72
					"<query>"
73
					//select the data
74
					+ "<selection>"
75
						+ "<datapackage id=\"" + docId + "\">"
76
							+ "<entity index=\"0\">"
77
								+ "<attribute index=\"0\"/>"
78
								+ "<attribute index=\"1\"/>"
79
								//omit student id attribute
80
								+ "<attribute index=\"3\"/>"
81
								+ "<attribute index=\"4\"/>"
82
							+ "</entity>"
83
						+ "</datapackage>";
84
						
85
						//select the metadata
86
						tempQuery += metadataAttributes;
87

  
88
						//select the question metadata						
89
						if (questionId.length > 0) {
90
							tempQuery +=
91
							"<datapackage id=\"" + questionId + "\">"
92
								+ "<entity id=\"" + questionId + "\">"
93
									+ "<attribute index=\"0\">"
94
										+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
95
									+ "</attribute>"
96
									+ "<attribute index=\"1\">"
97
										+ "<pathexpr label=\"qTitle\">//assessment/section/item/@title</pathexpr>"
98
									+ "</attribute>"
99
									+ "<attribute index=\"2\">"
100
										+ "<pathexpr label=\"qLabel\">//assessment/section/item/presentation/@label</pathexpr>"
101
									+ "</attribute>"
102
								+ "</entity>"
103
							+ "</datapackage>";
104
						}
105
						
106
					tempQuery += "</selection>";
107
					
108
					//join to the quesion "table"
109
					if (questionId.length > 0) {
110
						tempQuery +=
111
							"<where>"
112
								+ "<condition type=\"join\">"
113
									+ "<left>"
114
										+ "<datapackage id=\"" + docId + "\">"
115
											+ "<entity index=\"0\">"
116
												+ "<attribute index=\"1\"/>"
117
											+ "</entity>"
118
										+ "</datapackage>"
119
									+ "</left>"
120
									+ "<operator>=</operator>"
121
									+ "<right>"
122
										+ "<datapackage id=\"" + questionId + "\">"
123
											+ "<entity id=\"" + questionId + "\">"
124
												+ "<attribute index=\"0\">"
125
													+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
126
												+ "</attribute>"
127
											+ "</entity>"
128
										+ "</datapackage>"
129
									+ "</right>"
130
								+ "</condition>"
131
							+ "</where>";
132
					}
133
					
134
					tempQuery += "</query>";
135
				}
224
					"<where>"
225
						+ "<condition type=\"join\">"
226
							+ "<left>"
227
								+ "<datapackage id=\"" + docId + "\">"
228
									+ "<entity index=\"0\">"
229
										+ "<attribute index=\"1\"/>"
230
									+ "</entity>"
231
								+ "</datapackage>"
232
							+ "</left>"
233
							+ "<operator>=</operator>"
234
							+ "<right>"
235
								+ "<datapackage id=\"" + questionId + "\">"
236
									+ "<entity id=\"" + questionId + "\">"
237
										+ "<attribute index=\"0\">"
238
											+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
239
										+ "</attribute>"
240
									+ "</entity>"
241
								+ "</datapackage>"
242
							+ "</right>"
243
						+ "</condition>"
244
					+ "</where>";
245
			}
246
			
247
			tempQuery += "</query>";
248
		
249
		} // for questionId loop
250
		
251
	
252
	} //for docObjs loop
136 253
				 
137
			tempQuery +=
138
			 "</union>"	
139
        + "</dataquery>";
140
         
141
         submitFormObj.dataquery.value = tempQuery;
142
         
143
         //alert(submitFormObj.dataquery.value);
144
         
145
    return true;
254
	tempQuery +=
255
		 "</union>"	
256
		 + "</dataquery>";
257
	
258
	alert(tempQuery);
259
             
260
    return tempQuery;
146 261
}

Also available in: Unified diff