Skip to content

Commit

Permalink
Merge branch 'MDL-69529-master' of git://github.com/ilyatregubov/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Jan 12, 2021
2 parents 495f45e + 1ec93d4 commit 606a534
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 75 deletions.
27 changes: 9 additions & 18 deletions contentbank/classes/contentbank.php
Expand Up @@ -215,8 +215,7 @@ public function search_contents(?string $search = null, ?int $contextid = 0, ?ar

$records = $DB->get_records_select('contentbank_content', $sql, $params, 'name ASC');
foreach ($records as $record) {
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
$content = $this->get_content_from_id($record->id);
if ($content->is_view_allowed()) {
$contents[] = $content;
}
Expand Down Expand Up @@ -267,14 +266,10 @@ public function delete_contents(context $context): bool {
$result = true;
$records = $DB->get_records('contentbank_content', ['contextid' => $context->id]);
foreach ($records as $record) {
$contenttypeclass = "\\$record->contenttype\\contenttype";
if (class_exists($contenttypeclass)) {
$contenttype = new $contenttypeclass($context);
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
if (!$contenttype->delete_content($content)) {
$result = false;
}
$content = $this->get_content_from_id($record->id);
$contenttype = $content->get_content_type_instance();
if (!$contenttype->delete_content($content)) {
$result = false;
}
}
return $result;
Expand All @@ -293,14 +288,10 @@ public function move_contents(context $from, context $to): bool {
$result = true;
$records = $DB->get_records('contentbank_content', ['contextid' => $from->id]);
foreach ($records as $record) {
$contenttypeclass = "\\$record->contenttype\\contenttype";
if (class_exists($contenttypeclass)) {
$contenttype = new $contenttypeclass($from);
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
if (!$contenttype->move_content($content, $to)) {
$result = false;
}
$content = $this->get_content_from_id($record->id);
$contenttype = $content->get_content_type_instance();
if (!$contenttype->move_content($content, $to)) {
$result = false;
}
}
return $result;
Expand Down
40 changes: 19 additions & 21 deletions contentbank/classes/external/delete_content.php
Expand Up @@ -30,6 +30,7 @@
global $CFG;
require_once($CFG->libdir . '/externallib.php');

use core_contentbank\contentbank;
use external_api;
use external_function_parameters;
use external_multiple_structure;
Expand Down Expand Up @@ -70,34 +71,31 @@ public static function execute(array $contentids): array {
$warnings = [];

$params = self::validate_parameters(self::execute_parameters(), ['contentids' => $contentids]);
$cb = new contentbank();
foreach ($params['contentids'] as $contentid) {
try {
$record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST);
$contenttypeclass = "\\$record->contenttype\\contenttype";
if (class_exists($contenttypeclass)) {
$context = \context::instance_by_id($record->contextid, MUST_EXIST);
self::validate_context($context);
$contenttype = new $contenttypeclass($context);
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
// Check capability.
if ($contenttype->can_delete($content)) {
// This content can be deleted.
if (!$contenttype->delete_content($content)) {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'contentnotdeleted',
'message' => get_string('contentnotdeleted', 'core_contentbank')
];
}
} else {
// The user has no permission to delete this content.
$content = $cb->get_content_from_id($record->id);
$contenttype = $content->get_content_type_instance();
$context = \context::instance_by_id($record->contextid, MUST_EXIST);
self::validate_context($context);
// Check capability.
if ($contenttype->can_delete($content)) {
// This content can be deleted.
if (!$contenttype->delete_content($content)) {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'nopermissiontodelete',
'message' => get_string('nopermissiontodelete', 'core_contentbank')
'warningcode' => 'contentnotdeleted',
'message' => get_string('contentnotdeleted', 'core_contentbank')
];
}
} else {
// The user has no permission to delete this content.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'nopermissiontodelete',
'message' => get_string('nopermissiontodelete', 'core_contentbank')
];
}
} catch (\moodle_exception $e) {
// The content or the context don't exist.
Expand Down
57 changes: 28 additions & 29 deletions contentbank/classes/external/rename_content.php
Expand Up @@ -29,6 +29,7 @@
global $CFG;
require_once($CFG->libdir . '/externallib.php');

use core_contentbank\contentbank;
use external_api;
use external_function_parameters;
use external_single_structure;
Expand Down Expand Up @@ -76,35 +77,33 @@ public static function execute(int $contentid, string $name): array {
'name' => $name,
]);
$params['name'] = clean_param($params['name'], PARAM_TEXT);
try {
$record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST);
$contenttypeclass = "\\$record->contenttype\\contenttype";
if (class_exists($contenttypeclass)) {

// If name is empty don't try to rename and return a more detailed message.
if (empty(trim($params['name']))) {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'emptynamenotallowed',
'message' => get_string('emptynamenotallowed', 'core_contentbank')
];
} else {
try {
$record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST);
$cb = new contentbank();
$content = $cb->get_content_from_id($record->id);
$contenttype = $content->get_content_type_instance();
$context = \context::instance_by_id($record->contextid, MUST_EXIST);
self::validate_context($context);
$contenttype = new $contenttypeclass($context);
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
// Check capability.
if ($contenttype->can_manage($content)) {
if (empty(trim($name))) {
// If name is empty don't try to rename and return a more detailed message.
// This content can be renamed.
if ($contenttype->rename_content($content, $params['name'])) {
$result = true;
} else {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'emptynamenotallowed',
'message' => get_string('emptynamenotallowed', 'core_contentbank')
'warningcode' => 'contentnotrenamed',
'message' => get_string('contentnotrenamed', 'core_contentbank')
];
} else {
// This content can be renamed.
if ($contenttype->rename_content($content, $params['name'])) {
$result = true;
} else {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'contentnotrenamed',
'message' => get_string('contentnotrenamed', 'core_contentbank')
];
}
}
} else {
// The user has no permission to manage this content.
Expand All @@ -114,14 +113,14 @@ public static function execute(int $contentid, string $name): array {
'message' => get_string('nopermissiontomanage', 'core_contentbank')
];
}
} catch (\moodle_exception $e) {
// The content or the context don't exist.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'exception',
'message' => $e->getMessage()
];
}
} catch (\moodle_exception $e) {
// The content or the context don't exist.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'exception',
'message' => $e->getMessage()
];
}

return [
Expand Down
10 changes: 3 additions & 7 deletions contentbank/view.php
Expand Up @@ -58,13 +58,9 @@
$PAGE->set_title($title);
$PAGE->set_pagetype('contentbank');

$contenttypeclass = "\\$record->contenttype\\contenttype";
$contentclass = "\\$record->contenttype\\content";
if (!class_exists($contenttypeclass) || !class_exists($contentclass)) {
print_error('contenttypenotfound', 'error', $returnurl, $record->contenttype);
}
$contenttype = new $contenttypeclass($context);
$content = new $contentclass($record);
$cb = new \core_contentbank\contentbank();
$content = $cb->get_content_from_id($record->id);
$contenttype = $content->get_content_type_instance();

// Create the cog menu with all the secondary actions, such as delete, rename...
$actionmenu = new action_menu();
Expand Down

0 comments on commit 606a534

Please sign in to comment.