Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Linking a custom PostController Form to ->object->insert $sql

edited August 2012 in Vanilla 2.0 - 2.8

Normally I would just try to figure out this stuff on my own. But I am running out of time and any help would be greatly appreciated.

I'm trying to write a plugin. The Effect: a user rates their own post before submitting the Post Form. The user ratings show next to the DiscussionOptions. I've worked out all the css, HTML, Js( maybe). What I am having trouble with is the methods required to store a value('0-4') in a column('rating') of the table gdn_discussion taken from the xhtml radio selector structure... or if that is even the correct solution.

Here is the code I have so far: (oh boy...)

default.php

<?php if (!defined('APPLICATION')) exit();
$PluginInfo['PersonalRating'] = array(
   'Name' => 'Personal Rating',
   'Description' => "Add a special form, which allows user to rate thier own 'Post'",
   'Version' => '0.1',
   'RequiredApplications' => array('Vanilla' => '>=2.0.18'),
   'Author' => 'Drew Billings',
   'AuthorEmail' => 'andrewandroid@gmail.com',
   'AuthorUrl' => 'http://www.vanillaforums.org/profile/SonicNebula'
);

class PersonalRating extends Gdn_Plugin {

        public function setup(){

            return TRUE;    

}   


    public function Base_Render_Before($Sender) {
            $Sender->AddCssFile($this->GetResource('design/jquery.rating.css', FALSE, FALSE));
        $Sender->AddJsFile($this->GetResource('js/jquery.rating.js', FALSE, FALSE));
        $Sender->AddJsFile($this->GetResource('js/OnLoad.js', FALSE, FALSE));
    }

    public function DiscussionsController_DiscussionOptions_Handler($Sender){

            echo '<div class="PRateReadOnly">';
        echo '<input class="rate2 PRate0" type="radio" class="star" />';
        echo '<input class="rate2 PRate1" type="radio" class="star" />';
        echo '<input class="rate2 PRate2" type="radio" class="star" />';
        echo '<input class="rate2 PRate3" type="radio" class="star" />';
        echo '<input class="rate2 PRate4" type="radio" class="star" />';
        echo '</div>';
        }

    public function PostController_BeforeFormButtons_Handler($Sender){


        echo '<h3>Rate Your Customer:</h3>';
        echo '<div class="PRate">';
        echo '<input class="rate2 PRate0" type="radio" class="star" checked=""/>';
        echo '<input class="rate2 PRate1" type="radio" class="star" checked=""/>';
        echo '<input class="rate2 PRate2" type="radio" class="star" checked=""/>';
        echo '<input class="rate2 PRate3" type="radio" class="star" checked=""/>';
        echo '<input class="rate2 PRate4" type="radio" class="star" checked="checked"/>';
        echo '</div>';


    }

    public function PostController_BeforeFormSave_Handler($Sender){

            GetID();
        Set();

   }

   /*public function Setup() {
            $this->Structure();
      }

    public function Structure() {
            Gdn::Structure()
                ->Table('discussion')
            ->Column('Rating', 'varchar(255)', NULL)

        SaveToConfig('Plugins.PersonalRating.Version',$this->PluginInfo['Version']);
    }
    */
    public function OnDisable(){



    }


}

class.PersonalRatingModel.php

<?php if (!defined('APPLICATION')) exit();

class LinkRatingsModel extends VanillaModel{
    public function __construct() {
        parent::__construct('PersonlRating');
    }

    public function GetID($DiscussionID){
        $UserID = Gdn::Session()->UserID;
        return $this->SQL
        ->Select('Rating.*')
        ->From('Discussion Rating ')
        ->Where(
            array(
                'Discussion.DiscussionID'=>$DiscussionID,
            )
        )
        ->Get()
        ->FirstRow()
        ->Rating;
    }
    public function Set($DiscussionID, $Rating){
        $ExitingRating = $this->GetID($DiscussionID);
        if($ExitingRating)
            return FALSE;
        $UserID = Gdn::Session()->UserID;
        $Date=Gdn_Format::ToDateTime();
        $this->SQL
        ->Insert('Discussion',
            array(
                'Rating'=>$Rating,
            )
        ->Where 
            array(
               'Discussion.DiscussionID'=>$DiscussionID,
            )   
        );
    }   

Comments

  • Options
    edited August 2012

    Also, I thought this statement would set up a new column. It doesn't appear that it does. I am almost sure I am way off base with this stuff right now....

    public function Setup() {
         $this->Structure();
          }
    public function Structure() {
       Gdn::Structure()
       ->Table('discussion')
       ->Column('Rating', 'varchar(255)', NULL)
    
  • Options
    peregrineperegrine MVP
    edited August 2012

    if you post other 3 file .css and .js too. Then people can test it too.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Here is the plugin package. Thanks again, and of course the plugin will be posted back to the community. :)

  • Options

    try adding this to your default.php

        public function Base_BeforeDispatch_Handler($Sender){
            if(C('Plugins.PersonalRating.Version')!=$this->PluginInfo['Version'])
                $this->Structure();
        }
    

    also make sure references to Discussions table are Discussions not lowercase discussions.

    one step further on your way. I think there may be other issues for you to solve, but you are on your way.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
        public function Base_BeforeDispatch_Handler($Sender){
        if(C('Plugins.PersonalRating.Version')!=$this->PluginInfo['Version'])
            $this->Structure();
    }
    
    
    
    public function Setup() {
        $this->Structure();
    }
    
    
    public function Structure() {
        Gdn::Structure()
            ->Table('Discussion')
            ->Column('Rating', 'varchar(255)', NULL)
            ->Set();
    
    
        SaveToConfig('Plugins.PersonalRating.Version',$this->PluginInfo['Version']);
    }
    
    
    
    
    public function OnDisable(){
    
    
    
    }
    

    I'm not sure you need the NULL , I think it is TRUE or FALSE, but if it works like you want it works.
    ->Column('Rating', 'varchar(255)', NULL)

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    peregrineperegrine MVP
    edited August 2012

    I suspect following x00's ratemylink more closely as a model will help you immensely.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Is what I have been trying to do. :) Vanilla and Garden are just so rich and deep. It is hard to wrap my head around so quickly.

  • Options
    peregrineperegrine MVP
    edited August 2012

    SonicNebula said:

    Is what I have been trying to do. :) Vanilla and Garden are just so rich and deep. It is hard to wrap my head around so quickly.

    Yea, I have the same problem. each time you go back in you hold your breath and see some new thing, then you have to come back to the surface and rest. Then dive back in.

    BTW - the above writes the Rating column to the Discussions table.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Thanks for the sound advice. Your analogy perfectly describes my experience thus far. I am taking notes to hopefully and eventually help improve the documentation. I also noticed you have a tutorial plugin! I am working through that now. Thanks a ton for your help man. Some times it just takes either a life preserver and other times just a little nudge into the deep end. :) Even though I cannot possibly have the whole Vanilla and Gdn system figured out yet I can tell it's well worth the time. It appears highly modularized and well written regarding other good coding strategies.

    Also, I am posting this particular project for hire. I need it done by Monday. I figured I would give you and @x00 first dibs if you want it:
    80 USD
    Needs to be done to specs by Monday Morning PST.
    http://testing.gulpu.com has the scratch concept.
    You will have to sign up for an account to see it if your interested. There is no spam or anything like that.

    I think once I get this done I will be less stressed and more able to be in the beginners mind mode...
    Thanks again and please let me know if you are interested.

  • Options
    peregrineperegrine MVP
    edited August 2012

    It sounds right up @x00's alley. At this point in time I am not doing anything for hire and especially a 36 hour deadline - but thanks for the consideration.
    I'm sure there are others if @x00 doesn't pick it up for you.

    If you decide to drag it out and muddle through it I can try to help on this forum debugging specific stumbling blocks if I know the answer.

    You might also look at ignoreuser and ignore plugin as well as replyto plugin and x00's other plugins - all write to database

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Awesome. Yes I will continue to muddle through it until either it is done or someone accepts the job!

  • Options

    also the flagging plugin or split merge plugins might be worth looking at for ideas.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    edited August 2012

    Ok. Table updates upon instalation of plugin with the "Rating" Column. I have added this code to render the list of "stars" in the Post View:

    In Default.php

     public function PostController_BeforeFormButtons_Handler($Sender){
    
        $i=0;
        $rating=5;
        while ( $i != 5 ){          
    
            if ( $i != $rating )
                echo '<input class="'.$discussionID.'  star" type="radio" value="'.$i.'" />';
            else
                echo '<input class="'.$discussionID.' star" type="radio" value="'.$i.'" checked="checked" />';
            $i++;
        }
    }
    

    Works pretty good.

    Now the XHTML is changed during Parse via javaScript to this:

     <span class="star-rating-control">
          <div class="rating-cancel">
               <a title="Cancel Rating"></a>
          </div>
          <div class="star-rating rater-0 star star-rating-applied star-rating-live star-rating-on">
               <a title="0">0</a>
          </div>
          <div class="star-rating rater-0 star star-rating-applied star-rating-live star-rating-on">
               <a title="1">1</a>
          </div>
          <div class="star-rating rater-0 star star-rating-applied star-rating-live star-rating-on">
               <a title="2">2</a>
          </div>
          <div class="star-rating rater-0 star star-rating-applied star-rating-live">
               <a title="3">3</a>
          </div>
     </span>     
     <input class=" star star-rating-applied" type="radio" value="0" style="display: none;">
     <input class=" star star-rating-applied" type="radio" value="1" style="display: none;">
     <input class=" star star-rating-applied" type="radio" value="2" style="display: none;">
     <input class=" star star-rating-applied" type="radio" value="3" style="display: none;">
     <input class=" star star-rating-applied" type="radio" value="4" style="display: none;">
    

    Notice the inputs are switched to hidden.
    If a DIV is "checked" by the user, the a class .star-rating-on is added. Alternatively the hidden input associated with the DIV is given an XHTML property of 'Checked="checked"'
    i.e.

     <input class=" star star-rating-applied" type="radio" value="4" style="display: none;" Checked="checked">     
    

    I can't figure out how to send the 'value=n' to be stored in the database under Discussion.Rating where discussionID = this.DiscussionID

    My latest feeble attempt:

     public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
    
        $DiscussionData = $DiscussionModel->GetWhere(array('DiscussionID' => $DiscussionID));
        $Discussion = $DiscussionData->FirstRow('', DATASET_TYPE_ARRAY);
        $Discussioin['Rating'] = 3;
        $DiscussionModel->Save($Discussion);    
    
    }   
    

    I relaize I also need (In bad sudocode):

     $Discussion['Rating'] = input.isChecked().value
    

    Any Ideas, docs, ManPages(lol) you can point me to?

  • Options

    updated package

  • Options
    jaredNZjaredNZ New
    edited March 2013

    @SonicNebula -- I know this is a dead thread, but how does the handler know the DiscussionID? is this a global?


    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
    $DiscussionData = $DiscussionModel->GetWhere(array('DiscussionID' => $DiscussionID));
    $Discussion = $DiscussionData->FirstRow('', DATASET_TYPE_ARRAY);
    $Discussioin['Rating'] = 3;
    $DiscussionModel->Save($Discussion);
    }

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @jaredNZ said:
    SonicNebula -- I know this is a dead thread, but how does the handler know the DiscussionID? is this a global?

    No, it must be retrieved from the event arguments (it's contained in $Sender->EventArguments['DiscussionID']).
    If the purpose of the code is to add a rating to the values before saving the Discussion, the handler can be rewritten as follows:

    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
      $FormPostValues = &$Sender->EventArguments['FormPostValues'];
      $FormPostValues['Rating'] = 3;
    }  
    

    That's it. The model will automatically save the value to Rating fields.

  • Options

    thanks @businessdad

Sign In or Register to comment.