Permalink
Browse files

MDL-23520 mod-wiki-2.0: Added delete page and version feature as it w…

…as in 1.9
  • Loading branch information...
1 parent 9805fb9 commit ac0a82cf92d7cb9cec397f8d0d37a0f3599b31b7 Rajesh Taneja committed Aug 11, 2011
Showing with 639 additions and 1 deletion.
  1. +106 −0 mod/wiki/admin.php
  2. +7 −0 mod/wiki/lang/en/wiki.php
  3. +5 −0 mod/wiki/lib.php
  4. +130 −0 mod/wiki/locallib.php
  5. +36 −0 mod/wiki/module.js
  6. +337 −1 mod/wiki/pagelib.php
  7. +18 −0 mod/wiki/renderer.php
View
@@ -0,0 +1,106 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Delete wiki pages or versions
+ *
+ * This will show options for deleting wiki pages or purging page versions
+ * If user have wiki:managewiki ability then only this page will show delete
+ * options
+ *
+ * @package mod-wiki-2.0
+ * @copyright 2011 Rajesh Taneja
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once('../../config.php');
+require_once($CFG->dirroot . '/mod/wiki/lib.php');
+require_once($CFG->dirroot . '/mod/wiki/locallib.php');
+require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
+
+$pageid = required_param('pageid', PARAM_INT); // Page ID
+$delete = optional_param('delete', 0, PARAM_INT); // ID of the page to be deleted.
+$option = optional_param('option', 1, PARAM_INT); // Option ID
+$listall = optional_param('listall', 0, PARAM_INT); // list all pages
+$toversion = optional_param('toversion', 0, PARAM_INT); // max version to be deleted
+$fromversion = optional_param('fromversion', 0, PARAM_INT); // min version to be deleted
+
+if (!$page = wiki_get_page($pageid)) {
+ print_error('incorrectpageid', 'wiki');
+}
+if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
+ print_error('incorrectsubwikiid', 'wiki');
+}
+if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
+ print_error('invalidcoursemodule');
+}
+$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
+if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
+ print_error('incorrectwikiid', 'wiki');
+}
+
+require_login($course->id, true, $cm);
+
+
+$context = get_context_instance(CONTEXT_MODULE, $cm->id);
+require_capability('mod/wiki:managewiki', $context);
+add_to_log($course->id, "wiki", "admin", "admin.php?id=$cm->id", "$wiki->id");
+
+//Delete page if a page ID to delete was supplied
+if (!empty($delete) && confirm_sesskey()) {
+ wiki_delete_pages($context, $delete, $page->subwikiid);
+ //when current wiki page is deleted, then redirect user to create that page, as
+ //current pageid is invalid after deletion.
+ if ($pageid == $delete) {
+ $params = array('swid' => $page->subwikiid, 'title' => $page->title);
+ $url = new moodle_url('/mod/wiki/create.php', $params);
+ redirect($url);
+ }
+}
+
+//delete version if toversion and fromversion are set.
+if (!empty($toversion) && !empty($fromversion) && confirm_sesskey()) {
+ //make sure all versions should not be deleted...
+ $versioncount = wiki_count_wiki_page_versions($pageid);
+ $versioncount -= 1; //ignore version 0
+ $totalversionstodelete = $toversion - $fromversion;
+ $totalversionstodelete += 1; //added 1 as toversion should be included
+
+ if (($totalversionstodelete >= $versioncount) || ($versioncount <= 1)) {
+ print_error('incorrectdeleteversions', 'wiki');
+ } else {
+ $versions = array();
+ for ($i = $fromversion; $i <= $toversion; $i++) {
+ //Add all version to deletion list which exist
+ if (wiki_get_wiki_page_version($pageid, $i)) {
+ array_push($versions, $i);
+ }
+ }
+ $purgeversions[$pageid] = $versions;
+ wiki_delete_page_versions($purgeversions);
+ }
+}
+
+//show actual page
+$wikipage = new page_wiki_admin($wiki, $subwiki, $cm);
+
+$wikipage->set_page($page);
+$wikipage->print_header();
+$wikipage->set_view($option, empty($listall)?true:false);
+$wikipage->print_content();
+
+$wikipage->print_footer();
@@ -10,6 +10,8 @@
* @package wiki
*/
$string['addcomment'] = 'Add comment';
+$string['admin'] = 'Administration';
+$string['adminmenu'] = 'Admin menu';
$string['attachmentattach'] = 'Add as attachment';
$string['attachmentimage'] = 'Add as image';
$string['attachmentlink'] = 'Add as link';
@@ -43,6 +45,7 @@
$string['deleteupload'] = 'Delete';
$string['deletedbegins'] = 'Deleted begins';
$string['deletedends'] = 'Deleted ends';
+$string['deleteversions'] = 'Delete page versions';
$string['addedbegins'] = 'added begins';
$string['addedends'] = 'added ends';
$string['diff'] = 'Diff';
@@ -82,6 +85,7 @@
$string['history'] = 'History';
$string['history_help'] = 'The history lists links to previous versions of the page.';
$string['html'] = 'HTML';
+$string['incorrectdeleteversions'] = "Page versions provided for deletion are incorrect.";
$string['insertcomment'] = 'Insert comment';
$string['insertimage'] = 'Insert an image...';
$string['insertimage_help'] = 'This drop-down list will insert an image to the wiki editor. If you need to add more images to the wiki, please use "Files" tab.';
@@ -93,6 +97,8 @@
$string['lockingajaxtimeout'] = 'Edit page locking refresh time';
$string['lockingtimeout'] = 'Locking timeout';
$string['links'] = 'Links';
+$string['listall'] = 'List all';
+$string['listorphan'] = 'List orphan';
$string['map'] = 'Map';
$string['mapmenu'] = 'Map menu';
$string['migrationfinished'] = 'Migration finished successfully';
@@ -160,6 +166,7 @@
$string['repeatedsection'] = 'Wiki error: Section name cannot be repeated \'{$a}\'';
$string['restore'] = 'Restore';
$string['removeallwikitags'] = 'Remove all wiki tags';
+$string['removepages'] = 'Remove pages';
$string['restoreconfirm'] = 'Are you sure you want to restore version #{$a}?';
$string['restoreerror'] = 'Version #{$a} could not be restored';
$string['restorethis'] = 'Restore this version';
View
@@ -557,6 +557,11 @@ function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm)
$link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
$node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
}
+
+ if (has_capability('mod/wiki:managewiki', $context)) {
+ $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
+ $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
+ }
}
}
/**
View
@@ -1018,6 +1018,136 @@ function wiki_delete_old_locks() {
$DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600));
}
+/**
+ * Deletes wiki_links. It can be sepecific link or links attached in subwiki
+ *
+ * @global mixed $DB database object
+ * @param int $linkid id of the link to be deleted
+ * @param int $topageid links to the specific page
+ * @param int $frompageid links from specific page
+ * @param int $subwikiid links to subwiki
+ */
+function wiki_delete_links($linkid = null, $topageid = null, $frompageid = null, $subwikiid = null) {
+ global $DB;
+ $params = array();
+
+ // if link id is givien then don't check for anything else
+ if (!empty($linkid)) {
+ $params['id'] = $linkid;
+ } else {
+ if (!empty($topageid)) {
+ $params['topageid'] = $topageid;
+ }
+ if (!empty($frompageid)) {
+ $params['frompageid'] = $frompageid;
+ }
+ if (!empty($subwikiid)) {
+ $params['subwikiid'] = $subwikiid;
+ }
+ }
+
+ //Delete links if any params are passed, else nothing to delete.
+ if (!empty($params)) {
+ $DB->delete_records('wiki_links', $params);
+ }
+}
+
+/**
+ * Delete wiki synonyms related to subwikiid or page
+ *
+ * @param int $subwikiid id of sunbwiki
+ * @param int $pageid id of page
+ */
+function wiki_delete_synonym($subwikiid, $pageid = null) {
+ global $DB;
+
+ $params = array('subwikiid' => $subwikiid);
+ if (!is_null($pageid)) {
+ $params['pageid'] = $pageid;
+ }
+ $DB->delete_records('wiki_synonyms', $params, IGNORE_MISSING);
+}
+
+/**
+ * Delete pages and all related data
+ *
+ * @param mixed $context context in which page needs to be deleted.
+ * @param mixed $pageids id's of pages to be deleted
+ * @param int $subwikiid id of the subwiki for which all pages should be deleted
+ */
+function wiki_delete_pages($context, $pageids = null, $subwikiid = null) {
+ global $DB;
+
+ if (!empty($pageids) && is_int($pageids)) {
+ $pageids = array($pageids);
+ } else if (!empty($subwikiid)) {
+ $pageids = wiki_get_page_list($subwikiid);
+ }
+
+ //If there is no pageid then return as we can't delete anything.
+ if (empty($pageids)) {
+ return;
+ }
+
+ /// Delete page and all it's relevent data
+ foreach ($pageids as $pageid) {
+ if (is_object($pageid)) {
+ $pageid = $pageid->id;
+ }
+
+ //Delete page comments
+ $comments = wiki_get_comments($context->id, $pageid);
+ foreach ($comments as $commentid => $commentvalue) {
+ wiki_delete_comment($commentid, $context, $pageid);
+ }
+
+ //Delete page tags
+ $tags = tag_get_tags_array('wiki_pages', $pageid);
+ foreach ($tags as $tagid => $tagvalue) {
+ tag_delete_instance('wiki_pages', $pageid, $tagid);
+ }
+
+ //Delete Synonym
+ wiki_delete_synonym($subwikiid, $pageid);
+
+ //Delete all page versions
+ wiki_delete_page_versions(array($pageid=>array(0)));
+
+ //Delete all page locks
+ wiki_delete_locks($pageid);
+
+ //Delete all page links
+ wiki_delete_links(null, $pageid);
+
+ //Delete page
+ $params = array('id' => $pageid);
+ $DB->delete_records('wiki_pages', $params);
+ }
+}
+
+/**
+ * Delete specificed versions of a page or versions created by users
+ * if version is 0 then it will remove all versions of the page
+ *
+ * @param array $deleteversions delete versions for a page
+ */
+function wiki_delete_page_versions($deleteversions) {
+ global $DB;
+
+ /// delete page-versions
+ foreach ($deleteversions as $id => $versions) {
+ foreach ($versions as $version) {
+ $params = array('pageid' => $id);
+ //If version = 0, then remove all versions of this page, else remove
+ //specified version
+ if ($version != 0) {
+ $params['version'] = $version;
+ }
+ $DB->delete_records('wiki_versions', $params, IGNORE_MISSING);
+ }
+ }
+}
+
function wiki_get_comment($commentid){
global $DB;
return $DB->get_record('comments', array('id' => $commentid));
View
@@ -77,6 +77,42 @@ M.mod_wiki.history = function(Y, args) {
}
}
+M.mod_wiki.deleteversion = function(Y, args) {
+ var fromversion = false;
+ var toversion = false;
+ var radio = document.getElementsByName('fromversion');
+ var radio2 = document.getElementsByName('toversion');
+ var length = radio.length;
+ //version to should be more then version from
+ for (var i = 0; i < radio.length; i++) {
+ //if from-version is selected then disable all to-version options after that.
+ if (fromversion) {
+ radio2[i].disabled = true;
+ } else {
+ radio2[i].disabled = false;
+ }
+ //check when to-version option is selected
+ if (radio2[i].checked) {
+ toversion = true;
+ }
+ //make sure to-version should be >= from-version
+ if (radio[i].checked) {
+ fromversion = true;
+ if (!toversion) {
+ radio2[i].checked = true;
+ }
+ }
+ }
+ //avoid selecting first and last version
+ if (radio[0].checked && radio2[length-1].checked) {
+ radio2[length - 2].checked = true;
+ } else if(radio[length - 1].checked && radio2[0].checked) {
+ radio2[1].checked = true;
+ radio2[0].disabled = true;
+ toversion = true;
+ }
+}
+
M.mod_wiki.init_tree = function(Y, expand_all, htmlid) {
Y.use('yui2-treeview', function(Y) {
var tree = new YAHOO.widget.TreeView(htmlid);
Oops, something went wrong. Retry.

0 comments on commit ac0a82c

Please sign in to comment.