/**
 * Update grade item for given forumng.
 *
 * @param mod_forumng $forumng Forum with extra cmidnumber.
 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
 * @return int 0 if ok, error code otherwise
 */
function forumngfeature_userposts_grade_item_update(mod_forumng $forumng, $grades = null)
{
    global $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    $params = array('itemname' => $forumng->get_name());
    $gradingscale = $forumng->get_grading_scale();
    if ($gradingscale > 0) {
        $params['gradetype'] = GRADE_TYPE_VALUE;
        $params['grademax'] = $gradingscale;
        $params['grademin'] = 0;
    } else {
        if ($gradingscale < 0) {
            $params['gradetype'] = GRADE_TYPE_SCALE;
            $params['scaleid'] = -$gradingscale;
        } else {
            $params['gradetype'] = GRADE_TYPE_NONE;
        }
    }
    if ($grades === 'reset') {
        $params['reset'] = true;
        $grades = null;
    }
    return grade_update('mod/forumng', $forumng->get_course_id(), 'mod', 'forumng', $forumng->get_id(), 0, $grades, $params);
}
 /**
  * Updates the in-memory digest records to add a new post to the given
  * user's digests.
  * @param object $user User object (must include special ->emailtype, etc)
  * @param array $userdigests Array of user id => digest information object
  * @param mod_forumng_post $post Post object
  * @param mod_forumng_post $inreplyto Parent post
  * @param mod_forumng_discussion $discussion Discus
  * @param mod_forumng $forum
  * @param object $cm
  * @param object $course
  * @param object $context
  */
 private static function digest_add_post_for_user(&$user, &$userdigests, &$post, &$inreplyto, &$discussion, &$forum, &$cm, &$course, &$context)
 {
     global $CFG;
     // Set up digest for user if required
     if (!array_key_exists($user->id, $userdigests)) {
         $userdigests[$user->id] = new StdClass();
         $userdigests[$user->id]->discussionid = -1;
         // So we do header next
         $userdigests[$user->id]->user = $user;
         $userdigests[$user->id]->forumngid = -1;
         // Get header text
         $headerdata = new object();
         $headerdata->sitename = format_string($course->fullname, true);
         $headerdata->userprefs = $CFG->wwwroot . '/user/edit.php?id=' . $user->id . '&amp;course=' . $course->id;
         $userdigests[$user->id]->text = get_string('digestmailheader', 'forumng', $headerdata) . "\n\n";
         // Get header HTML
         $html = "<body id='forumng-email'>\n";
         $headerdata->userprefs = '<a target="_blank" href="' . $headerdata->userprefs . '">' . get_string('digestmailprefs', 'forumng') . '</a>';
         $html .= '<div class="forumng-emailheader"><p>' . get_string('digestmailheader', 'forumng', $headerdata) . '</p></div><hr size="1" noshade="noshade" />';
         $userdigests[$user->id]->html = $html;
         // Get email subject
         $userdigests[$user->id]->subject = get_string('digestmailsubject', 'forumng', format_string($course->shortname, true));
     }
     // New forum?
     if ($userdigests[$user->id]->forumngid != $forum->get_id()) {
         $userdigests[$user->id]->forumngid = $forum->get_id();
     }
     // Is this a new discussion?
     if ($userdigests[$user->id]->discussionid != $discussion->get_id()) {
         $strforums = get_string('forums', 'forumng');
         // Per-discussion header (text mode)
         $text = "\n \n";
         $text .= '=====================================================================';
         $text .= "\n \n";
         $text .= "{$course->shortname} -> {$strforums} -> " . format_string($forum->get_name(), true);
         if ($discussion->get_subject(false) !== $forum->get_name()) {
             $text .= " -> " . format_string($discussion->get_subject(false), true);
         }
         $text .= "\n";
         // HTML mode
         $html = '<hr size="1" noshade="noshade" />';
         $html .= "<div class='forumng-breadcrumbs'>" . "<a target='_blank' href='{$CFG->wwwroot}/course/view.php?" . "id={$course->id}'>{$course->shortname}</a> -> " . "<a target='_blank' href='{$CFG->wwwroot}/mod/forumng/index.php?" . "id={$course->id}'>{$strforums}</a> -> " . "<a target='_blank' href='{$CFG->wwwroot}/mod/forumng/view.php?" . $forum->get_link_params(mod_forumng::PARAM_HTML) . "'>" . format_string($forum->get_name(), true) . "</a>";
         if ($discussion->get_subject(false) !== $forum->get_name()) {
             $html .= " -> <a target='_blank' href='{$CFG->wwwroot}/mod/forumng/discuss.php?" . $discussion->get_link_params(mod_forumng::PARAM_HTML) . "'>" . format_string($discussion->get_subject(false), true) . "</a>";
         }
         $html .= '</div>';
         $userdigests[$user->id]->text .= $text;
         $userdigests[$user->id]->html .= $html;
         $userdigests[$user->id]->discussionid = $discussion->get_id();
     }
     // Get both plaintext and html versions (and subject).
     // The html version will be blank if set to
     // plain text mode.
     $post->build_email($inreplyto, $subject, $text, $html, $user->emailtype & 1, $user->emailtype & 2, $user->emailtype & 4, $user->lang, $user->timezone, true);
     $userdigests[$user->id]->text .= $text;
     $userdigests[$user->id]->html .= $html;
 }
 /**
  * Checks forum object created in test_get_forum()
  * In function so same tests can be carried out on multiple forum setups
  * @param object $forum
  * @param object $course
  * @param object $cm
  */
 private function check_forum_settings(mod_forumng $forum, $course, $cm)
 {
     $this->assertEquals('TEST', $forum->get_name());
     $this->assertEquals('abc123', $forum->get_intro());
     $this->assertEquals('abc123', $forum->get_intro(true));
     // Pick up errors in abbreviation.
     $this->assertEquals(FORMAT_MOODLE, $forum->get_intro_format());
     $this->assertEquals($cm->instance, $forum->get_id());
     $this->assertEquals($course->id, $forum->get_course_id());
     $this->assertEquals($course->id, $forum->get_course()->id);
     $this->assertEquals($cm->id, $forum->get_course_module_id());
     $this->assertEquals(mod_forumng::NO_GROUPS, $forum->get_activity_group($cm));
     $context = context_module::instance($cm->id);
     $this->assertEquals($context->id, $forum->get_context()->id);
     $this->assertInstanceOf('forumngtype', $forum->get_type());
 }