Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AJAX behaviour to ATutor #1

Open
wants to merge 28 commits into
base: gsoc2013
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d78b672
File Storage: Added AJAX Js file to comments page
sdaityari Jun 1, 2013
804fddf
File Storage: Changed links on Delete button and put confirm message
sdaityari Jun 1, 2013
9b7ef32
File Storage: Completed basic AJAX for deleting comments
sdaityari Jun 1, 2013
b9ab7f1
Include: Added php file to check for ajax requests
sdaityari Jun 8, 2013
8bf5208
Ajax_demo: Made ajax function reusable by separating it and adding ca…
sdaityari Jun 8, 2013
b44d6b6
Ajax_demo: Modified js code to follow closure, Moved Ajax check funct…
sdaityari Jun 9, 2013
a067a4b
ajax_demo: removed single quotes, made code more readable
sdaityari Jun 10, 2013
ba08935
ajax_demo: Added jquery dialog boxes for displaying messages
sdaityari Jun 11, 2013
1c4e21b
ajax_demo: increased code efficiency
sdaityari Jun 12, 2013
f341521
ajax_demo: ISSUE 5256- Corrected delete comment logic
sdaityari Jun 12, 2013
54bc9e9
ajax_demo: Edit comments in file storage through AJAX, also solved IS…
sdaityari Jun 13, 2013
6604843
ajax_demo: modified code to follow standard guidelines
sdaityari Jun 14, 2013
338aa04
ajax_demo: add question functionality through ajax
sdaityari Jun 15, 2013
2ecaae9
ajax_demo: generates html for ajax add comment in file_storage throug…
sdaityari Jun 17, 2013
0cb95f1
ajax_demo: removed code from comments.php in file_storage which are n…
sdaityari Jun 17, 2013
8ff3ed6
ajax_demo: Separated generateDialog into a different file for ajax fu…
sdaityari Jun 19, 2013
39e4814
ajax_demo: fixed JS error, added focus to textarea on edit comment
sdaityari Jun 19, 2013
7c514db
ajax_demo: Added AJAX functionality to delete reply/topic in forums
sdaityari Jun 19, 2013
ec4042b
ajax_demo: optimized javascript
sdaityari Jun 20, 2013
6519625
ajax_demo: modified code to follow guidelines, see discussion
sdaityari Jun 20, 2013
7914c5c
ajax_demo: Glossary- added ajax behavior to delete item (default theme)
sdaityari Jun 21, 2013
c84c739
ajax_demo: added AJAX behavior to glossary add term
sdaityari Jun 22, 2013
7c7c0b5
ajax_demo: added ajax behaviour to edit glossary term
sdaityari Jun 22, 2013
20afbf5
ajax_demo: improved code in ajax js and php files
sdaityari Jun 24, 2013
a63b3a1
ajax_demo: removed hardcoded js variables for css ids
sdaityari Jun 26, 2013
32a95c2
ajax_demo: changed code to follow guidelines
sdaityari Jun 28, 2013
e5a2d38
ajax_demo: removed redundancy in code
sdaityari Jun 28, 2013
e5edc01
Merged with new atutor
sdaityari Sep 23, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/lib/vital_funcs.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,17 @@ 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
* @date June 09, 2013
*/
function check_ajax_request(){
$server_request = $_SERVER["HTTP_X_REQUESTED_WITH"];
$ajax_request = "xmlhttprequest";
return strtolower($server_request) == $ajax_request? true : false;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simply return (strtolower($server_request) == $ajax_request);
In JS it works for sure, just do not remember 100% about PHP

}

?>
325 changes: 325 additions & 0 deletions jscripts/ajax/FileStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
/**
* @author Shaumik Daityari
* @copyright Copyright © 2013, ATutor, All rights reserved.
*/

var ATutor = ATutor || {};

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

ATutor.ajaxFunctions = ATutor.ajaxFunctions || {};

(function(fileStorage, ajaxFunctions) {

"use strict";

// Add ids of comments to get the DOM elements
var css = {
commentId : "comment",
editCommentId : "edit-comment-",
editDeleteButtonsId : "comment-edit-delete-",
commentDescriptionId : "comment-description-",
textAreaId : "textarea-"
};

//Function to be called on clicking Delete for a comment
fileStorage.deleteComment = function (options) {

var defaults = {
deleteMessage : "Are you sure you want to delete this comment?",
deleteTitle : "Delete Comment",
deleteUrl : "mods/_standard/file_storage/ajax/comments.php",
deleteId : "comment-delete-dialog"
};

options = options || {};

options = $.extend({}, defaults, options);

var deleteDialog = $("#" + options.deleteId);

/**
* Sets POST variables to be sent
* @parameters
* ot: owner type
* oid: owner id
* file_id: file id (primary key of files)
* id: comment id (primary key of filescomments)
*/
var parameters = {
ot : options.ot,
oid: options.oid,
fileId: options.fileId,
id: options.id,
deleteSubmit: true
};

var buttonOptions = {
"Delete Comment": function (){
$.ajax({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please fix indentation here

type: "POST",
url: options.deleteUrl,
data: parameters,
success: function(message) {
commentOnDelete(message, parameters.id);
}
});
deleteDialog.dialog("close");
},
"Cancel" : function () {
deleteDialog.dialog("close");
}
};

// Create dialog for the page if it doesn't exist
if (!deleteDialog.length) {
deleteDialog = $("<div />", {
title: options.deleteTitle,
text: options.deleteMessage,
id: options.deleteId
}).appendTo($("body"));
}

deleteDialog.dialog({
autoOpen: true,
width: 400,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not to hardcode the width of the dialog

modal: true,
closeOnEscape: false,
buttons: buttonOptions
});
};

//Callback function for AJAX Request
var commentOnDelete = function (responseMessage, id) {
if (responseMessage === ajaxFunctions.successfulCode) {
$("#" + css.commentId + id).fadeOut();
return;
} else {
ajaxFunctions.generateDialog(responseMessage);
}
};

//Function to be called on clicking Edit under a comment
fileStorage.editCommentShow = function (id) {
$("#" + css.editCommentId + id).show();
$("#" + css.editDeleteButtonsId + id).hide();
$("#" + css.commentDescriptionId + id).hide();
$("#" + css.textAreaId + id).focus();
};

//Function to be called on clicking submit after editing a comment
fileStorage.editCommentSubmit = function (options) {

var defaults = { updateUrl : "mods/_standard/file_storage/ajax/comments.php" };

options = options || {};

options = $.extend({}, defaults, options);

/**
* Sets POST variables to be sent
* @parameters
* ot: owner type
* oid: owner id
* file_id: file id (primary key of files)
* id: comment id (primary key of filescomments)
* comment: updated value of textarea
*/
var parameters = {
ot : options.ot,
oid: options.oid,
fileId: options.file_id,
id: options.id,
comment: $("#" + css.textAreaId + options.id).val(),
editSubmit: true
};

//Checking if the comment has been changed at all
if (parameters.comment === $("#" + css.commentDescriptionId + options.id).text()) {
fileStorage.editCommentHide(options.id);
return;
}

$.ajax({
type: "POST",
url: options.updateUrl,
data: parameters,
success: function(message) {
commentOnEdit(message, parameters.id, parameters.comment);
}
});
};

//Function to be called on clicking Cancel under a textarea
fileStorage.editCommentHide = function (id) {
$("#" + css.editDeleteButtonsId + id).show();
$("#" + css.commentDescriptionId + id).show();
$("#" + css.editCommentId + id).hide();
};

//Function to be called on successful AJAX request for comment edit
var commentOnEdit = function (message, id, comment) {
if (message === ajaxFunctions.successfulCode) {
$("#" + css.commentDesciptionId + id).html($("<div/>").text(comment).html());
fileStorage.editCommentHide(id);
} else {
ajaxFunctions.generateDialog(message);
}
};

//Function called on clicking post in when adding new comment
fileStorage.addComment = function (options) {

var options = options || {};

var defaults = {
addUrl : "mods/_standard/file_storage/ajax/comments.php",
textarea : $('#comment')
};

options = $.extend({}, defaults, options);

//In case the comment is empty
if (options.textarea.val() === ''){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the comment only empty spaces. Maybe you should use trim function before the check.
Also I think you can simply do if (options.textarea.val()) here

return;
}

/**
* Sets POST variables to be sent
* @parameters
* ot: owner type
* oid: owner id
* file_id: file id (primary key of files)
* comment: comment id (primary key of filescomments)
*/
var parameters = {
ot : options.ot,
oid: options.oid,
fileId: options.fileId,
id: options.id,
comment: options.textarea.val(),
addSubmit: true
};

//Sending AJAX request
$.ajax({
type: "POST",
url: options.addUrl,
data: parameters,
success: function(response) {
commentOnAdd(response, parameters);
}
});

fileStorage.cancelAddComment(options.textarea);
};

//Function called on clicking cancel when adding new comment
fileStorage.cancelAddComment = function (textarea){
var textarea = textarea || $("#comment");

//Reseting the text area
textarea.val("");
};

var commentOnAdd = function (response, parameters) {
var parsedResponse = $.parseJSON(response);

//Checking if there were any errors
if (parsedResponse.message !== ajaxFunctions.successfulCode) {
ajaxFunctions.generateDialog(parsedResponse.message);
return;
}

var addCommentDom = $("<div />", {
"class": "input-form",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me of there are indentation problems in the code here and there

id: "comment" + parsedResponse.id
}).insertBefore($("#comment-add-form"));

//adding wrapper for comment
var addCommentRow = $("<div />", {
"class": "row"
}).appendTo(addCommentDom);

//adding comment heading
$("<h4 />", {
text: parsedResponse.name + " - " + parsedResponse.date
}).appendTo(addCommentRow);

//adding comment description
$("<p />", {
html: parsedResponse.comment,
id: css.commentDescriptionId + parsedResponse.id
}).appendTo(addCommentRow);

//Setting new parameters for using in functions to edit and delete comments
var newParameters = {
ot : parameters.ot,
oid : parameters.oid,
fileId : parameters.fileId,
id : parsedResponse.id
};

//Setting variables to be used below
var alignRightStyle = "text-align:right; font-size: smaller", //for edit/delete/cancel buttons
separator = " | ",
hrefText = "javascript:(null);";

//Wrapper for Edit comment textarea and buttons
var editCommentDiv = $("<div />", {
style : "width:100%; display:none;",
id : css.editCommentId + parsedResponse.id
}).appendTo(addCommentRow);

//Edit comment textarea
$("<textarea />", {
text : parameters.comment,
id : css.textAreaId + parsedResponse.id
}).appendTo(editCommentDiv);

//wrapper for buttons below edit textarea
var editButtons = $("<div />", {
style : alignRightStyle,
text : separator
}).appendTo(editCommentDiv);

//Submit Edits button prepended to wrapper div
$("<a />", {
text : "Submit",
href : hrefText,
onclick : "ATutor.fileStorage.editCommentSubmit(" +
JSON.stringify(newParameters) + ");"
}).prependTo(editButtons);

//Cancel button appended to wrapper div
$("<a />", {
text : "Cancel",
href : hrefText,
onclick : "ATutor.fileStorage.editCommentHide('" + parsedResponse.id + "');"
}).appendTo(editButtons);

//Wrapper for Edit and Delete buttons which are shown initially
var editDeleteButtons = $("<div />", {
style : alignRightStyle,
text : separator,
id : css.editDeleteButtonsId + parsedResponse.id
}).appendTo(addCommentRow);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. You either have tabs somewhere or have indention problems. Double check that you do not have tabs anywhere in your code.


//Delete comment button appended to wrapper div
$("<a />", {
text : "Delete",
href : hrefText,
onclick : "ATutor.fileStorage.deleteComment(" +
JSON.stringify(newParameters) + ");"
}).appendTo(editDeleteButtons);

//Edit button prepended to wrapper div
$("<a />", {
text : "Edit",
href : hrefText,
onclick : "ATutor.fileStorage.editCommentShow('" + parsedResponse.id + "');"
}).prependTo(editDeleteButtons);

};

})(ATutor.fileStorage, ATutor.ajaxFunctions);
Loading