Skip to content

Commit

Permalink
MDL-74760 activity web services: refactor duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Jun 7, 2022
1 parent 5500d14 commit 6c161f3
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 452 deletions.
135 changes: 135 additions & 0 deletions course/classes/external/helper_for_get_mods_by_courses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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/>.

namespace core_course\external;

use context_module;
use external_description;
use external_files;
use external_format_value;
use external_util;
use external_value;

/**
* This class helps implement the get_..._by_courses web service that every activity should have.
*
* It has helper methods to add the standard course-module fields to the results, and the declaration of the return value.
*
* @package core_course
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class helper_for_get_mods_by_courses {

/**
* Add the value of all the standard fields to the results to be returned by the service.
* This is designed to be used in the implementation of the get_..._by_courses web service methods.
*
* Note that $modinstance is also updated in-place.
*
* @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}.
* @param string $component the plugin name, e.g. 'mod_book'.
* @param string $capabilityforgroups capability to check before including group/visible/section info in the results.
* @param string|null $capabilityforintro capability to check before including intro info in the results.
* null means always include (the default).
* @return array with the containing all the values declared in {@see standard_coursemodule_elements_returns()}.
*/
public static function standard_coursemodule_element_values(\stdClass $modinstance, string $component,
string $capabilityforgroups = 'moodle/course:manageactivities', string $capabilityforintro = null): array {
self::format_name_and_intro($modinstance, $component);
$context = context_module::instance($modinstance->coursemodule);

// First, we return information that any user can see in the web interface.
$moddetails['id'] = $modinstance->id;
$moddetails['coursemodule'] = $modinstance->coursemodule;
$moddetails['course'] = $modinstance->course;
$moddetails['name'] = $modinstance->name;
if (!$capabilityforintro || has_capability($capabilityforintro, $context)) {
$moddetails['intro'] = $modinstance->intro;
$moddetails['introformat'] = $modinstance->introformat;
$moddetails['introfiles'] = $modinstance->introfiles;
}

// Now add information only available to people who can edit.
if (has_capability($capabilityforgroups, $context)) {
$moddetails['section'] = $modinstance->section;
$moddetails['visible'] = $modinstance->visible;
$moddetails['groupmode'] = $modinstance->groupmode;
$moddetails['groupingid'] = $modinstance->groupingid;
}

return $moddetails;
}

/**
* Format the module name an introduction ready to be exported to a web service.
*
* Note that $modinstance is updated in-place.
*
* @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}.
* @param string $component the plugin name, e.g. 'mod_book'.
*/
public static function format_name_and_intro(\stdClass $modinstance, string $component) {
$context = context_module::instance($modinstance->coursemodule);

$modinstance->name = external_format_string($modinstance->name, $context->id);

[$modinstance->intro, $modinstance->introformat] = external_format_text(
$modinstance->intro, $modinstance->introformat, $context->id,
$component, 'intro', null, ['noclean' => true]);
$modinstance->introfiles = external_util::get_area_files($context->id, $component, 'intro', false, false);
}

/**
* Get the list of standard fields, to add to the declaration of the return values.
*
* Example usage combine the fields returned here with any extra ones your activity uses:
*
* public static function execute_returns() {
* return new external_single_structure([
* 'bigbluebuttonbns' => new external_multiple_structure(
* new external_single_structure(array_merge(
* helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
* [
* 'meetingid' => new external_value(PARAM_RAW, 'Meeting id'),
* 'timemodified' => new external_value(PARAM_INT, 'Last time the instance was modified'),
* ]
* ))
* ),
* 'warnings' => new external_warnings(),
* ]
* );
* }
*
* @param bool $introoptional if true, the intro fields are marked as optional. Default false.
* @return external_description[] array of standard fields, to which you can add your activity-specific ones.
*/
public static function standard_coursemodule_elements_returns(bool $introoptional = false): array {
return [
'id' => new external_value(PARAM_INT, 'Activity instance id'),
'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
'course' => new external_value(PARAM_INT, 'Course id'),
'name' => new external_value(PARAM_RAW, 'Activity name'),
'intro' => new external_value(PARAM_RAW, 'Activity introduction', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED),
'introformat' => new external_format_value('intro', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED),
'introfiles' => new external_files('Files in the introduction', VALUE_OPTIONAL),
'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL),
'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace mod_bigbluebuttonbn\external;

use context_module;
use core_course\external\helper_for_get_mods_by_courses;
use external_api;
use external_files;
use external_format_value;
Expand Down Expand Up @@ -86,15 +87,7 @@ public static function execute($courseids = []) {
// We can avoid then additional validate_context calls.
$bigbluebuttonbns = get_all_instances_in_courses("bigbluebuttonbn", $courses, $USER->id);
foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
$context = context_module::instance($bigbluebuttonbn->coursemodule);
// Entry to return.
$bigbluebuttonbn->name = external_format_string($bigbluebuttonbn->name, $context->id);

[$bigbluebuttonbn->intro, $bigbluebuttonbn->introformat] = external_format_text($bigbluebuttonbn->intro,
$bigbluebuttonbn->introformat, $context->id, 'mod_bigbluebuttonbn', 'intro', null);
$bigbluebuttonbn->introfiles = external_util::get_area_files($context->id,
'mod_bigbluebuttonbn', 'intro', false, false);

helper_for_get_mods_by_courses::format_name_and_intro($bigbluebuttonbn, 'mod_bigbluebuttonbn');
$returnedbigbluebuttonbns[] = $bigbluebuttonbn;
}
}
Expand All @@ -115,22 +108,13 @@ public static function execute($courseids = []) {
public static function execute_returns() {
return new external_single_structure([
'bigbluebuttonbns' => new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'Module id'),
'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
'course' => new external_value(PARAM_INT, 'Course id'),
'name' => new external_value(PARAM_RAW, 'Name'),
'intro' => new external_value(PARAM_RAW, 'Description'),
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'meetingid' => new external_value(PARAM_RAW, 'Meeting id'),
'introformat' => new external_format_value('intro', 'Summary format'),
'introfiles' => new external_files('Files in the introduction text'),
'timemodified' => new external_value(PARAM_INT, 'Last time the instance was modified'),
'section' => new external_value(PARAM_INT, 'Course section id'),
'visible' => new external_value(PARAM_INT, 'Module visibility'),
'groupmode' => new external_value(PARAM_INT, 'Group mode'),
'groupingid' => new external_value(PARAM_INT, 'Grouping id'),
]
)
))
),
'warnings' => new external_warnings(),
]
Expand Down
43 changes: 9 additions & 34 deletions mod/book/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @since Moodle 3.0
*/

use core_course\external\helper_for_get_mods_by_courses;

defined('MOODLE_INTERNAL') || die;

require_once("$CFG->libdir/externallib.php");
Expand Down Expand Up @@ -174,7 +176,6 @@ public static function get_books_by_courses_parameters() {
* @since Moodle 3.0
*/
public static function get_books_by_courses($courseids = array()) {
global $CFG;

$returnedbooks = array();
$warnings = array();
Expand All @@ -196,31 +197,16 @@ public static function get_books_by_courses($courseids = array()) {
// We can avoid then additional validate_context calls.
$books = get_all_instances_in_courses("book", $courses);
foreach ($books as $book) {
$context = context_module::instance($book->coursemodule);
// Entry to return.
$bookdetails = array();
// First, we return information that any user can see in the web interface.
$bookdetails['id'] = $book->id;
$bookdetails['coursemodule'] = $book->coursemodule;
$bookdetails['course'] = $book->course;
$bookdetails['name'] = external_format_string($book->name, $context->id);
// Format intro.
$options = array('noclean' => true);
list($bookdetails['intro'], $bookdetails['introformat']) =
external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null, $options);
$bookdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_book', 'intro', false, false);
$bookdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($book, 'mod_book');
$bookdetails['numbering'] = $book->numbering;
$bookdetails['navstyle'] = $book->navstyle;
$bookdetails['customtitles'] = $book->customtitles;

if (has_capability('moodle/course:manageactivities', $context)) {
if (has_capability('moodle/course:manageactivities', context_module::instance($book->coursemodule))) {
$bookdetails['revision'] = $book->revision;
$bookdetails['timecreated'] = $book->timecreated;
$bookdetails['timemodified'] = $book->timemodified;
$bookdetails['section'] = $book->section;
$bookdetails['visible'] = $book->visible;
$bookdetails['groupmode'] = $book->groupmode;
$bookdetails['groupingid'] = $book->groupingid;
}
$returnedbooks[] = $bookdetails;
}
Expand All @@ -241,31 +227,20 @@ public static function get_books_by_courses_returns() {
return new external_single_structure(
array(
'books' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Book id'),
'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
'course' => new external_value(PARAM_INT, 'Course id'),
'name' => new external_value(PARAM_RAW, 'Book name'),
'intro' => new external_value(PARAM_RAW, 'The Book intro'),
'introformat' => new external_format_value('intro'),
'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL),
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'numbering' => new external_value(PARAM_INT, 'Book numbering configuration'),
'navstyle' => new external_value(PARAM_INT, 'Book navigation style configuration'),
'customtitles' => new external_value(PARAM_INT, 'Book custom titles type'),
'revision' => new external_value(PARAM_INT, 'Book revision', VALUE_OPTIONAL),
'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL),
'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
), 'Books'
)
]
), 'Books')
),
'warnings' => new external_warnings(),
)
);
}

}
39 changes: 8 additions & 31 deletions mod/chat/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/chat/lib.php');

use core_course\external\helper_for_get_mods_by_courses;
use mod_chat\external\chat_message_exporter;

/**
Expand Down Expand Up @@ -541,18 +542,8 @@ public static function get_chats_by_courses($courseids = array()) {
$chats = get_all_instances_in_courses("chat", $courses);
foreach ($chats as $chat) {
$chatcontext = context_module::instance($chat->coursemodule);
// Entry to return.
$chatdetails = array();
// First, we return information that any user can see in the web interface.
$chatdetails['id'] = $chat->id;
$chatdetails['coursemodule'] = $chat->coursemodule;
$chatdetails['course'] = $chat->course;
$chatdetails['name'] = external_format_string($chat->name, $chatcontext->id);
// Format intro.
$options = array('noclean' => true);
list($chatdetails['intro'], $chatdetails['introformat']) =
external_format_text($chat->intro, $chat->introformat, $chatcontext->id, 'mod_chat', 'intro', null, $options);
$chatdetails['introfiles'] = external_util::get_area_files($chatcontext->id, 'mod_chat', 'intro', false, false);

$chatdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($chat, 'mod_chat');

if (has_capability('mod/chat:chat', $chatcontext)) {
$chatdetails['chatmethod'] = $CFG->chat_method;
Expand All @@ -564,10 +555,6 @@ public static function get_chats_by_courses($courseids = array()) {

if (has_capability('moodle/course:manageactivities', $chatcontext)) {
$chatdetails['timemodified'] = $chat->timemodified;
$chatdetails['section'] = $chat->section;
$chatdetails['visible'] = $chat->visible;
$chatdetails['groupmode'] = $chat->groupmode;
$chatdetails['groupingid'] = $chat->groupingid;
}
$returnedchats[] = $chatdetails;
}
Expand All @@ -588,28 +575,18 @@ public static function get_chats_by_courses_returns() {
return new external_single_structure(
array(
'chats' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Chat id'),
'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
'course' => new external_value(PARAM_INT, 'Course id'),
'name' => new external_value(PARAM_RAW, 'Chat name'),
'intro' => new external_value(PARAM_RAW, 'The Chat intro'),
'introformat' => new external_format_value('intro'),
'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL),
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'chatmethod' => new external_value(PARAM_PLUGIN, 'chat method (sockets, ajax, header_js)',
VALUE_OPTIONAL),
'keepdays' => new external_value(PARAM_INT, 'keep days', VALUE_OPTIONAL),
'studentlogs' => new external_value(PARAM_INT, 'student logs visible to everyone', VALUE_OPTIONAL),
'chattime' => new external_value(PARAM_INT, 'chat time', VALUE_OPTIONAL),
'schedule' => new external_value(PARAM_INT, 'schedule type', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'time of last modification', VALUE_OPTIONAL),
'section' => new external_value(PARAM_INT, 'course section id', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_BOOL, 'visible', VALUE_OPTIONAL),
'groupmode' => new external_value(PARAM_INT, 'group mode', VALUE_OPTIONAL),
'groupingid' => new external_value(PARAM_INT, 'group id', VALUE_OPTIONAL),
), 'Chats'
)
]
), 'Chats')
),
'warnings' => new external_warnings(),
)
Expand Down
Loading

0 comments on commit 6c161f3

Please sign in to comment.