Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This is the external API for blogs. 19 * 20 * @package core_blog 21 * @copyright 2018 Juan Leyva 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_blog; 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->dirroot .'/blog/lib.php'); 29 require_once($CFG->dirroot .'/blog/locallib.php'); 30 31 use core_external\external_api; 32 use core_external\external_function_parameters; 33 use core_external\external_multiple_structure; 34 use core_external\external_single_structure; 35 use core_external\external_value; 36 use core_external\external_warnings; 37 use context_system; 38 use context_course; 39 use moodle_exception; 40 use core_blog\external\post_exporter; 41 42 /** 43 * This is the external API for blogs. 44 * 45 * @copyright 2018 Juan Leyva 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 47 */ 48 class external extends external_api { 49 50 /** 51 * Validate access to the blog and the filters to apply when listing entries. 52 * 53 * @param array $rawwsfilters array containing the filters in WS format 54 * @return array context, filters to apply and the calculated courseid and user 55 * @since Moodle 3.6 56 */ 57 protected static function validate_access_and_filters($rawwsfilters) { 58 global $CFG; 59 60 if (empty($CFG->enableblogs)) { 61 throw new moodle_exception('blogdisable', 'blog'); 62 } 63 64 // Init filters. 65 $filterstype = array( 66 'courseid' => PARAM_INT, 67 'groupid' => PARAM_INT, 68 'userid' => PARAM_INT, 69 'tagid' => PARAM_INT, 70 'tag' => PARAM_NOTAGS, 71 'cmid' => PARAM_INT, 72 'entryid' => PARAM_INT, 73 'search' => PARAM_RAW 74 ); 75 $filters = array( 76 'courseid' => null, 77 'groupid' => null, 78 'userid' => null, 79 'tagid' => null, 80 'tag' => null, 81 'cmid' => null, 82 'entryid' => null, 83 'search' => null 84 ); 85 86 foreach ($rawwsfilters as $filter) { 87 $name = trim($filter['name']); 88 if (!isset($filterstype[$name])) { 89 throw new moodle_exception('errorinvalidparam', 'webservice', '', $name); 90 } 91 $filters[$name] = clean_param($filter['value'], $filterstype[$name]); 92 } 93 94 // Do not overwrite here the filters, blog_get_headers and blog_listing will take care of that. 95 list($courseid, $userid) = blog_validate_access($filters['courseid'], $filters['cmid'], $filters['groupid'], 96 $filters['entryid'], $filters['userid']); 97 98 if ($courseid && $courseid != SITEID) { 99 $context = context_course::instance($courseid); 100 self::validate_context($context); 101 } else { 102 $context = context_system::instance(); 103 if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) { 104 // Everybody can see anything - no login required unless site is locked down using forcelogin. 105 if ($CFG->forcelogin) { 106 self::validate_context($context); 107 } 108 } else { 109 self::validate_context($context); 110 } 111 } 112 // Courseid and userid may not be the same that the ones in $filters. 113 return array($context, $filters, $courseid, $userid); 114 } 115 116 /** 117 * Returns description of get_entries() parameters. 118 * 119 * @return external_function_parameters 120 * @since Moodle 3.6 121 */ 122 public static function get_entries_parameters() { 123 return new external_function_parameters( 124 array( 125 'filters' => new external_multiple_structure ( 126 new external_single_structure( 127 array( 128 'name' => new external_value(PARAM_ALPHA, 129 'The expected keys (value format) are: 130 tag PARAM_NOTAGS blog tag 131 tagid PARAM_INT blog tag id 132 userid PARAM_INT blog author (userid) 133 cmid PARAM_INT course module id 134 entryid PARAM_INT entry id 135 groupid PARAM_INT group id 136 courseid PARAM_INT course id 137 search PARAM_RAW search term 138 ' 139 ), 140 'value' => new external_value(PARAM_RAW, 'The value of the filter.') 141 ) 142 ), 'Parameters to filter blog listings.', VALUE_DEFAULT, array() 143 ), 144 'page' => new external_value(PARAM_INT, 'The blog page to return.', VALUE_DEFAULT, 0), 145 'perpage' => new external_value(PARAM_INT, 'The number of posts to return per page.', VALUE_DEFAULT, 10), 146 ) 147 ); 148 } 149 150 /** 151 * Return blog entries. 152 * 153 * @param array $filters the parameters to filter the blog listing 154 * @param int $page the blog page to return 155 * @param int $perpage the number of posts to return per page 156 * @return array with the blog entries and warnings 157 * @since Moodle 3.6 158 */ 159 public static function get_entries($filters = array(), $page = 0, $perpage = 10) { 160 global $PAGE; 161 162 $warnings = array(); 163 $params = self::validate_parameters(self::get_entries_parameters(), 164 array('filters' => $filters, 'page' => $page, 'perpage' => $perpage)); 165 166 list($context, $filters, $courseid, $userid) = self::validate_access_and_filters($params['filters']); 167 168 $PAGE->set_context($context); // Needed by internal APIs. 169 $output = $PAGE->get_renderer('core'); 170 171 // Get filters. 172 $blogheaders = blog_get_headers($filters['courseid'], $filters['groupid'], $filters['userid'], $filters['tagid'], 173 $filters['tag'], $filters['cmid'], $filters['entryid'], $filters['search']); 174 $bloglisting = new \blog_listing($blogheaders['filters']); 175 176 $page = $params['page']; 177 $limit = empty($params['perpage']) ? get_user_preferences('blogpagesize', 10) : $params['perpage']; 178 $start = $page * $limit; 179 $entries = $bloglisting->get_entries($start, $limit); 180 $totalentries = $bloglisting->count_entries(); 181 182 $exportedentries = array(); 183 foreach ($entries as $entry) { 184 $exporter = new post_exporter($entry, array('context' => $context)); 185 $exportedentries[] = $exporter->export($output); 186 } 187 return array( 188 'warnings' => $warnings, 189 'entries' => $exportedentries, 190 'totalentries' => $totalentries, 191 ); 192 } 193 194 /** 195 * Returns description of get_entries() result value. 196 * 197 * @return \core_external\external_description 198 * @since Moodle 3.6 199 */ 200 public static function get_entries_returns() { 201 return new external_single_structure( 202 array( 203 'entries' => new external_multiple_structure( 204 post_exporter::get_read_structure() 205 ), 206 'totalentries' => new external_value(PARAM_INT, 'The total number of entries found.'), 207 'warnings' => new external_warnings(), 208 ) 209 ); 210 } 211 212 /** 213 * Returns description of view_entries() parameters. 214 * 215 * @return external_function_parameters 216 * @since Moodle 3.6 217 */ 218 public static function view_entries_parameters() { 219 return new external_function_parameters( 220 array( 221 'filters' => new external_multiple_structure ( 222 new external_single_structure( 223 array( 224 'name' => new external_value(PARAM_ALPHA, 225 'The expected keys (value format) are: 226 tag PARAM_NOTAGS blog tag 227 tagid PARAM_INT blog tag id 228 userid PARAM_INT blog author (userid) 229 cmid PARAM_INT course module id 230 entryid PARAM_INT entry id 231 groupid PARAM_INT group id 232 courseid PARAM_INT course id 233 search PARAM_RAW search term 234 ' 235 ), 236 'value' => new external_value(PARAM_RAW, 'The value of the filter.') 237 ) 238 ), 'Parameters used in the filter of view_entries.', VALUE_DEFAULT, array() 239 ), 240 ) 241 ); 242 } 243 244 /** 245 * Trigger the blog_entries_viewed event. 246 * 247 * @param array $filters the parameters used in the filter of get_entries 248 * @return array with status result and warnings 249 * @since Moodle 3.6 250 */ 251 public static function view_entries($filters = array()) { 252 253 $warnings = array(); 254 $params = self::validate_parameters(self::view_entries_parameters(), array('filters' => $filters)); 255 256 list($context, $filters, $courseid, $userid) = self::validate_access_and_filters($params['filters']); 257 258 $eventparams = array( 259 'other' => array('entryid' => $filters['entryid'], 'tagid' => $filters['tagid'], 'userid' => $userid, 260 'modid' => $filters['cmid'], 'groupid' => $filters['groupid'], 'search' => $filters['search'] 261 ) 262 ); 263 if (!empty($userid)) { 264 $eventparams['relateduserid'] = $userid; 265 } 266 $eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid; 267 $event = \core\event\blog_entries_viewed::create($eventparams); 268 $event->trigger(); 269 270 return array( 271 'warnings' => $warnings, 272 'status' => true, 273 ); 274 } 275 276 /** 277 * Returns description of view_entries() result value. 278 * 279 * @return \core_external\external_description 280 * @since Moodle 3.6 281 */ 282 public static function view_entries_returns() { 283 return new external_single_structure( 284 array( 285 'status' => new external_value(PARAM_BOOL, 'status: true if success'), 286 'warnings' => new external_warnings(), 287 ) 288 ); 289 } 290 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body