Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…as in 1.9
  • Loading branch information...
commit ac0a82cf92d7cb9cec397f8d0d37a0f3599b31b7 1 parent 9805fb9
authored August 11, 2011
106  mod/wiki/admin.php
... ...
@@ -0,0 +1,106 @@
  1
+<?php
  2
+
  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.
  14
+//
  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/>.
  17
+
  18
+/**
  19
+ * Delete wiki pages or versions
  20
+ *
  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
  24
+ *
  25
+ * @package mod-wiki-2.0
  26
+ * @copyright 2011 Rajesh Taneja
  27
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28
+ */
  29
+
  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');
  34
+
  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
  41
+
  42
+if (!$page = wiki_get_page($pageid)) {
  43
+    print_error('incorrectpageid', 'wiki');
  44
+}
  45
+if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
  46
+    print_error('incorrectsubwikiid', 'wiki');
  47
+}
  48
+if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
  49
+    print_error('invalidcoursemodule');
  50
+}
  51
+$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  52
+if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
  53
+    print_error('incorrectwikiid', 'wiki');
  54
+}
  55
+
  56
+require_login($course->id, true, $cm);
  57
+
  58
+
  59
+$context = get_context_instance(CONTEXT_MODULE, $cm->id);
  60
+require_capability('mod/wiki:managewiki', $context);
  61
+add_to_log($course->id, "wiki", "admin", "admin.php?id=$cm->id", "$wiki->id");
  62
+
  63
+//Delete page if a page ID to delete was supplied
  64
+if (!empty($delete) && confirm_sesskey()) {
  65
+    wiki_delete_pages($context, $delete, $page->subwikiid);
  66
+    //when current wiki page is deleted, then redirect user to create that page, as
  67
+    //current pageid is invalid after deletion.
  68
+    if ($pageid == $delete) {
  69
+        $params = array('swid' => $page->subwikiid, 'title' => $page->title);
  70
+        $url = new moodle_url('/mod/wiki/create.php', $params);
  71
+        redirect($url);
  72
+    }
  73
+}
  74
+
  75
+//delete version if toversion and fromversion are set.
  76
+if (!empty($toversion) && !empty($fromversion) && confirm_sesskey()) {
  77
+    //make sure all versions should not be deleted...
  78
+    $versioncount = wiki_count_wiki_page_versions($pageid);
  79
+    $versioncount -= 1; //ignore version 0
  80
+    $totalversionstodelete = $toversion - $fromversion;
  81
+    $totalversionstodelete += 1; //added 1 as toversion should be included
  82
+
  83
+    if (($totalversionstodelete >= $versioncount) || ($versioncount <= 1)) {
  84
+        print_error('incorrectdeleteversions', 'wiki');
  85
+    } else {
  86
+        $versions = array();
  87
+        for ($i = $fromversion; $i <= $toversion; $i++) {
  88
+            //Add all version to deletion list which exist
  89
+            if (wiki_get_wiki_page_version($pageid, $i)) {
  90
+                array_push($versions, $i);
  91
+            }
  92
+        }
  93
+        $purgeversions[$pageid] = $versions;
  94
+        wiki_delete_page_versions($purgeversions);
  95
+    }
  96
+}
  97
+
  98
+//show actual page
  99
+$wikipage = new page_wiki_admin($wiki, $subwiki, $cm);
  100
+
  101
+$wikipage->set_page($page);
  102
+$wikipage->print_header();
  103
+$wikipage->set_view($option, empty($listall)?true:false);
  104
+$wikipage->print_content();
  105
+
  106
+$wikipage->print_footer();
7  mod/wiki/lang/en/wiki.php
@@ -10,6 +10,8 @@
10 10
  * @package wiki
11 11
  */
12 12
 $string['addcomment'] = 'Add comment';
  13
+$string['admin'] = 'Administration';
  14
+$string['adminmenu'] = 'Admin menu';
13 15
 $string['attachmentattach'] = 'Add as attachment';
14 16
 $string['attachmentimage'] = 'Add as image';
15 17
 $string['attachmentlink'] = 'Add as link';
@@ -43,6 +45,7 @@
43 45
 $string['deleteupload'] = 'Delete';
44 46
 $string['deletedbegins'] = 'Deleted begins';
45 47
 $string['deletedends'] = 'Deleted ends';
  48
+$string['deleteversions'] = 'Delete page versions';
46 49
 $string['addedbegins'] = 'added begins';
47 50
 $string['addedends'] = 'added ends';
48 51
 $string['diff'] = 'Diff';
@@ -82,6 +85,7 @@
82 85
 $string['history'] = 'History';
83 86
 $string['history_help'] = 'The history lists links to previous versions of the page.';
84 87
 $string['html'] = 'HTML';
  88
+$string['incorrectdeleteversions'] = "Page versions provided for deletion are incorrect.";
85 89
 $string['insertcomment'] = 'Insert comment';
86 90
 $string['insertimage'] = 'Insert an image...';
87 91
 $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 @@
93 97
 $string['lockingajaxtimeout'] = 'Edit page locking refresh time';
94 98
 $string['lockingtimeout'] = 'Locking timeout';
95 99
 $string['links'] = 'Links';
  100
+$string['listall'] = 'List all';
  101
+$string['listorphan'] = 'List orphan';
96 102
 $string['map'] = 'Map';
97 103
 $string['mapmenu'] = 'Map menu';
98 104
 $string['migrationfinished'] = 'Migration finished successfully';
@@ -160,6 +166,7 @@
160 166
 $string['repeatedsection'] = 'Wiki error: Section name cannot be repeated \'{$a}\'';
161 167
 $string['restore'] = 'Restore';
162 168
 $string['removeallwikitags'] = 'Remove all wiki tags';
  169
+$string['removepages'] = 'Remove pages';
163 170
 $string['restoreconfirm'] = 'Are you sure you want to restore version #{$a}?';
164 171
 $string['restoreerror'] = 'Version #{$a} could not be restored';
165 172
 $string['restorethis'] = 'Restore this version';
5  mod/wiki/lib.php
@@ -557,6 +557,11 @@ function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm)
557 557
             $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
558 558
             $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
559 559
         }
  560
+
  561
+        if (has_capability('mod/wiki:managewiki', $context)) {
  562
+            $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
  563
+            $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
  564
+        }
560 565
     }
561 566
 }
562 567
 /**
130  mod/wiki/locallib.php
@@ -1018,6 +1018,136 @@ function wiki_delete_old_locks() {
1018 1018
     $DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600));
1019 1019
 }
1020 1020
 
  1021
+/**
  1022
+ * Deletes wiki_links. It can be sepecific link or links attached in subwiki
  1023
+ *
  1024
+ * @global mixed $DB database object
  1025
+ * @param int $linkid id of the link to be deleted
  1026
+ * @param int $topageid links to the specific page
  1027
+ * @param int $frompageid links from specific page
  1028
+ * @param int $subwikiid links to subwiki
  1029
+ */
  1030
+function wiki_delete_links($linkid = null, $topageid = null, $frompageid = null, $subwikiid = null) {
  1031
+    global $DB;
  1032
+    $params = array();
  1033
+
  1034
+    // if link id is givien then don't check for anything else
  1035
+    if (!empty($linkid)) {
  1036
+        $params['id'] = $linkid;
  1037
+    } else {
  1038
+        if (!empty($topageid)) {
  1039
+            $params['topageid'] = $topageid;
  1040
+        }
  1041
+        if (!empty($frompageid)) {
  1042
+            $params['frompageid'] = $frompageid;
  1043
+        }
  1044
+        if (!empty($subwikiid)) {
  1045
+            $params['subwikiid'] = $subwikiid;
  1046
+        }
  1047
+    }
  1048
+
  1049
+    //Delete links if any params are passed, else nothing to delete.
  1050
+    if (!empty($params)) {
  1051
+        $DB->delete_records('wiki_links', $params);
  1052
+    }
  1053
+}
  1054
+
  1055
+/**
  1056
+ * Delete wiki synonyms related to subwikiid or page
  1057
+ *
  1058
+ * @param int $subwikiid id of sunbwiki
  1059
+ * @param int $pageid id of page
  1060
+ */
  1061
+function wiki_delete_synonym($subwikiid, $pageid = null) {
  1062
+    global $DB;
  1063
+
  1064
+    $params = array('subwikiid' => $subwikiid);
  1065
+    if (!is_null($pageid)) {
  1066
+        $params['pageid'] = $pageid;
  1067
+    }
  1068
+    $DB->delete_records('wiki_synonyms', $params, IGNORE_MISSING);
  1069
+}
  1070
+
  1071
+/**
  1072
+ * Delete pages and all related data
  1073
+ *
  1074
+ * @param mixed $context context in which page needs to be deleted.
  1075
+ * @param mixed $pageids id's of pages to be deleted
  1076
+ * @param int $subwikiid id of the subwiki for which all pages should be deleted
  1077
+ */
  1078
+function wiki_delete_pages($context, $pageids = null, $subwikiid = null) {
  1079
+    global $DB;
  1080
+
  1081
+    if (!empty($pageids) && is_int($pageids)) {
  1082
+       $pageids = array($pageids);
  1083
+    } else if (!empty($subwikiid)) {
  1084
+        $pageids = wiki_get_page_list($subwikiid);
  1085
+    }
  1086
+
  1087
+    //If there is no pageid then return as we can't delete anything.
  1088
+    if (empty($pageids)) {
  1089
+        return;
  1090
+    }
  1091
+
  1092
+    /// Delete page and all it's relevent data
  1093
+    foreach ($pageids as $pageid) {
  1094
+        if (is_object($pageid)) {
  1095
+            $pageid = $pageid->id;
  1096
+        }
  1097
+
  1098
+        //Delete page comments
  1099
+        $comments = wiki_get_comments($context->id, $pageid);
  1100
+        foreach ($comments as $commentid => $commentvalue) {
  1101
+            wiki_delete_comment($commentid, $context, $pageid);
  1102
+        }
  1103
+
  1104
+        //Delete page tags
  1105
+        $tags = tag_get_tags_array('wiki_pages', $pageid);
  1106
+        foreach ($tags as $tagid => $tagvalue) {
  1107
+            tag_delete_instance('wiki_pages', $pageid, $tagid);
  1108
+        }
  1109
+
  1110
+        //Delete Synonym
  1111
+        wiki_delete_synonym($subwikiid, $pageid);
  1112
+
  1113
+        //Delete all page versions
  1114
+        wiki_delete_page_versions(array($pageid=>array(0)));
  1115
+
  1116
+        //Delete all page locks
  1117
+        wiki_delete_locks($pageid);
  1118
+
  1119
+        //Delete all page links
  1120
+        wiki_delete_links(null, $pageid);
  1121
+
  1122
+        //Delete page
  1123
+        $params = array('id' => $pageid);
  1124
+        $DB->delete_records('wiki_pages', $params);
  1125
+    }
  1126
+}
  1127
+
  1128
+/**
  1129
+ * Delete specificed versions of a page or versions created by users
  1130
+ * if version is 0 then it will remove all versions of the page
  1131
+ *
  1132
+ * @param array $deleteversions delete versions for a page
  1133
+ */
  1134
+function wiki_delete_page_versions($deleteversions) {
  1135
+    global $DB;
  1136
+
  1137
+    /// delete page-versions
  1138
+    foreach ($deleteversions as $id => $versions) {
  1139
+        foreach ($versions as $version) {
  1140
+            $params = array('pageid' => $id);
  1141
+            //If version = 0, then remove all versions of this page, else remove
  1142
+            //specified version
  1143
+            if ($version != 0) {
  1144
+                $params['version'] = $version;
  1145
+            }
  1146
+            $DB->delete_records('wiki_versions', $params, IGNORE_MISSING);
  1147
+        }
  1148
+    }
  1149
+}
  1150
+
1021 1151
 function wiki_get_comment($commentid){
1022 1152
     global $DB;
1023 1153
     return $DB->get_record('comments', array('id' => $commentid));
36  mod/wiki/module.js
@@ -77,6 +77,42 @@ M.mod_wiki.history = function(Y, args) {
77 77
     }
78 78
 }
79 79
 
  80
+M.mod_wiki.deleteversion = function(Y, args) {
  81
+    var fromversion = false;
  82
+    var toversion = false;
  83
+    var radio  = document.getElementsByName('fromversion');
  84
+    var radio2 = document.getElementsByName('toversion');
  85
+    var length = radio.length;
  86
+    //version to should be more then version from
  87
+    for (var i = 0; i < radio.length; i++) {
  88
+        //if from-version is selected then disable all to-version options after that.
  89
+        if (fromversion) {
  90
+            radio2[i].disabled = true;
  91
+        } else {
  92
+            radio2[i].disabled = false;
  93
+        }
  94
+        //check when to-version option is selected
  95
+        if (radio2[i].checked) {
  96
+            toversion = true;
  97
+        }
  98
+        //make sure to-version should be >= from-version
  99
+        if (radio[i].checked) {
  100
+            fromversion = true;
  101
+            if (!toversion) {
  102
+                radio2[i].checked = true;
  103
+            }
  104
+        }
  105
+    }
  106
+    //avoid selecting first and last version
  107
+    if (radio[0].checked && radio2[length-1].checked) {
  108
+        radio2[length - 2].checked = true;
  109
+    } else if(radio[length - 1].checked && radio2[0].checked) {
  110
+        radio2[1].checked = true;
  111
+        radio2[0].disabled = true;
  112
+        toversion = true;
  113
+    }
  114
+}
  115
+
80 116
 M.mod_wiki.init_tree = function(Y, expand_all, htmlid) {
81 117
     Y.use('yui2-treeview', function(Y) {
82 118
         var tree = new YAHOO.widget.TreeView(htmlid);
338  mod/wiki/pagelib.php
@@ -76,7 +76,8 @@
76 76
      * @var array The tabs set used in wiki module
77 77
      */
78 78
     protected $tabs = array('view' => 'view', 'edit' => 'edit', 'comments' => 'comments',
79  
-                            'history' => 'history', 'map' => 'map', 'files' => 'files');
  79
+                            'history' => 'history', 'map' => 'map', 'files' => 'files',
  80
+                            'admin' => 'admin');
80 81
     /**
81 82
      * @var array tabs options
82 83
      */
@@ -2269,3 +2270,338 @@ private function print_overridelocks() {
2269 2270
     }
2270 2271
 
2271 2272
 }
  2273
+
  2274
+/**
  2275
+ * This class will let user to delete wiki pages and page versions
  2276
+ *
  2277
+ */
  2278
+class page_wiki_admin extends page_wiki {
  2279
+
  2280
+    public $view, $action;
  2281
+    public $listorphan = false;
  2282
+
  2283
+    /**
  2284
+     * Constructor
  2285
+     *
  2286
+     * @global object $PAGE
  2287
+     * @param mixed $wiki instance of wiki
  2288
+     * @param mixed $subwiki instance of subwiki
  2289
+     * @param stdClass $cm course module
  2290
+     */
  2291
+    function __construct($wiki, $subwiki, $cm) {
  2292
+        global $PAGE;
  2293
+        parent::__construct($wiki, $subwiki, $cm);
  2294
+        $PAGE->requires->js_init_call('M.mod_wiki.deleteversion', null, true);
  2295
+    }
  2296
+
  2297
+    /**
  2298
+     * Prints header for wiki page
  2299
+     */
  2300
+    function print_header() {
  2301
+        parent::print_header();
  2302
+        $this->print_pagetitle();
  2303
+    }
  2304
+
  2305
+    /**
  2306
+     * This function will display administration view to users with managewiki capability
  2307
+     */
  2308
+    function print_content() {
  2309
+        //make sure anyone trying to access this page has managewiki capabilities
  2310
+        require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
  2311
+
  2312
+        //update wiki cache if timedout
  2313
+        $page = $this->page;
  2314
+        if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
  2315
+            $fresh = wiki_refresh_cachedcontent($page);
  2316
+            $page = $fresh['page'];
  2317
+        }
  2318
+
  2319
+        //dispaly admin menu
  2320
+        echo $this->wikioutput->menu_admin($this->page->id, $this->view);
  2321
+
  2322
+        //Display appropriate admin view
  2323
+        switch ($this->view) {
  2324
+            case 1: //delete page view
  2325
+                $this->print_delete_content($this->listorphan);
  2326
+                break;
  2327
+            case 2: //delete version view
  2328
+                $this->print_delete_version();
  2329
+                break;
  2330
+            default: //default is delete view
  2331
+                $this->print_delete_content($this->listorphan);
  2332
+                break;
  2333
+        }
  2334
+    }
  2335
+
  2336
+    /**
  2337
+     * Sets admin view option
  2338
+     *
  2339
+     * @param int $view page view id
  2340
+     * @param bool $listorphan is only valid for view 1.
  2341
+     */
  2342
+    public function set_view($view, $listorphan = true) {
  2343
+        $this->view = $view;
  2344
+        $this->listorphan = $listorphan;
  2345
+    }
  2346
+
  2347
+    /**
  2348
+     * Sets page url
  2349
+     *
  2350
+     * @global object $PAGE
  2351
+     * @global object $CFG
  2352
+     */
  2353
+    function set_url() {
  2354
+        global $PAGE, $CFG;
  2355
+        $PAGE->set_url($CFG->wwwroot . '/mod/wiki/admin.php', array('pageid' => $this->page->id));
  2356
+    }
  2357
+
  2358
+    /**
  2359
+     * sets navigation bar for the page
  2360
+     *
  2361
+     * @global object $PAGE
  2362
+     */
  2363
+    protected function create_navbar() {
  2364
+        global $PAGE;
  2365
+
  2366
+        parent::create_navbar();
  2367
+        $PAGE->navbar->add(get_string('admin', 'wiki'));
  2368
+    }
  2369
+
  2370
+    /**
  2371
+     * Show wiki page delete options
  2372
+     *
  2373
+     * @param bool $showorphan
  2374
+     */
  2375
+    protected function print_delete_content($showorphan = true) {
  2376
+        $contents = array();
  2377
+        $table = new html_table();
  2378
+        $table->head = array('','Page name');
  2379
+        $table->attributes['class'] = 'generaltable mdl-align';
  2380
+        $swid = $this->subwiki->id;
  2381
+        if ($showorphan) {
  2382
+            if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
  2383
+                $this->add_page_delete_options($orphanedpages, $swid, $table);
  2384
+            } else {
  2385
+                $table->data[] = array('', get_string('noorphanedpages', 'wiki'));
  2386
+            }
  2387
+        } else {
  2388
+            if ($pages = wiki_get_page_list($swid)) {
  2389
+                $this->add_page_delete_options($pages, $swid, $table);
  2390
+            } else {
  2391
+                $table->data[] = array('', get_string('nopages', 'wiki'));
  2392
+            }
  2393
+        }
  2394
+
  2395
+        ///Print the form
  2396
+        echo html_writer::start_tag('form', array(
  2397
+                                                'action' => new moodle_url('/mod/wiki/admin.php'),
  2398
+                                                'method' => 'post'));
  2399
+        echo html_writer::tag('div', html_writer::empty_tag('input', array(
  2400
+                                                                         'type'  => 'hidden',
  2401
+                                                                         'name'  => 'pageid',
  2402
+                                                                         'value' => $this->page->id)));
  2403
+
  2404
+        echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
  2405
+        echo html_writer::table($table);
  2406
+        echo html_writer::start_tag('div', array('class' => 'mdl-align'));
  2407
+        if (!$showorphan) {
  2408
+            echo html_writer::empty_tag('input', array(
  2409
+                                                     'type'    => 'submit',
  2410
+                                                     'class'   => 'wiki_form-button',
  2411
+                                                     'value'   => get_string('listorphan', 'wiki'),
  2412
+                                                     'sesskey' => sesskey()));
  2413
+        } else {
  2414
+            echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'listall', 'value'=>'1'));
  2415
+            echo html_writer::empty_tag('input', array(
  2416
+                                                     'type'    => 'submit',
  2417
+                                                     'class'   => 'wiki_form-button',
  2418
+                                                     'value'   => get_string('listall', 'wiki'),
  2419
+                                                     'sesskey' => sesskey()));
  2420
+        }
  2421
+        echo html_writer::end_tag('div');
  2422
+        echo html_writer::end_tag('form');
  2423
+    }
  2424
+
  2425
+    /**
  2426
+     * helper function for print_delete_content. This will add data to the table.
  2427
+     *
  2428
+     * @global object $OUTPUT
  2429
+     * @param array $pages objects of wiki pages in subwiki
  2430
+     * @param int $swid id of subwiki
  2431
+     * @param object $table reference to the table in which data needs to be added
  2432
+     */
  2433
+    protected function add_page_delete_options($pages, $swid, &$table) {
  2434
+        global $OUTPUT;
  2435
+        foreach ($pages as $page) {
  2436
+            $link = wiki_parser_link($page->title, array('swid' => $swid));
  2437
+            $class = ($link['new']) ? 'class="wiki_newentry"' : '';
  2438
+            $pagelink = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
  2439
+            $urledit = new moodle_url('/mod/wiki/edit.php', array('pageid' => $page->id, 'sesskey' => sesskey()));
  2440
+            $urldelete = new moodle_url('/mod/wiki/admin.php', array(
  2441
+                                                                   'pageid'  => $this->page->id,
  2442
+                                                                   'delete'  => $page->id,
  2443
+                                                                   'option'  => $this->view,
  2444
+                                                                   'listall' => !$this->listorphan?'1': '',
  2445
+                                                                   'sesskey' => sesskey()));
  2446
+
  2447
+            $editlinks = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit')));
  2448
+            $editlinks .= $OUTPUT->action_icon($urldelete, new pix_icon('t/delete', get_string('delete')));
  2449
+            $table->data[] = array($editlinks, $pagelink);
  2450
+        }
  2451
+    }
  2452
+
  2453
+    /**
  2454
+     * Prints lists of versions which can be deleted
  2455
+     *
  2456
+     * @global object $OUTPUT
  2457
+     */
  2458
+    private function print_delete_version() {
  2459
+        global $OUTPUT;
  2460
+        $pageid = $this->page->id;
  2461
+
  2462
+        // versioncount is the latest version
  2463
+        $versioncount = wiki_count_wiki_page_versions($pageid) - 1;
  2464
+        $versions = wiki_get_wiki_page_versions($pageid, 0, $versioncount);
  2465
+
  2466
+        // We don't want version 0 to be displayed
  2467
+        // version 0 is blank page
  2468
+        if (end($versions)->version == 0) {
  2469
+            array_pop($versions);
  2470
+        }
  2471
+
  2472
+        $contents = array();
  2473
+        $version0page = wiki_get_wiki_page_version($this->page->id, 0);
  2474
+        $creator = wiki_get_user_info($version0page->userid);
  2475
+        $a = new stdClass();
  2476
+        $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
  2477
+        $a->username = $creator->username;
  2478
+        echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
  2479
+        if ($versioncount > 0) {
  2480
+            /// If there is only one version, we don't need radios nor forms
  2481
+            if (count($versions) == 1) {
  2482
+                $row = array_shift($versions);
  2483
+                $username = wiki_get_user_info($row->userid);
  2484
+                $picture = $OUTPUT->user_picture($username);
  2485
+                $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
  2486
+                $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
  2487
+                $versionid = wiki_get_version($row->id);
  2488
+                $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
  2489
+                $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
  2490
+                $picturelink = $picture . html_writer::link($userlink->out(false), fullname($username));
  2491
+                $historydate = $OUTPUT->container($date, 'wiki_histdate');
  2492
+                $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picturelink, $time, $historydate);
  2493
+
  2494
+                //Show current version
  2495
+                $table = new html_table();
  2496
+                $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
  2497
+                $table->data = $contents;
  2498
+                $table->attributes['class'] = 'mdl-align';
  2499
+
  2500
+                echo html_writer::table($table);
  2501
+            } else {
  2502
+                $lastdate = '';
  2503
+                $rowclass = array();
  2504
+
  2505
+                foreach ($versions as $version) {
  2506
+                    $user = wiki_get_user_info($version->userid);
  2507
+                    $picture = $OUTPUT->user_picture($user, array('popup' => true));
  2508
+                    $date = userdate($version->timecreated, get_string('strftimedate'));
  2509
+                    if ($date == $lastdate) {
  2510
+                        $date = '';
  2511
+                        $rowclass[] = '';
  2512
+                    } else {
  2513
+                        $lastdate = $date;
  2514
+                        $rowclass[] = 'wiki_histnewdate';
  2515
+                    }
  2516
+
  2517
+                    $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
  2518
+                    $versionid = wiki_get_version($version->id);
  2519
+                    if ($versionid) {
  2520
+                        $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
  2521
+                        $viewlink = html_writer::link($url->out(false), $version->version);
  2522
+                    } else {
  2523
+                        $viewlink = $version->version;
  2524
+                    }
  2525
+
  2526
+                    $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
  2527
+                    $picturelink = $picture . html_writer::link($userlink->out(false), fullname($user));
  2528
+                    $historydate = $OUTPUT->container($date, 'wiki_histdate');
  2529
+                    $radiofromelement = $this->choose_from_radio(array($version->version  => null), 'fromversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
  2530
+                    $radiotoelement = $this->choose_from_radio(array($version->version  => null), 'toversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
  2531
+                    $contents[] = array( $radiofromelement . $radiotoelement, $viewlink, $picturelink, $time, $historydate);
  2532
+                }
  2533
+
  2534
+                $table = new html_table();
  2535
+                $table->head = array(get_string('deleteversions', 'wiki'), get_string('version'), get_string('user'), get_string('modified'), '');
  2536
+                $table->data = $contents;
  2537
+                $table->attributes['class'] = 'generaltable mdl-align';
  2538
+                $table->rowclasses = $rowclass;
  2539
+
  2540
+                ///Print the form
  2541
+                echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/admin.php'), 'method' => 'post'));
  2542
+                echo html_writer::tag('div', html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pageid', 'value' => $pageid)));
  2543
+                echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
  2544
+                echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' =>  sesskey()));
  2545
+                echo html_writer::table($table);
  2546
+                echo html_writer::start_tag('div', array('class' => 'mdl-align'));
  2547
+                echo html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'wiki_form-button', 'value' => get_string('deleteversions', 'wiki')));
  2548
+                echo html_writer::end_tag('div');
  2549
+                echo html_writer::end_tag('form');
  2550
+            }
  2551
+        } else {
  2552
+            print_string('nohistory', 'wiki');
  2553
+        }
  2554
+    }
  2555
+
  2556
+    /**
  2557
+     * Given an array of values, creates a group of radio buttons to be part of a form
  2558
+     * helper function for print_delete_version
  2559
+     *
  2560
+     * @param array  $options  An array of value-label pairs for the radio group (values as keys).
  2561
+     * @param string $name     Name of the radiogroup (unique in the form).
  2562
+     * @param string $onclick  Function to be executed when the radios are clicked.
  2563
+     * @param string $checked  The value that is already checked.
  2564
+     * @param bool   $return   If true, return the HTML as a string, otherwise print it.
  2565
+     *
  2566
+     * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
  2567
+     */
  2568
+    private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
  2569
+
  2570
+        static $idcounter = 0;
  2571
+
  2572
+        if (!$name) {
  2573
+            $name = 'unnamed';
  2574
+        }
  2575
+
  2576
+        $output = '<span class="radiogroup ' . $name . "\">\n";
  2577
+
  2578
+        if (!empty($options)) {
  2579
+            $currentradio = 0;
  2580
+            foreach ($options as $value => $label) {
  2581
+                $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
  2582
+                $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
  2583
+                $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
  2584
+                if ($value == $checked) {
  2585
+                    $output .= ' checked="checked"';
  2586
+                }
  2587
+                if ($onclick) {
  2588
+                    $output .= ' onclick="' . $onclick . '"';
  2589
+                }
  2590
+                if ($label === '') {
  2591
+                    $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
  2592
+                } else {
  2593
+                    $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
  2594
+                }
  2595
+                $currentradio = ($currentradio + 1) % 2;
  2596
+            }
  2597
+        }
  2598
+
  2599
+        $output .= '</span>' . "\n";
  2600
+
  2601
+        if ($return) {
  2602
+            return $output;
  2603
+        } else {
  2604
+            echo $output;
  2605
+        }
  2606
+    }
  2607
+}
18  mod/wiki/renderer.php
@@ -278,6 +278,9 @@ public function tabs($page, $tabitems, $options) {
278 278
             if (($tab == 'view' || $tab == 'map' || $tab == 'history') && !has_capability('mod/wiki:viewpage', $context)) {
279 279
                 continue;
280 280
             }
  281
+            if ($tab == 'admin' && !has_capability('mod/wiki:managewiki', $context)) {
  282
+                continue;
  283
+            }
281 284
             $link = $baseurl . $tab . '.php?pageid=' . $pageid;
282 285
             if ($linked == $tab) {
283 286
                 $tabs[] = new tabobject($tab, $link, get_string($tab, 'wiki'), '', true);
@@ -494,6 +497,21 @@ public function render_wiki_files_tree(wiki_files_tree $tree) {
494 497
         return $html;
495 498
     }
496 499
 
  500
+    function menu_admin($pageid, $currentselect) {
  501
+        $options = array('removepages', 'deleteversions');
  502
+        $items = array();
  503
+        foreach ($options as $opt) {
  504
+            $items[] = get_string($opt, 'wiki');
  505
+        }
  506
+        $selectoptions = array();
  507
+        foreach ($items as $key => $item) {
  508
+            $selectoptions[$key + 1] = $item;
  509
+        }
  510
+        $select = new single_select(new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid)), 'option', $selectoptions, $currentselect);
  511
+        $select->label = get_string('adminmenu', 'wiki') . ': ';
  512
+        return $this->output->container($this->output->render($select), 'midpad');
  513
+    }
  514
+
497 515
     /**
498 516
      * Internal function - creates htmls structure suitable for YUI tree.
499 517
      */

0 notes on commit ac0a82c

Please sign in to comment.
Something went wrong with that request. Please try again.