/**
 * Update all wiki documents for ousearch.
 * @param bool $feedback If true, prints feedback as HTML list items
 * @param int $courseid If specified, restricts to particular courseid
 */
function ouwiki_ousearch_update_all($feedback = false, $courseid = 0)
{
    global $CFG;
    // Get list of all wikis. We need the coursemodule data plus
    // the type of subwikis
    $coursecriteria = $courseid === 0 ? '' : 'cm.course=' . $courseid . ' AND';
    $coursemodules = get_records_sql("\nSELECT\n    cm.id,cm.course,cm.instance,w.subwikis\nFROM\n    {$CFG->prefix}modules m\n    INNER JOIN {$CFG->prefix}course_modules cm ON cm.module=m.id\n    INNER JOIN {$CFG->prefix}ouwiki w ON cm.instance=w.id\nWHERE\n    {$coursecriteria}\n    m.name='ouwiki'");
    if (!$coursemodules) {
        return;
    }
    if ($feedback) {
        print '<li><strong>' . count($coursemodules) . '</strong> wikis to process.</li>';
        $dotcount = 0;
    }
    $count = 0;
    foreach ($coursemodules as $coursemodule) {
        // This condition is needed because if somebody creates some stuff
        // then changes the wiki type, it actually keeps the old bits
        // in the database. Maybe it shouldn't, not sure.
        switch ($coursemodule->subwikis) {
            case OUWIKI_SUBWIKIS_SINGLE:
                $where = "sw.userid IS NULL AND sw.groupid IS NULL";
                break;
            case OUWIKI_SUBWIKIS_GROUPS:
                $where = "sw.userid IS NULL AND sw.groupid IS NOT NULL";
                break;
            case OUWIKI_SUBWIKIS_INDIVIDUAL:
                $where = "sw.userid IS NOT NULL AND sw.groupid IS NULL";
                break;
        }
        // Get all pages in that wiki
        $rs = get_recordset_sql("\nSELECT\n    p.id,p.title,v.xhtml,v.timecreated,sw.groupid,sw.userid\nFROM\n    {$CFG->prefix}ouwiki_subwikis sw\n    INNER JOIN {$CFG->prefix}ouwiki_pages p ON p.subwikiid=sw.id\n    INNER JOIN {$CFG->prefix}ouwiki_versions v ON v.id=p.currentversionid\nWHERE\n    sw.wikiid={$coursemodule->instance} AND {$where}");
        while ($page = rs_fetch_next_record($rs)) {
            // Update the page for search
            $doc = new ousearch_document();
            $doc->init_module_instance('ouwiki', $coursemodule);
            if ($page->groupid) {
                $doc->set_group_id($page->groupid);
            }
            if ($page->title) {
                $doc->set_string_ref($page->title);
            }
            if ($page->userid) {
                $doc->set_user_id($page->userid);
            }
            $title = $page->title ? $page->title : '';
            if (!$doc->update($title, $page->xhtml, $page->timecreated)) {
                ouwiki_error('Failed to update database record for page ID ' . $page->id);
            }
        }
        rs_close($rs);
        $count++;
        if ($feedback) {
            if ($dotcount == 0) {
                print '<li>';
            }
            print '.';
            $dotcount++;
            if ($dotcount == 20 || $count == count($coursemodules)) {
                print 'done ' . $count . '</li>';
                $dotcount = 0;
            }
            flush();
        }
    }
}
示例#2
0
/**
 * Prints the subwiki selector if user has access to more than one subwiki.
 * Also displays the currently-viewing subwiki.
 *
 * @param object $subwiki Current subwiki object
 * @param object $ouwiki Wiki object
 * @param object $cm Course-module object
 * @param object $context Context for permissions
 * @param object $course Course object
 */
function ouwiki_display_subwiki_selector($subwiki, $ouwiki, $cm, $context, $course, $actionurl = 'view.php')
{
    global $USER, $DB;
    if ($ouwiki->subwikis == OUWIKI_SUBWIKIS_SINGLE) {
        return '';
    }
    $choicefield = '';
    switch ($ouwiki->subwikis) {
        case OUWIKI_SUBWIKIS_GROUPS:
            $groups = groups_get_activity_allowed_groups($cm);
            uasort($groups, create_function('$a,$b', 'return strcasecmp($a->name,$b->name);'));
            $wikifor = htmlspecialchars($groups[$subwiki->groupid]->name);
            // Do they have more than one?
            if (count($groups) > 1) {
                $choicefield = 'group';
                $choices = $groups;
            }
            break;
        case OUWIKI_SUBWIKIS_INDIVIDUAL:
            $user = $DB->get_record('user', array('id' => $subwiki->userid), 'id, firstname, lastname, username');
            $wikifor = ouwiki_display_user($user, $cm->course);
            if (has_capability('mod/ouwiki:viewallindividuals', $context)) {
                // Get list of everybody...
                $choicefield = 'user';
                try {
                    $choices = $DB->get_records_sql('SELECT u.id, u.firstname, u.lastname
                            FROM {ouwiki_subwikis} sw
                            INNER JOIN {user} u ON sw.userid = u.id
                            WHERE sw.wikiid = ?
                            ORDER BY u.lastname, u.firstname', array($ouwiki->id));
                } catch (Exception $e) {
                    ouwiki_dberror($e);
                }
                foreach ($choices as $choice) {
                    $choice->name = fullname($choice);
                }
            } else {
                if (has_capability('mod/ouwiki:viewgroupindividuals', $context)) {
                    $choicefield = 'user';
                    $choices = array();
                    // User allowed to view people in same group
                    $theirgroups = groups_get_all_groups($cm->course, $USER->id, $course->defaultgroupingid);
                    if (!$theirgroups) {
                        $theirgroups = array();
                    }
                    foreach ($theirgroups as $group) {
                        $members = groups_get_members($group->id, 'u.id, u.firstname, u.lastname');
                        foreach ($members as $member) {
                            $member->name = fullname($member);
                            $choices[$member->id] = $member;
                        }
                    }
                } else {
                    // Nope, only yours
                }
            }
            break;
        default:
            ouwiki_error("Unexpected subwikis value: {$ouwiki->subwikis}");
    }
    $out = '<div class="ouw_subwiki"><label for="wikiselect">' . get_string('wikifor', 'ouwiki') . '</label>';
    if ($choicefield && count($choices) > 1) {
        $selectedid = $choicefield == 'user' ? $subwiki->userid : $subwiki->groupid;
        $out .= '<form method="get" action="' . $actionurl . '" class="ouwiki_otherwikis">
            <div><input type="hidden" name="id" value="' . $cm->id . '"/>
            <select name="' . $choicefield . '" id="wikiselect">';
        foreach ($choices as $choice) {
            $selected = $choice->id == $selectedid ? ' selected="selected"' : '';
            $out .= '<option value="' . $choice->id . '"' . $selected . '>' . htmlspecialchars($choice->name) . '</option>';
        }
        $out .= '</select> <input type="submit" value="' . get_string('changebutton', 'ouwiki') . '" /></div></form>';
    } else {
        $out .= $wikifor;
    }
    $out .= '</div>';
    return $out;
}
/**
 * Prints the subwiki selector if user has access to more than one subwiki.
 * Also displays the currently-viewing subwiki.
 *
 * @param object $subwiki Current subwiki object
 * @param object $ouwiki Wiki object
 * @param object $cm Course-module object
 * @param object $context Context for permissions
 * @param object $course Course object
 * @param string $actionurl
 * @param string $querytext for use when changing groups against search criteria
 */
function ouwiki_display_subwiki_selector($subwiki, $ouwiki, $cm, $context, $course, $actionurl = 'view.php', $querytext = '')
{
    global $USER, $DB, $OUTPUT;
    if ($ouwiki->subwikis == OUWIKI_SUBWIKIS_SINGLE) {
        return '';
    }
    $choicefield = '';
    switch ($ouwiki->subwikis) {
        case OUWIKI_SUBWIKIS_GROUPS:
            $groups = groups_get_activity_allowed_groups($cm);
            uasort($groups, create_function('$a,$b', 'return strcasecmp($a->name,$b->name);'));
            $wikifor = htmlspecialchars($groups[$subwiki->groupid]->name);
            // Do they have more than one?
            if (count($groups) > 1) {
                $choicefield = 'group';
                $choices = $groups;
            }
            break;
        case OUWIKI_SUBWIKIS_INDIVIDUAL:
            $user = $DB->get_record('user', array('id' => $subwiki->userid), 'username, ' . user_picture::fields());
            $wikifor = ouwiki_display_user($user, $cm->course);
            $usernamefields = user_picture::fields('u');
            if (has_capability('mod/ouwiki:viewallindividuals', $context)) {
                // Get list of everybody...
                $choicefield = 'user';
                try {
                    $choices = $DB->get_records_sql('SELECT ' . $usernamefields . ' FROM {ouwiki_subwikis} sw
                            INNER JOIN {user} u ON sw.userid = u.id
                            WHERE sw.wikiid = ?
                            ORDER BY u.lastname, u.firstname', array($ouwiki->id));
                } catch (Exception $e) {
                    ouwiki_dberror($e);
                }
                foreach ($choices as $choice) {
                    $choice->name = fullname($choice);
                }
            } else {
                if (has_capability('mod/ouwiki:viewgroupindividuals', $context)) {
                    $choicefield = 'user';
                    $choices = array();
                    // User allowed to view people in same group
                    $theirgroups = groups_get_all_groups($cm->course, $USER->id, $course->defaultgroupingid);
                    if (!$theirgroups) {
                        $theirgroups = array();
                    }
                    foreach ($theirgroups as $group) {
                        $members = groups_get_members($group->id, 'u.id, ' . $usernamefields);
                        foreach ($members as $member) {
                            $member->name = fullname($member);
                            $choices[$member->id] = $member;
                        }
                    }
                }
            }
            break;
        default:
            ouwiki_error("Unexpected subwikis value: {$ouwiki->subwikis}");
    }
    $out = '<div class="ouw_subwiki">';
    if ($choicefield && count($choices) > 1) {
        $actionquery = '';
        if (!empty($querytext)) {
            $actionquery = '&amp;query=' . rawurlencode($querytext);
        }
        $actionurl = '/mod/ouwiki/' . $actionurl . '?id=' . $cm->id . $actionquery;
        $urlroot = new moodle_url($actionurl);
        if ($choicefield == 'user') {
            // Individuals.
            $individualsmenu = array();
            foreach ($choices as $choice) {
                $individualsmenu[$choice->id] = $choice->name;
            }
            $select = new single_select($urlroot, 'user', $individualsmenu, $subwiki->userid, null, 'selectuser');
            $select->label = get_string('wikifor', 'ouwiki');
            $output = $OUTPUT->render($select);
            $out .= '<div class="individualselector">' . $output . '</div>';
        } else {
            if ($choicefield == 'group') {
                // Group mode.
                $out .= groups_print_activity_menu($cm, $urlroot, true, true);
            }
        }
    } else {
        $out .= get_string('wikifor', 'ouwiki') . $wikifor;
    }
    $out .= '</div>';
    return $out;
}