Skip to content

Commit

Permalink
Add revision to experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas CARPi committed Mar 26, 2014
1 parent 2c183a2 commit dbbf0ce
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 3 deletions.
9 changes: 9 additions & 0 deletions editXP-exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@
'id' => $id
));

// we add a revision to the revision table
$sql = "INSERT INTO experiments_revisions (exp_id, body, userid) VALUES(:exp_id, :body, :userid)";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'exp_id' => $id,
'body' => $body,
'userid' => $_SESSION['userid']
));


// Check if insertion is successful
if ($result) {
Expand Down
14 changes: 14 additions & 0 deletions inc/editXP.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@
<option id='option_user' value="user">Only me</option>
</select>
<span id='visibility_msg_div'>Updated !</span>
<br>

<img src='themes/<?php echo $_SESSION['prefs']['theme'];?>/img/revisions.png'> <h4>Revisions</h4>
<?php
// get the list of revisions
$sql = "SELECT COUNT(id) FROM experiments_revisions WHERE exp_id = :exp_id AND userid = :userid ORDER BY savedate DESC";
$req = $pdo->prepare($sql);
$req->execute(array(
'exp_id' => $id,
'userid' => $_SESSION['userid']
));
$rev_count = $req->fetch();
echo $rev_count[0]." <a href='revision.php?exp_id=".$id."'>Browse</a>";
?>

</section>

Expand Down
16 changes: 15 additions & 1 deletion install/elabftw.sql
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ CREATE TABLE IF NOT EXISTS `experiments_templates` (

-- --------------------------------------------------------

--
-- Table structure for table `experiments_revisions`
--

CREATE TABLE IF NOT EXISTS `experiments_revisions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`exp_id` int(10) unsigned NOT NULL,
`body` text NOT NULL,
`savedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`userid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------
--
-- Table structure for table `items`
--
Expand Down Expand Up @@ -259,4 +273,4 @@ INSERT INTO `experiments_templates` (`body`, `name`, `userid`) VALUES
<p>&nbsp;</p>
<p><span style=\"font-size: 14pt;\"><strong>Procedure :</strong></span></p>
<p>&nbsp;</p>
<p><span style=\"font-size: 14pt;\"><strong>Results :</strong></span></p>', 'default', 0);
<p><span style=\"font-size: 14pt;\"><strong>Results :</strong></span></p><p>&nbsp;</p>', 'default', 0);
10 changes: 10 additions & 0 deletions quicksave.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
// SQL for quicksave
// we do a usercheck for experiments
if ($_POST['type'] == 'experiments') {
// we update the real experiment
$sql = "UPDATE experiments
SET title = :title, date = :date, body = :body
WHERE userid = :userid
Expand All @@ -83,6 +84,15 @@
'id' => $id
));

// we add a revision to the revision table
$sql = "INSERT INTO experiments_revisions (exp_id, body, userid) VALUES(:exp_id, :body, :userid)";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'exp_id' => $id,
'body' => $body,
'userid' => $_SESSION['userid']
));

} elseif ($_POST['type'] == 'items') {
$sql = "UPDATE items
SET title = :title, date = :date, body = :body
Expand Down
91 changes: 91 additions & 0 deletions revision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/********************************************************************************
* *
* Copyright 2012 Nicolas CARPi (nicolas.carpi@gmail.com) *
* http://www.elabftw.net/ *
* *
********************************************************************************/

/********************************************************************************
* This file is part of eLabFTW. *
* *
* eLabFTW is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* eLabFTW is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public *
* License along with eLabFTW. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
$page_title = 'Revisions';
require_once 'inc/head.php';
require_once 'inc/common.php';
require_once 'inc/menu.php';

if (isset($_GET['exp_id']) && !empty($_GET['exp_id']) && is_pos_int($_GET['exp_id'])) {
$exp_id = $_GET['exp_id'];
} else {
die('Bad experiment id.');
}

if (isset($_GET['action']) && $_GET['action'] === 'restore' && is_pos_int($_GET['rev_id'])) {
// get the body of the restored time
$sql = "SELECT body FROM experiments_revisions WHERE id = :rev_id";
$req = $pdo->prepare($sql);
$req->bindParam(':rev_id', $_GET['rev_id'], PDO::PARAM_INT);
$req->execute();
$revision = $req->fetch();

// we don't update if the experiment is locked
// first check if it's locked
$sql = "SELECT locked FROM experiments WHERE id = :exp_id";
$req = $pdo->prepare($sql);
$req->bindParam(':exp_id', $exp_id, PDO::PARAM_INT);
$req->execute();
$locked = $req->fetch();
if ($locked['locked'] == 1) {
$message = "You cannot restore a revision of a locked experiment !";
display_message('error', $message);
require_once 'inc/footer.php';
die();
}

// experiment is not locked, we can continue
// sql to update the body of the experiment with the restored one
$sql = "UPDATE experiments SET body = :body WHERE id = :exp_id";
$req = $pdo->prepare($sql);
$req->bindParam(':body', $revision['body']);
$req->bindParam(':exp_id', $exp_id, PDO::PARAM_INT);
$req->execute();
}

// Get the currently stored body
$sql = "SELECT * FROM experiments
WHERE id = :id";
$req = $pdo->prepare($sql);
$req->bindParam(':id', $exp_id, PDO::PARAM_INT);
$req->execute();
$experiment = $req->fetch();
echo "<div class='item'>Current :<br>".$experiment['body']."</div>";

// Get list of revisions
$sql = "SELECT * FROM experiments_revisions WHERE exp_id = :exp_id AND userid = :userid ORDER BY savedate DESC";
$req = $pdo->prepare($sql);
$req->execute(array(
'exp_id' => $exp_id,
'userid' => $_SESSION['userid']
));
while($revisions = $req->fetch()) {
echo "<div class='item'>Saved on : ".$revisions['savedate']." <a href='revision.php?exp_id=".$exp_id."&action=restore&rev_id=".$revisions['id']."'>Restore</a><br>";
echo $revisions['body']."</div>";
}
?>

<?php
require_once 'inc/footer.php';
Binary file added themes/default/img/revisions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added themes/l33t/img/revisions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 36 additions & 2 deletions update.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ function add_field($table, $field, $params, $added, $not_added) {
$req = $pdo->prepare($create_sql);
$result1 = $req->execute();

// Populate config table
// Populate status table
$sql = "INSERT INTO status (name, color, is_default) VALUES
('Running', '0000FF', true),
('Success', '00ac00', false),
Expand Down Expand Up @@ -571,8 +571,42 @@ function add_field($table, $field, $params, $added, $not_added) {
<p>&nbsp;</p>
<p><span style=\"font-size: 14pt;\"><strong>Procedure :</strong></span></p>
<p>&nbsp;</p>
<p><span style=\"font-size: 14pt;\"><strong>Results :</strong></span></p>', 'default', 0)";
<p><span style=\"font-size: 14pt;\"><strong>Results :</strong></span></p><p>&nbsp;</p>', 'default', 0)";
$req = $pdo->prepare($sql);
$req->execute();
echo ">>> There is now a default experiment template editable by admin.\n";
}

// CREATE table experiments_revisions
$sql = "SHOW TABLES";
$req = $pdo->prepare($sql);
$req->execute();
$table_is_here = false;
while ($show = $req->fetch()) {
if (in_array('experiments_revisions', $show)) {
$table_is_here = true;
}
}

if (!$table_is_here) {
$create_sql = "CREATE TABLE IF NOT EXISTS `experiments_revisions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`exp_id` int(10) unsigned NOT NULL,
`body` text NOT NULL,
`savedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`userid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$req = $pdo->prepare($create_sql);
$result = $req->execute();


if($result) {
echo ">>> There is now a revision system for experiments.\n";
} else {
die($die_msg);
}

} else {
echo "Table 'experiments_revisions' already exists. Nothing to do.\n";
}

0 comments on commit dbbf0ce

Please sign in to comment.