Skip to content

Commit

Permalink
Updated, fixed many bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunsuresh committed Dec 19, 2016
1 parent e2162e7 commit 63e90db
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 99 deletions.
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Better Points",
"description": "This plugin allows to add points for commenting, comment up/down votes and in case Blog/Exam plugins are being used for blog/exam creation and exams being taken (Blog and Exam plugins are Paid Ones)",
"version": "1.0",
"version": "1.1",
"date": "2016-11-25",
"uri": "https://github.com/arjunsuresh/q2a-betterpoints",
"update_uri": "https://raw.githubusercontent.com/arjunsuresh/q2a-betterpoints/master/metadata.json",
Expand Down
198 changes: 121 additions & 77 deletions qa-bp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class qa_bp_admin {
function option_default($option) {

switch($option) {
case 'qa_bp_enable':
return 0;
case 'qa_bp_commentpoints':
return 1;
case 'qa_bp_commentuppoints':
Expand Down Expand Up @@ -35,7 +37,42 @@ function option_default($option) {
function allow_template($template)
{
return ($template!='admin');
}
}

public function init_queries($tableslc)
{
//if(qa_opt('qa_bp_enable') && !qa_opt('qa_bp_initialized'))
if(qa_opt('qa_bp_enable'))// && !qa_opt('qa_bp_initialized'))
{

$queries = array();
$tablename = qa_db_add_table_prefix('userpoints');


$sqlCols = 'SHOW COLUMNS FROM '.$tablename;
$fields = qa_db_read_all_values(qa_db_query_sub($sqlCols));
$newcolumns = array('cupvotes', 'cdownvotes', 'cvoteds');
if(qa_opt('edit_history_active')){
$newcolumns [] = 'edits';
}
if(qa_opt('cache_exams_count') > 0){
$newcolumns [] = 'exams';
$newcolumns [] = 'examstaken';
}
if(qa_opt('cache_blog_pcount') > 0){
$newcolumns [] = 'blogs';

}
foreach ($newcolumns as $column){
if(!in_array($column, $fields)) {
$queries [] = 'ALTER TABLE '.$tablename.' ADD '.$column. ' MEDIUMINT(9) NOT NULL DEFAULT 0;';
}
}
qa_opt('qa_bp_initialized', '1');
return $queries;
}
return null;
}

function admin_form(&$qa_content)
{
Expand All @@ -45,6 +82,7 @@ function admin_form(&$qa_content)
$ok = null;

if (qa_clicked('qa_bp_save')) {
qa_opt('qa_bp_enable',(bool)qa_post_text('qa_bp_enable'));
qa_opt('qa_bp_commentpoints',qa_post_text('qa_bp_commentpoints'));
qa_opt('qa_bp_commentuppoints',qa_post_text('qa_bp_commentuppoints'));
qa_opt('qa_bp_commentdownpoints',qa_post_text('qa_bp_commentdownpoints'));
Expand All @@ -53,105 +91,111 @@ function admin_form(&$qa_content)
qa_opt('qa_bp_commentuplimitpoints',qa_post_text('qa_bp_commentuplimitpoints'));
qa_opt('qa_bp_commentdownlimitpoints',qa_post_text('qa_bp_commentdownlimitpoints'));
if(qa_opt('edit_history_active')){
qa_opt('qa_bp_editpoints_level',qa_post_text('qa_bp_editpoints'));
qa_opt('qa_bp_editpoints',qa_post_text('qa_bp_editpoints'));
}
if(qa_opt('cache_exams_count') >= 0){
qa_opt('qa_bp_exampoints',qa_post_text('qa_bp_exampoints'));
qa_opt('qa_bp_examtakenpoints',qa_post_text('qa_bp_examtakenpoints'));
}
if(qa_opt('cache_blog_pcount') > 0)
qa_opt('qa_bp_blogpoints',qa_post_text('qa_bp_blogpoints'));
$ok = qa_lang('admin/options_saved');
$ok = qa_lang('bp_lang/savemsg');
}

// Create the form for display

$fields = array();
$fields[] = array(
'label' => 'Points for a Comment Post',
'tags' => 'name="qa_bp_commentpoints"',
'value' => qa_opt('qa_bp_commentpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Receiving a Comment Upvote',
'tags' => 'name="qa_bp_commentuppoints"',
'value' => qa_opt('qa_bp_commentuppoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Limit in Points for a Comment Up Vote',
'tags' => 'name="qa_bp_commentuplimitpoints"',
'value' => qa_opt('qa_bp_commentuplimitpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Reduction in Points for Receiving a Comment DownVote',
'tags' => 'name="qa_bp_commentdownpoints"',
'value' => qa_opt('qa_bp_commentdownpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Limit in Points for a Comment Down Vote',
'tags' => 'name="qa_bp_commentdownlimitpoints"',
'value' => qa_opt('qa_bp_commentdownlimitpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Giving a Comment Upvote',
'tags' => 'name="qa_bp_commentupvotepoints"',
'value' => qa_opt('qa_bp_commentupvotepoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Giving a Comment DownVote',
'tags' => 'name="qa_bp_commentdownvotepoints"',
'value' => qa_opt('qa_bp_commentdownvotepoints'),
'type' => 'number',
);
if(qa_opt('edit_history_active')){
$fields[] = array(
'label' => 'Points for an Edit',
'tags' => 'name="qa_bp_editpoints"',
'value' => qa_opt('qa_bp_editpoints'),
'type' => 'number',
'label' => 'Enable Better Points',
'tags' => 'name="qa_bp_enable"',
'value' => (bool)qa_opt('qa_bp_enable'),
'type' => 'checkbox',
);
}
if(qa_opt('cache_exams_count') >= 0){
$fields[] = array(
'label' => 'Points for Creating an Exam',
'tags' => 'name="qa_bp_exampoints"',
'value' => qa_opt('qa_bp_exampoints'),
$fields[] = array(
'label' => 'Points for a Comment Post',
'tags' => 'name="qa_bp_commentpoints"',
'value' => qa_opt('qa_bp_commentpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Taking an Exam',
'tags' => 'name="qa_bp_examtakenpoints"',
'value' => qa_opt('qa_bp_examtakenpoints'),
$fields[] = array(
'label' => 'Points for Receiving a Comment Upvote',
'tags' => 'name="qa_bp_commentuppoints"',
'value' => qa_opt('qa_bp_commentuppoints'),
'type' => 'number',
);
}
if(qa_opt('cache_blog_pcount') >= 0){
$fields[] = array(
'label' => 'Points for Creating a Blog Post',
'tags' => 'name="qa_bp_blogpoints"',
'value' => qa_opt('qa_bp_blogpoints'),
$fields[] = array(
'label' => 'Limit in Points for a Comment Up Vote',
'tags' => 'name="qa_bp_commentuplimitpoints"',
'value' => qa_opt('qa_bp_commentuplimitpoints'),
'type' => 'number',
);
}
$fields[] = array(
'label' => 'Reduction in Points for Receiving a Comment DownVote',
'tags' => 'name="qa_bp_commentdownpoints"',
'value' => qa_opt('qa_bp_commentdownpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Limit in Points for a Comment Down Vote',
'tags' => 'name="qa_bp_commentdownlimitpoints"',
'value' => qa_opt('qa_bp_commentdownlimitpoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Giving a Comment Upvote',
'tags' => 'name="qa_bp_commentupvotepoints"',
'value' => qa_opt('qa_bp_commentupvotepoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Giving a Comment DownVote',
'tags' => 'name="qa_bp_commentdownvotepoints"',
'value' => qa_opt('qa_bp_commentdownvotepoints'),
'type' => 'number',
);
if(qa_opt('edit_history_active')){
$fields[] = array(
'label' => 'Points for an Edit',
'tags' => 'name="qa_bp_editpoints"',
'value' => qa_opt('qa_bp_editpoints'),
'type' => 'number',
);
}
if(qa_opt('cache_exams_count') >= 0){
$fields[] = array(
'label' => 'Points for Creating an Exam',
'tags' => 'name="qa_bp_exampoints"',
'value' => qa_opt('qa_bp_exampoints'),
'type' => 'number',
);
$fields[] = array(
'label' => 'Points for Taking an Exam',
'tags' => 'name="qa_bp_examtakenpoints"',
'value' => qa_opt('qa_bp_examtakenpoints'),
'type' => 'number',
);
}
if(qa_opt('cache_blog_pcount') >= 0){
$fields[] = array(
'label' => 'Points for Creating a Blog Post',
'tags' => 'name="qa_bp_blogpoints"',
'value' => qa_opt('qa_bp_blogpoints'),
'type' => 'number',
);
}

return array(
'ok' => ($ok && !isset($error)) ? $ok : null,
return array(
'ok' => ($ok && !isset($error)) ? $ok : null,

'fields' => $fields,
'fields' => $fields,

'buttons' => array(
array(
'label' => qa_lang_html('main/save_button'),
'tags' => 'NAME="qa_bp_save"',
),
),
);
'buttons' => array(
array(
'label' => qa_lang_html('main/save_button'),
'tags' => 'NAME="qa_bp_save"',
),
),
);
}
}

1 change: 1 addition & 0 deletions qa-bp-lang-default.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@

return array(
'edits' => 'No. of Edits: ',
'savemsg' => 'Point calculations saved, Please recalculate points from Admin/Points Page',
);
44 changes: 23 additions & 21 deletions qa-bp-overrides.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

function qa_db_points_calculations(){

$orig = qa_db_points_calculations_base();
if(!qa_opt('qa_bp_enable'))
return $orig;
$options=qa_get_options(qa_db_points_option_names());
$orig['cposts']['multiple'] = $options['points_multiple']*qa_opt('qa_bp_commentpoints');

Expand All @@ -26,30 +29,30 @@ function qa_db_points_calculations(){
if(qa_opt('edit_history_active')){
$orig['edits'] = array(
'multiple' => $options['points_multiple']*qa_opt('qa_bp_editpoints'),
'formula' => "count(distinct postid) as cnt from ^edit_history where ^edit_history.userid~",
'formula' => "count(distinct postid) as edits from ^userpoints AS userid_src JOIN ^edit_history on userid_src.userid=^edit_history.userid where ^edit_history.userid~",
);
}
if(qa_opt('cache_exams_count') > 0){
$orig['exams'] = array(
'multiple' => $options['points_multiple']*qa_opt('qa_bp_exampoints'),
'formula' => "count(*) as cnt from ^exams where ^exams.userid~",
'formula' => "count(*) as exams from ^userpoints AS userid_src JOIN ^exams on userid_src.userid=^exams.userid where ^exams.userid~",
);
$orig['examstaken'] = array(
'multiple' => $options['points_multiple']*qa_opt('qa_bp_examtakenpoints'),
'formula' => "count(distinct examid) as cnt from ^exam_results where ^exam_results.userid~",
'formula' => "count(distinct examid) as examstaken from ^userpoints AS userid_src JOIN ^exam_results on userid_src.userid=^exam_results.userid where ^exam_results.userid~",
);
}
if(qa_opt('cache_blog_pcount') > 0){
$orig['blogs'] = array(
'multiple' => $options['points_multiple']*qa_opt('qa_bp_blogpoints'),
'formula' => "count * as cnt from ^blogs where ^blogs.userid~",
'formula' => "count(*) as blogs from ^userpoints AS userid_src JOIN ^blogs on userid_src.userid=^blogs.userid where ^blogs.userid~",
);
}

return $orig;
}

function intable($field)
/*function intable($field)
{
$query="desc ^userpoints";
$result = qa_db_query_sub($query);
Expand All @@ -61,9 +64,11 @@ function intable($field)
}
return false;
}
}*/
function qa_db_points_update_ifuser($userid, $columns)
{
if(!qa_opt('qa_bp_enable'))
return qa_db_points_update_ifuser_base($userid, $columns);
if (qa_should_update_counts() && isset($userid)) {
require_once QA_INCLUDE_DIR.'app/options.php';
require_once QA_INCLUDE_DIR.'app/cookies.php';
Expand All @@ -87,31 +92,28 @@ function qa_db_points_update_ifuser($userid, $columns)

foreach ($calculations as $field => $calculation) {
$multiple=(int)$calculation['multiple'];
if(intable($field)){
// if(intable($field)){
if (isset($keycolumns[$field])) {
$insertfields.=$field.', ';
$insertvalues.='@_'.$field.':=(SELECT '.$calculation['formula'].'), ';
$updates.=$field.'=@_'.$field.', ';
$insertpoints.='+('.(int)$multiple.'*@_'.$field.')';
}
}
$updatepoints.='+('.$multiple.'*'.(isset($keycolumns[$field]) ? '@_' : '').$field.')';
}
/*}
else{
$updatepoints.='+('.$multiple.'*'.(isset($keycolumns[$field]) ? '@_' : '').'(select '.$calculation['formula'].'))';
}
}
}*/
}

$query='INSERT INTO ^userpoints ('.$insertfields.'points) VALUES ('.$insertvalues.$insertpoints.') '.
$query='INSERT INTO ^userpoints ('.$insertfields.'points) VALUES ('.$insertvalues.$insertpoints.') '.
'ON DUPLICATE KEY UPDATE '.$updates.'points='.$updatepoints.'+bonus';

qa_db_query_raw(str_replace('~', "='".qa_db_escape_string($userid)."'", qa_db_apply_sub($query, array($userid))));
qa_db_query_raw(str_replace('~', "='".qa_db_escape_string($userid)."'", qa_db_apply_sub($query, array($userid))));
// build like this so that a #, $ or ^ character in the $userid (if external integration) isn't substituted

if (qa_db_insert_on_duplicate_inserted())
qa_db_userpointscount_update();
}


}

?>
if (qa_db_insert_on_duplicate_inserted())
qa_db_userpointscount_update();
}
}
?>

0 comments on commit 63e90db

Please sign in to comment.