template_defs.php 59.7 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
#
8 9
# The template class is really just a wrapper around the DB data, plus
# some access routines and printing functions. 
10
#
11
class Template
12
{
13
    var	$template;
14
    var $experiment;
15
    
16 17 18 19 20 21 22
    function Template($guid, $vers) {
	$guid = addslashes($guid);
	$vers = addslashes($vers);
	
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");
23

24 25 26 27 28
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return;
	}
	$this->template = mysql_fetch_array($query_result);
29 30

	# 
31
	# Underlying experiment for easy access.
32 33 34 35
	#
	$pid = $this->pid();
	$eid = $this->eid();

36
	if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
37 38 39
	    $this->template = NULL;
	    return;
	}
40
	$this->experiment = $experiment;
41 42
    }

43 44 45
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->template);
46
    }
47

48 49 50
    # Do class level lookup.
    function Lookup($guid, $vers) {
	$foo = new Template($guid, $vers);
51

52 53 54 55 56 57 58
	if ($foo->IsValid())
	    return $foo;
	return null;
    }
    # Do class level lookup for the root template.
    function LookupRoot($guid) {
	$foo = new Template($guid, 1); 
59

60 61 62
	if ($foo->IsValid())
	    return $foo;
	return null;
63
    }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    # Look up by pid,eid is which also unique across templates.
    function LookupbyEid($pid, $eid) {
	$query_result =
	    DBQueryWarn("select guid,vers from experiment_templates  ".
			"where pid='$pid' and eid='$eid'");

	if (!$query_result || !mysql_num_rows($query_result))
	    return null;
	
	$row = mysql_fetch_array($query_result);
	$guid = $row['guid'];
	$vers = $row['vers'];
	
	$foo = new Template($guid, $vers); 

	if ($foo->IsValid())
	    return $foo;
	return null;
    }
83 84

    #
85 86 87
    # Refresh a template instance by reloading from the DB.
    #
    function Refresh() {
88
	if (! $this->IsValid())
89
	    return -1;
90

91 92
	$guid = $this->guid();
	$vers = $this->vers();
93
    
94 95 96 97 98 99 100 101 102
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return -1;
	}
	$this->template = mysql_fetch_array($query_result);
103 104 105
	return 0;
    }

106 107 108
    # accessors
    function guid() {
	return (is_null($this->template) ? -1 : $this->template['guid']);
109
    }
110 111
    function vers() {
	return (is_null($this->template) ? -1 : $this->template['vers']);
112
    }
113 114
    function pid() {
	return (is_null($this->template) ? -1 : $this->template['pid']);
115
    }
116 117
    function gid() {
	return (is_null($this->template) ? -1 : $this->template['gid']);
118
    }
119 120
    function eid() {
	return (is_null($this->template) ? -1 : $this->template['eid']);
121
    }
122 123
    function tid() {
	return (is_null($this->template) ? -1 : $this->template['tid']);
124
    }
125 126
    function uid() {
	return (is_null($this->template) ? -1 : $this->template['uid']);
127
    }
128
    function path() {
129 130 131 132
	if (!$this->experiment)
	    return -1;
	$experiment = $this->experiment;
	return $experiment->path();
133
    }
134 135
    function IsHidden() {
	return (is_null($this->template) ? -1 : $this->template['hidden']);
136
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137 138 139
    function IsActive() {
	return (is_null($this->template) ? -1 : $this->template['active']);
    }
140 141 142 143 144 145 146 147 148 149 150 151
    function created() {
	return (is_null($this->template) ? -1 : $this->template['created']);
    }
    function description() {
	return (is_null($this->template) ? -1 :
		$this->template['description']);
    }
    function parent_guid() {
	return (is_null($this->template) ? -1 :$this->template['parent_guid']);
    }
    function parent_vers() {
	return (is_null($this->template) ? -1 :$this->template['parent_vers']);
152
    }
153 154 155
    function child_vers() {
	return (is_null($this->template) ? -1 :$this->template['child_vers']);
    }
156

157 158 159 160
    # The root template has no parent guid.
    function IsRoot() {
	if (is_null($this->template))
	    return -1;
161

162
	return is_null($this->template['parent_guid']);
163 164
    }

165 166 167 168 169 170 171 172 173 174 175 176 177
    # Return the underlying experiment for this template.
    function GetExperiment() {
	return $this->experiment;
    }

    # Return the unixgid for operating on this template.
    function UnixGID() {
	$experiment = $this->experiment;

	return $experiment->UnixGID();
    }

    function AccessCheck($user, $access_type) {
178 179 180 181 182 183 184 185 186 187
	global $TB_EXPT_READINFO;
	global $TB_EXPT_MODIFY;
	global $TB_EXPT_DESTROY;
	global $TB_EXPT_UPDATE;
	global $TB_EXPT_MIN;
	global $TB_EXPT_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_PROJROOT;
188
	$mintrust = $TB_EXPT_READINFO;
189 190 191 192 193

	if ($access_type < $TB_EXPT_MIN ||
	    $access_type > $TB_EXPT_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}
194

195 196 197 198 199 200 201 202
        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}
	$pid = $this->pid();
	$gid = $this->gid();
203
	$uid = $user->uid();
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	if ($access_type == $TB_EXPT_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

        #
        # Either proper permission in the group, or group_root in the project.
        # This lets group_roots muck with other peoples experiments, including
        # those in groups they do not belong to.
        #
	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
219
    }
220

221
    #
222
    # Display a template in its own table.
223
    #
224 225 226 227 228 229 230 231 232
    function Show() {
	$guid        = $this->guid();
	$vers        = $this->vers();
	$pid         = $this->pid();
	$gid         = $this->gid();
	$uid         = $this->uid();
	$tid         = $this->tid();
	$created     = $this->created();
	$description = $this->description();
233
	$path        = $this->path();
234

235 236 237 238 239
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
	
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        #
        # We need the metadata guid/version for the TID and description since
        # they are mungable metadata.
        #
	$tid_metadata  = $this->LookupMetadataByName("TID");
	$desc_metadata = $this->LookupMetadataByName("description");
	
	if ($tid_metadata == NULL) {
	    TBERROR("Could not find Metadata 'TID' for $guid/$vers", 1);
	}
	if ($desc_metadata == NULL) {
	    TBERROR("Could not find Metadata 'description' for $guid/$vers",1);
	}
	$tid_guid  = $tid_metadata->guid();
	$tid_vers  = $tid_metadata->vers();
	$desc_guid = $desc_metadata->guid();
	$desc_vers = $desc_metadata->vers();
257
    
258 259 260 261 262 263 264 265
        #
        # Generate the table.
        #
	echo "<center>
               <h3>Details</h3>
              </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
266
    
267
	ShowItem("GUID",
268
		 MakeLink("template",
269 270 271
			  "guid=$guid&version=$vers", "$guid/$vers"));
	
	ShowItem("ID",
272 273 274
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${tid_guid}/${tid_vers}", $tid));
275 276 277
	
	ShowItem("Project", MakeLink("project", "pid=$pid", $pid));
	ShowItem("Group",   $gid);
278
	ShowItem("Creator", MakeAnchor($showuser_url, $uid));
279
	ShowItem("Created", $created);
280

281 282 283
	echo "<tr><td align=center colspan=2>Datastore Directory</td></tr>\n";
	echo "<tr><td align=center colspan=2>$path</td></tr>\n";

284 285 286 287
	$onmouseover = MakeMouseOver($description);
	if (strlen($description) > 40) {
	    $description = substr($description, 0, 40) . " <b>... </b>";
	}
288 289 290 291 292
	ShowItem("Description",
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${desc_guid}/${desc_vers}",
			    $description, $onmouseover));
293
    
294 295 296 297 298 299 300 301 302 303 304 305
	if (! $this->IsRoot()) {
	    $parent_guid = $this->parent_guid();
	    $parent_vers = $this->parent_vers();
	    
	    ShowItem("Parent Template",
		     MakeLink("template",
			      "guid=$parent_guid&version=$parent_vers",
			      "$parent_guid/$parent_vers"));
	}
	echo "</table>\n";
    }

306 307 308 309 310 311 312 313 314 315 316 317 318 319
    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$guid = $this->guid();
	$vers = $this->vers();
	$html = "<font size=+2>Template <b>" .
	    MakeLink("template",
		     "guid=$guid&version=$vers", "$guid/$vers") . 
	    "</b></font>";

	return $html;
    }

320 321 322 323 324 325 326 327
    #
    # Display template parameters and default values in a table
    #
    function ShowParameters() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
328 329 330 331 332 333 334 335 336 337
	    DBQueryFatal("select p.name,p.value, ".
			 "       p.metadata_guid,p.metadata_vers, ".
			 "       m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
338 339 340
	if (!$query_result ||
	    !mysql_num_rows($query_result))
	    return 0;
341 342

	echo "<center>
343
               <h3>Parameters</h3>
344 345 346 347 348 349
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Default Value</th>
350
                <th>Description</th>
351 352 353
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
354 355 356 357 358 359
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['metadata_guid'];
	    $metadata_vers = $row['metadata_vers'];
	    $description   = $row['description'];
	    
360 361 362 363 364 365
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
366 367 368
                   <td>$value</td>";

	    if (is_null($description)) {
369 370
		$url = CreateURL("template_metadata", $this);
		
371
		echo "<td>".
372 373 374
		     "<a href='${url}&action=add&".
		     "metadata_type=parameter_description&".
		     "formfields[name]=${name}'>Click to add</a></td>\n";
375 376 377 378 379 380 381
	    }
	    else {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description =
			substr($description, 0, 30) . " <b>... </b>";
		}
382 383 384 385 386
		$url = CreateURL("template_metadata", $this);
		
		echo "<td><a href='${url}&action=modify".
		     "&metadata=${metadata_guid}/${metadata_vers}' " .
		     "$onmouseover>$description</a></td>\n";
387 388 389
	    }
	    echo "</tr>\n";
	    }
390 391 392
	echo "</table>\n";
	return 1;
    }
393
	
394 395 396 397 398 399 400 401 402
    #
    # Display template metadata and values in a table
    #
    function ShowMetadata() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select i.* from experiment_template_metadata as m ".
403 404 405
		     "left join experiment_template_metadata_items as i on ".
		     "     m.metadata_guid=i.guid and m.metadata_vers=i.vers ".
		     "where m.parent_guid='$guid' and ".
406 407
		     "      m.parent_vers='$vers' and ".
		     "      m.internal=0 and m.hidden=0");
408 409 410

	if (! mysql_num_rows($query_result))
	    return 0;
411 412

	echo "<center>
413
               <h3>Metadata</h3>
414 415 416 417 418
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Edit</th>
419
                <th>Delete</th>
420 421 422 423 424 425 426 427 428
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['guid'];
	    $metadata_vers = $row['vers'];
429 430 431
	    $onmouseover   = "";
	    
	    if (!isset($value) || $value == "") {
432 433
		$value = "&nbsp";
	    }
434 435 436 437 438 439
	    else {
		$onmouseover = MakeMouseOver($value);
		
		if (strlen($value) > 40) {
		    $value = substr($value, 0, 40) . "<b> ... </b></a>";
		}
440
	    }
441

442 443 444
	    $meta_url = CreateURL("template_metadata", $this) .
		"&metadata=${metadata_guid}/${metadata_vers}";
		
445 446
	    echo "<tr>
   	           <td align=center>
447 448
                     <a href='${meta_url}&action=modify'>
                       <img border=0 alt='modify' src='greenball.gif'></A></td>
449
   	           <td align=center>
450 451
                     <a href='${meta_url}&action=delete'>
                       <img border=0 alt='delete' src='redball.gif'></A></td>
452
                   <td>$name</td>
453
                   <td $onmouseover>$value</td>
454 455 456 457 458 459
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

460 461 462 463 464 465
    #
    # Show the instance list for a template.
    #
    function ShowInstances() {
	$guid = $this->guid();
	$vers = $this->vers();
466 467

	$query_result =
468 469 470 471 472 473 474 475 476 477 478 479 480 481
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
			 "    round(minimum_nodes+.1,0) as min_nodes ".
			 "from experiment_template_instances as i ".
			 "left join experiments as e on e.idx=i.exptidx ".
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
			 "where e.pid is not null and ".
			 "      (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "group by e.pid,e.eid order by e.state,e.eid");

	if (! mysql_num_rows($query_result))
	    return;
	
482
	echo "<center>
483
               <h3>Template Instances</h3>
484 485 486 487 488 489
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
               <th>EID</th>
               <th>State</th>
490
               <th align=center>Nodes</th>
491 492 493 494 495 496 497 498 499 500 501 502 503
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid       = $row['pid'];
	    $eid       = $row['eid'];
	    $state     = $row['state'];
	    $nodes     = $row['nodes'];
	    $minnodes  = $row['min_nodes'];
	    $ignore    = $row['idle_ignore'];
	    $name      = $row['expt_name'];
504 505 506 507 508 509

	    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		TBERROR("Could not map $pid/$eid to its object", 1);
	    }
	    $idlehours = $experiment->IdleTime();
	    $stale     = $experiment->IdleStale();
510 511 512 513
	    
	    if ($nodes == 0) {
		$nodes = "<font color=green>$minnodes</font>";
	    }
514
	    elseif ($row["swap_requests"] > 0) {
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
		$nodes .= $idlemark;
	    }

	    $idlestr = $idlehours;
	    
	    if ($idlehours > 0) {
		if ($stale) {
		    $idlestr .= $stalemark;
		}
		if ($ignore) {
		    $idlestr = "($idlestr)";
		}
	    }
	    elseif ($idlehours == -1) {
		$idlestr = "&nbsp;";
	    }
	    
	    echo "<tr>
                   <td><A href='showexp.php3?pid=$pid&eid=$eid'>$eid</A></td>
  		   <td>$state</td>
                   <td align=center>$nodes</td>
                 </tr>\n";
	}
	echo "</table>\n";
    }

541 542 543 544 545 546
    #
    # Show the historical instance list for a template.
    #
    function ShowHistory($expand) {
	$guid = $this->guid();
	$vers = $this->vers();
547

548
	$query_result =
549 550 551 552 553 554
	    DBQueryFatal("select i.*,r.archive_tag ".
			 "  from experiment_template_instances as i ".
			 "left join experiment_stats as s on ".
			 "     s.exptidx=i.exptidx ".
			 "left join experiment_resources as r on ".
			 "     r.idx=s.rsrcidx ".
555 556 557 558 559 560 561
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "order by i.start_time");

	if (! mysql_num_rows($query_result))
	    return 0;
	
562
	echo "<center>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
563
               <h3>Template History (Swapins)</h3>
564 565 566 567
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
568
               <th align=center>Expand</th>
569 570 571 572
               <th>EID</th>
               <th>UID</th>
               <th>Start Time</th>
               <th>Stop Time</th>
573
               <th align=center>Show</th>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
574
               <th align=center>Archive</th>
575
               <th align=center>Export</th>
576
               <th align=center>Replay</th>
577
               <th align=center>Load DBs</th>
578 579 580 581 582 583 584 585 586 587 588
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid       = $row['pid'];
	    $eid       = $row['eid'];
	    $uid       = $row['uid'];
	    $start     = $row['start_time'];
	    $stop      = $row['stop_time'];
589
	    $exptidx   = $row['exptidx'];
590
	    $idx       = $row['idx'];
591
	    $tag       = $row['archive_tag'];
592 593 594 595

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
596 597

	    $expandit = ((isset($expand) && $expand == $idx) ? 1 : 0);
598
	    
599
	    echo "<tr>\n";
600 601 602
	    echo " <td align=center>";
	    if ($expandit) {
		echo "<a href=template_history.php".
603
                         "?guid=$guid&version=$vers>".
604 605 606 607
		         "<img border=0 alt='c' src='/icons/down.png'></a>\n";
	    }
	    else {
 		echo "<a href=template_history.php".
608
                         "?guid=$guid&version=$vers&expand=$idx#$idx>".
609 610 611 612 613
		         "<img border=0 alt='e' src='/icons/right.png'></a>\n";
	    }
	    echo " </td>
                   <td>$eid</td>\n";
	    
614 615 616 617
	    echo " <td>$uid</td>
                   <td>$start</td>
                   <td>$stop</td>\n";

618
	    echo " <td align=center>".
619
		    MakeLink("instance", "instance=$exptidx",
620
			     "<img border=0 alt='Show' src='greenball.gif'>");
621
	    echo " </td>\n";
622

623
 	    echo " <td align=center>
624
                     <a href=archive_view.php3/$exptidx/history/$tag/".
625
		        "?instance=$exptidx>
626 627
                     <img border=0 alt='i' src='greenball.gif'></a></td>";
	    
628 629
	    echo " <td align=center>".
 		    MakeLink("export",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
630
			     "instance=$exptidx",
631
			     "<img border=0 alt='Show' src='greenball.gif'>");
632 633

	    echo " <td align=center>".
634 635 636
 		    MakeLink("swapin",
			     "guid=$guid&version=$vers".
			     "&replay_instance_idx=$exptidx",
637
			     "<img border=0 alt='Show' src='greenball.gif'>");
638 639 640

	    echo " <td align=center>".
 		    MakeLink("analyze",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
641
			     "instance=$exptidx",
642
			     "<img border=0 alt='Show' src='greenball.gif'>");
643
	    echo " </td>
644
                 </tr>\n";
645 646

	    if ($expandit) {
647 648
		$instance = new TemplateInstance($exptidx);
		
649 650 651
		echo "<tr>\n";
		echo " <a NAME=$idx></a>\n";
		echo "<td>&nbsp</td>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
652
		echo " <td colspan=7>\n";
653
		$instance->ShowRunList(0);
654 655 656
		echo " </td>\n";
		echo "</tr>\n";
	    }
657 658 659
	}
	echo "</table>\n";
    }
660 661 662 663 664 665

    #
    # Dump the image map for a template to the output.
    #
    function ShowGraph() {
	$guid = $this->guid();
666
	$vers = $this->vers();
667
	# Make the link unique to force reload on the client side.
668
	$now  = time();
669 670 671 672 673 674 675 676 677 678 679 680 681

	$query_result =
	    DBQueryFatal("select imap from experiment_template_graphs ".
			 "where parent_guid='$guid'");

	if (!mysql_num_rows($query_result)) {
	    USERERROR("Experiment Template $guid is no longer in the DB!", 1);
	}
	$row  = mysql_fetch_array($query_result);
	$imap = $row['imap'];

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
682 683
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
684
	echo "<div id=\"mygraphdiv\" style='position:relative;'>\n";
685

686 687 688 689 690
	echo "<div id='CurrentTemplate' style='display: block; opacity: 1; ".
	    "visibility: hidden; ".
	    "position: absolute; z-index:1020; left: 0px; top: 0px;".
	    "height: 0px; width: 0px; border: 2px solid red;'></div>\n";

691
	echo $imap;
692 693
	echo "<img id=\"mygraphimg\" border=0 usemap=\"#TemplateGraph\" ";
	echo "      onLoad=\"setTimeout('ShowGraphInit();', 10);\" ";
694
	echo "      style='cursor: move;' ";
695
	echo "      src='template_graph.php?guid=$guid&version=1&now=$now'>\n";
696 697 698 699
	echo "</div>\n";
	echo "</div>\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713
    #
    # Dump the NS file into its own iframe.
    #
    function ShowNS() {
	$guid = $this->guid();
	$vers = $this->vers();

	echo "<center>";
	echo "<iframe width=700 height=400 scrolling=auto
                      src='spitnsdata.php3?guid=$guid&version=$vers'
                      border=2></iframe>\n";
	echo "</center>";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
714 715 716 717 718 719 720
    #
    # Dump the visualization into its own iframe.
    #
    function ShowVis($zoom = 1.25, $detail = 1) {
	$guid = $this->guid();
	$pid  = $this->pid();
	$eid  = $this->eid();
721 722
	# Make the link unique to force reload on the client side.
	$now  = time();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
723 724 725 726 727

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
728
	echo "<div id=\"myvisdiv\" style='position:relative;'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
729

730
	echo "<img id=\"myvizimg\" border=0 style='cursor: move;' ";
731
	echo "      onLoad=\"setTimeout('ShowVisInit();', 100);\" ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
732
	echo "      src='top2image.php3?pid=$pid&eid=$eid".
733
	    "&zoom=$zoom&detail=$detail&now=$now'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	echo "</div>\n";
	echo "</div>\n";
    }

    #
    # Grab the zoom and detail for the current viz picture, as a default.
    #
    function CurrentVisDetails() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select zoom,detail from vis_graphs ".
			 "where pid='$pid' and eid='$eid'");

	if (!mysql_num_rows($query_result)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
750
	    return array(1.25, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
751 752 753 754 755 756 757
	}
	$row    = mysql_fetch_array($query_result);
	$zoom   = $row['zoom'];
	$detail = $row['detail'];
	return array($zoom, $detail);
    }

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    #
    # Grab array of input files for a template, indexed by input_idx.
    #
    function InputFiles() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$input_list = array();

	$query_result =
	    DBQueryFatal("select * from experiment_template_inputs ".
			 "where parent_guid='$guid' and parent_vers='$vers'");

	while ($row = mysql_fetch_array($query_result)) {
	    $input_idx = $row['input_idx'];

	    $input_query =
		DBQueryFatal("select input ".
			     "  from experiment_template_input_data ".
			     "where idx='$input_idx'");

	    $input_row = mysql_fetch_array($input_query);
	    $input_list[] = $input_row['input'];
	}
	return $input_list;
    }

    #
    # Return number of parameters.
    #
    function ParameterCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers'");

	return mysql_num_rows($query_result);
    }

    #
    # Return number of metadata items
    #
    function MetadataCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select internal ".
			 "  from experiment_template_metadata as m ".
			 "where m.parent_guid='$guid' and ".
812 813
			 "      m.parent_vers='$vers' and ".
			 "      m.internal=0 and m.hidden=0");
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

	return mysql_num_rows($query_result);
    }

    #
    # Return number of instances
    #
    function InstanceCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select * from experiment_template_instances as i ".
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers')");

	return mysql_num_rows($query_result);
    }

    #
    # Look for a metadata item in a template, by the guid/vers of the
    # metadata. Returns a class instance (see below).
    #
    function LookupMetadataByGUID($metadata_guid, $metadata_vers) {
	return TemplateMetadata::TemplateLookupByGUID($this,
						      $metadata_guid,
						      $metadata_vers);
    }
    # Ditto by name,
    function LookupMetadataByName($metadata_name) {
	return TemplateMetadata::TemplateLookupByName($this, $metadata_name);
    }

    # Grab the graph data.
    function GraphImage(&$image) {
	$guid = $this->guid();

	$query_result =
	    DBQueryWarn("select image from experiment_template_graphs ".
			"where parent_guid='$guid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$image = $row['image'];
	return 0;
    }

    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameters(&$parameters) {
	$parameters = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
875 876
			 "      parent_vers='$vers' ".
			 "order by name");
877 878 879 880 881 882 883 884 885

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];

	    $parameters[$name] = $value;
	}
	return 0;
    }
886

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameterMouseOvers(&$mouseovers) {
	$mouseovers = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select p.name,m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
	while ($row = mysql_fetch_array($query_result)) {
	    $name	 = $row['name'];
	    $description = $row['description'];

	    if (isset($description) && $description != "") {
		$mouseovers[$name] = MakeMouseOver($description);
	    }
	}
	return 0;
    }

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
    #
    # Find next candidate for a template (modify) TID
    #
    function NextTID() {
	$tid  = $this->tid();
	$guid = $this->guid();

	$query_result =
	    DBQueryFatal("select MAX(vers) from experiment_templates ".
			 "where guid='$guid'");

	if (mysql_num_rows($query_result) == 0) {
	    return "T" . substr(md5(uniqid($foo, true)), 0, 10);
	}
	$row = mysql_fetch_array($query_result);
	$foo = $row[0] + 1;
	return "${tid}-V${foo}"; 
    }
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

    #
    # Return array of template events, ordered by time.
    #
    function EventList(&$eventlist) {
	$eventlist = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select * from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ".
			 "order by time");

	$i = 0;
	while ($row = mysql_fetch_array($query_result)) {
	    $eventlist[$i++] = $row;
	}
	return 0;
    }
    function EventCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select count(*) from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ");

	$row   = mysql_fetch_array($query_result);
	$count = $row[0];
	return $count;
    }
    function DeleteEvent($vname) {
	$guid = $this->guid();
	$vers = $this->vers();
	
	DBQueryFatal("delete from experiment_template_events ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
    function ModifyEvent($vname, $changes) {
	$guid = $this->guid();
	$vers = $this->vers();
	$sets = array();
	
	while (list ($key, $value) = each ($changes)) {
	    $value  = addslashes($value);
	    $sets[] = "$key='$value'";
	}
	
	DBQueryFatal("update experiment_template_events set ".
		     implode(",", $sets) . " ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
996 997
}

998
#
999
# This is the class for a template instance (swapin).
1000
#
1001
class TemplateInstance
1002
{
1003 1004
    var	$template;
    var $instance;
1005
    var $experiment;
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    #
    # Instances are found by their experiment index. 
    #
    function TemplateInstance($exptidx) {
	$exptidx = addslashes($exptidx);

	$query_result =
	    DBQueryFatal("select * ".
			 "  from experiment_template_instances ".
			 "where exptidx='$exptidx'");
	
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    $this->instance = NULL;
	    return;
	}
	$this->instance = mysql_fetch_array($query_result);
	$this->template = new Template($this->instance['parent_guid'],
				       $this->instance['parent_vers']);
1026
	$this->experiment = Experiment::Lookup($exptidx);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    }
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->instance);
    }

    # Do class level lookup.
    function LookupByExptidx($exptidx) {
	$foo = new TemplateInstance($exptidx);

	if ($foo->IsValid())
	    return $foo;
	return null;
    }

    # accessors
    function idx() {
	return (is_null($this->instance) ? -1 : $this->instance['idx']);
    }
    function exptidx() {
	return (is_null($this->instance) ? -1 : $this->instance['exptidx']);
    }
    function runidx() {
	return (is_null($this->instance) ? -1 : $this->instance['runidx']);
    }
    function pid() {
	return (is_null($this->instance) ? -1 : $this->instance['pid']);
    }
    function eid() {
	return (is_null($this->instance) ? -1 : $this->instance['eid']);
    }
    function uid() {
	return (is_null($this->instance) ? -1 : $this->instance['uid']);
    }
    function guid() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_guid']);
    }
    function vers() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_vers']);
    }
    function start_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['start_time']);
    }
    function stop_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['stop_time']);
    }
1078 1079 1080 1081
    function pause_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['pause_time']);
    }
1082 1083 1084
    function template() {
	return (is_null($this->instance) ? -1 : $this->template);
    }
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    function GetTemplate() {
	return $this->template;
    }
    function GetExperiment() {
	return $this->experiment;
    }

    #
    # Load the project object for an experiment.
    #
    function Project() {
	$pid = $this->pid();

	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $project;
    }
1103

1104 1105
    # Is instance actually running (current experiment).
    function Instantiated() {
1106
	return ($this->experiment ? 1 : 0);
1107 1108
    }

1109 1110 1111
    #
    # Show an instance.
    #
1112
    function Show($detailed, $withanno = 0) {
1113
	$exptidx = $this->exptidx();
1114
	$runidx  = $this->runidx();
1115 1116 1117 1118 1119 1120
	$guid    = $this->guid();
	$vers    = $this->vers();
	$pid     = $this->pid();
	$uid     = $this->uid();
	$start   = $this->start_time();
	$stop    = $this->stop_time();
1121 1122 1123
	$template= $this->template();
	$pcount  = $template->ParameterCount();

1124 1125 1126 1127
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
1128 1129 1130 1131 1132 1133

	# If the instance is swapped in, we can provide a link to
	# experiment.
	if ($this->experiment) {
	    $showexp_url  = CreateURL("showexp", $this->experiment);
	}
1134
	
1135
	echo "<center>\n";
1136 1137 1138 1139 1140 1141
	if ($detailed && $pcount) {
	    echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
		 " cellpadding=0 cellspacing=0 align=center>\n";
            echo "<tr valign=top>";
	    echo "<td class=stealth align=center>\n";
	}
1142
	echo "<center>
1143
               <h3>Instance Details</h3>
1144 1145 1146 1147 1148 1149
             </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
    
	ShowItem("Template",
		 MakeLink("template",
1150 1151
			  "guid=$guid&version=$vers", "$guid/$vers"));
	ShowItem("ID",          $exptidx);
1152 1153 1154
	if ($this->experiment) {
	    ShowItem("Experiment",  MakeAnchor($showexp_url, $this->eid()));
	}
1155
	ShowItem("Project",     MakeLink("project", "pid=$pid", $pid));
1156
	ShowItem("Creator",     MakeAnchor($showuser_url, $uid));
1157 1158
	ShowItem("Started",     $start);
	ShowItem("Stopped",     (isset($stop) ? $stop : "&nbsp"));
1159
	ShowItem("Current Run", (isset($runidx) ? $runidx : "&nbsp"));
1160 1161
	echo "</table>\n";

1162 1163 1164 1165 1166 1167 1168 1169
	if ($detailed && $pcount) {
	    echo "</td>";
	    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	    echo "<td class=stealth align=center>\n";
	    $this->ShowBindings();
	    echo "</tr>";
	    echo "</table>\n";
	}
1170

1171 1172 1173 1174
	if ($withanno) {
	    $this->ShowAnnotation();
	}

1175 1176
	if ($detailed) {
	    $this->ShowRunList(1);
1177
	}
1178

1179 1180
	echo "</center>\n";
	
1181
    }
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
1195
	    MakeLink("instance", "instance=$exptidx", $eid) .
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	    "</b></font>";
	return $html;
    }

    #
    # A variant that points to the active experiment.
    #
    function ExpPageHeader() {
	$template = $this->template();	
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$pid      = $this->pid();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
	    MakeLink("project", "pid=$pid", $pid) . "/" .
	    MakeLink("experiment", "pid=$pid&eid=$eid", $eid) . 
	    "</b></font>";

	return $html;
    }
    
    #
    # Ditto for a run, although run should its own class.
    #
    function RunPageHeader($runidx) {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $this->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();
	$runid    = $this->GetRunID($runidx);

	$html .= "<font size=+2>, Run <b>" .
1232 1233
	    MakeAnchor(CreateURL("experimentrun_show", $this,
				 "runidx", $runidx), $runid) .
1234 1235 1236
	    "</b></font>";
	return $html;
    }
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

    #
    # Show the current bindings. This depends on the state of the instance.
    # If not swapped in, the instance bindings are current. If swapped in,
    # the most recent run bindings are current (current run, or last run).
    #
    function ShowCurrentBindings() {
	global $TB_EXPTSTATE_SWAPPED;
	
	if ($this->experiment) {
	    $experiment = $this->experiment;

	    if ($experiment->state() == $TB_EXPTSTATE_SWAPPED) {
		# Show the instance bindings is swapped.
		return $this->ShowBindings();
	    }
	    elseif ($this->runidx()) {
		return $this->ShowRunBindings();
	    }
	    return $this->ShowLastRunBindings();
	}
	return $this->ShowBindings();
    }
1260 1261 1262 1263 1264 1265
    
    #
    # Display instance bindings in a table
    #
    function ShowBindings() {
	$instance_idx = $this->idx();
1266

1267 1268 1269
	$query_result =
	    DBQueryWarn("select * from experiment_template_instance_bindings ".
			"where instance_idx='$instance_idx'");
1270

1271 1272 1273 1274
	if (!mysql_num_rows($query_result))
	    return 0;

	echo "<center>
1275
               <h3>Instance Bindings</h3>
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
Kevin Atkinson's avatar
Kevin Atkinson committed
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	    $value	= $row['value'];
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
                   <td>$value</td>
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

    #
1301
    # Display run bindings in a table
Kevin Atkinson's avatar
Kevin Atkinson committed
1302
    #
1303
    function ShowRunBindings($runidx = null) {
Kevin Atkinson's avatar
Kevin Atkinson committed
1304 1305
	$exptidx      = $this->exptidx();

1306 1307 1308 1309
	if (! $runidx) {
	    $runidx = $this->runidx();
	}
	
Kevin Atkinson's avatar
Kevin Atkinson committed
1310 1311 1312 1313 1314 1315 1316 1317 1318
	$query_result =
	    DBQueryWarn("select * from experiment_run_bindings ".
			 "where exptidx='$exptidx' and runidx='$runidx' ".
			 "order by name");

	if (!mysql_num_rows($query_result))
	    return 0;

	echo "<center>
1319
               <h3>Run Bindings</h3>
Kevin Atkinson's avatar
Kevin Atkinson committed
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	    $value	= $row['value'];
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
                   <td>$value</td>
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

1344 1345 1346 1347 1348 1349 1350
    #
    # Display last run bindings in a table
    #
    function ShowLastRunBindings() {
	return $this->ShowRunBindings($this->LastRunIdx());
    }

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    #
    # Show the run list for an instance.
    #
    function ShowRunList($withheader) {
	$exptidx = $this->exptidx();
	$guid    = $this->guid();
	$vers    = $this->vers();
	
	$query_result =
	    DBQueryFatal("select * from experiment_runs ".
			 "where exptidx='$exptidx'");
	
	if (! mysql_num_rows($query_result))
	    return 0;
	
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	if ($withheader) {
	    echo "<center>
                    <h3>Instance History (Runs)</h3>
                  </center> \n";
	}
	echo "<table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
               <th>RunID</th>
               <th>ID</th>
               <th>Start Time</th>
               <th>Stop Time</th>
               <th>Description</th>
              </tr>\n";

	while ($rrow = mysql_fetch_array($query_result)) {
	    $runidx    = $rrow['idx'];
	    $runid     = $rrow['runid'];
	    $start     = $rrow['start_time'];
	    $stop      = $rrow['stop_time'];
	    $exptidx   = $rrow['exptidx'];
1387 1388
	    $start_tag = $rrow['starting_archive_tag'];
	    $end_tag   = $rrow['ending_archive_tag'];
1389
	    $description = $rrow['description'];
1390
	    $onmouseover = "";
1391 1392 1393 1394

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

	    if (isset($description) && $description != "") {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description = substr($description, 0, 30) . " <b>...</b>";
		}
	    }
	    else {
		$description = "&nbsp ";
	    }

1406
	    if (isset($end_tag) && $end_tag != "") {
1407
		$archive_link =
1408
		    "<a href=archive_view.php3".
1409
		    "/$exptidx/history/$end_tag/?instance=$exptidx>".
1410 1411 1412 1413 1414
		    "<img border=0 alt='i' src='greenball.gif'></a>";
	    }
	    else {
		$archive_link = "&nbsp ";
	    }
1415 1416 1417
	    
	    echo "<tr>\n";
	    echo " <td align=center>".
1418 1419
		MakeAnchor(CreateURL("experimentrun_show", $this,
				     "runidx", $runidx), "$runid");
1420 1421 1422 1423
	    echo " </td>
  		    <td>$runidx</td>
                    <td>$start</td>
                    <td>$stop</td>
1424
                    <td $onmouseover>$description</td>
1425 1426 1427 1428 1429
                   </tr>\n";
	}
	echo "</table>\n";
    }

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
    #
    # Get the annotation for an instance, or a link to create one.
    #
    function GetAnnotation() {
	$exptidx  = $this->exptidx();
	$annoname = "__instance_annotation_${exptidx}";
	$template = $this->template();

	if (($metadata = $template->LookupMetadataByName($annoname))) {
	    return $metadata->value();
	}
	return null;
    }
    function GetRunAnnotation($runidx) {
	$exptidx  = $this->exptidx();
	$annoname = "__experimentrun_annotation_${exptidx}_${runidx}";
	$template = $this->template();

	if (($metadata = $template->LookupMetadataByName($annoname))) {
	    return $metadata->value();
	}
	return null;
    }

    function SetAnnotation($this_user, $annotation) {
	$exptidx  = $this->exptidx();
	$annoname = "__instance_annotation_${exptidx}";
	$template = $this->template();
	$uid      = $this_user->uid();
	$foo      = rand();
	$datafile = "/tmp/${uid}-${foo}.txt";
	$guid     = $template->guid();
	$vers     = $template->vers();
	$action   = ($this->GetAnnotation() ? "modify" : "add");

	if (! ($fp = fopen($datafile, "w"))) {
	    TBERROR("Could not create temporary file $datafile", 1);
	}
	fwrite($fp, $annotation);
	fclose($fp);
	chmod($datafile, 0666);

	SUEXEC($this_user->uid(), $template->pid(),
	       "webtemplate_metadata -a $action -f $datafile -t annotation ".
	       " $annoname $guid/$vers",
	       SUEXEC_ACTION_CONTINUE);
	unlink($datafile);
	return 0;
    }
    
    function SetRunAnnotation($this_user, $runidx, $annotation) {
	$exptidx  = $this->exptidx();
	$annoname = "__experimentrun_annotation_${exptidx}_${runidx}";
	$template = $this->template();
	$uid      = $this_user->uid();
	$foo      = rand();
	$datafile = "/tmp/${uid}-${foo}.txt";
	$guid     = $template->guid();
	$vers     = $template->vers();
	$action   = ($this->GetRunAnnotation($runidx) ? "modify" : "add");

	if (! ($fp = fopen($datafile, "w"))) {
	    TBERROR("Could not create temporary file $datafile", 1);
	}
	fwrite($fp, $annotation);
	fclose($fp);
	chmod($datafile, 0666);

	SUEXEC($this_user->uid(), $template->pid(),
	       "webtemplate_metadata -a $action -f $datafile -t annotation ".
	       " $annoname $guid/$vers",
	       SUEXEC_ACTION_CONTINUE);
	unlink($datafile);
	return 0;
    }

    #
    # Display annotation in text box with button to change. 
    #
1509
    function ShowAnnotation($longform = 0) {
1510
	$annotation = $this->GetAnnotation();
1511
	$cols       = ($longform ? 25 : 5);
1512 1513 1514 1515 1516 1517 1518
	if (! $annotation) {
	    $annotation = "";
	}

	echo "<center>";
	echo "<b>Annotation</b><br>\n";
	echo "<form action='fee' method=post>\n";
1519
	echo "<textarea id='annotation' rows=$cols cols=80>$annotation".
1520 1521 1522 1523
	     "</textarea></form>";
	echo "<button name=submit type=button value=submit ".
	     "onclick=\"ModifyAnno();\">Submit Changes</button>\n";
	echo "<center>";
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	if (! $longform) {
	    echo "<script type='text/javascript' language='javascript'>
                  function ModifyAnno() {
                      textarea = getObjbyName('annotation');
                      x_ModifyAnno(textarea.value, ModifyAnno_cb);
                  }
                  function ModifyAnno_cb(val) {
                  }
                  </script>\n";
	}
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    }
    function ShowRunAnnotation($runidx) {
	$annotation = $this->GetRunAnnotation($runidx);
	if (! $annotation) {
	    $annotation = "";
	}

	echo "<center>";
	echo "<b>Annotation</b><br>\n";
	echo "<form action='fee' method=post>\n";
	echo "<textarea id='annotation' rows=5 cols=80>$annotation".
	     "</textarea></form>";
	echo "<button name=submit type=button value=submit ".
	     "onclick=\"ModifyAnno();\">Submit Changes</button>\n";
	echo "<center>";
	echo "<script type='text/javascript' language='javascript'>
              function ModifyAnno() {
                  textarea = getObjbyName('annotation');
                  x_ModifyAnno(textarea.value, ModifyAnno_cb);
              }
              function ModifyAnno_cb(val) {
              }
              </script>\n";
    }

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
    #
    # Check if a valid run.
    #
    function ValidRun($runidx) {
	$exptidx = $this->exptidx();
	$runidx  = addslashes($runidx);
	
	$query_result =
	    DBQueryFatal("select * from experiment_runs ".
			 "where exptidx='$exptidx' and idx='$runidx'");
1569

1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	return mysql_num_rows(