/** * Test choice_get_availability_status * @return void */ public function test_choice_get_availability_status() { global $USER; $this->resetAfterTest(); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); // No time restrictions and updates allowed. list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(true, $status); $this->assertCount(0, $warnings); // No updates allowed, but haven't answered yet. $choice->allowupdate = false; list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(true, $status); $this->assertCount(0, $warnings); // No updates allowed and have answered. $cm = get_coursemodule_from_instance('choice', $choice->id); $choicewithoptions = choice_get_choice($choice->id); $optionids = array_keys($choicewithoptions->option); choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(false, $status); $this->assertCount(1, $warnings); $this->assertEquals('choicesaved', array_keys($warnings)[0]); $choice->allowupdate = true; // With time restrictions, still open. $choice->timeopen = time() - DAYSECS; $choice->timeclose = time() + DAYSECS; list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(true, $status); $this->assertCount(0, $warnings); // Choice not open yet. $choice->timeopen = time() + DAYSECS; $choice->timeclose = $choice->timeopen + DAYSECS; list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(false, $status); $this->assertCount(1, $warnings); $this->assertEquals('notopenyet', array_keys($warnings)[0]); // Choice closed. $choice->timeopen = time() - DAYSECS; $choice->timeclose = time() - 1; list($status, $warnings) = choice_get_availability_status($choice, false); $this->assertEquals(false, $status); $this->assertCount(1, $warnings); $this->assertEquals('expired', array_keys($warnings)[0]); }
} $PAGE->set_url($url); if (!($cm = get_coursemodule_from_id('choice', $id))) { print_error('invalidcoursemodule'); } if (!($course = $DB->get_record("course", array("id" => $cm->course)))) { print_error('coursemisconf'); } require_course_login($course, false, $cm); if (!($choice = choice_get_choice($cm->instance))) { print_error('invalidcoursemodule'); } $strchoice = get_string('modulename', 'choice'); $strchoices = get_string('modulenameplural', 'choice'); $context = context_module::instance($cm->id); list($choiceavailable, $warnings) = choice_get_availability_status($choice); if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate and $choiceavailable) { $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id)); if ($answercount > 0) { $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id)); // Update completion state $completion = new completion_info($course); if ($completion->is_enabled($cm) && $choice->completionsubmit) { $completion->update_state($cm, COMPLETION_INCOMPLETE); } redirect("view.php?id={$cm->id}"); } } $PAGE->set_title($choice->name); $PAGE->set_heading($course->fullname); /// Submit any new data if there is any
/** * Check if the module has any update that affects the current user since a given time. * * @param cm_info $cm course module data * @param int $from the time to check updates from * @param array $filter if we need to check only specific updates * @return stdClass an object with the different type of areas indicating if they were updated or not * @since Moodle 3.2 */ function choice_check_updates_since(cm_info $cm, $from, $filter = array()) { global $DB; $updates = new stdClass(); $choice = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); list($available, $warnings) = choice_get_availability_status($choice); if (!$available) { return $updates; } $updates = course_check_module_updates_since($cm, $from, array(), $filter); if (!choice_can_view_results($choice)) { return $updates; } // Check if there are new responses in the choice. $updates->answers = (object) array('updated' => false); $select = 'choiceid = :id AND timemodified > :since'; $params = array('id' => $choice->id, 'since' => $from); $answers = $DB->get_records_select('choice_answers', $select, $params, '', 'id'); if (!empty($answers)) { $updates->answers->updated = true; $updates->answers->itemids = array_keys($answers); } return $updates; }