We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46220
    • 66 Posts
    Hi there,

    I've been working on a website using discuss as one of the pillars. Now that I have the basics in place, I would like to implement the extra's and now I run into a few problems. My problem is that I have no background in IT or web development, everything I have done is self taught and with loads of help from the MODx community.

    I actually have 2 questions for now (see gc.haulehuisje.nl for visual support):

    1. is it possible to get the most popular threads into the sidebar?
    2. is it possible to mark threads as featured (and remove it when I want), and show into the sidebar?

    Hope any of you is able to help?

    Thanks!
    Coen

    This question has been answered by gormytorysh. See the first response.

      • 46886
      • 1,154 Posts
      There are two solutions in the forums, one is forums.modx.com/thread/81757/recent-posts and the other is http://forums.modx.com/thread/85888/can-i-display-latest-forum-posts-on-pages-outside-of-discuss.

      Looks like you already have it on your front page now, did you use one of these two?
        • 46220
        • 66 Posts
        I have the most recent posts on my home page. Now I would like to add a feature thread option and a most popular thread option. I am just taking my first steps into php, which is hard enough as it is for me wink, though I want to learn by trying it. So pointers into a direction is very helpful and much appreciated.

        I'll look at the code used for recent posts and try to adjust it just to be able to use it for most popular threads.
          • 46886
          • 1,154 Posts
          You are ahead of me by a bit, I am avoiding php so far haha. What did you use for recent posts?

          If this one works, it would seem easy enough to change to hot or popular posts, by changing the "orderby" array:
          <?php
          $discuss = $modx->getService('discuss','Discuss',$modx->getOption('discuss.core_path',null,$modx->getOption('core_path').'components/discuss/').'model/discuss/');
          if (!($discuss instanceof Discuss)) return true;
           
          $c = $modx->newQuery('disPost');
          $c->where(array('private' => 0));
          $c->limit(10);
          $c->orderby('createdon', 'DESC');
           
          $posts = $modx->getCollection('disPost', $c);
           
          foreach ($posts as $post) {
              $temp = $post->toArray();
              $temp['url'] = $post->getUrl();
              // call chunks or what you want
              $out[] = $modx->getChunk('rowTpl', $temp);
          }
          return implode("\n", $out);


          That's from Petri, one of the modx engineers (Dunnock)

          But I don't know the values to use for the ordering. I did notice in the system settings they have an item which is something like "enable hot posts" or something related. But I don't know how to utilize it.
            • 46220
            • 66 Posts
            I used the code entered in http://forums.modx.com/thread/81757/recent-posts#dis-post-499186 and altered it by replacing disPost with disThread and set sortby to replies, desc. It is deffinately not a clean code and I am not there yet, but it works for now.

            This is what I have done:

            I created a snippet called Discuss.Popular:

            <?php
            $weghalen = array(",", "/", "?", "<", ">", ";", ":", "[", "]", "{", "}", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "=");
            $vervangen = array(" ", ".");
             
            $c = $modx->newQuery('modUser');
            $c->leftJoin('modUserProfile','Profile');
            $c->sortby('id','ASC');
            // add column names that u want to show
            $c->select(array(
                'modUser.*',
                'Profile.fullname',
                'Profile.email',
            ));
              
            $users = $modx->getCollection("modUser",$c);
            $naam[0] = 'Anoniem';
            foreach($users as $user){
                $usr = $user->toArray();
             
            $naam[$usr['id']] = $usr['username'];
            $email[$usr['id']] = $usr['email'];
            }
             
            $tpl = $modx->getOption('tpl',$scriptProperties,'Forumthreads.Popular');
             
            $package_path = $modx->getOption('core_path').'components/discuss/model/';
            $modx->addPackage('discuss', $package_path);
            $vraag = $modx->newQuery('disThread');
            $vraag->sortby('replies','DESC');
            $vraag->where(array(
            ));
            $vraag->limit(6,0);
             
            $regels = $modx->getCollection('disThread',$vraag);
            $output = '';
             
            foreach($regels as $regel){
            $rglArray['post']= $regel->get('id');
            $rglArray['thread']= $regel->get('thread');
            $titel = strtolower($regel->get('title'));
            if(substr($titel, 0,4)=="re: "){$titel = substr($titel,4);}
            $titel= str_replace($vervangen, "-", str_replace($weghalen, "", $titel));
            if(substr($titel,-1)=="-"){$titel = substr($titel,0,-1);}
            $rglArray['threadname'] = $titel;
            //$rglArray['threadname'] = $titel2;
            $rglArray['title']= $regel->get('title');
            $rglArray['message']= $regel->get('message');
            $rglArray['author'] = $naam[$regel->get('author')];
            $rglArray['createdon']= $regel->get('createdon');
            $rglArray['gravatar'] = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email[$regel->get('author')])))."?d=mm&s=50";
            $rglArray['email'] = $email[$regel->get('author')];
            $rglArray['nummer'] =$nummer;
             
            $output .= $modx->getChunk($tpl,$rglArray);
            }
            return $output;


            Then created a Chunk called Forumthreads.Popular:

            <ul>
            <li> 
            <h5><a href="/forums/thread/[[+thread]]/[[+threadname]]#dis-post-[[+post]]">[[+title]]</a></h5>
            <p style='margin-top:-15px'>[[+message:strip:stripTags:ellipsis=`50`]]</p>
                                <div class="clear"></div>
            </li>
            </ul>


            In the page template it is called like:

            [[!Discuss.Popular]]


            The basic, but effective, test was; create a new thread (I had 1 thread with 2 posts) and have it contain 3 posts. It jumped from 2nd place to 1st place. So it does work, but not is not entirely what I am looking for. Having said that, the basics are there and I have something to work from.

            EDIT: I forgot to say thanks to mintnl, so here it is! (Dank je wel!! smiley)
              • 46886
              • 1,154 Posts
              Wow! Its a huge step forward! Congrats on doing this!

              We will get there, I am sure. Finally, within a short time here, this tool will be refined or replaced.

              I want to mention something I noticed when using mintnl's code directly, he had kept out boards 9 and 10 as they were private, and with no change private pms between users also are visible

              Yeah, modx is so strong in NL, I am here because my partner is from NL and is the tech guy, and when we started he recommended modx for our website ;-)

              By the way, if you see any bugs, don't be afraid to go over to github and document them. I will be making a bunch real soon.
                • 46886
                • 1,154 Posts
                The part of the code where you tell it what factors to use is here:

                $tpl = $modx->getOption('tpl',$scriptProperties,'Forumthreads.Popular');
                  
                $package_path = $modx->getOption('core_path').'components/discuss/model/';
                $modx->addPackage('discuss', $package_path);
                $vraag = $modx->newQuery('disThread');
                $vraag->sortby('replies','DESC');
                $vraag->where(array(
                ));
                $vraag->limit(6,0);


                I don't understand fully, and may get some parts wrong, (signified by ?) but it goes like this:

                Go get popular threads's template and path to file ?
                Addpackage - Go get discuss (remember, this is on a page outside discuss) part of database and path to file
                Query the db - go get the Discuss threads for my query, pls,
                Sort - Now get what I want, which is replies (going from most to least, DESC in other words)
                Now give me top 6, pls.

                Then finally, getCollection, is, ok, go really do it. For real now. Then output.

                And the limitation about which parts of the db to focus on that mintnl had should go in there too, probably after getting the discuss part of the db but maybe before.

                This what my point before. That could change to likes, DESC, (I plan to implement likes), or age, DESC, or anything really, maybe something like this:

                $vraag->sortby('most_replies_within_last_day','DESC');


                Which isn't how it would be, because most_replies_within_last_day here *should* be the name of the data column in the db, if that makes sense. So it would probably be some php to sort in this fashion.

                $vraag = $modx->newQuery('disThread');


                is also interesting, because it could be disPost I would expect, and maybe some other terms. Do we want hot *threads* or hot *posts*. And right there, might be another capability...and there might be more, possibly...
                  • 46220
                  • 66 Posts
                  Quote from: nuan88 at Jun 12, 2014, 05:11 AM
                  Wow! Its a huge step forward! Congrats on doing this!

                  I want to mention something I noticed when using mintnl's code directly, he had kept out boards 9 and 10 as they were private, and with no change private pms between users also are visible

                  Thanks! I deleted boards 9 and 10, because I assumed that these boards are private ones in his forum. So is it true that private pm's are visible now? How does that work?

                  Quote from: nuan88 at Jun 12, 2014, 05:44 AM
                  I don't understand fully, and may get some parts wrong, (signified by ?) but it goes like this:

                  Go get popular threads's template and path to file ?
                  Addpackage - Go get discuss (remember, this is on a page outside discuss) part of database and path to file
                  Query the db - go get the Discuss threads for my query, pls,
                  Sort - Now get what I want, which is replies (going from most to least, DESC in other words)
                  Now give me top 6, pls.

                  Then finally, getCollection, is, ok, go really do it. For real now. Then output.

                  That is how I read it as well and as said, I am no expert.... I think I don't even qualify as a beginner when it comes to php.

                  Quote from: nuan88 at Jun 12, 2014, 05:44 AM
                  Which isn't how it would be, because most_replies_within_last_day here *should* be the name of the data column in the db, if that makes sense. So it would probably be some php to sort in this fashion.

                  Let's figure it out next to see what more is possible. [ed. note: gormytorysh last edited this post 9 years, 11 months ago.]
                    • 46886
                    • 1,154 Posts
                    Yeah we have to exclude private messages somehow. It seems that they are just threads like any other thread, but should have some private flag on them.

                    I noticed the code from Petri that we never fixed has a line that might be useful:
                    $c = $modx->newQuery('disPost');
                    $c->where(array('private' => 0));
                    $c->limit(10);
                    $c->orderby('createdon', 'DESC');


                    Comparing it to the tool which works from mintnl:

                    $modx->addPackage('discuss', $package_path);
                    $vraag = $modx->newQuery('disThread');
                    $vraag->sortby('replies','DESC');
                    $vraag->where(array(
                    ));
                    $vraag->limit(6,0);


                    So I guess it would end up being something like this:

                    $modx->addPackage('discuss', $package_path);
                    $vraag = $modx->newQuery('disThread');
                    $vraag->where(array('private' => 0));
                    $vraag->sortby('replies','DESC');
                    $vraag->where(array(
                    ));
                    $vraag->limit(6,0);


                    That should do it...
                      • 46886
                      • 1,154 Posts
                      Also notice Petri's code used orderby while mintnl's uses sortby, wonder if that's the problem with Petri's, as both requests are basically the same.