MDL-78408 core: fix restoration of anchor to wantsurl during login
[moodle.git] / mod / wiki / admin.php
blob96fc300c2ea7f38a8bafc1af7d34f1aff836e23a
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Delete wiki pages or versions
21 * This will show options for deleting wiki pages or purging page versions
22 * If user have wiki:managewiki ability then only this page will show delete
23 * options
25 * @package mod_wiki
26 * @copyright 2011 Rajesh Taneja
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once('../../config.php');
31 require_once($CFG->dirroot . '/mod/wiki/lib.php');
32 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
33 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
35 $pageid = required_param('pageid', PARAM_INT); // Page ID
36 $delete = optional_param('delete', 0, PARAM_INT); // ID of the page to be deleted.
37 $option = optional_param('option', 1, PARAM_INT); // Option ID
38 $listall = optional_param('listall', 0, PARAM_INT); // list all pages
39 $toversion = optional_param('toversion', 0, PARAM_INT); // max version to be deleted
40 $fromversion = optional_param('fromversion', 0, PARAM_INT); // min version to be deleted
42 if (!$page = wiki_get_page($pageid)) {
43 throw new \moodle_exception('incorrectpageid', 'wiki');
45 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
46 throw new \moodle_exception('incorrectsubwikiid', 'wiki');
48 if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
49 throw new \moodle_exception('invalidcoursemodule');
51 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
52 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
53 throw new \moodle_exception('incorrectwikiid', 'wiki');
56 require_login($course, true, $cm);
58 if (!wiki_user_can_view($subwiki, $wiki)) {
59 throw new \moodle_exception('cannotviewpage', 'wiki');
62 $context = context_module::instance($cm->id);
63 require_capability('mod/wiki:managewiki', $context);
65 //Delete page if a page ID to delete was supplied
66 if (!empty($delete) && confirm_sesskey()) {
67 if ($pageid != $delete) {
68 // Validate that we are deleting from the same subwiki.
69 $deletepage = wiki_get_page($delete);
70 if (!$deletepage || $deletepage->subwikiid != $page->subwikiid) {
71 throw new \moodle_exception('incorrectsubwikiid', 'wiki');
74 wiki_delete_pages($context, $delete, $page->subwikiid);
75 //when current wiki page is deleted, then redirect user to create that page, as
76 //current pageid is invalid after deletion.
77 if ($pageid == $delete) {
78 $params = array('swid' => $page->subwikiid, 'title' => $page->title);
79 $url = new moodle_url('/mod/wiki/create.php', $params);
80 redirect($url);
84 //delete version if toversion and fromversion are set.
85 if (!empty($toversion) && !empty($fromversion) && confirm_sesskey()) {
86 //make sure all versions should not be deleted...
87 $versioncount = wiki_count_wiki_page_versions($pageid);
88 $versioncount -= 1; //ignore version 0
89 $totalversionstodelete = $toversion - $fromversion;
90 $totalversionstodelete += 1; //added 1 as toversion should be included
92 if (($totalversionstodelete >= $versioncount) || ($versioncount <= 1)) {
93 throw new \moodle_exception('incorrectdeleteversions', 'wiki');
94 } else {
95 $versions = array();
96 for ($i = $fromversion; $i <= $toversion; $i++) {
97 //Add all version to deletion list which exist
98 if (wiki_get_wiki_page_version($pageid, $i)) {
99 array_push($versions, $i);
102 $purgeversions[$pageid] = $versions;
103 wiki_delete_page_versions($purgeversions, $context);
107 //show actual page
108 $wikipage = new page_wiki_admin($wiki, $subwiki, $cm, 'modulepage');
110 $wikipage->set_page($page);
111 $wikipage->print_header();
112 $wikipage->set_view($option, empty($listall)?true:false);
113 $wikipage->print_content();
115 $wikipage->print_footer();