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.

Sort Discussions By Vote

Hey, I am slowly starting to understand vanilla a bit better, however I am unable the following Problem.
I enabled tagging and voting in my forum.I would like to do the following:
Search for a tag
Display the discussion with the tag sorted by the best votes.
Thanks in advance



  • dominikldominikl New
    edited December 2015

    I am trying to phrase my question more precise. By default all discussions are sorted by the date they where last changed. Only under the section popular, the discussions are orderd by their votes.
    In my forum I always want discussions to be sorted by the votes, not just in the popular section, but also when I am displayed search results or when I am looking at discussions in a category.
    Since the feature that I am looking for already exists and I only want to apply it globally I thought it should be quite simple.

  • R_JR_J Ex-Fanboy Munich Admin

    I think using the following config settings in your /conf/config.php should do what you like:

    $Configuration['Vanilla']['Discussions']['SortField'] = 'Score';
    $Configuration['Vanilla']['Discussions']['SortDirection'] = 'desc';
  • dominikldominikl New
    edited December 2015

    Hey RJ thanks a lot for answering, your solution is almost working.
    I uploaded 3 screenshots from my forum to show where it is working and where it isn´t. I will go from left to right.
    1. picture
    Under /discussions/mine all my discussions are ordered by vote great!.
    2. picture
    If I search for a tagg the search results are not ordered by vote but by the date.
    Under category/examplecategory the discussions are not order by vote but by the date, in this instance I think this has to do with the fact, that it is by default in a "Recent Discussion Mode" as indicated by the blue shading of the "Recent Discussion" Button.

  • R_JR_J Ex-Fanboy Munich Admin

    Normally database access is made through a "model" just a convention of calling a class like that. The framework Vanilla is based on has a model class which is able to get data from a table. The DiscussionModel has all the ability the underlying model has and adds some more features. It also uses the config setting "SortField".
    The tagging plugin has its own model which doesn't need to have all the features of the discussionModel. So it only uses the base model, which doesn't even "know" about any SortField config possibilities.

    Any other plugin that gets the discussions directly by accessing the db will most probably not respect that config setting, too. I think something like that is a good example why using an existing model if possible should always be preferred. But after all, it has not been done.

    I'm not sure if replacing class TagModel extends Gdn_Model { with class TagModel extends DiscussionModel { in the file /plugins/Tagging/class.Tagmodel.php would be all you need or make your board unusable, but I would definitely try it ;)
    The more I think the more I guess it wouldn't help, so you're lost...
    Well not lost, but it would take some coding skills to mix what you need from the tagging model getWhere() and the discussionModel getWhere() functions.

    I have not taken a closer look at the categories, but I would expect that the categoryModel also implements its own way to load discussions from db. That it does not use the SortField setting is an inconsistency to me. But I do not think that there will be any action taken from the Vanilla staff soon to correct this. There are far more important things on the roadmap. But I'm sure if you come up with a pull request, they will be very thankful!

  • dominikldominikl New
    edited December 2015

    Unfortunatly you where right and changing Gdn_Model to Discussion Model did not work. But thank you alot for all the information. Do you know in which folder I can find the Gdn_Model, I would like to take a look at it and see if I can change it =D

  • R_JR_J Ex-Fanboy Munich Admin

    Look at the code, it is really helpful, but don't change it. It will cause you a lot of pain. I've said it more than one time in English so here it is in German for future reference ;)

    Wenn neue Updates verfügbar sind, sollten diese als Gesamtpaket über die bestehende Installation kopiert werden (mit Ausnahme der .htaccess, falls diese angepasst wurde). Alle anderen Dateien können jederzeit neu überschrieben werden. Das macht es sehr einfach ein Update einzuspielen oder eine total verkorkste Installation wieder mit einem Schwung zum Laufen zu bringen.
    Deshalb werden config Einträge auch nicht in der config-defaults geändert (was durchaus möglich wäre), sondern in einer eigenen config Datei überschrieben.

    Wenn du die original Dateien änderst, gehen deine Änderungen mit jedem Update verloren. Du müsstest sie also immer wieder manuell nachbessern.
    Falls du auf ein Problem stößt und hier nach Support fragst, kann es sein dass die Ursache deine manuellen Änderungen sind. Das kann aber keiner wissen und deshalb hätte ich persönlich überhaupt keine Lust mich näher mit Problemen zu befassen, die ggf. durch selbstgestrickte "Workarounds" auftauchen.

    Beinahe alles lässt sich in Vanilla mit Plugins lösen.

    Start with looking at the method getWhere() in plugins/Tagging/class.tagmodel.php and applications/vanilla/models/class.discussionmodel.php and look how discussionModel uses the config setting. Add some lines of code to the tagmodel to see if you can get it working. Create a pull request on GitHub to make it official so that an updated Tagging plugin already has your enhancement so that you do not have to work on your piece of software. That's how you can benefit from open source.

    Don't hesitate to ask if you get stuck.

    The basic model is in /library/core/class.model.php. But please only study the code and don't alter it...

  • Thank you! Now I am able to sort the tags by vote (it was quite easy).
    One simply has to replace every $SortField = 'd.DateLastComment'; by $SortField = 'Score'; in the class.tagmodule.php. I will try to make a more elegant method but replacing by $SortField = self::GetSortField(); did not work

  • R_JR_J Ex-Fanboy Munich Admin

    self::GetSortField(); is a reference to a method in the class you are looking at. Replacing "self" with the name of the class should be enough.

    Try this instead:

    $SortField = DiscussionModel::GetSortField();
    $SortDirection = c('Vanilla.Discussions.SortDirection', 'desc');
  • Really Cool it works =)

Sign In or Register to comment.