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 order=\"true\">";
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
				//at least one metadata field
344
				tempQuery +=
345
					"<attribute name=\"qMetadataVocabulary\">"
346
						+ "<pathexpr label=\"qMetadataVocabulary\">//assessment/section/item/itemmetadata/qtimetadata/vocabulary</pathexpr>"
347
					+ "</attribute>"
348
					+ "<attribute name=\"qMetadataValue\">"
349
						+ "<pathexpr label=\"qMetadataValue\">//assessment/section/item/itemmetadata/qtimetadata/qtimetadatafield/fieldentry</pathexpr>"
350
					+ "</attribute>";
351
				//control multiple metadata fields
352
				for (var q = 2; q <= questionMetadataCount; q++) {
353
					tempQuery +=
354
						"<attribute name=\"qMetadataVocabulary_" + q +"\">"
355
							+ "<pathexpr label=\"qMetadataVocabulary\">//assessment/section/item/itemmetadata/qtimetadata/vocabulary</pathexpr>"
356
						+ "</attribute>"
357
						+ "<attribute name=\"qMetadataValue_" + q +"\">"
358
							+ "<pathexpr label=\"qMetadataValue\">//assessment/section/item/itemmetadata/qtimetadata/qtimetadatafield/fieldentry</pathexpr>"
359
						+ "</attribute>";
360
				}
361
				tempQuery +=
362
					"</entity>"
363
				+ "</datapackage>";
364
			}
365
				
366
			tempQuery += "</selection>";
367
			
368
			//join to the question "table"
369
			if (questionId.length > 0) {
370
				tempQuery +=
371
					"<where>"
372
						+ "<condition type=\"join\">"
373
							+ "<left>"
374
								+ "<datapackage id=\"" + docId + "\">"
375
									+ "<entity index=\"0\">"
376
										+ "<attribute index=\"1\"/>"
377
									+ "</entity>"
378
								+ "</datapackage>"
379
							+ "</left>"
380
							+ "<operator>=</operator>"
381
							+ "<right>"
382
								+ "<datapackage id=\"" + questionId + "\">"
383
									+ "<entity id=\"" + questionId + "\">"
384
										+ "<attribute name=\"qId\">"
385
											+ "<pathexpr label=\"qId\">//assessment/section/item/@ident</pathexpr>"
386
										+ "</attribute>"
387
									+ "</entity>"
388
								+ "</datapackage>"
389
							+ "</right>"
390
						+ "</condition>"
391
					+ "</where>";
392
			}
393
			
394
			tempQuery += "</query>";
395
		
396
		} // for questionId loop
397
		
398
	
399
	} //for docObjs loop
400
				 
401
	tempQuery +=
402
		 "</union>"	
403
		 + "</dataquery>";
404
	
405
	//alert(tempQuery);
406
             
407
    return tempQuery;
408
}
409

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

    
523

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

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

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