Skip to content

Commit

Permalink
Merge branch 'MDL-63806-master' of git://github.com/jleyva/moodle int…
Browse files Browse the repository at this point in the history
…o master
  • Loading branch information
stronk7 committed Oct 13, 2020
2 parents 11b094b + 8441d55 commit 05f18c4
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 92 deletions.
14 changes: 13 additions & 1 deletion mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected static function fill_entry_details($entry, $context) {
* @param int $id The glossary ID.
* @return array Contains glossary, context, course and cm.
*/
protected static function validate_glossary($id) {
public static function validate_glossary($id) {
global $DB;
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
Expand Down Expand Up @@ -1406,10 +1406,16 @@ public static function get_entry_by_id($id) {
$entry = glossary_get_entry_by_id($id);
self::fill_entry_details($entry, $context);

// Permissions (for entry edition).
$permissions = [
'candelete' => mod_glossary_can_delete_entry($entry, $glossary, $context),
];

return array(
'entry' => $entry,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry',
array($entry)),
'permissions' => $permissions,
'warnings' => $warnings
);
}
Expand All @@ -1424,6 +1430,12 @@ public static function get_entry_by_id_returns() {
return new external_single_structure(array(
'entry' => self::get_entry_return_structure(),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'permissions' => new external_single_structure(
[
'candelete' => new external_value(PARAM_BOOL, 'Whether the user can delete the entry.'),
],
'User permissions for the managing the entry.', VALUE_OPTIONAL
),
'warnings' => new external_warnings()
));
}
Expand Down
97 changes: 97 additions & 0 deletions mod/glossary/classes/external/delete_entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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/>.

/**
* This is the external method for deleting a content.
*
* @package mod_glossary
* @since Moodle 3.10
* @copyright 2020 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_glossary\external;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/glossary/lib.php');

use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use external_warnings;

/**
* This is the external method for deleting a content.
*
* @copyright 2020 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_entry extends external_api {
/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'entryid' => new external_value(PARAM_INT, 'Glossary entry id to delete'),
]);
}

/**
* Delete the indicated entry from the glossary.
*
* @param int $entryid The entry to delete
* @return array with result and warnings
* @throws moodle_exception
*/
public static function execute(int $entryid): array {
global $DB;

$params = self::validate_parameters(self::execute_parameters(), compact('entryid'));
$id = $params['entryid'];

// Get and validate the glossary.
$entry = $DB->get_record('glossary_entries', ['id' => $id], '*', MUST_EXIST);
list($glossary, $context, $course, $cm) = \mod_glossary_external::validate_glossary($entry->glossaryid);

// Check and delete.
mod_glossary_can_delete_entry($entry, $glossary, $context, false);
mod_glossary_delete_entry($entry, $glossary, $cm, $context, $course);

return [
'result' => true,
'warnings' => [],
];
}

/**
* Return.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'result' => new external_value(PARAM_BOOL, 'The processing result'),
'warnings' => new external_warnings()
]);
}
}
8 changes: 8 additions & 0 deletions mod/glossary/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_glossary_delete_entry' => [
'classname' => 'mod_glossary\external\delete_entry',
'methodname' => 'execute',
'classpath' => '',
'description' => 'Delete the given entry from the glossary.',
'type' => 'write',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE]
],
);
92 changes: 4 additions & 88 deletions mod/glossary/deleteentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,107 +46,23 @@

require_login($course, false, $cm);
$context = context_module::instance($cm->id);
$manageentries = has_capability('mod/glossary:manageentries', $context);

if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
print_error('invalidid', 'glossary');
}


$strareyousuredelete = get_string("areyousuredelete","glossary");

if (($entry->userid != $USER->id) and !$manageentries) { // guest id is never matched, no need for special check here
print_error('nopermissiontodelentry');
}
$ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
if (!$ineditperiod and !$manageentries) {
print_error('errdeltimeexpired', 'glossary');
}
// Throws an exception if the user cannot delete the entry.
mod_glossary_can_delete_entry($entry, $glossary, $context, false);

/// If data submitted, then process and store.

if ($confirm and confirm_sesskey()) { // the operation was confirmed.
// if it is an imported entry, just delete the relation

$origentry = fullclone($entry);
if ($entry->sourceglossaryid) {
if (!$newcm = get_coursemodule_from_instance('glossary', $entry->sourceglossaryid)) {
print_error('invalidcoursemodule');
}
$newcontext = context_module::instance($newcm->id);

$entry->glossaryid = $entry->sourceglossaryid;
$entry->sourceglossaryid = 0;
$DB->update_record('glossary_entries', $entry);

// move attachments too
$fs = get_file_storage();

if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) {
foreach ($oldfiles as $oldfile) {
$file_record = new stdClass();
$file_record->contextid = $newcontext->id;
$fs->create_file_from_storedfile($file_record, $oldfile);
}
$fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
$entry->attachment = '1';
} else {
$entry->attachment = '0';
}
$DB->update_record('glossary_entries', $entry);

} else {
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
$DB->delete_records("comments", array('itemid'=>$entry->id, 'commentarea'=>'glossary_entry', 'contextid'=>$context->id));
$DB->delete_records("glossary_alias", array("entryid"=>$entry->id));
$DB->delete_records("glossary_entries", array("id"=>$entry->id));

// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $entry->userid);
}

//delete glossary entry ratings
require_once($CFG->dirroot.'/rating/lib.php');
$delopt = new stdClass;
$delopt->contextid = $context->id;
$delopt->component = 'mod_glossary';
$delopt->ratingarea = 'entry';
$delopt->itemid = $entry->id;
$rm = new rating_manager();
$rm->delete_ratings($delopt);
}

// Delete cached RSS feeds.
if (!empty($CFG->enablerssfeeds)) {
require_once($CFG->dirroot.'/mod/glossary/rsslib.php');
glossary_rss_delete_file($glossary);
}

core_tag_tag::remove_all_item_tags('mod_glossary', 'glossary_entries', $origentry->id);

$event = \mod_glossary\event\entry_deleted::create(array(
'context' => $context,
'objectid' => $origentry->id,
'other' => array(
'mode' => $prevmode,
'hook' => $hook,
'concept' => $origentry->concept
)
));
$event->add_record_snapshot('glossary_entries', $origentry);
$event->trigger();

// Reset caches.
if ($entry->usedynalink and $entry->approved) {
\mod_glossary\local\concept_cache::reset_glossary($glossary);
}

mod_glossary_delete_entry($entry, $glossary, $cm, $context, $course, $hook, $prevmode);
redirect("view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=$hook");

} else { // the operation has not been confirmed yet so ask the user to do so
$strareyousuredelete = get_string("areyousuredelete", "glossary");
$PAGE->navbar->add(get_string('delete'));
$PAGE->set_title($glossary->name);
$PAGE->set_heading($course->fullname);
Expand Down
Loading

0 comments on commit 05f18c4

Please sign in to comment.