Skip to content

Commit

Permalink
Merge pull request #201 from opitz/CTP-2871
Browse files Browse the repository at this point in the history
Resolving issue with Course Completion in Moodle 4.3 (#200)
  • Loading branch information
NicoAlexH authored Jan 4, 2024
2 parents cb0c49f + 449277e commit c1b5943
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$PAGE->set_url('/mod/choicegroup/index.php', array('id' => $id));

if (!$course = $DB->get_record('course', array('id' => $id))) {
print_error('invalidcourseid');
throw new moodle_exception('invalidcourseid');
}

require_course_login($course);
Expand Down
32 changes: 16 additions & 16 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ function choicegroup_add_instance($choicegroup) {

//insert answers
$choicegroup->id = $DB->insert_record("choicegroup", $choicegroup);

// deserialize the selected groups

$groupIDs = explode(';', $choicegroup->serializedselectedgroups);
$groupIDs = array_diff( $groupIDs, array( '' ) );

foreach ($groupIDs as $groupID) {
$groupID = trim($groupID);
if (isset($groupID) && $groupID != '') {
Expand All @@ -216,7 +216,7 @@ function choicegroup_add_instance($choicegroup) {
}
$option->timemodified = time();
$DB->insert_record("choicegroup_options", $option);
}
}
}

if (class_exists('\core_completion\api')) {
Expand Down Expand Up @@ -252,10 +252,10 @@ function choicegroup_update_instance($choicegroup) {
if (empty($choicegroup->multipleenrollmentspossible)) {
$choicegroup->multipleenrollmentspossible = 0;
}


// deserialize the selected groups

$groupIDs = explode(';', $choicegroup->serializedselectedgroups);
$groupIDs = array_diff( $groupIDs, array( '' ) );

Expand Down Expand Up @@ -285,9 +285,9 @@ function choicegroup_update_instance($choicegroup) {
continue 2; // continue the big loop
}
}
$DB->insert_record("choicegroup_options", $option);
$DB->insert_record("choicegroup_options", $option);
}

}
// remove all remaining pre-existing groups which did not appear in the form (and are thus assumed to have been deleted)
foreach ($preExistingGroups as $preExistingGroup) {
Expand Down Expand Up @@ -413,13 +413,13 @@ function choicegroup_user_submit_response($formanswer, $choicegroup, $userid, $c

$countanswers=0;
groups_add_member($selected_option->groupid, $userid);
$groupmember_added = true;
$groupmember_added = true;
if ($choicegroup->limitanswers) {
$groupmember = $DB->get_record('groups_members', array('groupid' => $selected_option->groupid, 'userid'=>$userid));
$select_count = 'groupid='.$selected_option->groupid.' and id<='.$groupmember->id;
$countanswers = $DB->count_records_select('groups_members', $select_count);
$maxans = $choicegroup->maxanswers[$formanswer];
if ($countanswers > $maxans) {
if ($countanswers > $maxans) {
groups_remove_member($selected_option->groupid, $userid);
$groupmember_added = false;
}
Expand Down Expand Up @@ -454,7 +454,7 @@ function choicegroup_user_submit_response($formanswer, $choicegroup, $userid, $c
}
} else {
if (!$current || !($current->id==$selected_option->groupid)) { //check to see if current choicegroup already selected - if not display error
print_error('choicegroupfull', 'choicegroup', $CFG->wwwroot.'/mod/choicegroup/view.php?id='.$cm->id);
throw new moodle_exception('choicegroupfull', 'choicegroup', $CFG->wwwroot.'/mod/choicegroup/view.php?id='.$cm->id);
}
}
}
Expand Down Expand Up @@ -960,13 +960,13 @@ function choicegroup_get_response_data($choicegroup, $cm, $groupmode, $onlyactiv
return $allresponses;
}

/* Return an array with the options selected of users of the $choicegroup
*
/* Return an array with the options selected of users of the $choicegroup
*
* @param object $choicegroup choicegroup record
* @param context_module $ctx Context instance
* @param int $currentgroup Current group
* @param bool $onlyactive Whether to get responses for active users only
* @return array of selected options by all users
* @return array of selected options by all users
*/
function choicegroup_get_responses($choicegroup, $ctx, $currentgroup, $onlyactive) {

Expand Down Expand Up @@ -1044,7 +1044,7 @@ function choicegroup_extend_settings_navigation(settings_navigation $settings, n
groups_get_activity_group($cm, true);
}
if (!$choicegroup = choicegroup_get_choicegroup($cm->instance)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule');
return false;
}

Expand Down
14 changes: 11 additions & 3 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ function definition()
$a->linkcourse = $CFG->wwwroot . '//course/view.php?id=' . $COURSE->id;
$message = get_string('pleasesetonegroupor', 'choicegroup', $a);
\core\notification::add($message, \core\notification::WARNING);
print_error('nogroupincourse', 'choicegroup', new moodle_url('/course/view.php?id=' . $COURSE->id), $a);
}

$db_groupings = $DB->get_records('groupings', array('courseid' => $COURSE->id));
Expand Down Expand Up @@ -335,10 +334,19 @@ function get_data()

function add_completion_rules()
{
global $CFG;

$mform =& $this->_form;

$mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choicegroup'));
return array('completionsubmit');
// Changes for Moodle 4.3 - MDL-78516.
if ($CFG->branch < 403) {
$suffix = '';
} else {
$suffix = $this->get_suffix();
}

$mform->addElement('checkbox', 'completionsubmit' . $suffix, '', get_string('completionsubmit', 'choicegroup'));
return ['completionsubmit' . $suffix];
}

function completion_rule_enabled($data)
Expand Down
6 changes: 3 additions & 3 deletions report.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
$PAGE->set_url($url);

if (! $cm = get_coursemodule_from_id('choicegroup', $id)) {
print_error("invalidcoursemodule");
throw new moodle_exception("invalidcoursemodule");
}

if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error("coursemisconf");
throw new moodle_exception("coursemisconf");
}

require_login($course->id, false, $cm);
Expand All @@ -60,7 +60,7 @@
require_capability('mod/choicegroup:readresponses', $context);

if (!$choicegroup = choicegroup_get_choicegroup($cm->instance)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule');
}

$strchoicegroup = get_string("modulename", "choicegroup");
Expand Down
8 changes: 4 additions & 4 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
$PAGE->set_url($url);

if (! $cm = get_coursemodule_from_id('choicegroup', $id)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule');
}

if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
throw new moodle_exception('coursemisconf');
}

require_login($course, false, $cm);
$PAGE->requires->js_call_amd('mod_choicegroup/choicegroupdatadisplay', 'init');
if (!$choicegroup = choicegroup_get_choicegroup($cm->instance)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule');
}
$choicegroup_groups = choicegroup_get_groups($choicegroup);
$choicegroup_users = array();
Expand All @@ -60,7 +60,7 @@
$strchoicegroups = get_string('modulenameplural', 'choicegroup');

if (!$context = context_module::instance($cm->id)) {
print_error('badcontext');
throw new moodle_exception('badcontext');
}

$eventparams = array(
Expand Down

0 comments on commit c1b5943

Please sign in to comment.