/**
  * This function handles all aspects of page processing and then calls
  * methods in $selector at the appropriate moments.
  * @param post_selector $selector Object that extends this base class
  * @param string $rawurl Raw 'name' part of url e.g. '/mod/forumng/feature/frog/frog.php'
  */
 public static function go($selector)
 {
     global $PAGE, $FULLME;
     $d = required_param('d', PARAM_INT);
     $cloneid = optional_param('clone', 0, PARAM_INT);
     $PAGE->set_url($FULLME);
     $fromselect = optional_param('fromselect', 0, PARAM_INT);
     $all = optional_param('all', '', PARAM_RAW);
     $select = optional_param('select', '', PARAM_RAW);
     // Get basic objects
     $discussion = mod_forumng_discussion::get_from_id($d, $cloneid);
     if (optional_param('cancel', '', PARAM_RAW)) {
         // CALL TYPE 6
         redirect('../../discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN));
     }
     $forum = $discussion->get_forum();
     $cm = $forum->get_course_module();
     $course = $forum->get_course();
     $isform = optional_param('postselectform', 0, PARAM_INT);
     // Page name and permissions
     $pagename = $selector->get_page_name();
     $buttonname = $selector->get_button_name();
     $discussion->require_view();
     $selector->require_capability($forum->get_context(), $discussion);
     if (!($fromselect || $isform || $all)) {
         // Either an initial request (non-JS) to display the 'dialog' box,
         // or a request to show the list of posts with checkboxes for
         // selection
         // Both types share same navigation
         $out = $discussion->init_page($discussion->get_moodle_url(), $pagename);
         print $out->header();
         if (!$select) {
             // Show initial dialog
             print $out->box_start();
             print html_writer::tag('h2', $buttonname);
             print html_writer::start_tag('form', array('action' => $_SERVER['PHP_SELF'], 'method' => 'get'));
             print html_writer::start_tag('div');
             print $discussion->get_link_params(mod_forumng::PARAM_FORM);
             print html_writer::tag('p', get_string('selectorall', 'forumng'));
             print html_writer::start_tag('div', array('class' => 'forumng-buttons'));
             print html_writer::empty_tag('input', array('name' => 'all', 'type' => 'submit', 'value' => get_string('discussion', 'forumng')));
             print html_writer::empty_tag('input', array('name' => 'select', 'type' => 'submit', 'value' => get_string('selectedposts', 'forumng')));
             print html_writer::end_tag('div');
             print html_writer::end_tag('div');
             print html_writer::end_tag('form');
             print $out->box_end();
         } else {
             // Show list of posts to select
             print html_writer::start_tag('div', array('class' => 'forumng-selectintro'));
             print html_writer::tag('p', get_string('selectintro', 'forumng'));
             print html_writer::end_tag('div');
             print html_writer::start_tag('form', array('action' => $_SERVER['PHP_SELF'], 'method' => 'post'));
             print html_writer::start_tag('div');
             print $discussion->get_link_params(mod_forumng::PARAM_FORM);
             print html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'fromselect', 'value' => '1'));
             print $out->render_discussion($discussion, array(mod_forumng_post::OPTION_NO_COMMANDS => true, mod_forumng_post::OPTION_CHILDREN_EXPANDED => true, mod_forumng_post::OPTION_SELECTABLE => true));
             print html_writer::start_tag('div', array('class' => 'forumng-selectoutro'));
             print html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('confirmselection', 'forumng')));
             print html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel')));
             print html_writer::end_tag('div');
             print html_writer::end_tag('div');
             print html_writer::end_tag('form');
         }
         // Display footer
         print $out->footer();
     } else {
         // Call types 3, 4, and 5 use the form (and may include list of postids)
         if ($all) {
             $postids = false;
         } else {
             $postids = array();
             foreach ($_POST as $field => $value) {
                 $matches = array();
                 if (!is_array($value) && (string) $value !== '0' && preg_match('~^selectp([0-9]+)$~', $field, $matches)) {
                     $postids[] = $matches[1];
                 }
             }
         }
         $out = $discussion->init_page($discussion->get_moodle_url(), $pagename);
         // Get form to use
         $mform = $selector->get_form($discussion, $all, $postids);
         if (!$mform) {
             // Some options do not need a confirmation form; in that case,
             // just apply the action immediately.
             $selector->apply($discussion, $all, $postids, null);
             exit;
         }
         // Check cancel
         if ($mform->is_cancelled()) {
             redirect('../../discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN));
         }
         if ($fromform = $mform->get_data()) {
             // User submitted form to confirm process, which should now be
             // applied by selector.
             $selector->apply($discussion, $all, $postids, $fromform);
             exit;
         } else {
             print $out->header();
             // User requested form either via JavaScript or the other way, and
             // either with all messages or the whole discussion.
             // Print form
             print $mform->display();
             // Print optional content that goes after form
             print $selector->get_content_after_form($discussion, $all, $postids, $fromform);
             // Display footer
             print $out->footer();
         }
     }
 }
    /**
     * This function handles all aspects of page processing and then calls
     * methods in $selector at the appropriate moments.
     * @param post_selector $selector Object that extends this base class
     */
    static function go($selector)
    {
        $d = required_param('d', PARAM_INT);
        $cloneid = optional_param('clone', 0, PARAM_INT);
        $fromselect = optional_param('fromselect', 0, PARAM_INT);
        $all = optional_param('all', '', PARAM_RAW);
        $select = optional_param('select', '', PARAM_RAW);
        try {
            // Get basic objects
            $discussion = forum_discussion::get_from_id($d, $cloneid);
            if (optional_param('cancel', '', PARAM_RAW)) {
                // CALL TYPE 6
                redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN));
            }
            $forum = $discussion->get_forum();
            $cm = $forum->get_course_module();
            $course = $forum->get_course();
            $isform = optional_param('postselectform', 0, PARAM_INT);
            // Page name and permissions
            $pagename = $selector->get_page_name();
            $buttonname = $selector->get_button_name();
            $discussion->require_view();
            $selector->require_capability($forum->get_context(), $discussion);
            if (!($fromselect || $isform || $all)) {
                // Either an initial request (non-JS) to display the 'dialog' box,
                // or a request to show the list of posts with checkboxes for
                // selection
                // Both types share same navigation
                $discussion->print_subpage_header($pagename);
                if (!$select) {
                    // Show initial dialog
                    print_box_start();
                    ?>
<h2><?php 
                    print $buttonname;
                    ?>
</h2>
<form action="<?php 
                    echo $_SERVER['PHP_SELF'];
                    ?>
" method="get"><div>
<?php 
                    print $discussion->get_link_params(forum::PARAM_FORM);
                    ?>
<p><?php 
                    print_string('selectorall', 'forumng');
                    ?>
</p>
<div class="forumng-buttons">
<input type="submit" name="all" value="<?php 
                    print_string('discussion', 'forumng');
                    ?>
" />
<input type="submit" name="select" value="<?php 
                    print_string('selectedposts', 'forumng');
                    ?>
" />
</div>
</div></form>
<?php 
                    print_box_end();
                } else {
                    // Show list of posts to select
                    ?>
<div class="forumng-selectintro">
  <p><?php 
                    print_string('selectintro', 'forumng');
                    ?>
</p>
</div>
<form action="<?php 
                    echo $_SERVER['PHP_SELF'];
                    ?>
" method="post"><div>
<?php 
                    print $discussion->get_link_params(forum::PARAM_FORM);
                    ?>
<input type="hidden" name="fromselect" value="1" />
<?php 
                    print $forum->get_type()->display_discussion($discussion, array(forum_post::OPTION_NO_COMMANDS => true, forum_post::OPTION_CHILDREN_EXPANDED => true, forum_post::OPTION_SELECTABLE => true));
                    ?>
<div class="forumng-selectoutro">
<input type="submit" value="<?php 
                    print_string('confirmselection', 'forumng');
                    ?>
" />
<input type="submit" name="cancel" value="<?php 
                    print_string('cancel');
                    ?>
" />
</div>
</div></form>
<?php 
                }
                // Display footer
                print_footer($course);
            } else {
                // Call types 3, 4, and 5 use the form (and may include list of postids)
                if ($all) {
                    $postids = false;
                } else {
                    $postids = array();
                    foreach ($_POST as $field => $value) {
                        $matches = array();
                        if ((string) $value !== '0' && preg_match('~^selectp([0-9]+)$~', $field, $matches)) {
                            $postids[] = $matches[1];
                        }
                    }
                }
                // Get form to use
                $mform = $selector->get_form($discussion, $all, $postids);
                if (!$mform) {
                    // Some options do not need a confirmation form; in that case,
                    // just apply the action immediately.
                    $selector->apply($discussion, $all, $postids, null);
                    exit;
                }
                // Check cancel
                if ($mform->is_cancelled()) {
                    redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN));
                }
                if ($fromform = $mform->get_data()) {
                    // User submitted form to confirm process, which should now be
                    // applied by selector.
                    $selector->apply($discussion, $all, $postids, $fromform);
                    exit;
                } else {
                    $discussion->print_subpage_header($pagename);
                    // User requested form either via JavaScript or the other way, and
                    // either with all messages or the whole discussion.
                    // Print form
                    print $mform->display();
                    // Print optional content that goes after form
                    print $selector->get_content_after_form($discussion, $all, $postids, $fromform);
                    // Display footer
                    print_footer($course);
                }
            }
        } catch (forum_exception $e) {
            forum_utils::handle_exception($e);
        }
    }
 /**
  * This function handles all aspects of page processing and then calls
  * methods in $selector at the appropriate moments.
  * @param post_selector $selector Object that extends this base class
  */
 public static function go($selector)
 {
     global $PAGE, $FULLME, $SESSION;
     $id = required_param('id', PARAM_INT);
     $cloneid = optional_param('clone', 0, PARAM_INT);
     $fromselect = optional_param('fromselect', 0, PARAM_INT);
     $all = optional_param('all', '', PARAM_RAW);
     $select = optional_param('select', '', PARAM_RAW);
     $isform = optional_param('postselectform', 0, PARAM_INT);
     $PAGE->set_url($FULLME);
     // Get basic objects.
     $forum = mod_forumng::get_from_cmid($id, $cloneid);
     $forumngid = $forum->get_id();
     $params = array_merge($_REQUEST, $forum->get_link_params_array());
     if (optional_param('cancel', '', PARAM_RAW)) {
         // CALL TYPE 6.
         redirect('../../view.php?' . $forum->get_link_params(mod_forumng::PARAM_PLAIN));
     }
     $cm = $forum->get_course_module();
     $course = $forum->get_course();
     $groupid = mod_forumng::get_activity_group($cm, true);
     // Page name and permissions.
     $pagename = $selector->get_page_name();
     $buttonname = $selector->get_button_name();
     $forum->require_view($groupid);
     $selector->require_capability($forum->get_context(), $forum);
     if (!($fromselect || $isform || $all)) {
         // Either an initial request (non-JS) to display the 'dialog' box,
         // or a request to show the list of posts with checkboxes for selection.
         // Both types share same navigation.
         $out = $forum->init_page(new moodle_url('/mod/forumng/view.php', $forum->get_link_params_array()));
         print $out->header();
         if (!$select) {
             // Show initial dialog.
             print $out->box_start();
             print html_writer::tag('h2', $buttonname);
             print html_writer::start_tag('form', array('action' => $_SERVER['PHP_SELF'], 'method' => 'get', 'id' => 'discsel'));
             print html_writer::start_tag('div');
             foreach ($params as $param => $paramval) {
                 print html_writer::empty_tag('input', array('name' => $param, 'type' => 'hidden', 'value' => $paramval));
             }
             print html_writer::tag('p', get_string('selectordiscall', 'forumng'));
             print html_writer::start_tag('div', array('class' => 'forumng-buttons'));
             print html_writer::empty_tag('input', array('name' => 'all', 'type' => 'submit', 'value' => get_string('selectoralldisc', 'forumng')));
             print html_writer::empty_tag('input', array('name' => 'select', 'type' => 'submit', 'value' => get_string('selectorselecteddisc', 'forumng')));
             print html_writer::empty_tag('input', array('name' => 'cancel', 'type' => 'submit', 'value' => get_string('cancel')));
             print html_writer::end_tag('div');
             print html_writer::end_tag('div');
             print html_writer::end_tag('form');
             print $out->box_end();
         } else {
             // Show list of posts to select.
             print html_writer::start_tag('div', array('class' => 'forumng-selectintro'));
             print html_writer::tag('p', get_string('selectdiscintro', 'forumng'));
             print html_writer::end_tag('div');
             print html_writer::start_tag('form', array('action' => $_SERVER['PHP_SELF'], 'method' => 'post', 'id' => 'discsel'));
             print html_writer::start_tag('div');
             print $forum->get_link_params(mod_forumng::PARAM_FORM);
             print html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'fromselect', 'value' => '1'));
             foreach ($params as $param => $paramval) {
                 print html_writer::empty_tag('input', array('name' => $param, 'type' => 'hidden', 'value' => $paramval));
             }
             // Now show discussions, allow for override at type level if following is no good.
             if (method_exists($forum->get_type(), 'print_select_page')) {
                 print $forum->get_type()->print_select_page($forum, $groupid);
             } else {
                 // Use default processing, get view and hack in selectors.
                 ob_start();
                 $forum->get_type()->print_view_page($forum, $groupid);
                 $discussionhtml = ob_get_contents();
                 ob_end_clean();
                 // Try and hack into the discussion list - must be xhtml...
                 $doc = new DOMDocument('1.0', 'utf-8');
                 @$doc->loadHTML($discussionhtml);
                 $docnew = new DOMDocument('1.0', 'utf-8');
                 $xpath = new DOMXPath($doc);
                 $lists = $xpath->query("//table[contains(concat(' ',normalize-space(@class),' '),' forumng-discussionlist ')]");
                 // Remove all links.
                 foreach ($lists as $list) {
                     $links = $xpath->query("//a|//form", $list);
                     foreach ($links as $node) {
                         if ($node->nodeName == 'a') {
                             // Disable links.
                             $node->removeAttribute('href');
                         } else {
                             // Remove any forms.
                             $node->parentNode->removeChild($node);
                         }
                     }
                     // Add in discussion select.
                     $rows = $xpath->query("//table[@class='generaltable forumng-discussionlist']\n                                //tr[not(@class) or @class!='forumng-divider']", $list);
                     for ($a = 0, $len = $rows->length; $a < $len; $a++) {
                         // Add in select options for each row, checking types.
                         $row = $rows->item($a);
                         if ($a == 0) {
                             $newcell = $doc->createElement('th', get_string('selectorselectdisc', 'mod_forumng'));
                             $newcell->setAttribute('class', 'header');
                             $newcell->setAttribute('scope', 'col');
                             $row->appendChild($newcell);
                         } else {
                             $id = $row->getAttribute('id');
                             if (strpos($id, 'discrow') === false) {
                                 continue;
                             }
                             // Get discussion id from row id as added by renderer.
                             $id = str_replace('discrow_', '', $id);
                             // Check if we include checkbox or not.
                             $classar = explode(' ', $row->getAttribute('class'));
                             $includematches = array_intersect($selector->only_discussion_types(), $classar);
                             $excludematches = array_intersect($selector->exclude_discussion_types(), $classar);
                             if ((count($selector->only_discussion_types()) == 0 || count($includematches) > 0) && count($excludematches) == 0) {
                                 // OK to include, add checkbox and label.
                                 $select = $doc->createElement('input');
                                 $select->setAttribute('type', 'checkbox');
                                 $select->setAttribute('name', "selectd{$id}");
                                 $select->setAttribute('id', "selectd{$id}");
                                 $label = $doc->createElement('label', get_string('selectorselectdisc', 'mod_forumng'));
                                 $label->setAttribute('for', "selectd{$id}");
                                 $label->setAttribute('class', 'accesshide');
                                 $newcell = $doc->createElement('td');
                                 $newcell->setAttribute('class', 'dselect');
                                 $newcell->appendChild($select);
                                 $newcell->appendChild($label);
                                 $row->appendChild($newcell);
                             } else {
                                 $newcell = $doc->createElement('td', '&nbsp;');
                                 $row->appendChild($newcell);
                             }
                         }
                     }
                     // Keep only discussion list by moving to new xml doc.
                     $newnode = $docnew->importNode($list, true);
                     $docnew->appendChild($newnode);
                 }
                 print $docnew->saveHTML();
             }
             print html_writer::start_tag('div', array('class' => 'forumng-selectoutro'));
             print html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('confirmselection', 'forumng')));
             print html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel')));
             print html_writer::end_tag('div');
             print html_writer::end_tag('div');
             print html_writer::end_tag('form');
         }
         // Display footer.
         print $out->footer();
     } else {
         // Call types 3, 4, and 5 use the form (and may include list of postids).
         $postids = array();
         $selectedids = array();
         foreach ($_POST as $field => $value) {
             $matches = array();
             if (!is_array($value) && (string) $value !== '0' && preg_match('~^selectd([0-9]+)$~', $field, $matches)) {
                 $selectedids[] = $matches[1];
             }
         }
         if (!empty($selectedids)) {
             // Check access.
             foreach ($selectedids as $id) {
                 $discuss = mod_forumng_discussion::get_from_id($id, $cloneid);
                 if ($forum->get_type()->can_view_discussion($discuss)) {
                     $postids[] = $id;
                 }
             }
         } else {
             if (!$all) {
                 // No slections made.
                 redirect('../../view.php?' . $forum->get_link_params(mod_forumng::PARAM_PLAIN));
             }
             // Work out discussion list for this page (e.g. selected All).
             $sortorder = optional_param('sort', 'd', PARAM_ALPHA);
             if (isset($SESSION->forumng_discussionlist[$forumngid]->sort)) {
                 $sortorder = $SESSION->forumng_discussionlist[$forumngid]->sort;
             }
             $page = optional_param('page', 1, PARAM_INT);
             if (isset($SESSION->forumng_discussionlist[$forumngid]->page)) {
                 $page = $SESSION->forumng_discussionlist[$forumngid]->page;
             }
             $sortchar = substr($sortorder, 0, 1);
             if (strlen($sortorder) == 2) {
                 $sortreverse = substr($sortorder, 1, 1) == 'r' ? true : false;
             } else {
                 $sortreverse = false;
             }
             $sort = mod_forumng::get_sort_code($sortchar);
             $list = $forum->get_discussion_list($groupid, $forum->can_view_hidden(), $page, $sort, $sortreverse);
             $discussionsarr = array_merge($list->get_sticky_discussions(), $list->get_normal_discussions());
             // Double check ID is valid and user can view.
             for ($a = 0; $a < count($discussionsarr); $a++) {
                 if ($forum->get_type()->can_view_discussion($discussionsarr[$a])) {
                     $postids[] = $discussionsarr[$a]->get_id();
                 }
             }
         }
         $out = $forum->init_page(new moodle_url('/mod/forumng/view.php', $forum->get_link_params_array()), $pagename);
         // Get form to use.
         $mform = $selector->get_form($forum, $all, $postids);
         if (!$mform) {
             // Some options do not need a confirmation form; in that case,
             // just apply the action immediately.
             $selector->apply($forum, $all, $postids, null);
             exit;
         }
         // Check cancel.
         if ($mform->is_cancelled()) {
             redirect('../../view.php?' . $forum->get_link_params(mod_forumng::PARAM_PLAIN));
         }
         if ($fromform = $mform->get_data()) {
             // User submitted form to confirm process, which should now be
             // applied by selector.
             $selector->apply($forum, $all, $postids, $fromform);
             exit;
         } else {
             print $out->header();
             // User requested form either via JavaScript or the other way, and
             // either with all messages or the whole discussion.
             // Print form.
             print $mform->display();
             // Print optional content that goes after form.
             print $selector->get_content_after_form($forum, $all, $postids, $fromform);
             // Display footer.
             print $out->footer();
         }
     }
 }