Skip to content

Commit

Permalink
Ajax_demo: Modified js code to follow closure, Moved Ajax check funct…
Browse files Browse the repository at this point in the history
…ion to vital_funcs.inc.php
  • Loading branch information
sdaityari committed Jun 9, 2013
1 parent 8bf5208 commit b44d6b6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 70 deletions.
24 changes: 0 additions & 24 deletions include/lib/ajax.inc.php

This file was deleted.

18 changes: 18 additions & 0 deletions include/lib/vital_funcs.inc.php
Expand Up @@ -1119,4 +1119,22 @@ function debug($var, $title='') {
echo $str;
echo '</pre>';
}

/**
* This function is used to check if an AJAX request is present.
* @access public
* @author Shaumik Daityari
*/
function check_ajax_request(){
//Uncomment the following line for debugging
//print strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
$ajax_request = 'xmlhttprequest';
if( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&

This comment has been minimized.

Copy link
@anvk

anvk Jun 10, 2013

you should reuse $_SERVER['HTTP_X_REQUESTED_WITH']. assign a variable to it and use in that IF statement

strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == $ajax_request ) {

return True;
}
return False;
}

?>
17 changes: 0 additions & 17 deletions jscripts/ajax/AjaxRequest.js

This file was deleted.

59 changes: 34 additions & 25 deletions jscripts/ajax/FileStorageComments.js
Expand Up @@ -3,38 +3,47 @@
* @copyright Copyright © 2013, ATutor, All rights reserved.
*/

var deleteMessage = "Are you sure you want to delete this comment?";
var deleteTitle = "Delete Comment";
var deleteUrl = "mods/_standard/file_storage/ajax/delete_comment.php";
var ATutor = ATutor || {};

var responseMessage;
var parameters;

var deleteComment = function (ot, oid, file_id, id) {
(function() {
"use strict";
if (confirm (deleteMessage)) {


ATutor.fileStorage = ATutor.fileStorage || {};

This comment has been minimized.

Copy link
@anvk

anvk Jun 10, 2013

Better put it out of the closure for readability purposes. This way a developer will see immediately what namespaces are used.


var deleteMessage = "Are you sure you want to delete this comment?",
deleteTitle = "Delete Comment",
deleteUrl = "mods/_standard/file_storage/ajax/delete_comment.php";

ATutor.fileStorage.deleteComment = function (ot, oid, file_id, id) {

This comment has been minimized.

Copy link
@anvk

anvk Jun 10, 2013

Maybe instead of defining ATutor.fileStorage.deleteComment you can do the modular code:

(function (fileStorage) {

// use fileStorage here

fileStorage.deleteComment = function (ot, ...
})(ATutor.fileStorage);

if (!confirm (deleteMessage)) {
return;
}
var parameters = {
'ot' : ot,
'oid': oid,
'file_id': file_id,
'id': id,
'submit_yes': true
}
};

ajaxRequest(deleteUrl, parameters, commentOnDelete);

}
};

var commentOnDelete = function (responseMessage, parameters) {
if (responseMessage === 'ACTION_COMPLETED_SUCCESSFULLY') {
$('#comment' + parameters.id).fadeOut();
} else if (responseMessage === 'ACCESS_DENIED') {
alert ('Access denied');
} else {
alert ('Action not completed. Unknown Error Occurred.');
}
}

$.ajax({
type: "POST",
url: deleteUrl,
data: parameters,
success: function(message) {
commentOnDelete(message, parameters);
}
});
};

var commentOnDelete = function (responseMessage, parameters) {
if (responseMessage === 'ACTION_COMPLETED_SUCCESSFULLY') {

This comment has been minimized.

Copy link
@anvk

anvk Jun 10, 2013

You should follow the same syntax in the code by choosing to use only double quotes or single quotes. ATutor project uses double quotes all over the files JavaScript and PHP. I recommend to stick to this convention.

$('#comment' + parameters.id).fadeOut();
} else if (responseMessage === 'ACCESS_DENIED') {
alert ('Access denied');

This comment has been minimized.

Copy link
@anvk

anvk Jun 10, 2013

You should look for other ways of displaying an error message. Using alert() is highly inadvisable since it halts any JavaScript execution on the page as well as it is inaccessible.

} else {
alert ('Action not completed. Unknown Error Occurred.');
}
};

})();
6 changes: 5 additions & 1 deletion mods/_standard/file_storage/ajax/delete_comment.php
Expand Up @@ -15,7 +15,11 @@
define('AT_INCLUDE_PATH', '../../../../include/');
require(AT_INCLUDE_PATH.'vitals.inc.php');
require(AT_INCLUDE_PATH.'../mods/_standard/file_storage/file_storage.inc.php');
require(AT_INCLUDE_PATH.'lib/ajax.inc.php');
include_once(AT_INCLUDE_PATH . 'lib/vital_funcs.inc.php');

if (!check_ajax_request()) {
exit;
}

$owner_type = abs($_REQUEST['ot']);
$owner_id = abs($_REQUEST['oid']);
Expand Down
4 changes: 1 addition & 3 deletions mods/_standard/file_storage/comments.php
Expand Up @@ -162,9 +162,8 @@
<p><?php echo nl2br(htmlspecialchars($row['comment'])); ?></p>
<?php if ($row['member_id'] == $_SESSION['member_id'] || $current_file['member_id'] == $_SESSION['member_id']): ?>
<div style="text-align:right; font-size: smaller">
<a href="<?php echo url_rewrite('mods/_standard/file_storage/comments.php'.$owner_arg_prefix.'id='.$id.SEP.'comment_id='.$row['comment_id'].'#c'.$row['comment_id']); ?>"><?php echo _AT('edit'); ?></a> | <a href="javascript:null();" onclick="deleteComment('<?php echo $owner_type; ?>', '<?php echo $owner_id; ?>', '<?php echo $id; ?>', '<?php echo $row['comment_id']; ?>');"><?php echo _AT('delete'); ?></a>
<a href="<?php echo url_rewrite('mods/_standard/file_storage/comments.php'.$owner_arg_prefix.'id='.$id.SEP.'comment_id='.$row['comment_id'].'#c'.$row['comment_id']); ?>"><?php echo _AT('edit'); ?></a> | <a href="javascript:null();" onclick="ATutor.fileStorage.deleteComment('<?php echo $owner_type; ?>', '<?php echo $owner_id; ?>', '<?php echo $id; ?>', '<?php echo $row['comment_id']; ?>');"><?php echo _AT('delete'); ?></a>

<!--<a href="mods/_standard/file_storage/delete_comment.php<?php echo $owner_arg_prefix . 'file_id='.$id.SEP; ?>id=<?php echo $row['comment_id']; ?>"><?php echo _AT('delete'); ?></a>-->
</div>
<?php endif; ?>
</div>
Expand Down Expand Up @@ -196,6 +195,5 @@
</div>
</form>
<?php endif; ?>
<script type="text/javascript" src="<?php echo AT_BASE_HREF; ?>jscripts/ajax/AjaxRequest.js"></script>
<script type="text/javascript" src="<?php echo AT_BASE_HREF; ?>jscripts/ajax/FileStorageComments.js"></script>
<?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>

0 comments on commit b44d6b6

Please sign in to comment.