- Timestamp:
- 2013-05-07T19:32:42+12:00 (11 years ago)
- Location:
- main/trunk/greenstone2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/macros/style.dm
r27310 r27313 264 264 doc_id = doc_id.substring(0, period); 265 265 \} 266 267 var metapos=0; // counter variable 268 var username = null; 269 var timestamp = null; 270 var comment = null; 271 272 var comments_arraymap = []; // array of comment records with timestamp, username and comment fields 273 274 // First retrieve all the usercomments (usernames, associated usertimestamps and usercomments) 275 // from the archives folder, where it is arranged easier than in index because it's in sequence already. 276 277 username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 278 279 // Keep looping, retrieving username, comment and timestamp 280 // Store each triplet in the comments array. Stop when there's no further usernames in archives 281 282 while(username) \{ //&& !username.indexOf("ERROR") == -1) \{ 283 284 var errorIndex = username.indexOf("ERROR"); 285 if(errorIndex != -1) \{ 286 var endIndex = username.indexOf("("); 287 var error = username.substring(errorIndex,endIndex); 288 var errormessage=document.createTextNode("Error retrieving comments. " + error); 289 usercommentdiv.appendChild(errormessage); 290 //alert(username); 291 break; 292 \} 293 294 timestamp = gsapi.getDocumentMetadata(doc_id, "usertimestamp", metapos); 295 comment = gsapi.getDocumentMetadata(doc_id, "usercomment", metapos); 296 297 //alert("username: " + username + "\\ntime: " + timestamp + "\\ncomment: " + comment); 298 299 // array of records (arraymap) 300 // http://stackoverflow.com/questions/8190658/sorting-objects-in-a-javascript-array-by-a-key-value 301 // http://stackoverflow.com/questions/4969121/in-javascript-is-there-an-easy-way-to-sort-key-value-pairs-by-the-value-and-re 302 // http://stackoverflow.com/questions/6298169/how-to-create-a-map-object-in-a-javascript 303 304 // object with member vars t, u and c. 305 var comment_record = \{ 306 t: timestamp, 307 u: username, 308 c: comment 309 \}; 310 311 comments_arraymap.push(comment_record); 312 313 metapos++; 314 username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 266 267 var username_rec = \{ 268 metaname: "username", 269 metapos: "all" 270 \}; 271 272 var timestamp_rec = \{ 273 metaname: "usertimestamp", 274 metapos: "all" 275 \}; 276 277 var comment_rec = \{ 278 metaname: "usercomment", 279 metapos: "all" 280 \}; 281 282 var doc_rec = \{ 283 docid: doc_id, 284 metatable: [username_rec, timestamp_rec, comment_rec] 285 \}; 286 287 var docArray = [doc_rec]; 288 //alert(JSON.stringify(docArray)); 289 290 var json_result_str = gsapi.getMetadataArray(docArray, "index"); 291 // alert(json_result_str); 292 var result = JSON.parse(json_result_str); 293 // result contains only one docrec (result[0]), since we asked for the usercomments of one docid 294 var metatable = result[0].metatable; 295 // alert(JSON.stringify(metatable)); 296 297 var i = 0; 298 var looping = true; 299 var print_heading = true; 300 301 // metatable[0] = list of usernames, metatable[1] = list of timestamps, metatable[2] = list of comments 302 // the 3 lists/arrays should be of even length. Assuming this, loop as long as there's another username 303 while(looping) \{ 304 var metaval_rec = metatable[0].metavals[i]; 305 if(metaval_rec == undefined) \{ 306 looping = false; 307 \} 308 else \{ 309 310 if(print_heading) \{ 311 var heading=document.createElement("div"); 312 var attr=document.createAttribute("class"); 313 attr.nodeValue="usercommentheading"; 314 heading.setAttributeNode(attr); 315 var txt=document.createTextNode("_text-usercomments-section_"); 316 heading.appendChild(txt); 317 usercommentdiv.appendChild(heading); 318 319 print_heading = false; 320 \} 321 322 var username = metaval_rec.metavalue; 323 var timestamp = metatable[1].metavals[i].metavalue; 324 var comment = metatable[2].metavals[i].metavalue; 325 326 //alert("Comment: " + username + " " + timestamp + " " + comment); 327 328 // No need to sort by time, as the meta are already stored sorted 329 // and hence retrieved in the right order by using the i (metapos) counter 330 // If sorting the array of comment records, which would be by timestamp, see 331 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort 332 333 // for each usercomment, create a child div with the username, timestamp and comment 334 displayInUserCommentList(usercommentdiv, username, timestamp, comment); 335 336 i++; 337 \} 315 338 \} 316 339 317 // No need to sort by time,318 // as the array is already stored sorted and hence retrieved in the right order by using metapos counter319 // If sorting the array of comment records, which would be by timestamp, see320 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort321 322 //comments_arraymap.sort(function(rec1, rec2) \{323 // return rec1.t-rec2.t; // sort by timestamp324 //\});325 326 //alert("Found " + metapos + " number of comments in the archives folder for doc " + doc_id);327 328 // create the output HTML for all comments in one go now329 // (we do this here for all values together to hopefully avoid reloading the page for each comment)330 331 if(metapos > 0) \{332 var heading=document.createElement("div");333 var attr=document.createAttribute("class");334 attr.nodeValue="usercommentheading";335 heading.setAttributeNode(attr);336 var txt=document.createTextNode("_text-usercomments-section_");337 heading.appendChild(txt);338 usercommentdiv.appendChild(heading);339 \}340 341 for(var i = 0; i < metapos; i++) \{342 var comment_record = comments_arraymap[i];343 344 username = comment_record.u;345 timestamp = comment_record.t;346 comment = comment_record.c;347 348 // for each usercomment, create a child div with the username, timestamp and comment349 displayInUserCommentList(usercommentdiv, username, timestamp, comment);350 \}351 352 353 340 \} 341 354 342 355 343 function displayInUserCommentList(usercommentdiv, username, timestamp, comment) \{ -
main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
r27312 r27313 270 270 271 271 # takes a JSON string and returns a JSON string 272 # resulting string is of the form: 273 # [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}] 272 274 sub _get_index_metadata_array 273 275 { … … 304 306 $json_result_str .= ","; 305 307 } 306 $json_result_str = $json_result_str . "{ docid:\"" . $docid . "\"";308 $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\""; 307 309 308 310 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong 309 $json_result_str = $json_result_str . ", metatable:[";311 $json_result_str = $json_result_str . ",\"metatable\":["; 310 312 311 313 my $first_rec = 1; … … 318 320 319 321 my $metaname = $metatable_rec->{'metaname'}; 320 $json_result_str .= "{ metaname:\"$metaname\",metavals:[";322 $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":["; 321 323 322 324 my $metapos = $metatable_rec->{'metapos'}; # 0... 1|all|undefined 323 325 if(!defined $metapos) { 324 $metapos = "all";326 $metapos = 0; 325 327 } 326 328 … … 344 346 345 347 $metavalue = $doc_rec->{$metaname}->[$metapos]; 346 $json_result_str .= "{ metapos:\"$metapos\",metavalue:\"$metavalue\"}";348 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}"; 347 349 348 350 } else { … … 358 360 } 359 361 360 $json_result_str .= "{ metapos:\"$metapos\",metavalue:\"$metavalue\"}";362 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}"; 361 363 362 364 $metapos++; -
main/trunk/greenstone2/web/script/gsajaxapi.js
r27281 r27313 271 271 } 272 272 273 this.getMetadataArray = function(docArray,where) 274 { 275 docArrayJSON = JSON.stringify(docArray); 276 277 var mdserver = this.metadataserverURL(); 278 279 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array"; 280 if(where != null) { 281 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta 282 //} else { 283 // params += "&where=import|archives|index"; 284 } 285 params += "&c="+escape(collect_); 286 params += "&json="+escape(docArrayJSON); 287 288 //this.urlGetSync(mdserver + "?" + params); 289 return this.urlPostSync(mdserver,params); 290 } 291 292 273 293 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode) 274 294 {
Note:
See TracChangeset
for help on using the changeset viewer.