Skip to content

Commit

Permalink
MDL-49028 mod_wiki: Fully reset wiki pages.
Browse files Browse the repository at this point in the history
All the wiki pages as well as comments and tags
can now be reset.

A thank you to Daniel Kosinski and Pascal Maury for
providing patches for this improvement.
  • Loading branch information
abgreeve committed Oct 5, 2015
1 parent 35d3e8b commit 30836b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions mod/wiki/lang/en/wiki.php
Expand Up @@ -43,6 +43,7 @@
* HTML - The HTML editor is available
* Creole - A common wiki markup language for which a small edit toolbar is available
* Nwiki - Mediawiki-like markup language used in the contributed Nwiki module';
$string['deleteallpages'] = 'Delete all wiki pages';
$string['deletecomment'] = 'Deleting comment';
$string['deletecommentcheck'] = 'Delete comment';
$string['deletecommentcheckfull'] = 'Are you sure you want to delete the comment?';
Expand Down
61 changes: 43 additions & 18 deletions mod/wiki/lib.php
Expand Up @@ -145,6 +145,7 @@ function wiki_reset_userdata($data) {
global $CFG,$DB;
require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
require_once($CFG->dirroot . '/tag/lib.php');
require_once($CFG->dirroot . "/mod/wiki/locallib.php");

$componentstr = get_string('modulenameplural', 'wiki');
$status = array();
Expand All @@ -156,40 +157,64 @@ function wiki_reset_userdata($data) {
$errors = false;
foreach ($wikis as $wiki) {

// remove all comments
if (!empty($data->reset_wiki_comments)) {
if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
continue;
}
$context = context_module::instance($cm->id);
$DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
continue;
}
$context = context_module::instance($cm->id);

if (!empty($data->reset_wiki_tags)) {
# Get subwiki information #
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
// Remove tags or all pages.
if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {

// Get subwiki information.
$subwikis = wiki_get_subwikis($wiki->id);

foreach ($subwikis as $subwiki) {
if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
foreach ($pages as $page) {
$tags = tag_get_tags_array('wiki_pages', $page->id);
foreach ($tags as $tagid => $tagname) {
// Delete the related tag_instances related to the wiki page.
$errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
$status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors);
// Get existing pages.
if ($pages = wiki_get_page_list($subwiki->id)) {
if (!empty($data->reset_wiki_tags)) {
// Go through each page and delete the tags.
foreach ($pages as $page) {

$tags = tag_get_tags_array('wiki_pages', $page->id);
foreach ($tags as $tagid => $tagname) {
// Delete the related tag_instances related to the wiki page.
$errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
$status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'),
'error' => $errors);
}
}
} else {
// Delete pages.
wiki_delete_pages($context, $pages, $subwiki->id);
}
}
if (!empty($data->reset_wiki_pages)) {
// Delete any subwikis.
$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);

// Delete any attached files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_wiki', 'attachments');

$status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
'error' => $errors);
}
}
}

// Remove all comments.
if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
$DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
}
}
return $status;
}


function wiki_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
$mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
$mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
$mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
}
Expand Down

0 comments on commit 30836b1

Please sign in to comment.