Skip to content

Commit

Permalink
Merge pull request #251 from learnweb/fix/teachers_can_delete_groups
Browse files Browse the repository at this point in the history
Fix/teachers can delete groups
  • Loading branch information
Laur0r authored May 16, 2023
2 parents 2725c82 + 5cdf2a0 commit 9b2fc18
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 65 deletions.
39 changes: 32 additions & 7 deletions backup/moodle2/backup_ratingallocate_activity_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class backup_ratingallocate_activity_structure_step extends backup_activity_stru

protected function define_structure() {

// To know if we are including userinfo
// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');

// Define each element separated
// Define each element separated.
$class = 'ratingallocate\db\ratingallocate';
$ratingallocate = new backup_nested_element(get_tablename_for_tableClass($class), get_id_for_tableClass($class),
get_fields_for_tableClass($class));
Expand All @@ -60,10 +60,25 @@ protected function define_structure() {
get_id_for_tableClass($groupchoiceclass),
get_fields_for_tableClass($groupchoiceclass));

$choicegroupclass = 'ratingallocate\db\ratingallocate_ch_gengroups';
$ratingallocatechoicegroups = new backup_nested_element(get_tablename_for_tableClass($choicegroupclass) . 's');
$ratingallocatechoicegroup = new backup_nested_element(get_tablename_for_tableClass($choicegroupclass),
get_id_for_tableClass($choicegroupclass),
get_fields_for_tableClass($choicegroupclass));

$groupingclass = 'ratingallocate\db\ratingallocate_groupings';
$ratingallocategroupings = new backup_nested_element(get_tablename_for_tableClass($groupingclass) . 's');
$ratingallocategrouping = new backup_nested_element(get_tablename_for_tableClass($groupingclass),
get_id_for_tableClass($groupingclass),
get_fields_for_tableClass($groupingclass));

// Build the tree.
$ratingallocate->add_child($ratingallocatechoices);
$ratingallocatechoices->add_child($ratingallocatechoice);

$ratingallocate->add_child($ratingallocategroupings);
$ratingallocategroupings->add_child($ratingallocategrouping);

$ratingallocatechoice->add_child($ratingallocateratings);
$ratingallocateratings->add_child($ratingallocaterating);

Expand All @@ -73,16 +88,24 @@ protected function define_structure() {
$ratingallocatechoice->add_child($groupchoices);
$groupchoices->add_child($groupchoice);

// Define sources
$ratingallocatechoice->add_child($ratingallocatechoicegroups);
$ratingallocatechoicegroups->add_child($ratingallocatechoicegroup);

// Define sources.
$ratingallocate->set_source_table(get_tablename_for_tableClass('ratingallocate\db\ratingallocate'),
array(this_db\ratingallocate::ID => backup::VAR_ACTIVITYID), this_db\ratingallocate_choices::ID . ' ASC');
$ratingallocatechoice->set_source_table(get_tablename_for_tableClass('ratingallocate\db\ratingallocate_choices'),
array(this_db\ratingallocate_choices::RATINGALLOCATEID => backup::VAR_PARENTID),
this_db\ratingallocate_choices::ID . ' ASC');
$groupchoice->set_source_table(get_tablename_for_tableClass($groupchoiceclass), ['choiceid' => backup::VAR_PARENTID]);
$ratingallocatechoicegroup->set_source_table(
get_tablename_for_tableClass($choicegroupclass), ['choiceid' => backup::VAR_PARENTID]);
$ratingallocategrouping->set_source_table(
get_tablename_for_tableClass($groupingclass), ['ratingallocateid' => backup::VAR_PARENTID]);

if ($userinfo) {
$ratingallocaterating->set_source_table(get_tablename_for_tableClass('ratingallocate\db\ratingallocate_ratings'),
$ratingallocaterating->set_source_table(get_tablename_for_tableClass(
'ratingallocate\db\ratingallocate_ratings'),
array(this_db\ratingallocate_ratings::CHOICEID => backup::VAR_PARENTID),
this_db\ratingallocate_ratings::ID . ' ASC');
$ratingallocateallocation->set_source_table(get_tablename_for_tableClass('ratingallocate\db\ratingallocate_allocations'),
Expand All @@ -93,15 +116,17 @@ protected function define_structure() {
);
}

// Define id annotations
// Define id annotations.
$ratingallocateallocation->annotate_ids('user', this_db\ratingallocate_allocations::USERID);
$ratingallocaterating->annotate_ids('user', this_db\ratingallocate_ratings::USERID);
$groupchoice->annotate_ids('group', 'groupid');
$ratingallocatechoicegroup->annotate_ids('group', 'groupid');
$ratingallocategrouping->annotate_ids('grouping', 'groupingid');

// Define file annotations
// Define file annotations.
$ratingallocate->annotate_files('mod_' . RATINGALLOCATE_MOD_NAME, 'intro', null);

// Return the root element (ratingallocate), wrapped into standard activity structure
// Return the root element (ratingallocate), wrapped into standard activity structure.
return $this->prepare_activity_structure($ratingallocate);
}
}
4 changes: 2 additions & 2 deletions backup/moodle2/backup_ratingallocate_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class backup_ratingallocate_activity_task extends backup_activity_task {
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity
// No particular settings for this activity.
}

/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// ratingallocate only has one structure step
// Ratingallocate only has one structure step.
$this->add_step(new backup_ratingallocate_activity_structure_step('ratingallocate_structure', 'ratingallocate.xml'));
}

Expand Down
42 changes: 36 additions & 6 deletions backup/moodle2/restore_ratingallocate_activity_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ protected function define_structure() {
$ratingallocatepath . '/' . this_db\ratingallocate_choices::TABLE . 's/' . this_db\ratingallocate_choices::TABLE;
$paths[] = new restore_path_element(this_db\ratingallocate_choices::TABLE, $choicespath);
$paths[] = new restore_path_element(this_db\ratingallocate_group_choices::TABLE,
$choicespath .'/' . this_db\ratingallocate_group_choices::TABLE .'s/' . this_db\ratingallocate_group_choices::TABLE);
$choicespath .'/' . this_db\ratingallocate_group_choices::TABLE .'s/' . this_db\ratingallocate_group_choices::TABLE);
$paths[] = new restore_path_element(this_db\ratingallocate_ch_gengroups::TABLE,
$choicespath .'/'. this_db\ratingallocate_ch_gengroups::TABLE . 's/' . this_db\ratingallocate_ch_gengroups::TABLE);
$paths[] = new restore_path_element(this_db\ratingallocate_groupings::TABLE,
$ratingallocatepath . '/' . this_db\ratingallocate_groupings::TABLE . 's/' . this_db\ratingallocate_groupings::TABLE);
if ($userinfo) {
$paths[] = new restore_path_element(this_db\ratingallocate_ratings::TABLE,
$choicespath . '/' . this_db\ratingallocate_ratings::TABLE . 's/' . this_db\ratingallocate_ratings::TABLE);
Expand All @@ -46,7 +50,7 @@ protected function define_structure() {
this_db\ratingallocate_allocations::TABLE);
}

// Return the paths wrapped into standard activity structure
// Return the paths wrapped into standard activity structure.
return $this->prepare_activity_structure($paths);
}

Expand All @@ -66,9 +70,9 @@ protected function process_ratingallocate($data) {
$data->{this_db\ratingallocate::PUBLISHED} = false;
}

// insert the record
// Insert the record.
$newitemid = $DB->insert_record(this_db\ratingallocate::TABLE, $data);
// immediately after inserting "activity" record, call this
// Immediately after inserting "activity" record, call this.
$this->apply_activity_instance($newitemid);
}

Expand All @@ -80,7 +84,7 @@ protected function process_ratingallocate_choices($data) {
$data->{this_db\ratingallocate_choices::RATINGALLOCATEID} = $this->get_new_parentid(this_db\ratingallocate::TABLE);
$newitemid = $DB->insert_record(this_db\ratingallocate_choices::TABLE, $data);
// No need to save this mapping as far as nothing depend on it
// (child paths, file areas nor links decoder)
// (child paths, file areas nor links decoder).
$this->set_mapping(this_db\ratingallocate_choices::TABLE, $oldid, $newitemid);
}

Expand Down Expand Up @@ -130,8 +134,34 @@ protected function process_ratingallocate_group_choices($data) {
$this->set_mapping(this_db\ratingallocate_group_choices::TABLE, $oldid, $newitemid);
}

protected function process_ratingallocate_ch_gengroups($data) {
global $DB;
$data = (object) $data;
$oldid = $data->id;
$data->choiceid = $this->get_new_parentid(this_db\ratingallocate_choices::TABLE);
if ((int) $data->groupid !== 0) {
$data->groupid = $this->get_mappingid('group', $data->groupid);
}

$newitemid = $DB->insert_record(this_db\ratingallocate_ch_gengroups::TABLE, $data);
$this->set_mapping(this_db\ratingallocate_ch_gengroups::TABLE, $oldid, $newitemid);
}

protected function process_ratingallocate_groupings($data) {
global $DB;
$data = (object) $data;
$oldid = $data->id;
$data->ratingallocateid = $this->get_new_parentid(this_db\ratingallocate::TABLE);
if ((int) $data->groupingid !== 0) {
$data->groupingid = $this->get_mappingid('grouping', $data->groupingid);
}

$newitemid = $DB->insert_record(this_db\ratingallocate_groupings::TABLE, $data);
$this->set_mapping(this_db\ratingallocate_groupings::TABLE, $oldid, $newitemid);
}

protected function after_execute() {
// Add ratingallocate related files
// Add ratingallocate related files.
$this->add_related_files('mod_' . RATINGALLOCATE_MOD_NAME, 'intro', null);
// $this->add_related_files('mod_' . RATINGALLOCATE_MOD_NAME, ratingallocate_FILEAREA_NAME, RATINGALLOCATE_MOD_NAME);
}
Expand Down
4 changes: 2 additions & 2 deletions backup/moodle2/restore_ratingallocate_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class restore_ratingallocate_activity_task extends restore_activity_task {
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity
// No particular settings for this activity.
}

/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// ratingallocate only has one structure step
// Ratingallocate only has one structure step.
$this->add_step(new restore_ratingallocate_activity_structure_step('ratingallocate_structure', 'ratingallocate.xml'));
}

Expand Down
5 changes: 3 additions & 2 deletions classes/allocations_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function build_table_by_sql() {
}

// If there are users, which rated but were not allocated, add them to a special row.
if (count($userwithrating) > 0 and ($this->currpage + 1) * $this->pagesize >= $this->totalrows) {
if (count($userwithrating) > 0 && ($this->currpage + 1) * $this->pagesize >= $this->totalrows) {
$noallocation = new \stdClass();
$noallocation->choicetitle = get_string(
'allocations_table_noallocation',
Expand Down Expand Up @@ -211,7 +211,8 @@ public function init_sql() {
$fields = "u.*, c.title as choicetitle";

$from =
"{ratingallocate_allocations} a JOIN {ratingallocate_choices} c ON a.choiceid = c.id JOIN {user} u ON a.userid = u.id";
"{ratingallocate_allocations} a JOIN {ratingallocate_choices} c ON a.choiceid = c.id
JOIN {user} u ON a.userid = u.id";
} else {
$fields = "c.id, c.title as choicetitle";

Expand Down
83 changes: 83 additions & 0 deletions classes/ratingallocate_observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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/>.

/**
* Event observer for ratingallocate.
*
* @package mod_ratingallocate
* @copyright 2023 I Hoppe
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_ratingallocate;
use coding_exception;
use dml_exception;

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

class ratingallocate_observer {

/**
* Triggered if group_deleted event is triggered.
*
* @param \core\event\group_deleted $event
* @return void
* @throws coding_exception
* @throws dml_exception
*/
public static function ch_gengroups_delete (\core\event\group_deleted $event) {
global $DB;

$eventdata = $event->get_record_snapshot('groups', $event->objectid);
if ($DB->record_exists(
'ratingallocate_ch_gengroups',
['groupid' => $eventdata->id])) {

// Delete the group from ratingallocate_ch_gengroups table.
$DB->delete_records(
'ratingallocate_ch_gengroups',
['groupid' => $eventdata->id]
);
}

}

/**
* Triggered if grouping_deleted event is triggered.
*
* @param \core\event\grouping_deleted $event
* @return void
* @throws coding_exception
* @throws dml_exception
*/
public static function ra_groupings_delete (\core\event\grouping_deleted $event) {
global $DB;

$eventdata = $event->get_record_snapshot('groupings', $event->objectid);
if ($DB->record_exists(
'ratingallocate_groupings',
['groupingid' => $eventdata->id])) {

// Delete the grouping from the ratingallocate_groupings table.
$DB->delete_records(
'ratingallocate_groupings',
['groupingid' => $eventdata->id]
);

}
}

}
17 changes: 17 additions & 0 deletions db/db_structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

namespace ratingallocate\db;

defined('MOODLE_INTERNAL') || die();
class ratingallocate {
const TABLE = 'ratingallocate';
const ID = 'id';
Expand Down Expand Up @@ -62,6 +64,21 @@ class ratingallocate_group_choices {
const GROUPID = 'groupid';
}


class ratingallocate_ch_gengroups {
const TABLE = 'ratingallocate_ch_gengroups';
const ID = 'id';
const GROUPID = 'groupid';
const CHOICEID = 'choiceid';
}

class ratingallocate_groupings {
const TABLE = 'ratingallocate_groupings';
const ID = 'id';
const RATINGALLOCATEID = 'ratingallocateid';
const GROUPINGID = 'groupingid';
}

class ratingallocate_ratings {
const TABLE = 'ratingallocate_ratings';
const ID = 'id';
Expand Down
45 changes: 45 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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/>.

/**
* Definition of ratingallocate event observers.
*
* The observers defined in this file are notified when respective events are triggered.
*
* @package mod_ratingallocate
* @category event
* @copyright 2023 I Hoppe
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/

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


$handlers = array();

// List of observers for group_deleted and grouping_deleted.

$observers = array(
array(
'eventname' => '\core\event\group_deleted',
'callback' => 'mod_ratingallocate\ratingallocate_observer::ch_gengroups_delete',
),
array(
'eventname' => '\core\event\grouping_deleted',
'callback' => 'mod_ratingallocate\ratingallocate_observer::ra_groupings_delete'
)
);
Loading

0 comments on commit 9b2fc18

Please sign in to comment.