Skip to content

Commit

Permalink
Merge pull request #252 from learnweb/feature/disable_rating_selection
Browse files Browse the repository at this point in the history
Feature/disable rating selection
  • Loading branch information
Laur0r authored May 12, 2023
2 parents c0dd429 + fe1babb commit 2725c82
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 8 deletions.
63 changes: 63 additions & 0 deletions classes/event/all_ratings_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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/>.

/**
* The mod_ratingallocate rating_deleted event.
*
* @package mod_ratingallocate
* @copyright 2020 Robin Tschudi
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_ratingallocate\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_ratingallocate rating_deleted event class.
*
* @copyright 2020 Robin Tschudi
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
class all_ratings_deleted extends \core\event\base {

public static function create_simple($modulecontext, $ratingallocateid) {
return self::create(array('context' => $modulecontext, 'objectid' => $ratingallocateid));
}
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'ratingallocate_ratings';
}

public static function get_name() {
return get_string('log_all_ratings_deleted', 'mod_ratingallocate');
}

public function get_description() {
return get_string('log_all_ratings_deleted_description', 'mod_ratingallocate',
array('userid' => $this->userid, 'ratingallocateid' => $this->objectid));
}

public function get_url() {
return new \moodle_url('/mod/ratingallocate/view.php', array('m' => $this->objectid));
}

public static function get_objectid_mapping() {
return array('db' => 'ratingallocate', 'restore' => 'ratingallocate');
}

public static function get_other_mapping() {
return false;
}
}
17 changes: 15 additions & 2 deletions lang/en/ratingallocate.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@
$string['start_distribution'] = 'Run Allocation Algorithm';
$string['confirm_start_distribution'] =
'Running the algorithm will delete all existing allocations, if any. Are you sure to continue?';
$string['delete_all_ratings'] = 'Delete all student ratings';
$string['delete_all_ratings_explanation'] = 'Deletes all ratings that students have submitted so far and all allocations
that may have been created based on these ratings. Use with caution.';
$string['confirm_delete_all_ratings'] = 'Are you sure you want to delete all ratings students have submitted so far?';
$string['error_deleting_all_insufficient_permission'] = 'You don\'t have the permission to do that';
$string['error_deleting_all_no_rating_possible'] = 'You can\'t delete the ratings when the rating phase is already over';
$string['success_deleting_all'] = 'Deleted all ratings';
$string['unassigned_users'] = 'Unassigned Users';
$string['unassigned_users_assigned'] = 'Unassigned users have been assigned as good as possible.';
$string['invalid_dates'] = 'Dates are invalid. Starting date must be before ending date.';
Expand Down Expand Up @@ -244,7 +251,6 @@
$string['csvupload_test_upload'] = 'Test upload';
$string['csvupload_test_upload_help'] = 'When checked: test the uploaded CSV file for data problems, but do not commit to the database.';
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Form to edit the instance(administrator)">
$string['choice_active'] = 'Choice is active';
$string['choice_active_help'] = 'Only active choices are displayed to the user. Inactive choices are not displayed.';
Expand Down Expand Up @@ -275,9 +281,13 @@
* **Likert Scale** The user can rate each choice with a number from a defined range. The range of numbers can be defined individually (beginning with 0). A high number corresponds to a high preference.
* **Give Points** The user can rate the choices by assigning a number of points. The maximum number of points can be defined individually. A high number of points corresponds to a high preference.
* **Rank Choices** The user has to rank the available choices. How many choices need to be rated can be defined individually.
* **Tick Accept** The user can state for each choice whether it is acceptable for him/her.';
* **Tick Accept** The user can state for each choice whether it is acceptable for him/her.
This option can\'t be changed if a student has already submitted their preference.
';
$string['strategy_not_specified'] = 'You have to select a strategy.';
$string['strategyspecificoptions'] = 'Strategy specific options';
$string['strategy_altered_after_preferences'] = 'Strategy cannot be changed after preferences where submitted';

$string['err_required'] = 'You need to provide a value for this field.';
$string['err_minimum'] = 'The minimum value for this field is {$a}.';
Expand Down Expand Up @@ -409,6 +419,9 @@
$string['log_index_viewed'] = 'User viewed all instances of Fair Allocation';
$string['log_index_viewed_description'] = 'The user with id "{$a->userid}" viewed all instances of Fair Allocation in this course.';

$string['log_all_ratings_deleted'] = 'All ratings of a Fair Allocation instance were deleted';
$string['log_all_ratings_deleted_description'] = 'The user with id "{$a->userid}" has deleted all ratings for the Fair Allocation with id "{$a->ratingallocateid}".';

$string['no_id_or_m_error'] = 'You must specify a course_module ID or an instance ID';

// Language strings for Privacy API
Expand Down
60 changes: 59 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static function get_strategies() {
define('ACTION_DISABLE_CHOICE', 'disable_choice');
define('ACTION_DELETE_CHOICE', 'delete_choice');
define('ACTION_START_DISTRIBUTION', 'start_distribution');
define('ACTION_DELETE_ALL_RATINGS', 'delete_all_ratings');
define('ACTION_MANUAL_ALLOCATION', 'manual_allocation');
define('ACTION_DISTRIBUTE_UNALLOCATED_FILL', 'distribute_unallocated_fill');
define('ACTION_DISTRIBUTE_UNALLOCATED_EQUALLY', 'distribute_unallocated_equally');
Expand Down Expand Up @@ -195,7 +196,6 @@ public function get_group_candidates() {
foreach ($groupcandidates as $group) {
$options[$group->id] = $group->name;
}

return $options;
}

Expand Down Expand Up @@ -261,6 +261,26 @@ private function process_action_start_distribution() {
return;
}

private function delete_all_student_ratings() {
global $USER;
// Disallow to delete ratings for students and tutors.
if (!has_capability('mod/ratingallocate:start_distribution', $this->context, null, false)) {
redirect(new moodle_url('/mod/ratingallocate/view.php', array('id' => $this->coursemodule->id)),
get_string('error_deleting_all_insufficient_permission', RATINGALLOCATE_MOD_NAME));
return;
}
// Disallow deletion when there can't be new ratings submitted
$status = $this->get_status();
if ($status !== self::DISTRIBUTION_STATUS_RATING_IN_PROGRESS and $status !== self::DISTRIBUTION_STATUS_TOO_EARLY) {
redirect(new moodle_url('/mod/ratingallocate/view.php', array('id' => $this->coursemodule->id)),
get_string('error_deleting_all_no_rating_possible', RATINGALLOCATE_MOD_NAME));
return;
}
$this->delete_all_ratings();
redirect(new moodle_url('/mod/ratingallocate/view.php', array('id' => $this->coursemodule->id)),
get_string('success_deleting_all', RATINGALLOCATE_MOD_NAME));
}

private function process_action_give_rating() {
global $CFG;

Expand Down Expand Up @@ -1067,6 +1087,10 @@ public function handle_view() {
$this->process_action_delete_rating();
break;

case ACTION_DELETE_ALL_RATINGS:
$this->delete_all_student_ratings();
break;

case ACTION_SHOW_CHOICES:
$this->process_action_show_choices();
return "";
Expand Down Expand Up @@ -1554,6 +1578,40 @@ public function get_users_with_ratings() {
));
}

/**
* Deletes all ratings in this ratingallocate
*/
public function delete_all_ratings() {
global $DB;

$transaction = $DB->start_delegated_transaction();

try {
$choices = $this->get_choices();

foreach ($choices as $id => $choice) {
$data = array(
'choiceid' => $id
);

// Delete the allocations associated with this rating.
$DB->delete_records('ratingallocate_allocations', $data);

// Actually delete the rating.
$DB->delete_records('ratingallocate_ratings', $data);
}

$transaction->allow_commit();

// Logging.
$event = \mod_ratingallocate\event\all_ratings_deleted::create_simple(
context_module::instance($this->coursemodule->id), $this->ratingallocateid);
$event->trigger();
} catch (Exception $e) {
$transaction->rollback($e);
}
}

/**
* Delete all ratings of a users
* @param int $userid
Expand Down
49 changes: 45 additions & 4 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function definition() {
global $CFG, $PAGE;
$mform = $this->_form;

$disablestrategy = $this->get_disable_strategy();

// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));

Expand All @@ -92,9 +94,13 @@ public function definition() {
foreach (\strategymanager::get_strategies() as $strategy) {
$selectoptions[$strategy] = get_string($strategy . '_name', self::MOD_NAME);
}
$mform->addElement('select', $elementname, get_string('select_strategy', self::MOD_NAME), $selectoptions);
$mform->addElement('select', $elementname, get_string('select_strategy', self::MOD_NAME), $selectoptions,
$disablestrategy ? ['disabled' => ''] : null);
$mform->addHelpButton($elementname, 'select_strategy', self::MOD_NAME);
$mform->addRule('strategy', null, 'required', null, 'client');
if (!$disablestrategy) {
// disabled elements don't get posted so disable the required rule if strategy selection is disabled.
$mform->addRule('strategy', null, 'required', null, 'client');
}

// Start/end time.
$elementname = 'accesstimestart';
Expand Down Expand Up @@ -139,6 +145,31 @@ public function definition() {
$this->add_action_buttons();
}

public function get_disable_strategy($includeratingallocate = false) {
$update = $this->optional_param('update', 0, PARAM_INT);
if ($update != 0) {
global $DB;
$courseid = $update;
$cm = get_coursemodule_from_id('ratingallocate', $courseid, 0, false, MUST_EXIST);
$course = get_course($cm->course);
$ratingallocatedb = $DB->get_record('ratingallocate', array('id' => $cm->instance), '*', MUST_EXIST);
$context = context_module::instance($cm->id);
$ratingallocate = new ratingallocate($ratingallocatedb, $course, $cm, $context);
$disablestrategy = $ratingallocate->get_number_of_active_raters() > 0;
} else {
$ratingallocate = null;
$disablestrategy = false;
}
if (!$includeratingallocate) {
return $disablestrategy;
} else {
return [
'ratingallocate' => $ratingallocate,
'disable_strategy' => $disablestrategy
];
}
}

/**
* Add an settings element to the form. It is enabled only if the strategy it belongs to is selected.
* @param string $stratfieldid id of the element to be added
Expand Down Expand Up @@ -232,8 +263,19 @@ public function validation($data, $files) {
$errors['publishdate'] = get_string('invalid_publishdate', self::MOD_NAME);
}

// User has to select one strategy.
$info = $this->get_disable_strategy(true);
$disablestrategy = $info['disable_strategy'];
$ratingallocate = $info['ratingallocate'];

if ($disablestrategy) {
// If strategy selection is disabled make sure the user didn't change it.
if ($ratingallocate->ratingallocate->dbrecord->strategy !== $data['strategy']) {
$errors['strategy'] = get_string('strategy_altered_after_preferences', self::MOD_NAME);
}
}

if (empty($data['strategy'])) {
// User has to select one strategy.
$errors['strategy'] = get_string('strategy_not_specified', self::MOD_NAME);
} else {
$strategyclassp = 'ratingallocate\\' . $data['strategy'] . '\\strategy';
Expand All @@ -257,5 +299,4 @@ public function validation($data, $files) {
private function get_settingsfield_identifier($strategy, $key) {
return self::STRATEGY_OPTIONS . '[' . $strategy . '][' . $key . ']';
}

}
9 changes: 9 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public function modify_allocation_group($ratingallocateid, $coursemoduleid,
$status !== ratingallocate::DISTRIBUTION_STATUS_RATING_IN_PROGRESS;

$starturl = new moodle_url($this->page->url, array('action' => ACTION_START_DISTRIBUTION));
$deleteurl = new moodle_url($this->page->url, array('id' => $coursemoduleid, 'action' => ACTION_DELETE_ALL_RATINGS));

// Get description dependent on status
$descriptionbaseid = 'modify_allocation_group_desc_';
Expand All @@ -319,6 +320,14 @@ public function modify_allocation_group($ratingallocateid, $coursemoduleid,
'action' => ACTION_MANUAL_ALLOCATION)), get_string('manual_allocation_form', RATINGALLOCATE_MOD_NAME), 'get',
array('disabled' => !$ratingover));

// Add delete all ratings button
$deletebutton = new single_button($deleteurl, get_string('delete_all_ratings', RATINGALLOCATE_MOD_NAME, 'get'));
$deletebutton->disabled = $ratingover; // Only allow deletion if new submission is possible.
$deletebutton->tooltip = get_string('delete_all_ratings_explanation', RATINGALLOCATE_MOD_NAME);
$deletebutton->add_action(new confirm_action(get_string('confirm_delete_all_ratings', RATINGALLOCATE_MOD_NAME)));

$output .= $this->render($deletebutton);

if (has_capability('mod/ratingallocate:distribute_unallocated', context_module::instance($coursemoduleid))) {
$output .= html_writer::start_div('ratingallocate_distribute_unallocated');

Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

$plugin->version = 2022120101; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2023050501; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2020061500; // Requires Moodle 3.9+.
$plugin->maturity = MATURITY_STABLE;
$plugin->release = 'v4.0-r1';
Expand Down

0 comments on commit 2725c82

Please sign in to comment.