Skip to content

Commit

Permalink
"MDL-19118, comments api"
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed Jul 24, 2009
1 parent 3209773 commit 1bcb7eb
Show file tree
Hide file tree
Showing 22 changed files with 1,484 additions and 10 deletions.
3 changes: 2 additions & 1 deletion admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
}


$ADMIN->add('reports', new admin_externalpage('comments', get_string('comments'), $CFG->wwwroot.'/comment/', 'moodle/site:viewreports'));
/// Now add reports

foreach (get_plugin_list('report') as $plugin => $plugindir) {
Expand Down Expand Up @@ -283,4 +284,4 @@
include($settings_path);
continue;
}
}
}
1 change: 1 addition & 0 deletions admin/settings/subsystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$optionalsubsystems->add(new admin_setting_configcheckbox('enablegroupings', get_string('enablegroupings', 'admin'), get_string('configenablegroupings', 'admin'), 0));

$optionalsubsystems->add(new admin_setting_configcheckbox('enableoutcomes', get_string('enableoutcomes', 'grades'), get_string('configenableoutcomes', 'grades'), 0));
$optionalsubsystems->add(new admin_setting_configcheckbox('usecomments', get_string('enablecomments', 'admin'), get_string('configenablecomments', 'admin'), 1));

$optionalsubsystems->add(new admin_setting_configcheckbox('usetags', get_string('usetags','admin'),get_string('configusetags', 'admin'), '1'));

Expand Down
23 changes: 23 additions & 0 deletions blocks/moodleblock.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,29 @@ function _add_edit_controls() {
function config_print() {
throw new coding_exception('config_print() can no longer be used. Blocks should use a settings.php file.');
}
/** @callback callback functions for comments api */
public static function comment_template($options) {
$ret = <<<EOD
<div class="comment-userpicture">___picture___</div>
<div class="comment-content">
___name___ - <span>___time___</span>
<div>___content___</div>
</div>
EOD;
return $ret;
}
public static function comment_permissions($options) {
return array('view'=>true, 'post'=>true);
}
public static function comment_url($options) {
return null;
}
public static function comment_display(&$comments, $options) {
return true;
}
public static function comment_add(&$comments, $options) {
return true;
}
}

/**
Expand Down
44 changes: 44 additions & 0 deletions comment/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
YAHOO.util.Event.onDOMReady(init);
function init() {
var select_all = document.getElementById('comment_select_all');
select_all.onclick = function() {
var comments = document.getElementsByName('comments');
var checked = false;
for (var i in comments) {
if (comments[i].checked) {
checked=true;
}
}
for (var i in comments) {
comments[i].checked = !checked;
}
this.checked = !checked;
}
var comments_delete = document.getElementById('comments_delete');
comments_delete.onclick = function() {
delete_comments();
}
}
function delete_comments() {
var url = moodle_cfg.wwwroot + '/comment/index.php';
var cb = {
success:function(o) {
if (o.responseText == 'yes') {
location.reload();
}
}
}
var comments = document.getElementsByName('comments');
var list = '';
for (var i in comments) {
if (comments[i].checked) {
list += (comments[i].value + '-');
}
}
var data = {
'commentids': list,
'sesskey': moodle_cfg.sesskey
}
var trans = YAHOO.util.Connect.asyncRequest('POST',
url+'?action=delete', cb, build_querystring(data));
}
206 changes: 206 additions & 0 deletions comment/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/**
* Javascript for comments 2.0
*/

function cmt_replace(client_id,list,newcmt) {
var ret = {};
ret.ids = [];
var template = document.getElementById('cmt-tmpl');
var html = '';
for(var i in list) {
var htmlid = 'comment-'+list[i].id+'-'+client_id;
var val = template.innerHTML;
val = val.replace('___name___', list[i].username);
if (list[i]['delete']||newcmt) {
list[i].time += ' <a href="###" title="'+mstr.moodle.delete+'" onclick="delete_comment(\''+client_id+'\',\''+list[i].id+'\')"><img src="'+moodle_cfg.wwwroot+'/pix/t/delete.gif" /></a>';
}
val = val.replace('___time___', list[i].time);
val = val.replace('___picture___', list[i].avatar);
val = val.replace('___content___', list[i].content);
val = '<li id="'+htmlid+'">'+val+'</li>';
ret.ids.push(htmlid);
html = (val+html);
}
ret.html = html;
return ret;
}
function cmt_load(cid) {
var container = document.getElementById('comment-list-'+cid);
container.innerHTML = '<div style="text-align:center"><img src="'+moodle_cfg.wwwroot+'/pix/i/loading.gif'+'" /></div>';
}
function get_comments(client_id, area, itemid, page) {
var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
var data = {
'courseid': comment_params.courseid,
'contextid': comment_params.contextid,
'area': area,
'itemid': itemid,
'page': page,
'client_id': client_id,
'sesskey': moodle_cfg.sesskey
}
this.cb = {
success: function(o) {
try {
var ret = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert("JSON ERROR: "+o.responseText);
}
if (!comment_check_response(ret)) {
return;
}
var container = document.getElementById('comment-list-'+ret.client_id);
var pagination = document.getElementById('comment-pagination-'+ret.client_id);
if (ret.pagination) {
pagination.innerHTML = ret.pagination;
} else {
//empty paging bar
pagination.innerHTML = '';
}
var result = cmt_replace(ret.client_id, ret.list);
container.innerHTML = result.html;
}
}
cmt_load(client_id);
var trans = YAHOO.util.Connect.asyncRequest('POST',
url+'?action=get', this.cb, build_querystring(data));
}
function post_comment(cid) {
this.cb = {
success: function(o) {
try {
var resp = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert("JSON ERROR: "+o.responseText);
}
if (!comment_check_response(resp)) {
return;
}
if(resp) {
var cid = resp.client_id;
var ta = document.getElementById('dlg-content-'+cid);
ta.value = '';
var container = document.getElementById('comment-list-'+cid);
var result = cmt_replace(cid,[resp], true);
container.innerHTML += result.html;
var ids = result.ids;
for(var i in ids) {
var attributes = {
color: { to: '#06e' },
backgroundColor: { to: '#FFE390' }
};
var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
anim.animate();
}
}
}
}
var ta = document.getElementById('dlg-content-'+cid);
if (ta.value && ta.value != mstr.moodle.addcomment) {
var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
var formObject = document.getElementById('comment-form-'+cid);
YAHOO.util.Connect.setForm(formObject);
var trans = YAHOO.util.Connect.asyncRequest('POST', url+'?action=add', this.cb);
} else {
var attributes = {
backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
};
var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
anim.animate();
}
}
function delete_comment(client_id, comment_id) {
var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
var data = {
'courseid': comment_params.courseid,
'contextid': comment_params.contextid,
'commentid': comment_id,
'client_id': client_id,
'sesskey': moodle_cfg.sesskey
}
this.cb = {
success: function(o) {
try {
var resp = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert("JSON ERROR: "+o.responseText);
}
if (!comment_check_response(resp)) {
return;
}
var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
this.el = document.getElementById(htmlid);
this.el.style.overflow = 'hidden';
var attributes = {
width:{to:0},
height:{to:0}
};
var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
anim.onComplete.subscribe(this.remove_dom, [], this);
anim.animate();
},
remove_dom: function() {
this.el.parentNode.removeChild(this.el);
}
}
var trans = YAHOO.util.Connect.asyncRequest('POST',
url+'?action=delete', this.cb, build_querystring(data));
}
function view_comments(client_id, area, itemid, page) {
var container = document.getElementById('comment-ctrl-'+client_id);
var ta = document.getElementById('dlg-content-'+client_id);
var img = document.getElementById('comment-img-'+client_id);
if (container.style.display=='none'||container.style.display=='') {
// show
get_comments(client_id, area, itemid, page);
container.style.display = 'block';
img.src=moodle_cfg.wwwroot+'/pix/t/expanded.png';
} else {
// hide
container.style.display = 'none';
img.src=moodle_cfg.wwwroot+'/pix/t/collapsed.png';
ta.value = '';
}
toggle_textarea.apply(ta, [false]);
// reset textarea size
ta.onclick = function() {
toggle_textarea.apply(this, [true]);
}
ta.onkeypress = function() {
if (this.scrollHeight > this.clientHeight && !window.opera)
this.rows += 1;
}
ta.onblur = function() {
toggle_textarea.apply(this, [false]);
}
return false;
}
function comment_hide_link(cid) {
var link = document.getElementById('comment-link-'+cid);
if(link){
link.style.display='none';
} else {
alert('wront');
}
}
function toggle_textarea(focus) {
if (focus) {
if (this.value == mstr.moodle.addcomment) {
this.value = '';
this.style.color = 'black';
}
}else{
if (this.value == '') {
this.value = mstr.moodle.addcomment;
this.style.color = 'grey';
this.rows = 1;
}
}
}
function comment_check_response(data) {
if (data.error) {
alert(data.error);
return false;
}
return true;
}
Loading

0 comments on commit 1bcb7eb

Please sign in to comment.