/**
 * Prints detailed summary information about what a user has done,
 * for user activity reports.
 *
 * @param stdClass $course
 * @param stdClass $user
 * @param stdClass $mod
 * @param stdClass $wiki
 */
function ouwiki_user_complete($course, $user, $mod, $wiki)
{
    global $DB, $CFG, $OUTPUT, $USER, $PAGE;
    require_once "{$CFG->libdir}/gradelib.php";
    require_once "{$CFG->dirroot}/mod/ouwiki/locallib.php";
    $grades = grade_get_grades($course->id, 'mod', 'ouwiki', $wiki->id, $user->id);
    if (!empty($grades->items[0]->grades)) {
        $grade = reset($grades->items[0]->grades);
        if ($grade != '-') {
            echo $OUTPUT->container(get_string('grade') . ': ' . $grade->str_long_grade);
            if ($grade->str_feedback) {
                echo $OUTPUT->container(get_string('feedback') . ': ' . $grade->str_feedback);
            }
        }
    }
    $usergroups = array();
    if ($wiki->subwikis == 1) {
        $wikigroups = groups_get_activity_allowed_groups($mod);
        foreach ($wikigroups as $mygroup) {
            if (groups_is_member($mygroup->id, $user->id)) {
                $usergroups[] = $mygroup;
            }
        }
    }
    $context = context_module::instance($mod->id);
    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
    $ouwikioutput = $PAGE->get_renderer('mod_ouwiki');
    echo '<div class="ouwiki_user_complete_report">';
    if (empty($usergroups)) {
        $subwiki = ouwiki_get_subwiki($course, $wiki, $mod, $context, 0, $user->id, true);
        $canview = ouwiki_can_view_participation($course, $wiki, $subwiki, $mod, $USER->id);
        list($newuser, $changes) = ouwiki_get_user_participation($user->id, $subwiki);
        echo $ouwikioutput->ouwiki_render_user_participation($user, $changes, $mod, $course, $wiki, $subwiki, '', 0, '', $canview, $context, $fullname, false, '');
    } else {
        foreach ($usergroups as $group) {
            $subwiki = ouwiki_get_subwiki($course, $wiki, $mod, $context, $group->id, $user->id, true);
            $canview = ouwiki_can_view_participation($course, $wiki, $subwiki, $mod, $USER->id);
            list($newuser, $changes) = ouwiki_get_user_participation($user->id, $subwiki);
            echo $OUTPUT->heading(get_string('group') . ': ' . $group->name, 5);
            echo $ouwikioutput->ouwiki_render_user_participation($user, $changes, $mod, $course, $wiki, $subwiki, '', $group->id, '', $canview, $context, $fullname, false, $group->name);
        }
    }
    echo '</div>';
}
 public function test_participation()
 {
     $ouwiki = $this->get_new_ouwiki($this->courses['single']->id, OUWIKI_SUBWIKIS_SINGLE);
     $cm = $this->get_new_course_module($this->courses['single']->id, $ouwiki->id, NOGROUPS);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     $subwiki = ouwiki_get_subwiki($this->courses['single'], $ouwiki, $cm, $context, 0, $this->admin->id, true);
     $pageversion = ouwiki_get_current_page($subwiki, 'TEST PAGE', OUWIKI_GETPAGE_CREATE);
     $user = $this->get_new_user('student');
     $content = 'content';
     $plus = ' plus';
     for ($i = 1; $i <= 5; $i++) {
         $content .= $plus . $i;
         $wordcount = ouwiki_count_words($content);
         $this->save_new_version($pageversion->pageid, $content, $user->id, $wordcount);
     }
     // remove one word
     $content = preg_replace('/plus3/', '', $content);
     $wordcount = ouwiki_count_words($content);
     $this->save_new_version($pageversion->pageid, $content, $user->id, $wordcount);
     list($returneduser, $participation) = ouwiki_get_user_participation($user->id, $subwiki);
     $this->assertEqual($returneduser->id, $user->id);
     $this->assertNotNull($participation);
     // another user
     $user2 = $this->get_new_user('student');
     $users[0] = $user->id;
     $users[1] = $user2->id;
     $participation = ouwiki_get_participation($ouwiki, $subwiki, $context, 0);
     $this->assertNotNull($participation);
     $userexists = array_key_exists($user->id, $participation);
     $this->assertTrue($userexists);
     // a user who isn't enrolled
     $userexists = array_key_exists($this->nouser->id, $participation);
     $this->assertFalse($userexists);
 }
 public function test_participation()
 {
     global $USER;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = $this->getDataGenerator()->create_course();
     $ouwiki = $this->get_new_ouwiki($course->id, OUWIKI_SUBWIKIS_SINGLE);
     $cm = get_coursemodule_from_instance('ouwiki', $ouwiki->id);
     $this->assertNotEmpty($cm);
     $context = context_module::instance($cm->id);
     $subwiki = ouwiki_get_subwiki($course, $ouwiki, $cm, $context, 0, $USER->id, true);
     $pageversion = ouwiki_get_current_page($subwiki, 'TEST PAGE', OUWIKI_GETPAGE_CREATE);
     $user = $this->get_new_user('student', $course->id);
     $content = 'content';
     $plus = ' plus';
     for ($i = 1; $i <= 5; $i++) {
         $content .= $plus . $i;
         $wordcount = ouwiki_count_words($content);
         $this->save_new_version($pageversion->pageid, $content, $user->id, $wordcount);
     }
     // Remove one word.
     $content = str_replace('plus3', '', $content);
     $wordcount = ouwiki_count_words($content);
     $this->save_new_version($pageversion->pageid, $content, $user->id, $wordcount);
     // User participation.
     list($returneduser, $participation) = ouwiki_get_user_participation($user->id, $subwiki);
     $this->assertEquals($user->id, $returneduser->id);
     $this->assertNotNull($participation);
     $this->assertEquals(6, count($participation));
     // All participation.
     $participation = ouwiki_get_participation($ouwiki, $subwiki, $context, 0);
     $this->assertNotNull($participation);
     $userexists = array_key_exists($user->id, $participation);
     $this->assertTrue($userexists);
     $this->assertEquals(1, count($participation));
     $this->assertEquals(6, $participation[$user->id]->pageedits);
     $this->assertEquals(25, $participation[$user->id]->wordsadded);
     $this->assertEquals(0, $participation[$user->id]->wordsdeleted);
     $this->assertEquals(0, $participation[$user->id]->pagecreates);
     $user2 = $this->get_new_user('student', $course->id);
     $participation = ouwiki_get_participation($ouwiki, $subwiki, $context, 0);
     // A user who is enrolled, but with no contribution.
     $userexists = array_key_exists($user2->id, $participation);
     $this->assertTrue($userexists);
     $this->assertEquals(fullname($user2), fullname($participation[$user2->id]));
 }
    print_error('invalidcoursemodule');
}
// Checking course instance
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
if (!($ouwiki = $DB->get_record('ouwiki', array('id' => $cm->instance)))) {
    print_error('invalidcoursemodule');
}
$PAGE->set_cm($cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$PAGE->set_pagelayout('incourse');
require_course_login($course, true, $cm);
$canview = ouwiki_can_view_participation($course, $ouwiki, $subwiki, $cm, $USER->id);
if ($canview == OUWIKI_NO_PARTICIPATION || $USER->id != $userid && $canview == OUWIKI_MY_PARTICIPATION) {
    print_error('nopermissiontoshow');
}
list($user, $changes) = ouwiki_get_user_participation($userid, $subwiki);
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
$cangrade = has_capability('mod/ouwiki:grade', $context);
$groupname = '';
if ($groupid) {
    $groupname = $DB->get_field('groups', 'name', array('id' => $groupid));
}
$ouwikioutput = $PAGE->get_renderer('mod_ouwiki');
// Headers
if (empty($download)) {
    $nav = array();
    $groupparams = $groupid ? '&group=' . $groupid : '';
    if ($canview == OUWIKI_USER_PARTICIPATION) {
        $nav[] = array('name' => get_string('userparticipation', 'ouwiki'), 'link' => "/mod/ouwiki/participation.php?id={$cm->id}" . "&pagename={$pagename}{$groupparams}");
    }
    $nav[] = array('name' => $fullname, 'link' => null);