Commit 51930729 authored by Nigel McNie's avatar Nigel McNie
Browse files

Cleaned up the friends page a lot, by moving the javascript out to a separate...

Cleaned up the friends page a lot, by moving the javascript out to a separate page. Added a pieform for controlling the 'friendscontrol' user account setting
parent e128d1f5
......@@ -33,148 +33,49 @@ define('SECTION_PAGE', 'friends');
require(dirname(dirname(__FILE__)) . '/init.php');
define('TITLE', get_string('myfriends'));
$wwwroot = get_config('wwwroot');
$profileurl = $wwwroot . 'thumb.php?type=profileicon&size=40x40&id=';
$viewurl = $wwwroot . 'user/view.php?id=';
$viewsavailable = get_string('viewsavailable');
$viewavailable = get_string('viewavailable');
$remove = get_string('removefromfriendslist');
$accept = get_string('accept');
$reject = get_string('reject');
$allviews = get_string('allviews');
$removeheader = get_string('remove');
$reasonheader = get_string('reason');
$friendcontrolfailed = get_string('friendlistfailure');
$enc_confirm_remove = json_encode(get_string('confirmremovefriend'));
$inlinejs = <<<EOF
var friendslist = new TableRenderer(
'friendslist',
'index.json.php',
[
function (r) {
return TD(null, IMG({'src': '{$profileurl}' + r.id}));
},
function (r) {
return TD(null, A({'href': '{$viewurl}' + r.id}, r.name));
},
function (r, d) {
if (d.pending) {
var reason = '';
if (r.reason) {
reason = r.reason;
}
return [ TD(null, reason),
TD(null, A({'href': '', 'onclick': 'friendControl(\'accept\', ' + r.id + '); return false'}, '{$accept}')),
TD({'id': 'pending-' + r.id},
A({'href': '', 'onclick': 'showReject(' + r.id + '); return false'}, '{$reject}')) ];
}
else {
var viewcol;
if (typeof(d.views) == 'object' && d.views[r.id] && d.numviews[r.id] > 0) {
var len = d.numviews[r.id];
var views = '';
if (len == 1) {
views = len + ' {$viewavailable}';
}
else {
views = len + ' {$viewsavailable}';
}
var theLink = A({'href': ''}, views);
connect(theLink, 'onclick', partial(expandViews, d.views[r.id], r.id));
connect(theLink, 'onclick', function (e) { e.stop(); } );
viewcol = TD({'id': 'friend-' + r.id}, theLink);
}
else {
views = '0 {$viewsavailable}';
viewcol = TD(null, views);
}
return [ viewcol, TD(null, A({'href': '', 'onclick': 'friendControl(\'remove\', ' + r.id + '); return false;'}, '{$remove}')) ];
}
}
]
);
friendslist.pending = 0;
friendslist.statevars.push('pending');
friendslist.updateOnLoad();
function friendControl(type, id, reason) {
var pd = {'id': id, 'control': 1};
if (type == 'remove' && !confirm({$enc_confirm_remove})) {
logDebug(type, id);
return false;
}
if (type == 'reject') {
type = 'accept';
pd['rejectsubmit'] = 'reject';
if (reason) {
pd['rejectreason'] = reason;
}
}
pd['type'] = type;
sendjsonrequest('index.json.php', pd, 'GET');
friendslist.doupdate();
// Generate a form for controlling the friendscontrol setting for users
require_once('pieforms/pieform.php');
$form = array(
'name' => 'friendscontrol',
'jsform' => true,
'plugintype' => 'core',
'pluginname' => 'account',
'autofocus' => false,
'elements' => array(
'friendscontrol' => array(
'type' => 'radio',
'defaultvalue' => $USER->get_account_preference('friendscontrol'),
'separator' => HTML_BR,
'options' => array(
'nobody' => get_string('friendsnobody', 'account'),
'auth' => get_string('friendsauth', 'account'),
'auto' => get_string('friendsauto', 'account')
),
'rules' => array(
'required' => true
),
'help' => true
),
'submit' => array(
'type' => 'submit',
'value' => get_string('save')
),
)
);
// Make a sideblock to put the friendscontrol block in
$sideblock = array(
'name' => 'friendscontrol',
'weight' => -5,
'data' => pieform($form)
);
function friendscontrol_submit(Pieform $form, $values) {
global $USER;
$USER->set_account_preference('friendscontrol', $values['friendscontrol']);
$form->json_reply(PIEFORM_OK, 'Updated friend control setting successfully');
}
function showReject(id) {
if ($('row-reject-' + id)) {
removeElement('row-reject-' + id);
return;
}
var tr = TR({'id': 'row-reject-' + id},
TD(null),
TD({'colspan': 3},
INPUT({'type': 'text', 'name': 'reject-reason-' + id, 'id': 'reject-reason-' + id}),
INPUT({'type': 'button', 'value': '{$reject}',
'onclick': 'friendControl(\'reject\', ' + id + ', $(\'reject-reason-' + id + '\').value);'})));
insertSiblingNodesAfter($('pending-' + id).parentNode, tr);
}
function expandViews(views, id) {
if ($('row-views-' + id)) {
removeElement('row-views-' + id);
forEach (views, function (view) {
removeElement('row-views-view-' + view.id);
});
return false;
}
var ts = [];
forEach (views, function(view) {
ts.push(TR({'id': 'row-views-view-' + view.id},
TD(null),
TD({'colspan': 3},
A({'href': '{$wwwroot}/view/view.php?view=' + view.id}, view.title))));
});
ts.push(TR({'id': 'row-views-' + id}, TD(null),
TD({'colspan': 3},
A({'href': '{$wwwroot}/user/view.php?id=' + id}, '{$allviews}'))));
insertSiblingNodesAfter($('friend-' + id).parentNode, ts);
return false;
}
function pendingChange() {
var pending = $('pendingopts').options[$('pendingopts').selectedIndex].value;
friendslist.doupdate({'pending': pending});
if (pending == 1) {
$('viewsheader').style.display = 'none';
$('removeorreason').innerHTML = '{$reasonheader}';
}
else {
$('viewsheader').style.display = '';
$('removeorreason').innerHTML = '{$removeheader}';
}
}
EOF;
$smarty = smarty(array('tablerenderer'));
$smarty->assign('INLINEJAVASCRIPT', $inlinejs);
$smarty = smarty(array('mahara', 'tablerenderer', 'friends'), array(), array(), array('sideblocks' => array($sideblock)));
$smarty->display('contacts/index.tpl');
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment