$PAGE->set_cm($cm);
}
$context = context_module::instance($cm->id);
require_course_login($course, true, $cm);
require_sesskey();
// participation capability check
$canview = oublog_can_view_participation($course, $oublog, $cm, $groupid);
if ($canview != OUBLOG_USER_PARTICIPATION) {
    print_error('nopermissiontoshow');
}
// grading capability check
if (!oublog_can_grade($course, $oublog, $cm, $groupid)) {
    print_error('nopermissiontoshow');
}
$mode = '';
if (!empty($_POST['menu'])) {
    $gradeinfo = $_POST['menu'];
    $oldgrades = oublog_get_participation($oublog, $context, $groupid, $cm, $course);
} else {
    if ($userid && !empty($_POST['grade'])) {
        $gradeinfo[$userid] = $_POST['grade'];
        $user = oublog_get_user_participation($oublog, $context, $userid, $groupid, $cm, $course);
        $oldgrades = array($userid => $user);
    }
}
// update grades
if (!empty($gradeinfo)) {
    oublog_update_grades($gradeinfo, $oldgrades, $cm, $oublog, $course);
}
// redirect
redirect('participation.php?id=' . $id . '&group=' . $groupid);
 public function test_participation_grades()
 {
     global $USER, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     // Whole course.
     $course = $this->get_new_course();
     $student1 = $this->get_new_user('student', $course->id);
     $student2 = $this->get_new_user('student', $course->id);
     $oublog = $this->get_new_oublog($course->id, array('grade' => 100));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $context = context_module::instance($oublog->cmid);
     $participation = oublog_get_participation($oublog, $context, 0, $cm, $course);
     $this->assertArrayHasKey($student1->id, $participation);
     $this->assertTrue(isset($participation[$student1->id]->gradeobj));
     $this->assertNotEmpty($participation[$student1->id]->gradeobj);
     $this->assertEmpty($participation[$student1->id]->gradeobj->grade);
     oublog_update_manual_grades(array($student1->id => 55), $participation, $cm, $oublog, $course);
     $participation = oublog_get_participation($oublog, $context, 0, $cm, $course);
     $this->assertTrue(isset($participation[$student1->id]->gradeobj));
     $this->assertNotEmpty($participation[$student1->id]->gradeobj);
     $this->assertEquals(55, $participation[$student1->id]->gradeobj->grade);
     $userparticipation = oublog_get_user_participation($oublog, $context, $student1->id, 0, $cm, $course, null, null, true, true, null, null, true);
     $this->assertTrue(isset($userparticipation->gradeobj));
     $this->assertNotEmpty($userparticipation->gradeobj);
     $this->assertEquals(55, $userparticipation->gradeobj->grade);
 }
if (!empty($download)) {
    $limitnum = null;
    $limitfrom = null;
}
// Customise data sought based on current tab.
switch ($tab) {
    case 1:
        $getposts = false;
        $getcomments = true;
        break;
    case 2:
        $getposts = false;
        $getgrades = true;
        break;
}
$participation = oublog_get_user_participation($oublog, $context, $userid, $groupid, $cm, $course, $start, $end, $getposts, $getcomments, $limitfrom, $limitnum, $getgrades);
// Add extra navigation link for users who can see all participation.
$canviewall = oublog_can_view_participation($course, $oublog, $cm, $groupid);
if ($canviewall == OUBLOG_USER_PARTICIPATION) {
    $allusersurl = new moodle_url('/mod/oublog/participation.php', array('id' => $cm->id, 'group' => $groupid));
    $PAGE->navbar->add(get_string('userparticipation', 'oublog'), $allusersurl);
}
$PAGE->navbar->add(fullname($participation->user, $viewfullnames));
$PAGE->set_title(format_string($oublog->name));
$PAGE->set_heading(format_string($oublog->name));
$groupname = '';
if ($groupid) {
    $groupname = groups_get_group_name($groupid);
}
$oublogoutput = $PAGE->get_renderer('mod_oublog');
if (empty($download)) {
/**
 * Generates oublog single user participation statistics output.
 * @param object $oublog
 * @param object $cm
 * @param mod_oublog_renderer $renderer
 * @param bool $ajax true to return data object rather than html
 */
function oublog_stats_output_myparticipation($oublog, $cm, $renderer = null, $course, $currentindividual, $globalindividual = null)
{
    global $PAGE, $DB, $USER, $OUTPUT;
    if (!isloggedin()) {
        // My participation is only visible to actual users.
        return;
    }
    if (!$renderer) {
        $renderer = $PAGE->get_renderer('mod_oublog');
    }
    // Setup My Participation capability check.
    $curgroup = oublog_get_activity_group($cm);
    $canview = oublog_can_view_participation($course, $oublog, $cm, $curgroup);
    if ($oublog->global) {
        $currentindividual = $globalindividual;
    }
    // Dont show the 'block' if user cant participate.
    if ($oublog->global && $currentindividual != $USER->id || $oublog->individual > OUBLOG_NO_INDIVIDUAL_BLOGS && $currentindividual != $USER->id) {
        return;
    }
    if (!$oublog->global && $canview == OUBLOG_NO_PARTICIPATION) {
        return;
    }
    $context = context_module::instance($cm->id);
    // Get the participation object containing User, Posts and Comments.
    $participation = oublog_get_user_participation($oublog, $context, $USER->id, $curgroup, $cm, $course, null, null, true, true, null, 8);
    // Generate content data to send to renderer.
    $maintitle = get_string('myparticipation', 'oublog');
    // The title of the block 'section'.
    $content = '';
    $postedcount = $commentedcount = $commenttotal = 0;
    $postshow = 8;
    $postscount = $participation->numposts;
    if ($participation->numcomments <= 4) {
        $commenttotal = $participation->numcomments;
    } else {
        $commenttotal = 4;
    }
    if (!$participation->posts) {
        $content .= html_writer::tag('p', get_string('nouserposts', 'oublog'));
    } else {
        $percent = $stat = null;
        $content .= html_writer::tag('h3', get_string('numberposts', 'oublog', $participation->numposts));
        foreach ($participation->posts as $post) {
            if ($postedcount >= $postshow - $commenttotal) {
                break;
            }
            $url = new moodle_url('/mod/oublog/viewpost.php', array('post' => $post->id));
            $postname = !empty($post->title) ? $post->title : get_string('untitledpost', 'oublog');
            $label = html_writer::div(html_writer::link($url, $postname), 'oublogstats_posts_posttitle');
            $label .= html_writer::div(oublog_date($post->timeposted), 'oublogstats_commentposts_blogname');
            $statinfo = new oublog_statsinfo($participation->user, $percent, $stat, $url, $label);
            $content .= $renderer->render($statinfo);
            $postedcount++;
        }
    }
    // Pre test the numbers of posts/comments for display upto max.
    $postspluscount = $participation->numposts - $postedcount;
    if ($postspluscount >= 1) {
        $content .= html_writer::tag('p', get_string('numberpostsmore', 'oublog', $postspluscount));
    }
    if (!$participation->comments) {
        $content .= html_writer::tag('p', get_string('nousercomments', 'oublog'));
    } else {
        $percent = $stat = null;
        // Removing all stats div.
        $content .= html_writer::tag('h3', get_string('numbercomments', 'oublog', $participation->numcomments));
        foreach ($participation->comments as $comment) {
            if ($commentedcount + $postedcount >= $postshow) {
                break;
            }
            $url = new moodle_url('/mod/oublog/viewpost.php', array('post' => $comment->postid));
            $lnkurl = $url->out() . '#cid' . $comment->id;
            $commentname = !empty($comment->title) ? $comment->title : get_string('untitledcomment', 'oublog');
            $label = html_writer::div(html_writer::link($lnkurl, $commentname), 'oublogstats_commentposts_posttitle');
            $label .= html_writer::div(oublog_date($comment->timeposted), 'oublogstats_commentposts_blogname');
            $statinfo = new oublog_statsinfo($participation->user, $percent, $stat, $url, $label);
            $content .= $renderer->render($statinfo);
            $commentedcount++;
        }
    }
    // If the number of comments is more than can be shown.
    $commentspluscount = $participation->numcomments - $commentedcount;
    if ($commentspluscount >= 1) {
        $content .= html_writer::tag('p', get_string('numbercommentsmore', 'oublog', $commentspluscount));
    }
    $params = array('id' => $cm->id, 'group' => $curgroup, 'user' => $participation->user->id);
    $url = new moodle_url('/mod/oublog/userparticipation.php', $params);
    $viewmyparticipation = html_writer::link($url, get_string('viewmyparticipation', 'oublog'));
    $content .= html_writer::start_tag('div', array('class' => 'oublog-post-content'));
    $content .= html_writer::tag('h3', $viewmyparticipation, array('class' => 'oublog-post-title'));
    $content .= html_writer::end_tag('div');
    return $renderer->render_stats_view('myparticipation', $maintitle, $content, null, null, null, false);
}
    }
    if ($submitted->end) {
        $end = strtotime('23:59:59', $submitted->end);
    }
} else {
    if (!$timefilter->is_submitted()) {
        // Recieved via post back.
        if ($start = optional_param('start', null, PARAM_INT)) {
            $start = strtotime('00:00:00', $start);
        }
        if ($end = optional_param('end', null, PARAM_INT)) {
            $end = strtotime('23:59:59', $end);
        }
    }
}
$participation = oublog_get_user_participation($oublog, $context, $userid, $groupid, $cm, $course, $start, $end);
// Add extra navigation link for users who can see all participation.
$canviewall = oublog_can_view_participation($course, $oublog, $cm, $groupid);
if ($canviewall == OUBLOG_USER_PARTICIPATION) {
    $allusersurl = new moodle_url('/mod/oublog/participation.php', array('id' => $cm->id, 'group' => $groupid));
    $PAGE->navbar->add(get_string('userparticipation', 'oublog'), $allusersurl);
}
$PAGE->navbar->add(fullname($participation->user, $viewfullnames));
$PAGE->set_title(format_string($oublog->name));
$PAGE->set_heading(format_string($oublog->name));
$groupname = '';
if ($groupid) {
    $groupname = $DB->get_field('groups', 'name', array('id' => $groupid));
}
$oublogoutput = $PAGE->get_renderer('mod_oublog');
if (empty($download)) {