first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+309
View File
@@ -0,0 +1,309 @@
<?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/>.
/**
* Rating external API
*
* @package core_rating
* @category external
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->dirroot/rating/lib.php");
use core_external\external_api;
use core_external\external_value;
use core_external\external_single_structure;
use core_external\external_multiple_structure;
use core_external\external_function_parameters;
use core_external\external_warnings;
/**
* Rating external functions
*
* @package core_rating
* @category external
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class core_rating_external extends external_api {
/**
* Returns description of get_item_ratings parameters.
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function get_item_ratings_parameters() {
return new external_function_parameters (
array(
'contextlevel' => new external_value(PARAM_ALPHA, 'context level: course, module, user, etc...'),
'instanceid' => new external_value(PARAM_INT, 'the instance id of item associated with the context level'),
'component' => new external_value(PARAM_COMPONENT, 'component'),
'ratingarea' => new external_value(PARAM_AREA, 'rating area'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'scaleid' => new external_value(PARAM_INT, 'scale id'),
'sort' => new external_value(PARAM_ALPHA, 'sort order (firstname, rating or timemodified)')
)
);
}
/**
* Retrieve a list of ratings for a given item (forum post etc)
*
* @param string $contextlevel course, module, user...
* @param int $instanceid the instance if for the context element
* @param string $component the name of the component
* @param string $ratingarea rating area
* @param int $itemid the item id
* @param int $scaleid the scale id
* @param string $sort sql order (firstname, rating or timemodified)
* @return array Result and possible warnings
* @throws moodle_exception
* @since Moodle 2.9
*/
public static function get_item_ratings($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $sort) {
global $USER, $PAGE;
$warnings = array();
$arrayparams = array(
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid,
'sort' => $sort
);
// Validate and normalize parameters.
$params = self::validate_parameters(self::get_item_ratings_parameters(), $arrayparams);
$context = self::get_context_from_params($params);
self::validate_context($context);
// Minimal capability required.
$callbackparams = array('contextid' => $context->id,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid);
if (!has_capability('moodle/rating:view', $context) ||
!component_callback($component, 'rating_can_see_item_ratings', array($callbackparams), true)) {
throw new moodle_exception('noviewrate', 'rating');
}
list($context, $course, $cm) = get_context_info_array($context->id);
// Can we see all ratings?
$canviewallratings = has_capability('moodle/rating:viewall', $context);
// Create the Sql sort order string.
switch ($params['sort']) {
case 'firstname':
$sqlsort = "u.firstname ASC";
break;
case 'rating':
$sqlsort = "r.rating ASC";
break;
default:
$sqlsort = "r.timemodified ASC";
}
$ratingoptions = new stdClass;
$ratingoptions->context = $context;
$ratingoptions->component = $params['component'];
$ratingoptions->ratingarea = $params['ratingarea'];
$ratingoptions->itemid = $params['itemid'];
$ratingoptions->sort = $sqlsort;
$rm = new rating_manager();
$ratings = $rm->get_all_ratings_for_item($ratingoptions);
$scalemenu = make_grades_menu($params['scaleid']);
// If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
// We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
$maxrating = max(array_keys($scalemenu));
$results = array();
foreach ($ratings as $rating) {
if ($canviewallratings || $USER->id == $rating->userid) {
if ($rating->rating > $maxrating) {
$rating->rating = $maxrating;
}
$result = array();
$result['id'] = $rating->id;
$result['userid'] = $rating->userid;
$result['userfullname'] = fullname($rating);
$result['rating'] = $scalemenu[$rating->rating];
$result['timemodified'] = $rating->timemodified;
// The rating object has all the required fields for generating the picture url.
// Undo the aliasing of the user id column from fields::get_sql.
$rating->id = $rating->userid;
$userpicture = new user_picture($rating);
$userpicture->size = 1; // Size f1.
$result['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
$results[] = $result;
}
}
return array(
'ratings' => $results,
'warnings' => $warnings
);
}
/**
* Returns description of get_item_ratings result values.
*
* @return external_single_structure
* @since Moodle 2.9
*/
public static function get_item_ratings_returns() {
return new external_single_structure(
array(
'ratings' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'rating id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'userpictureurl' => new external_value(PARAM_URL, 'URL user picture'),
'userfullname' => new external_value(PARAM_NOTAGS, 'user fullname'),
'rating' => new external_value(PARAM_NOTAGS, 'rating on scale'),
'timemodified' => new external_value(PARAM_INT, 'time modified (timestamp)')
), 'Rating'
), 'list of ratings'
),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of add_rating parameters.
*
* @return external_function_parameters
* @since Moodle 3.2
*/
public static function add_rating_parameters() {
return new external_function_parameters (
array(
'contextlevel' => new external_value(PARAM_ALPHA, 'context level: course, module, user, etc...'),
'instanceid' => new external_value(PARAM_INT, 'the instance id of item associated with the context level'),
'component' => new external_value(PARAM_COMPONENT, 'component'),
'ratingarea' => new external_value(PARAM_AREA, 'rating area'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'scaleid' => new external_value(PARAM_INT, 'scale id'),
'rating' => new external_value(PARAM_INT, 'user rating'),
'rateduserid' => new external_value(PARAM_INT, 'rated user id'),
'aggregation' => new external_value(PARAM_INT, 'agreggation method', VALUE_DEFAULT, RATING_AGGREGATE_NONE)
)
);
}
/**
* Adds a rating to an item
*
* @param string $contextlevel course, module, user...
* @param int $instanceid the instance if for the context element
* @param string $component the name of the component
* @param string $ratingarea rating area
* @param int $itemid the item id
* @param int $scaleid the scale id
* @param int $rating the user rating
* @param int $rateduserid the rated user id
* @param int $aggregation the aggregation method
* @return array result and possible warnings
* @throws moodle_exception
* @since Moodle 3.2
*/
public static function add_rating($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $rating, $rateduserid,
$aggregation = RATING_AGGREGATE_NONE) {
$warnings = array();
$params = array(
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid,
'rating' => $rating,
'rateduserid' => $rateduserid,
'aggregation' => $aggregation,
);
// Validate and normalize parameters.
$params = self::validate_parameters(self::add_rating_parameters(), $params);
$context = self::get_context_from_params($params);
self::validate_context($context);
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
require_capability('moodle/rating:rate', $context);
$rm = new rating_manager();
$result = $rm->add_rating($cm, $context, $params['component'], $params['ratingarea'], $params['itemid'], $params['scaleid'],
$params['rating'], $params['rateduserid'], $params['aggregation']);
if (!empty($result->error)) {
throw new moodle_exception($result->error, 'rating');
}
$returndata = array(
'success' => $result->success,
'warnings' => $warnings
);
if (isset($result->aggregate)) {
$returndata['aggregate'] = $result->aggregate;
$returndata['count'] = $result->count;
$returndata['itemid'] = $result->itemid;
}
return $returndata;
}
/**
* Returns description of add_rating result values.
*
* @return external_single_structure
* @since Moodle 3.2
*/
public static function add_rating_returns() {
return new external_single_structure(
array(
'success' => new external_value(PARAM_BOOL, 'Whether the rate was successfully created'),
'aggregate' => new external_value(PARAM_TEXT, 'New aggregate', VALUE_OPTIONAL),
'count' => new external_value(PARAM_INT, 'Ratings count', VALUE_OPTIONAL),
'itemid' => new external_value(PARAM_INT, 'Rating item id', VALUE_OPTIONAL),
'warnings' => new external_warnings(),
)
);
}
}
+188
View File
@@ -0,0 +1,188 @@
<?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/>.
/**
* Rating external functions utility class.
*
* @package core_rating
* @copyright 2017 Juan Leyva
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_rating\external;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/rating/lib.php');
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use rating_manager;
use stdClass;
/**
* Rating external functions utility class.
*
* @package core_rating
* @copyright 2017 Juan Leyva
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.4
*/
class util {
/**
* Returns the ratings definition for external functions.
*/
public static function external_ratings_structure() {
return new external_single_structure (
[
'contextid' => new external_value(PARAM_INT, 'Context id.'),
'component' => new external_value(PARAM_COMPONENT, 'Context name.'),
'ratingarea' => new external_value(PARAM_AREA, 'Rating area name.'),
'canviewall' => new external_value(PARAM_BOOL, 'Whether the user can view all the individual ratings.',
VALUE_OPTIONAL),
'canviewany' => new external_value(PARAM_BOOL, 'Whether the user can view aggregate of ratings of others.',
VALUE_OPTIONAL),
'scales' => new external_multiple_structure(
new external_single_structure (
[
'id' => new external_value(PARAM_INT, 'Scale id.'),
'courseid' => new external_value(PARAM_INT, 'Course id.', VALUE_OPTIONAL),
'name' => new external_value(PARAM_TEXT, 'Scale name (when a real scale is used).', VALUE_OPTIONAL),
'max' => new external_value(PARAM_INT, 'Max value for the scale.'),
'isnumeric' => new external_value(PARAM_BOOL, 'Whether is a numeric scale.'),
'items' => new external_multiple_structure(
new external_single_structure (
[
'value' => new external_value(PARAM_INT, 'Scale value/option id.'),
'name' => new external_value(PARAM_NOTAGS, 'Scale name.'),
]
), 'Scale items. Only returned for not numerical scales.', VALUE_OPTIONAL
)
], 'Scale information'
), 'Different scales used information', VALUE_OPTIONAL
),
'ratings' => new external_multiple_structure(
new external_single_structure (
[
'itemid' => new external_value(PARAM_INT, 'Item id.'),
'scaleid' => new external_value(PARAM_INT, 'Scale id.', VALUE_OPTIONAL),
'userid' => new external_value(PARAM_INT, 'User who rated id.', VALUE_OPTIONAL),
'aggregate' => new external_value(PARAM_FLOAT, 'Aggregated ratings grade.', VALUE_OPTIONAL),
'aggregatestr' => new external_value(PARAM_NOTAGS, 'Aggregated ratings as string.', VALUE_OPTIONAL),
'aggregatelabel' => new external_value(PARAM_NOTAGS, 'The aggregation label.', VALUE_OPTIONAL),
'count' => new external_value(PARAM_INT, 'Ratings count (used when aggregating).', VALUE_OPTIONAL),
'rating' => new external_value(PARAM_INT, 'The rating the user gave.', VALUE_OPTIONAL),
'canrate' => new external_value(PARAM_BOOL, 'Whether the user can rate the item.', VALUE_OPTIONAL),
'canviewaggregate' => new external_value(PARAM_BOOL, 'Whether the user can view the aggregated grade.',
VALUE_OPTIONAL),
]
), 'The ratings', VALUE_OPTIONAL
),
], 'Rating information', VALUE_OPTIONAL
);
}
/**
* Returns rating information inside a data structure like the one defined by external_ratings_structure.
*
* @param stdClass $mod course module object
* @param stdClass $context context object
* @param str $component component name
* @param str $ratingarea rating area
* @param array $items items to add ratings
* @return array ratings ready to be returned by external functions.
*/
public static function get_rating_info($mod, $context, $component, $ratingarea, $items) {
global $USER;
$ratinginfo = [
'contextid' => $context->id,
'component' => $component,
'ratingarea' => $ratingarea,
'canviewall' => null,
'canviewany' => null,
'scales' => [],
'ratings' => [],
];
if ($mod->assessed != RATING_AGGREGATE_NONE) {
$ratingoptions = new stdClass;
$ratingoptions->context = $context;
$ratingoptions->component = $component;
$ratingoptions->ratingarea = $ratingarea;
$ratingoptions->items = $items;
$ratingoptions->aggregate = $mod->assessed;
$ratingoptions->scaleid = $mod->scale;
$ratingoptions->userid = $USER->id;
$ratingoptions->assesstimestart = $mod->assesstimestart;
$ratingoptions->assesstimefinish = $mod->assesstimefinish;
$rm = new rating_manager();
$allitems = $rm->get_ratings($ratingoptions);
foreach ($allitems as $item) {
if (empty($item->rating)) {
continue;
}
$rating = [
'itemid' => $item->rating->itemid,
'scaleid' => $item->rating->scaleid,
'userid' => $item->rating->userid,
'rating' => $item->rating->rating,
'canrate' => $item->rating->user_can_rate(),
'canviewaggregate' => $item->rating->user_can_view_aggregate(),
];
// Fill the capabilities fields the first time (the rest are the same values because they are not item dependent).
if ($ratinginfo['canviewall'] === null) {
$ratinginfo['canviewall'] = $item->rating->settings->permissions->viewall &&
$item->rating->settings->pluginpermissions->viewall;
$ratinginfo['canviewany'] = $item->rating->settings->permissions->viewany &&
$item->rating->settings->pluginpermissions->viewany;
}
// Return only the information the user can see.
if ($rating['canviewaggregate']) {
$rating['aggregate'] = $item->rating->aggregate;
$rating['aggregatestr'] = $item->rating->get_aggregate_string();
$rating['aggregatelabel'] = $rm->get_aggregate_label($item->rating->settings->aggregationmethod);
$rating['count'] = $item->rating->count;
}
// If the user can rate, return the scale information only one time.
if ($rating['canrate'] &&
!empty($item->rating->settings->scale->id) &&
!isset($ratinginfo['scales'][$item->rating->settings->scale->id])) {
$scale = $item->rating->settings->scale;
// Return only non numeric scales (to avoid return lots of data just including items from 0 to $scale->max).
if (!$scale->isnumeric) {
$scaleitems = [];
foreach ($scale->scaleitems as $value => $name) {
$scaleitems[] = [
'name' => $name,
'value' => $value,
];
}
$scale->items = $scaleitems;
}
$ratinginfo['scales'][$item->rating->settings->scale->id] = (array) $scale;
}
$ratinginfo['ratings'][] = $rating;
}
}
return $ratinginfo;
}
}
+135
View File
@@ -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/>.
/**
* Helpers for the core_rating subsystem implementation of privacy.
*
* @package core_rating
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_rating\phpunit;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\tests\request\content_writer;
global $CFG;
/**
* Helpers for the core_rating subsystem implementation of privacy.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait privacy_helper {
/**
* Fetch all ratings on a subcontext.
*
* @param \context $context The context being stored.
* @param array $subcontext The subcontext path to check.
* @return \stdClass|array
*/
protected function get_ratings_on_subcontext(\context $context, array $subcontext) {
$writer = \core_privacy\local\request\writer::with_context($context);
return $writer->get_related_data($subcontext, 'rating');
}
/**
* Check that all included ratings belong to the specified user.
*
* @param int $userid The ID of the user being stored.
* @param \context $context The context being stored.
* @param array $subcontext The subcontext path to check.
* @param string $component The component being stored.
* @param string $ratingarea The rating area to store results for.
* @param int $itemid The itemid to store.
*/
protected function assert_all_own_ratings_on_context(
int $userid,
\context $context,
array $subcontext,
$component,
$ratingarea,
$itemid
) {
$writer = \core_privacy\local\request\writer::with_context($context);
$rm = new \rating_manager();
$dbratings = $rm->get_all_ratings_for_item((object) [
'context' => $context,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
]);
$exportedratings = $this->get_ratings_on_subcontext($context, $subcontext);
foreach ($exportedratings as $ratingid => $rating) {
$this->assertTrue(isset($dbratings[$ratingid]));
$this->assertEquals($userid, $rating->author);
$this->assert_rating_matches($dbratings[$ratingid], $rating);
}
foreach ($dbratings as $rating) {
if ($rating->userid == $userid) {
$this->assertEquals($rating->id, $ratingid);
}
}
}
/**
* Check that all included ratings are valid. They may belong to any user.
*
* @param \context $context The context being stored.
* @param array $subcontext The subcontext path to check.
* @param string $component The component being stored.
* @param string $ratingarea The rating area to store results for.
* @param int $itemid The itemid to store.
*/
protected function assert_all_ratings_on_context(\context $context, array $subcontext, $component, $ratingarea, $itemid) {
$writer = \core_privacy\local\request\writer::with_context($context);
$rm = new \rating_manager();
$dbratings = $rm->get_all_ratings_for_item((object) [
'context' => $context,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
]);
$exportedratings = $this->get_ratings_on_subcontext($context, $subcontext);
foreach ($exportedratings as $ratingid => $rating) {
$this->assertTrue(isset($dbratings[$ratingid]));
$this->assert_rating_matches($dbratings[$ratingid], $rating);
}
foreach ($dbratings as $rating) {
$this->assertTrue(isset($exportedratings[$rating->id]));
}
}
/**
* Assert that the rating matches.
*
* @param \stdClass $expected The expected rating structure
* @param \stdClass $stored The actual rating structure
*/
protected function assert_rating_matches($expected, $stored) {
$this->assertEquals($expected->rating, $stored->rating);
$this->assertEquals($expected->userid, $stored->author);
}
}
+260
View File
@@ -0,0 +1,260 @@
<?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/>.
/**
* Privacy Subsystem implementation for core_ratings.
*
* @package core_rating
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_rating\privacy;
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\userlist;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/rating/lib.php');
/**
* Privacy Subsystem implementation for core_ratings.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// The ratings subsystem contains data.
\core_privacy\local\metadata\provider,
// The ratings subsystem is only ever used to store data for other components.
// It does not store any data of its own and does not need to implement the \core_privacy\local\request\subsystem\provider
// as a result.
// The ratings subsystem provides a data service to other components.
\core_privacy\local\request\subsystem\plugin_provider,
\core_privacy\local\request\shared_userlist_provider
{
/**
* Returns metadata about the ratings subsystem.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through the subsystem.
*/
public static function get_metadata(collection $collection): collection {
// The table 'rating' cotains data that a user has entered.
// It stores the user-entered rating alongside a mapping to describe what was mapped.
$collection->add_database_table('rating', [
'rating' => 'privacy:metadata:rating:rating',
'userid' => 'privacy:metadata:rating:userid',
'timecreated' => 'privacy:metadata:rating:timecreated',
'timemodified' => 'privacy:metadata:rating:timemodified',
], 'privacy:metadata:rating');
return $collection;
}
/**
* Export all ratings which match the specified component, areaid, and itemid.
*
* If requesting ratings for a users own content, and you wish to include all ratings of that content, specify
* $onlyuser as false.
*
* When requesting ratings for another users content, you should only export the ratings that the specified user
* made themselves.
*
* @param int $userid The user whose information is to be exported
* @param \context $context The context being stored.
* @param array $subcontext The subcontext within the context to export this information
* @param string $component The component to fetch data from
* @param string $ratingarea The ratingarea that the data was stored in within the component
* @param int $itemid The itemid within that ratingarea
* @param bool $onlyuser Whether to only export ratings that the current user has made, or all ratings
*/
public static function export_area_ratings(
int $userid,
\context $context,
array $subcontext,
string $component,
string $ratingarea,
int $itemid,
bool $onlyuser = true
) {
global $DB;
$rm = new \rating_manager();
$ratings = $rm->get_all_ratings_for_item((object) [
'context' => $context,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
]);
if ($onlyuser) {
$ratings = array_filter($ratings, function($rating) use ($userid){
return ($rating->userid == $userid);
});
}
if (empty($ratings)) {
return;
}
$toexport = array_map(function($rating) {
return (object) [
'rating' => $rating->rating,
'author' => $rating->userid,
];
}, $ratings);
$writer = \core_privacy\local\request\writer::with_context($context)
->export_related_data($subcontext, 'rating', $toexport);
}
/**
* Get the SQL required to find all submission items where this user has had any involvements.
*
* If possible an inner join should be used.
*
* @param string $alias The name of the table alias to use.
* @param string $component The na eof the component to fetch ratings for.
* @param string $ratingarea The rating area to fetch results for.
* @param string $itemidjoin The right-hand-side of the JOIN ON clause.
* @param int $userid The ID of the user being stored.
* @param bool $innerjoin Whether to use an inner join (preferred)
* @return \stdClass
*/
public static function get_sql_join($alias, $component, $ratingarea, $itemidjoin, $userid, $innerjoin = false) {
static $count = 0;
$count++;
$userwhere = '';
if ($innerjoin) {
// Join the rating table with the specified alias and the relevant join params.
$join = "JOIN {rating} {$alias} ON ";
$join .= "{$alias}.itemid = {$itemidjoin}";
$userwhere .= "{$alias}.userid = :ratinguserid{$count} AND ";
$userwhere .= "{$alias}.component = :ratingcomponent{$count} AND ";
$userwhere .= "{$alias}.ratingarea = :ratingarea{$count}";
} else {
// Join the rating table with the specified alias and the relevant join params.
$join = "LEFT JOIN {rating} {$alias} ON ";
$join .= "{$alias}.userid = :ratinguserid{$count} AND ";
$join .= "{$alias}.component = :ratingcomponent{$count} AND ";
$join .= "{$alias}.ratingarea = :ratingarea{$count} AND ";
$join .= "{$alias}.itemid = {$itemidjoin}";
// Match against the specified user.
$userwhere = "{$alias}.id IS NOT NULL";
}
$params = [
'ratingcomponent' . $count => $component,
'ratingarea' . $count => $ratingarea,
'ratinguserid' . $count => $userid,
];
$return = (object) [
'join' => $join,
'params' => $params,
'userwhere' => $userwhere,
];
return $return;
}
/**
* Deletes all ratings for a specified context, component, ratingarea and itemid.
*
* Only delete ratings when the item itself was deleted.
*
* We never delete ratings for one user but not others - this may affect grades, therefore ratings
* made by particular user are not considered personal information.
*
* @param \context $context Details about which context to delete ratings for.
* @param string $component Component to delete.
* @param string $ratingarea Rating area to delete.
* @param int $itemid The item ID for use with deletion.
*/
public static function delete_ratings(\context $context, string $component = null,
string $ratingarea = null, int $itemid = null) {
global $DB;
$options = ['contextid' => $context->id];
if ($component) {
$options['component'] = $component;
}
if ($ratingarea) {
$options['ratingarea'] = $ratingarea;
}
if ($itemid) {
$options['itemid'] = $itemid;
}
$DB->delete_records('rating', $options);
}
/**
* Deletes all tag instances for given context, component, itemtype using subquery for itemids
*
* In most situations you will want to specify $userid as null. Per-user tag instances
* are possible in Tags API, however there are no components or standard plugins that actually use them.
*
* @param \context $context Details about which context to delete ratings for.
* @param string $component Component to delete.
* @param string $ratingarea Rating area to delete.
* @param string $itemidstest an SQL fragment that the itemid must match. Used
* in the query like WHERE itemid $itemidstest. Must use named parameters,
* and may not use named parameters called contextid, component or ratingarea.
* @param array $params any query params used by $itemidstest.
*/
public static function delete_ratings_select(\context $context, string $component,
string $ratingarea, $itemidstest, $params = []) {
global $DB;
$params += ['contextid' => $context->id, 'component' => $component, 'ratingarea' => $ratingarea];
$DB->delete_records_select('rating',
'contextid = :contextid AND component = :component AND ratingarea = :ratingarea AND itemid ' . $itemidstest,
$params);
}
/**
* Add the list of users who have rated in the specified constraints.
*
* @param userlist $userlist The userlist to add the users to.
* @param string $alias An alias prefix to use for rating selects to avoid interference with your own sql.
* @param string $component The component to check.
* @param string $area The rating area to check.
* @param string $insql The SQL to use in a sub-select for the itemid query.
* @param array $params The params required for the insql.
*/
public static function get_users_in_context_from_sql(
userlist $userlist, string $alias, string $component, string $area, string $insql, $params) {
// Discussion authors.
$sql = "SELECT {$alias}.userid
FROM {rating} {$alias}
WHERE {$alias}.component = :{$alias}component
AND {$alias}.ratingarea = :{$alias}ratingarea
AND {$alias}.itemid IN ({$insql})";
$params["{$alias}component"] = $component;
$params["{$alias}ratingarea"] = $area;
$userlist->add_from_sql('userid', $sql, $params);
}
}