Project

General

Profile

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, fieldFormObj) {
7
	
8
	//alert("submitFormObj=" + submitFormObj);
9
	//harvest the metadata fields we want to include
10
	var metadataObjs = new Array();
11
	var index = 0;
12
	for (var i=0; i < fieldFormObj.length; i++) {
13
		var formElement = fieldFormObj.elements[i];
14
		var metadataObj = new Object();
15
		metadataObj.name = formElement.name;
16
		metadataObj.value = formElement.value;
17
		metadataObjs[index] = metadataObj;
18
		index++;
19
	}
20
	
21
	//TODO option for all questions vs. just one
22
	//var questionId = submitFormObj.questionId.value;
23
	//alert("questionId=" + questionId);
24
	
25
	var documentObjects = new Array();
26
	var index = 0;
27
	if (submitFormObj.docid.length > 1) {
28
		for (var i=0; i < submitFormObj.docid.length; i++) {
29
			if (submitFormObj.docid[i].value != "") {
30
				var documentObject = new Object();
31
				documentObject.docid = submitFormObj.docid[i].value;
32
				documentObject.demographicData = submitFormObj[documentObject.docid + "demographicData"].value;
33
				
34
				var questionIds = new Array();
35
				for (var z = 0; z < submitFormObj[documentObject.docid].length; z++) {
36
					questionIds[z] = submitFormObj[documentObject.docid][z].value;
37
				}
38
			
39
				documentObject.questionIds = questionIds;
40
				documentObjects[index] = documentObject;
41
				index++;
42
			}
43
		}
44
	}
45
	else {
46
		//alert("submitFormObj.docid=" + submitFormObj.docid);
47
		if (submitFormObj.docid.value != "") {
48
			var documentObject = new Object();
49
			documentObject.docid = submitFormObj.docid.value;
50
			documentObject.demographicData = submitFormObj[documentObject.docid + "demographicData"].value;
51
				
52
			var questionIds = new Array();
53
			if (submitFormObj[documentObject.docid].length > 1) {
54
				for (var z = 0; z < submitFormObj[documentObject.docid].length; z++) {
55
					questionIds[z] = submitFormObj[documentObject.docid][z].value;
56
				}
57
			}
58
			else {
59
				questionIds[0] = submitFormObj[documentObject.docid].value;
60
			}
61
			
62
			documentObject.questionIds = questionIds;
63
			documentObjects[0] = documentObject;
64
		}
65
	}
66
	
67
	var itemMetadataCount = submitFormObj.metadataCount.value;
68
	
69
	var query = generateQuery(documentObjects, metadataObjs, itemMetadataCount);
70
	
71
	submitFormObj.dataquery.value = query;
72
	
73
	return true;
74
		
75
}
76

    
77
/****************************************************************************
78
* Single Assessment download
79
* @param form containing the neceassary input items
80
* @return true/false for form submission
81
*****************************************************************************/
82
function assessmentSearch(submitFormObj) {
83

    
84
	//harvest the metadata fields we want to include
85
	var metadataObjs = new Array();
86
	var index = 0;
87
	for (var i=0; i < submitFormObj.length; i++) {
88
		var formElement = submitFormObj.elements[i];
89
		if (formElement.type == "checkbox" && formElement.checked) {
90
			//ignore certain other checkboxes, kind of a hack 
91
			if (formElement.name == "includeQuestions") {
92
				continue;
93
			}
94
			var metadataObj = new Object();
95
			metadataObj.name = formElement.name;
96
			metadataObj.value = formElement.value;
97
			metadataObjs[index] = metadataObj;
98
			index++;
99
		}
100
	}
101
	
102
	//var checkBox = document.getElementById("searchAll");
103
	var docId = submitFormObj.docid.value;
104
	//alert("docId=" + docId);
105
	
106
	//do we want question metadata?
107
	var includeQuestions = submitFormObj.includeQuestions.checked;
108
	var questionIds = new Array();
109
	questionIds[0] = "";
110
	
111
	if (includeQuestions) {
112
		if (submitFormObj.assessmentItemId.length > 1) {
113
			for (var i=0; i < submitFormObj.assessmentItemId.length; i++) {
114
				questionIds[i] = submitFormObj.assessmentItemId[i].value;
115
			}
116
		}
117
		else {
118
			questionIds[0] = submitFormObj.assessmentItemId.value;
119
		}
120
	}
121
	
122
	//set up the list of objects to pass to the query assembler, just one document
123
	var documentObjects = new Array();
124
	var documentObject = new Object();
125
	documentObject.docid = docId;
126
	documentObject.questionIds = questionIds;
127
	documentObjects[0] = documentObject;
128
	
129
	var query = generateQuery(documentObjects, metadataObjs);
130
	
131
	submitFormObj.dataquery.value = query;
132
	
133
	return true;
134
		
135
}
136

    
137
/****************************************************************************
138
* Save fields for the attribute maping
139
* @param formId containing the neceassary input items
140
* @return true/false for form submission
141
*****************************************************************************/
142
function saveFields(formId, metacatURL) {
143

    
144
	var submitFormObj = document.getElementById(formId);
145
	
146
	//send the request to clear
147
	var myRequest = new Ajax.Request(
148
	metacatURL,
149
	{	method: 'post',
150
		parameters: { 
151
			action: 'editcart', 
152
			operation: 'clearfields'},
153
		evalScripts: true, 
154
		//onSuccess: function(transport) {alert('success: ' + transport.status);},
155
		onFailure: function(transport) {alert('failure clearing fields');}
156
	 });
157
	
158
	//go through the check boxes and set the ones we want
159
	var count = 0;
160
	for (var i=0; i < submitFormObj.length; i++) {
161
		var formElement = submitFormObj.elements[i];
162
		if (formElement.type == "checkbox" && formElement.checked) {
163
			//ignore certain other checkboxes, kind of a hack, but it's javascript...
164
			if (formElement.name == "includeQuestions") {
165
				continue;
166
			}
167
			if (formElement.name == "docid") {
168
				continue;
169
			}
170
			
171
			//send the request
172
			var myRequest = new Ajax.Request(
173
			metacatURL,
174
			{	method: 'post',
175
				parameters: { 
176
					action: 'editcart', 
177
					operation: 'addfield', 
178
					field: formElement.name, 
179
					path: formElement.value},
180
				evalScripts: true, 
181
				onSuccess: function(transport) {
182
					//alert('Selections saved: ' + operation); 
183
					//refresh after the save
184
					if (document.getElementById("ajaxCartResults")) {
185
						window.location.reload();
186
					}
187
					else {
188
						window.document.getElementById("iframeheaderclass").src=window.document.getElementById("iframeheaderclass").src;
189
					}
190
				}, 
191
				onFailure: function(transport) {alert('failure saving field: ' + formElement.name);}
192
			 });
193
		 	count++;
194
		}
195
	}
196
	
197
	//window.document.getElementById("iframeheaderclass").src = window.document.getElementById("iframeheaderclass").src;
198
	
199
	//alert(count + ' Field selections saved.'); 
200
	
201
	return true;
202
		
203
}
204

    
205
/****************************************************************************
206
* Query Generation function
207
* @param docObjs is an Array of Objects with "docid" (String) and "questionIds" (Array) properties
208
* @param metadataObjs is an Array of Objects with "name" and "value" properties (both String)
209
* @return generated query string
210
*****************************************************************************/
211
function generateQuery(docObjs, metadataObjs, itemMetadataCount) {
212
	//alert("calling method");
213
	
214
	//make parameters at some point
215
	var includeDemographicData = false;
216
	var questionMetadataCount = 2;
217
	if (itemMetadataCount) {
218
		questionMetadataCount = itemMetadataCount;
219
	}
220
	var questionChoiceCount = 5;
221
	
222
	//construct the assessment metadata attribute selection snippet
223
	var metadataAttributes = "";
224
	var index = 0;
225
	for (var j=0; j < metadataObjs.length; j++) {
226
		var metadataObj = metadataObjs[j];
227
		
228
		metadataAttributes += "<attribute index=\"";
229
		metadataAttributes += index;
230
		metadataAttributes += "\">";
231
		
232
		metadataAttributes += "<pathexpr label=\"";
233
		metadataAttributes += metadataObj.name;
234
		metadataAttributes += "\">";
235
		metadataAttributes += metadataObj.value;
236
		metadataAttributes += "</pathexpr>";
237
		
238
		metadataAttributes += "</attribute>";
239
		
240
		index++;
241
	}//metadataObjs loop
242
	
243
	
244
	//construct the begining of the query
245
	var tempQuery = 
246
        "<?xml version=\"1.0\"?>"
247
        + "<dataquery>"
248
			+ "<union>";
249
			
250
	for (var i=0; i < docObjs.length; i++) {
251

    
252
		var docId = docObjs[i].docid;
253
		var containsDemographicData = docObjs[i].demographicData;
254
		//alert("containsDemographicData=" + containsDemographicData);
255
		
256
		//get the question ids for this question
257
		var questionIds = docObjs[i].questionIds;
258
	
259
		//alert("questionIds=" + questionIds);
260
		
261
		//assemble the assessment metadata
262
		var metadataAttributeSelection = "";
263
		if (metadataAttributes.length > 0) {
264
			metadataAttributeSelection =
265
				"<datapackage id=\"" + docId + "\">"
266
					+ "<entity id=\"" + docId + "\">"
267
						+ metadataAttributes
268
					+ "</entity>"
269
				+ "</datapackage>";
270
		}
271
				
272
		//loop for each question item
273
		for (var k=0; k < questionIds.length; k++) {
274
			var questionId = questionIds[k];
275
		
276
			tempQuery +=
277
			"<query>"
278
			+ "<selection>";
279
			
280
			//select the data
281
			tempQuery +=
282
				"<datapackage id=\"" + docId + "\">"
283
					+ "<entity index=\"0\">"
284
						+ "<attribute index=\"0\"/>"
285
						+ "<attribute index=\"1\"/>"
286
						//DO NOT omit student id attribute - used for transpose
287
						+ "<attribute index=\"2\"/>"
288
						+ "<attribute index=\"3\"/>"
289
						+ "<attribute index=\"4\"/>"
290
						// the external response file
291
						+ "<attribute index=\"6\"/>"
292
					+ "</entity>"
293
				+ "</datapackage>";
294
				
295
			//select the demographic data
296
			if (includeDemographicData) {
297
				if (containsDemographicData) {
298
					tempQuery +=
299
						"<datapackage id=\"" + docId + "\">"
300
							+ "<entity index=\"1\">"
301
								//omit student id attribute
302
								+ "<attribute index=\"1\"/>"
303
								+ "<attribute index=\"2\"/>"
304
								+ "<attribute index=\"3\"/>"
305
							+ "</entity>"
306
						+ "</datapackage>";	
307
				}
308
				else {
309
					tempQuery +=
310
						"<staticItem name=\"demographic_1\" value=\"\" />"
311
						+ "<staticItem name=\"demographic_2\" value=\"\" />"
312
						+ "<staticItem name=\"demographic_3\" value=\"\" />";
313
				}
314
			}
315
				
316
			//select the metadata
317
			tempQuery += metadataAttributeSelection;
318
	
319
			//select the question metadata						
320
			if (questionId.length > 0) {
321
				tempQuery +=
322
				"<datapackage id=\"" + questionId + "\">"
323
					+ "<entity id=\"" + questionId + "\">"
324
						+ "<attribute name=\"qId\">"
325
							+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
326
						+ "</attribute>"
327
						+ "<attribute name=\"qTitle\">"
328
							+ "<pathexpr label=\"qTitle\">//assessment/section/item/@title</pathexpr>"
329
						+ "</attribute>"
330
						+ "<attribute name=\"qLabel\">"
331
							+ "<pathexpr label=\"qLabel\">//assessment/section/item/presentation/@label</pathexpr>"
332
						+ "</attribute>"
333
						+ "<attribute name=\"qPrompt\">"
334
							+ "<pathexpr label=\"qPrompt\">(//assessment/section/item/presentation/flow/response_lid/render_choice/material/mattext)|(//assessment/section/item/presentation/flow/response_str/material/mattext)|(//assessment/section/item/presentation/flow/material/mattext)</pathexpr>"
335
						+ "</attribute>";
336
				//control multiple choices
337
				for (var p = 1; p <= questionChoiceCount; p++) {
338
					tempQuery +=
339
						"<attribute name=\"qChoice_" + p + "\">"
340
							+ "<pathexpr label=\"qChoice\">//assessment/section/item/presentation/flow/response_lid/render_choice/flow_label/response_label/material/mattext</pathexpr>"
341
						+ "</attribute>";
342
				}
343
				//control multiple metadata fields
344
				for (var q = 1; q <= questionMetadataCount; q++) {
345
					tempQuery +=
346
						"<attribute name=\"qMetadataVocabulary_" + q +"\">"
347
							+ "<pathexpr label=\"qMetadataVocabulary\">//assessment/section/item/itemmetadata/qtimetadata/vocabulary</pathexpr>"
348
						+ "</attribute>"
349
						+ "<attribute name=\"qMetadataValue_" + q +"\">"
350
							+ "<pathexpr label=\"qMetadataValue\">//assessment/section/item/itemmetadata/qtimetadata/qtimetadatafield/fieldentry</pathexpr>"
351
						+ "</attribute>";
352
				}
353
				tempQuery +=
354
					"</entity>"
355
				+ "</datapackage>";
356
			}
357
				
358
			tempQuery += "</selection>";
359
			
360
			//join to the question "table"
361
			if (questionId.length > 0) {
362
				tempQuery +=
363
					"<where>"
364
						+ "<condition type=\"join\">"
365
							+ "<left>"
366
								+ "<datapackage id=\"" + docId + "\">"
367
									+ "<entity index=\"0\">"
368
										+ "<attribute index=\"1\"/>"
369
									+ "</entity>"
370
								+ "</datapackage>"
371
							+ "</left>"
372
							+ "<operator>=</operator>"
373
							+ "<right>"
374
								+ "<datapackage id=\"" + questionId + "\">"
375
									+ "<entity id=\"" + questionId + "\">"
376
										+ "<attribute name=\"qId\">"
377
											+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
378
										+ "</attribute>"
379
									+ "</entity>"
380
								+ "</datapackage>"
381
							+ "</right>"
382
						+ "</condition>"
383
					+ "</where>";
384
			}
385
			
386
			tempQuery += "</query>";
387
		
388
		} // for questionId loop
389
		
390
	
391
	} //for docObjs loop
392
				 
393
	tempQuery +=
394
		 "</union>"	
395
		 + "</dataquery>";
396
	
397
	//alert(tempQuery);
398
             
399
    return tempQuery;
400
}
401

    
402
/**
403
*	@param searchTerms - and object (hashtable) with, pay attention now:
404
	keys are search values
405
*   values are pathexprs
406
**/
407
function generateSearchString(
408
		searchTerms,
409
		assessmentItemIds,
410
		operator, 
411
		searchAssessments, 
412
		searchAssessmentItems) {
413
	var queryString = 
414
		"<pathquery version=\"1.2\">"
415
			+"<querytitle>Web-Search</querytitle>";
416
		
417
	/** assessments **/
418
	if (searchAssessments) {
419
	
420
		queryString +=
421
			"<returndoctype>edml://ecoinformatics.org/edml</returndoctype>"
422
			
423
			//assessment fields
424
               +"<returnfield>assessment/duration</returnfield>"
425
               +"<returnfield>assessment/title</returnfield>"
426
               +"<returnfield>assessment/@id</returnfield>"
427
               +"<returnfield>dataset/dataTable/entityName</returnfield>"
428
               +"<returnfield>lom/general/title/string</returnfield>"
429
               +"<returnfield>lom/general/keyword/string</returnfield>"
430
               +"<returnfield>individualName/surName</returnfield>"
431
               +"<returnfield>organizationName</returnfield>"
432
               
433
               +"<returnfield>assessmentItems/assessmentItem/assessmentItemId</returnfield>";
434
               
435
	}
436
						
437
	/** questions **/
438
	if (searchAssessmentItems) {
439
		queryString +=
440
			"<returndoctype>http://www.imsglobal.org/xsd/ims_qtiasiv1p2</returndoctype>"
441
			
442
			//question (qti) fields
443
			+"<returnfield>item/@title</returnfield>"
444
			+"<returnfield>item/@ident</returnfield>"
445
               +"<returnfield>qtimetadata/qtimetadatafield/fieldlabel</returnfield>"
446
               +"<returnfield>qtimetadata/qtimetadatafield/fieldentry</returnfield>"
447
			//classification
448
			+"<returnfield>fieldlabel</returnfield>"
449
			+"<returnfield>fieldentry</returnfield>"
450
			+"<returnfield>objectives/material/mattext</returnfield>"
451
			//question content
452
			+"<returnfield>presentation/flow/response_lid/render_choice/material/mattext</returnfield>"
453
			+"<returnfield>response_label/@ident</returnfield>"
454
			+"<returnfield>response_label/material/mattext</returnfield>";
455
	}		
456
    
457
	// a query group for search terms
458
	var termQueryString = "";
459
    termQueryString +=
460
    	"<querygroup operator=\"" + operator + "\">";                    
461
    for (var i in searchTerms) {
462
    	var key = i;
463
    	var value = searchTerms[i];
464
    	//only if we have a value  
465
    	if (value.length > 0) {                   
466
    		termQueryString +=
467
				"<queryterm searchmode=\"contains\" casesensitive=\"false\">";
468
				if (key != "anyValue") {
469
					termQueryString += "<pathexpr>" + key + "</pathexpr>";
470
				}	
471
				termQueryString += "<value>" + value + "</value>";
472
				termQueryString +="</queryterm>";
473
		}
474
    }
475
    termQueryString += "</querygroup>";
476
	
477
	// querygroup for assessmentItemIds
478
	var itemQueryString = "";
479
	if (assessmentItemIds) {
480
		//the pathexpr
481
		var pathexpr = "assessmentItems/assessmentItem/assessmentItemId";
482
		// a query group for search terms
483
		itemQueryString +=
484
	    	"<querygroup operator=\"" + "UNION" + "\">";
485
	    for (var i in assessmentItemIds) {
486
	    	var itemId = assessmentItemIds[i];
487
	    	//only if we have a value  
488
	    	if (itemId.length > 0) {                   
489
	    		itemQueryString +=
490
					"<queryterm searchmode=\"contains\" casesensitive=\"false\">";
491
					if (key != "anyValue") {
492
						itemQueryString += "<pathexpr>" + pathexpr + "</pathexpr>";
493
					}	
494
					itemQueryString += "<value>" + itemId + "</value>";
495
					itemQueryString +="</queryterm>";
496
			}
497
	    }
498
	    itemQueryString += "</querygroup>";
499
	}
500
	
501
	//combine the various conditions
502
	queryString +=
503
    	"<querygroup operator=\"" + "UNION" + "\">";
504
	queryString += termQueryString;
505
	queryString += itemQueryString;
506
	queryString += "</querygroup>";
507
	
508
	//end the pathquery
509
	queryString += "</pathquery>";
510
	
511
	return queryString;
512
				
513
}
514

    
515

    
516
function generateAssessmentSearchString(assessmentItemId) {
517
	var query = 
518
		"<pathquery version='1.2'>"
519
		     +"<querytitle>Containing-Assessment-Search</querytitle>"
520
		     
521
		     +"<returndoctype>edml://ecoinformatics.org/edml</returndoctype>"
522
		                           
523
		     +"<returnfield>assessment/duration</returnfield>"
524
             +"<returnfield>assessment/title</returnfield>"
525
             +"<returnfield>assessment/@id</returnfield>"
526
             +"<returnfield>dataset/dataTable/entityName</returnfield>"
527
             +"<returnfield>lom/general/title/string</returnfield>"
528
             +"<returnfield>lom/general/keyword/string</returnfield>"
529
             +"<returnfield>individualName/surName</returnfield>"
530
             +"<returnfield>organizationName</returnfield>"
531
                      
532
             +"<returnfield>assessmentItems/assessmentItem/assessmentItemId</returnfield>";
533
                      
534
		    if (assessmentItemId.length > 0) {
535
			    query += "<querygroup operator='UNION'>";
536
			    
537
			    //add the assessmentId if included
538
			    query +=
539
			     		"<queryterm searchmode='contains' casesensitive='false'>"
540
			                  +"<value>"
541
			                  + assessmentItemId
542
			                  +"</value>"
543
			                  +"<pathexpr>assessmentItemId</pathexpr>"
544
			          +"</queryterm>";
545
				
546
			    //close the query group      
547
			     query +=
548
			     "</querygroup>";
549
			}
550
			     
551
		query += "</pathquery>";
552
		
553
	return query;	
554
}
555

    
556
function generateAssessmentListString(assessmentIds) {
557
	var query = 
558
		"<pathquery version='1.2'>"
559
		     +"<querytitle>Assessment-List</querytitle>"
560
		     
561
		     +"<returndoctype>edml://ecoinformatics.org/edml</returndoctype>"
562
		                           
563
                      +"<returnfield>assessment/title</returnfield>"
564
                      +"<returnfield>assessment/@id</returnfield>"
565
                      +"<returnfield>assessment/duration</returnfield>"
566
           			  +"<returnfield>dataset/dataTable/entityName</returnfield>"
567
                      +"<returnfield>lom/general/title/string</returnfield>"
568
                      +"<returnfield>lom/general/keyword/string</returnfield>"
569
                      +"<returnfield>individualName/surName</returnfield>"
570
                      +"<returnfield>organizationName</returnfield>"
571
                      
572
                      +"<returnfield>assessmentItems/assessmentItem/assessmentItemId</returnfield>";
573
                      
574
		    if (assessmentIds.length > 0) {
575
			    query += "<querygroup operator='UNION'>";
576
			    
577
			    //add the assessmentId if included
578
			    for (var i=0; i < assessmentIds.length; i++) {
579
				    query +=
580
				     		"<queryterm searchmode='equals' casesensitive='false'>"
581
				                  +"<value>"
582
				                  + assessmentIds[i]
583
				                  +"</value>"
584
				                  +"<pathexpr>@packageId</pathexpr>"
585
				          +"</queryterm>";
586
				}
587
			    //close the query group      
588
			     query +=
589
			     "</querygroup>";
590
			}
591
			     
592
		query += "</pathquery>";
593
		
594
	return query;	
595
}
596

    
597
function callAjax(metacatURL, myQuery, qfmt, divId, callback) {
598
														
599
	//alert("calling ajax: " + metacatURL);
600
	//alert("myQuery: " + myQuery);
601
	
602
	//var myRequest = new Ajax.Request(
603
	var myUpdate = new Ajax.Updater(
604
		divId,
605
		metacatURL,
606
		{	method: 'post',
607
			parameters: { action: 'squery', qformat: qfmt, query: myQuery},
608
			evalScripts: true, 
609
			onComplete: callback,
610
			onFailure: function(transport) {alert('failure making ajax call');}
611
		 });
612
		 
613
	 //alert("done calling ajax");
614
}
615
function getIframeDocument(iframeId) {
616
	//look up the document
617
	var iframe = window.document.getElementById(iframeId);
618
	var doc = null;
619
	if (iframe.contentDocument) {
620
		doc = iframe.contentDocument;
621
	}
622
	else {
623
		doc = iframe.contentWindow.document;
624
	}
625
	return doc;
626
}
(22-22/22)