/** * Obtains the appropriate subwiki object for a request. If one cannot * be obtained, either creates one or calls error() and stops. * * @param object $ouwiki Wiki object * @param object $cm Course-module object * @param object $context Context to use for checking permissions * @param int $groupid Group ID or 0 to use any appropriate group * @param int $userid User ID or 0 to use current user * @param bool $create If true, creates a wiki if it doesn't exist * @return mixed Object with the data from the subwiki table. Also has extra 'canedit' field * set to true if that's allowed. */ function ouwiki_get_subwiki($course, $ouwiki, $cm, $context, $groupid, $userid, $create = null) { global $USER, $DB; switch ($ouwiki->subwikis) { case OUWIKI_SUBWIKIS_SINGLE: $subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND groupid IS NULL AND userid IS NULL', array($ouwiki->id)); if ($subwiki) { ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context); return $subwiki; } if ($create) { $subwiki = ouwiki_create_subwiki($ouwiki, $cm, $course); ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context); ouwiki_init_pages($course, $cm, $ouwiki, $subwiki, $ouwiki); return $subwiki; } ouwiki_error('Wiki does not exist. View wikis before attempting other actions.'); break; case OUWIKI_SUBWIKIS_GROUPS: $groupid = groups_get_activity_group($cm, true); if (!$groupid) { $groups = groups_get_activity_allowed_groups($cm); if (!$groups) { if (!groups_get_all_groups($cm->course, 0, $cm->groupingid)) { ouwiki_error('This wiki cannot be displayed because it is a group wiki, but no groups have been set up for the course (or grouping, if selected).'); } else { ouwiki_error('You do not have access to any of the groups in this wiki.'); } } $groupid = reset($groups)->id; } $othergroup = !groups_is_member($groupid); $subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND groupid = ? AND userid IS NULL', array($ouwiki->id, $groupid)); if ($subwiki) { ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context, $othergroup); return $subwiki; } if ($create) { $subwiki = ouwiki_create_subwiki($ouwiki, $cm, $course, null, $groupid); ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context, $othergroup); ouwiki_init_pages($course, $cm, $ouwiki, $subwiki, $ouwiki); return $subwiki; } ouwiki_error('Wiki does not exist. View wikis before attempting other actions.'); break; case OUWIKI_SUBWIKIS_INDIVIDUAL: if ($userid == 0) { $userid = $USER->id; } $otheruser = false; if ($userid != $USER->id) { $otheruser = true; // Is user allowed to view everybody? if (!has_capability('mod/ouwiki:viewallindividuals', $context)) { // Nope. Are they allowed to view people in same group? if (!has_capability('mod/ouwiki:viewgroupindividuals', $context)) { ouwiki_error('You do not have access to view somebody else\'s wiki.'); } // Check user is in same group. Note this isn't now restricted to the // module grouping $ourgroups = groups_get_all_groups($cm->course, $USER->id); $theirgroups = groups_get_all_groups($cm->course, $userid); $found = false; foreach ($ourgroups as $ourgroup) { foreach ($theirgroups as $theirgroup) { if ($ourgroup->id == $theirgroup->id) { $found = true; break; } } if ($found) { break; } } if (!$found) { ouwiki_error('You do not have access to view this user\'s wiki.'); } } } // OK now find wiki $subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND groupid IS NULL AND userid = ?', array($ouwiki->id, $userid)); if ($subwiki) { ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context, $otheruser, !$otheruser); return $subwiki; } // Create one if ($create) { $subwiki = ouwiki_create_subwiki($ouwiki, $cm, $course, $userid); ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context, $otheruser, !$otheruser); ouwiki_init_pages($course, $cm, $ouwiki, $subwiki, $ouwiki); return $subwiki; } ouwiki_error('Wiki does not exist. View wikis before attempting other actions.'); break; default: ouwiki_error("Unexpected subwikis value: {$ouwiki->subwikis}"); } }
function ouwiki_update_instance($data, $mform) { global $CFG, $DB; $data->id = $data->instance; // Update main record. $DB->update_record('ouwiki', $data); // Set up null values $nullvalues = array('editbegin', 'editend', 'timeout'); foreach ($nullvalues as $nullvalue) { if (empty($data->{$nullvalue})) { unset($data->{$nullvalue}); $DB->set_field('ouwiki', $nullvalue, null, array('id' => $data->id)); } } if (strlen(preg_replace('/(<.*?>)|(&.*?;)|\\s/', '', $data->intro)) == 0) { unset($data->intro); $DB->set_field('ouwiki', 'intro', null, array('id' => $data->id)); } ouwiki_grade_item_update($data); if (!($cm = get_coursemodule_from_id('ouwiki', $data->coursemodule))) { print_error('invalidcoursemodule'); } // Checking course instance. $course = $DB->get_record('course', array('id' => $data->course), '*', MUST_EXIST); if ($filename = $mform->get_new_filename('template_file')) { // Delete any previous template files. $cmid = $data->coursemodule; $context = context_module::instance($cmid); $fs = get_file_storage(); $fs->delete_area_files($context->id, 'mod_ouwiki', 'template', $data->id); // Add template file. $file = $mform->save_stored_file('template_file', $context->id, 'mod_ouwiki', 'template', $data->id, '/', $filename); $DB->set_field('ouwiki', 'template', '/' . $file->get_filename(), array('id' => $data->id)); // Check for empty wikis (i.e. wikis without a start page already created). $subwikis = ouwiki_get_subwikis($data->id); $ouwiki = $DB->get_record_select('ouwiki', 'id = ?', array($data->id)); foreach ($subwikis as $subwiki) { if (!ouwiki_subwiki_content_exists($subwiki->id)) { // Amend any empty wikis from template. ouwiki_init_pages($course, $cm, $ouwiki, $subwiki); } } } return true; }
function test_ouwiki_init_pages() { $user = $this->get_new_user(); $course = $this->get_new_course(); $ouwiki = $this->get_new_ouwiki($course->id, OUWIKI_SUBWIKIS_SINGLE); $cm = $this->get_new_course_module($course->id, $ouwiki->id, NOGROUPS); $context = get_context_instance(CONTEXT_MODULE, $cm->instance); $groupid = 0; $subwiki = ouwiki_get_subwiki($course, $ouwiki, $cm, $context, $groupid, $user->id, true); // doesn't return anything ouwiki_init_pages($course, $cm, $ouwiki, $subwiki, $ouwiki); }