/**
  * 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();
         }
     }
 }