MDL-51986 wiki: New WS mod_wiki_get_page_contents
authorDani Palou <dani@moodle.com>
Fri, 30 Oct 2015 11:20:33 +0000 (12:20 +0100)
committerDani Palou <dani@moodle.com>
Fri, 11 Mar 2016 13:09:36 +0000 (14:09 +0100)
mod/wiki/classes/external.php
mod/wiki/db/services.php
mod/wiki/version.php

index a6634c5..767a662 100644 (file)
@@ -594,4 +594,109 @@ class mod_wiki_external extends external_api {
         );
     }
 
+    /**
+     * Describes the parameters for get_page_contents.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.1
+     */
+    public static function get_page_contents_parameters() {
+        return new external_function_parameters (
+            array(
+                'pageid' => new external_value(PARAM_INT, 'Page ID.')
+            )
+        );
+    }
+
+    /**
+     * Get a page contents.
+     *
+     * @param int $pageid The page ID.
+     * @return array of warnings and page data.
+     * @since Moodle 3.1
+     */
+    public static function get_page_contents($pageid) {
+
+        $params = self::validate_parameters(self::get_page_contents_parameters(),
+                                            array(
+                                                'pageid' => $pageid
+                                            )
+            );
+        $warnings = array();
+
+        // Get wiki page.
+        if (!$page = wiki_get_page($params['pageid'])) {
+            throw new moodle_exception('incorrectpageid', 'wiki');
+        }
+
+        // Get wiki instance.
+        if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
+            throw new moodle_exception('incorrectwikiid', 'wiki');
+        }
+
+        // Permission validation.
+        $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
+        $context = context_module::instance($cm->id);
+        self::validate_context($context);
+
+        // Check if user can view this wiki.
+        if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
+            throw new moodle_exception('incorrectsubwikiid', 'wiki');
+        }
+        if (!wiki_user_can_view($subwiki, $wiki)) {
+            throw new moodle_exception('cannotviewpage', 'wiki');
+        }
+
+        $returnedpage = array();
+        $returnedpage['id'] = $page->id;
+        $returnedpage['wikiid'] = $wiki->id;
+        $returnedpage['subwikiid'] = $page->subwikiid;
+        $returnedpage['groupid'] = $subwiki->groupid;
+        $returnedpage['userid'] = $subwiki->userid;
+        $returnedpage['title'] = $page->title;
+
+        // Refresh page cached content if needed.
+        if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
+            if ($content = wiki_refresh_cachedcontent($page)) {
+                $page = $content['page'];
+            }
+        }
+
+        list($returnedpage['cachedcontent'], $returnedpage['contentformat']) = external_format_text(
+                            $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
+        $returnedpage['caneditpage'] = wiki_user_can_edit($subwiki);
+
+        $result = array();
+        $result['page'] = $returnedpage;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Describes the get_page_contents return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.1
+     */
+    public static function get_page_contents_returns() {
+        return new external_single_structure(
+            array(
+                'page' => new external_single_structure(
+                    array(
+                        'id' => new external_value(PARAM_INT, 'Page ID.'),
+                        'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID.'),
+                        'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'),
+                        'groupid' => new external_value(PARAM_INT, 'Page\'s group ID.'),
+                        'userid' => new external_value(PARAM_INT, 'Page\'s user ID.'),
+                        'title' => new external_value(PARAM_RAW, 'Page title.'),
+                        'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.'),
+                        'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
+                        'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.')
+                    ), 'Page'
+                ),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
+
 }
index 6894260..396e572 100644 (file)
@@ -69,5 +69,14 @@ $functions = array(
         'type'          => 'read',
         'capabilities'  => 'mod/wiki:viewpage',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
+
+    'mod_wiki_get_page_contents' => array(
+        'classname'     => 'mod_wiki_external',
+        'methodname'    => 'get_page_contents',
+        'description'   => 'Returns the contents of a page.',
+        'type'          => 'read',
+        'capabilities'  => 'mod/wiki:viewpage',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     )
 );
index b7707ef..f0472bd 100644 (file)
@@ -33,7 +33,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016011102;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016011103;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015111000;    // Requires this Moodle version
 $plugin->component = 'mod_wiki';       // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;