function definition()
 {
     global $CFG, $COURSE;
     $mform =& $this->_form;
     $coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id);
     $forumng = $this->_instance ? get_record('forumng', 'id', $this->_instance) : null;
     $this->clone = $forumng ? $forumng->originalcmid : 0;
     // If this is a clone, don't show the normal form
     if ($this->clone) {
         $mform->addElement('hidden', 'name', $forumng->name);
         $mform->addElement('static', 'sharedthing', '', get_string('sharedinfo', 'forumng', $CFG->wwwroot . '/course/modedit.php?update=' . $this->clone . '&return=1'));
         $this->shared_definition_part($coursecontext);
         return;
     }
     $mform->addElement('header', 'general', get_string('general', 'form'));
     // Forum name
     $mform->addElement('text', 'name', get_string('forumname', 'forumng'), array('size' => '64'));
     if (!empty($CFG->formatstringstriptags)) {
         $mform->setType('name', PARAM_TEXT);
     } else {
         $mform->setType('name', PARAM_CLEAN);
     }
     $mform->addRule('name', null, 'required', null, 'client');
     $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
     // Forum types
     $types = forum_type::get_all();
     $options = array();
     foreach ($types as $type) {
         if ($type->is_user_selectable()) {
             $options[$type->get_id()] = $type->get_name();
         }
     }
     $mform->addElement('select', 'type', get_string('forumtype', 'forumng'), $options);
     $mform->setHelpButton('type', array('forumtype', get_string('forumtype', 'forumng'), 'forumng'));
     $mform->setDefault('type', 'general');
     $mform->addElement('htmleditor', 'intro', get_string('forumintro', 'forumng'));
     $mform->setType('intro', PARAM_RAW);
     $mform->setHelpButton('intro', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton');
     // Subscription option displays only if enabled at site level
     if ($CFG->forumng_subscription == -1) {
         $options = forum::get_subscription_options();
         $mform->addElement('select', 'subscription', get_string('subscription', 'forumng'), $options);
         $mform->setDefault('subscription', forum::SUBSCRIPTION_PERMITTED);
         $mform->setHelpButton('subscription', array('subscription', get_string('subscription', 'forumng'), 'forumng'));
     } else {
         // Hidden element contains default value (not used anyhow)
         $mform->addElement('hidden', 'subscription', forum::SUBSCRIPTION_PERMITTED);
     }
     // Max size of attachments
     $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
     $choices[-1] = get_string('uploadnotallowed');
     $choices[0] = get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')';
     $mform->addElement('select', 'attachmentmaxbytes', get_string('attachmentmaxbytes', 'forumng'), $choices);
     $mform->setHelpButton('attachmentmaxbytes', array('attachmentmaxbytes', get_string('attachmentmaxbytes', 'forumng'), 'forumng'));
     $mform->setDefault('attachmentmaxbytes', $CFG->forumng_attachmentmaxbytes);
     //Email address for reporting unacceptable post for this forum, default is blank
     $mform->addElement('text', 'reportingemail', get_string('reportingemail', 'forumng'), array('size' => 48));
     $mform->setType('reportingemail', PARAM_NOTAGS);
     $mform->setHelpButton('reportingemail', array('reportingemail', get_string('reportingemail', 'forumng'), 'forumng'));
     // Atom/RSS feed on/off/discussions-only
     if ($CFG->enablerssfeeds && !empty($CFG->forumng_enablerssfeeds)) {
         if ($CFG->forumng_feedtype == -1 || $CFG->forumng_feeditems == -1) {
             $mform->addElement('header', '', get_string('feeds', 'forumng'));
         }
         if ($CFG->forumng_feedtype == -1) {
             $mform->addElement('select', 'feedtype', get_string('feedtype', 'forumng'), forum::get_feedtype_options());
             $mform->setDefault('feedtype', forum::FEEDTYPE_ALL_POSTS);
             $mform->setHelpButton('feedtype', array('feedtype', get_string('feedtype', 'forumng'), 'forumng'));
         }
         // Atom/RSS feed item count
         if ($CFG->forumng_feeditems == -1) {
             $mform->addElement('select', 'feeditems', get_string('feeditems', 'forumng'), forum::get_feeditems_options());
             $mform->setDefault('feeditems', 20);
             $mform->setHelpButton('feeditems', array('feeditems', get_string('feeditems', 'forumng'), 'forumng'));
         }
     }
     // Ratings header
     /////////////////
     $mform->addElement('header', '', get_string('ratings', 'forumng'));
     $mform->addElement('checkbox', 'enableratings', get_string('enableratings', 'forumng'));
     $mform->setHelpButton('enableratings', array('enableratings', get_string('enableratings', 'forumng'), 'forumng'));
     // Scale
     $mform->addElement('modgrade', 'ratingscale', get_string('scale'), null, true);
     $mform->disabledIf('ratingscale', 'enableratings', 'notchecked');
     $mform->setDefault('ratingscale', 5);
     // From/until times
     $mform->addElement('date_time_selector', 'ratingfrom', get_string('ratingfrom', 'forumng'), array('optional' => true));
     $mform->disabledIf('ratingfrom', 'enableratings', 'notchecked');
     $mform->addElement('date_time_selector', 'ratinguntil', get_string('ratinguntil', 'forumng'), array('optional' => true));
     $mform->disabledIf('ratinguntil', 'enableratings', 'notchecked');
     $mform->addElement('text', 'ratingthreshold', get_string('ratingthreshold', 'forumng'));
     $mform->setType('ratingthreshold', PARAM_INT);
     $mform->setDefault('ratingthreshold', 1);
     $mform->addRule('ratingthreshold', get_string('error_ratingthreshold', 'forumng'), 'regex', '/[1-9][0-9]*/', 'client');
     $mform->setHelpButton('ratingthreshold', array('ratingthreshold', get_string('ratingthreshold', 'forumng'), 'forumng'));
     $mform->disabledIf('ratingthreshold', 'enableratings', 'notchecked');
     // Grading
     $mform->addElement('select', 'grading', get_string('grade'), forum::get_grading_options());
     $mform->setDefault('grading', forum::GRADING_NONE);
     $mform->setHelpButton('grading', array('grading', get_string('grade'), 'forumng'));
     $mform->disabledIf('grading', 'enableratings', 'notchecked');
     // Blocking header
     //////////////////
     $mform->addElement('header', '', get_string('limitposts', 'forumng'));
     // Post dates
     $mform->addElement('date_time_selector', 'postingfrom', get_string('postingfrom', 'forumng'), array('optional' => true));
     $mform->addElement('date_time_selector', 'postinguntil', get_string('postinguntil', 'forumng'), array('optional' => true));
     // User limits
     $limitgroup = array();
     $limitgroup[] = $mform->createElement('checkbox', 'enablelimit', '');
     $options = forum::get_max_posts_period_options();
     $limitgroup[] = $mform->createElement('text', 'maxpostsblock', '', array('size' => 3));
     $limitgroup[] = $mform->createElement('static', 'staticthing', '', ' ' . get_string('postsper', 'forumng') . ' ');
     $limitgroup[] = $mform->createElement('select', 'maxpostsperiod', '', $options);
     $mform->addGroup($limitgroup, 'limitgroup', get_string('enablelimit', 'forumng'));
     $mform->disabledIf('limitgroup[maxpostsblock]', 'limitgroup[enablelimit]');
     $mform->disabledIf('limitgroup[maxpostsperiod]', 'limitgroup[enablelimit]');
     $mform->setHelpButton('limitgroup', array('enablelimit', get_string('enablelimit', 'forumng'), 'forumng'));
     $mform->setType('limitgroup[maxpostsblock]', PARAM_INT);
     $mform->setDefault('limitgroup[maxpostsblock]', '10');
     // Remove old discussion
     $options = array();
     $options[0] = get_string('removeolddiscussionsdefault', 'forumng');
     for ($i = 1; $i <= 36; $i++) {
         $options[$i * 2592000] = $i;
     }
     $mform->addElement('header', '', get_string('removeolddiscussions', 'forumng'));
     $mform->addElement('select', 'removeafter', get_string('removeolddiscussionsafter', 'forumng'), $options);
     $mform->setHelpButton('removeafter', array('removeolddiscussions', get_string('removeolddiscussions', 'forumng'), 'forumng'));
     $options = array();
     $options[0] = get_string('deletepermanently', 'forumng');
     $modinfo = get_fast_modinfo($COURSE);
     $targetforumid = $this->_instance ? $this->_instance : 0;
     // Add all in stances to drop down if the user can access them and it's not the same as the current forum
     if (array_key_exists('forumng', $modinfo->instances)) {
         foreach ($modinfo->instances['forumng'] as $info) {
             if ($info->uservisible && $targetforumid != $info->instance) {
                 $options[$info->instance] = $info->name;
             }
         }
     }
     $mform->addElement('select', 'removeto', get_string('withremoveddiscussions', 'forumng'), $options);
     $mform->disabledIf('removeto', 'removeafter', 'eq', 0);
     $mform->setHelpButton('removeto', array('withremoveddiscussions', get_string('withremoveddiscussions', 'forumng'), 'forumng'));
     // Sharing options are advanced and for administrators only
     if ($CFG->forumng_enableadvanced && has_capability('moodle/site:config', $coursecontext)) {
         $mform->addElement('header', '', get_string('sharing', 'forumng'));
         $mform->addElement('advcheckbox', 'shared', get_string('shared', 'forumng'));
         $mform->setHelpButton('shared', array('shared', get_string('shared', 'forumng'), 'forumng'));
         // Only when creating a forum, you can choose to make it a clone
         if (!$this->_instance) {
             $sharegroup = array();
             $sharegroup[] = $mform->createElement('checkbox', 'useshared', '');
             $sharegroup[] = $mform->createElement('text', 'originalcmidnumber', '');
             $mform->addGroup($sharegroup, 'usesharedgroup', get_string('useshared', 'forumng'));
             $mform->disabledIf('usesharedgroup[originalcmidnumber]', 'usesharedgroup[useshared]', 'notchecked');
             $mform->setHelpButton('usesharedgroup', array('useshared', get_string('useshared', 'forumng'), 'forumng'));
         }
     }
     // Do definition that is shared with clone version of form
     $this->shared_definition_part($coursecontext);
     if (count(forum_utils::get_convertible_forums($COURSE)) > 0 && !$this->_instance) {
         $mform->addElement('static', '', '', '<div class="forumng-convertoffer">' . get_string('offerconvert', 'forumng', $CFG->wwwroot . '/mod/forumng/convert.php?course=' . $COURSE->id) . '</div>');
     }
 }
 /**
  * Internal method. Queries for a number of forums, including additional
  * data about unread posts etc. Returns the database result.
  * @param array $cmids If specified, array of course-module IDs of desired
  *   forums
  * @param object $course If specified, course object
  * @param int $userid User ID, 0 = current user
  * @param int $unread Type of unread data to obtain (UNREAD_xx constant).
  * @param array $groups Array of group IDs to which the given user belongs
  *   (may be null if unread data not required)
  * @param array $aagforums Array of forums in which the user has
  *   'access all groups' (may be null if unread data not required)
  * @param array $viewhiddenforums Array of forums in which the user has
  *   'view hidden discussions' (may be null if unread data not required)
  * @return array Array of row objects
  */
 private static function query_forums($cmids = array(), $course = null, $userid, $unread, $groups, $aagforums, $viewhiddenforums)
 {
     global $CFG, $USER;
     if (!count($cmids) && !$course) {
         throw new forum_exception("forum::query_forums requires course id or cmids");
     }
     if (count($cmids)) {
         $conditions = "cm.id " . forum_utils::in_or_equals($cmids);
     } else {
         $conditions = "f.course = {$course->id}";
     }
     $singleforum = count($cmids) == 1 ? reset($cmids) : false;
     $inviewhiddenforums = forum_utils::in_or_equals($viewhiddenforums);
     // This array of additional results is used later if combining
     // standard results with single-forum calls.
     $plusresult = array();
     // For read tracking, we get a count of total number of posts in
     // forum, and total number of read posts in the forum (this
     // is so we can display the number of UNread posts, but the query
     // works that way around because it will return 0 if no read
     // information is stored).
     if ($unread != self::UNREAD_NONE && forum::enabled_read_tracking()) {
         // Work out when unread status ends
         $endtime = time() - $CFG->forumng_readafterdays * 24 * 3600;
         if (!$userid) {
             $userid = $USER->id;
         }
         $ingroups = forum_utils::in_or_equals($groups);
         $inaagforums = forum_utils::in_or_equals($aagforums);
         $restrictionsql = '';
         if ($singleforum) {
             // If it is for a single forum, get the restriction from the
             // forum type
             $forum = forum::get_from_cmid($singleforum, forum::CLONE_DIRECT);
             $type = $forum->get_type();
             if ($type->has_unread_restriction()) {
                 $value = $type->get_unread_restriction_sql($forum);
                 if ($value) {
                     $restrictionsql = 'AND ' . $value;
                 }
             }
         } else {
             // When it is not for a single forum, we can only group together
             // results for types that do not place restrictions on the
             // unread count.
             $modinfo = self::get_modinfo_special($course, $cmids);
             $okayids = array();
             if (array_key_exists('forumng', $modinfo->instances)) {
                 foreach ($modinfo->instances['forumng'] as $info) {
                     if (count($cmids) && !in_array($info->id, $cmids)) {
                         continue;
                     }
                     $type = self::get_type_from_modinfo_info($info);
                     if (forum_type::get_new($type)->has_unread_restriction()) {
                         // This one's a problem! Do it individually
                         $problemresults = self::query_forums(array($info->id), null, $userid, $unread, $groups, $aagforums, $viewhiddenforums);
                         foreach ($problemresults as $problemresult) {
                             $plusresult[$problemresult->f_id] = $problemresult;
                         }
                     } else {
                         $okayids[] = $info->id;
                     }
                 }
             }
             if (count($okayids) == 0) {
                 // There are no 'normal' forums, so return result so far
                 // after sorting it
                 uasort($plusresult, 'forum::sort_forum_result');
                 return $plusresult;
             } else {
                 // Fall through to normal calculation, but change conditions
                 // to include only the 'normal' forums
                 $conditions .= " AND cm.id " . forum_utils::in_or_equals($okayids);
             }
         }
         // NOTE fpfirst is used only by forum types, not here
         $now = time();
         $sharedquerypart = "\nFROM\n    {$CFG->prefix}forumng_discussions fd\n    INNER JOIN {$CFG->prefix}forumng_posts fplast ON fd.lastpostid = fplast.id\n    INNER JOIN {$CFG->prefix}forumng_posts fpfirst ON fd.postid = fpfirst.id\n    LEFT JOIN {$CFG->prefix}forumng_read fr ON fd.id = fr.discussionid AND fr.userid={$userid}\nWHERE\n    fd.forumid=f.id AND fplast.modified>{$endtime}\n    AND (\n        (fd.groupid IS NULL)\n        OR (fd.groupid {$ingroups})\n        OR cm.groupmode=" . VISIBLEGROUPS . "\n        OR (fd.forumid {$inaagforums})\n    )\n    AND fd.deleted=0\n    AND (\n        ((fd.timestart=0 OR fd.timestart <= {$now})\n        AND (fd.timeend=0 OR fd.timeend > {$now}))\n        OR (fd.forumid {$inviewhiddenforums})\n    )\n    AND ((fplast.edituserid IS NOT NULL AND fplast.edituserid<>{$userid})\n        OR fplast.userid<>{$userid})\n    AND (fr.time IS NULL OR fplast.modified>fr.time)\n    {$restrictionsql}";
         // Note: There is an unusual case in which this number can
         // be inaccurate. It is to do with ignoring messages the user
         // posted. We consider a discussion as 'not unread' if the last
         // message is by current user. In actual fact, a discussion could
         // contain unread messages if messages were posted by other users
         // after this user viewed the forum last, but before they posted
         // their reply. Since this should be an infrequent occurrence I
         // believe this behaviour is acceptable.
         if ($unread == self::UNREAD_BINARY && ($CFG->dbtype == 'postgres7' || $CFG->dbtype == 'mysql')) {
             // Query to get 0/1 unread discussions count
             $readtracking = "\n(SELECT\n    COUNT(1)\nFROM (\n    SELECT\n        1\n    {$sharedquerypart}\n    LIMIT 1) innerquery\n) AS f_hasunreaddiscussions";
         } else {
             // Query to get full unread discussions count
             $readtracking = "\n(SELECT\n    COUNT(1)\n{$sharedquerypart}\n) AS f_numunreaddiscussions";
         }
     } else {
         $readtracking = "NULL AS numreadposts, NULL AS timeread";
     }
     $now = time();
     $orderby = "LOWER(f.name)";
     // Main query. This retrieves:
     // - Full forum fields
     // - Basic course-module and course data (not whole tables)
     // - Discussion count
     // - Unread data, if enabled
     // - User subscription data
     $rs = get_recordset_sql($sql = "\nSELECT\n    " . forum_utils::select_forum_fields('f') . ",\n    " . forum_utils::select_course_module_fields('cm') . ",\n    " . forum_utils::select_course_fields('c') . ",\n    (SELECT COUNT(1)\n        FROM {$CFG->prefix}forumng_discussions cfd\n        WHERE cfd.forumid=f.id AND cfd.deleted=0\n        AND (\n            ((cfd.timestart=0 OR cfd.timestart <= {$now})\n            AND (cfd.timeend=0 OR cfd.timeend > {$now}))\n            OR (cfd.forumid {$inviewhiddenforums})\n        )\n        ) AS f_numdiscussions,\n    {$readtracking}\nFROM\n    {$CFG->prefix}forumng f\n    INNER JOIN {$CFG->prefix}course_modules cm ON cm.instance=f.id\n        AND cm.module=(SELECT id from {$CFG->prefix}modules WHERE name='forumng')\n    INNER JOIN {$CFG->prefix}course c ON c.id=f.course\nWHERE\n    {$conditions}\nORDER BY\n    {$orderby}");
     if (!$rs) {
         throw new forum_exception("Failed to retrieve forums");
     }
     $result = recordset_to_array($rs);
     rs_close($rs);
     if (count($plusresult) > 0) {
         foreach ($plusresult as $key => $value) {
             $result[$key] = $value;
         }
         uasort($result, 'forum::sort_forum_result');
     }
     return $result;
 }