// 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 if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) { $timenow = time(); if (has_capability('mod/choice:deleteresponses', $context) && $action == 'delete') { //some responses need to be deleted choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses. redirect("view.php?id={$cm->id}"); } // Redirection after all POSTs breaks block editing, we need to be more specific! if ($choice->allowmultiple) { $answer = optional_param_array('answer', array(), PARAM_INT); } else { $answer = optional_param('answer', '', PARAM_INT); } if (!$choiceavailable) { $reason = current(array_keys($warnings)); throw new moodle_exception($reason, 'choice', '', $warnings[$reason]); } if ($answer) { choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
if (!($context = get_context_instance(CONTEXT_MODULE, $cm->id))) { print_error('badcontext'); } if ($action == 'delchoice') { if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) { //print_object($answer); delete_records('choice_answers', 'id', $answer->id); } } /// Submit any new data if there is any if ($form = data_submitted() && has_capability('mod/choice:choose', $context)) { $timenow = time(); if (has_capability('mod/choice:deleteresponses', $context)) { if ($action == 'delete') { //some responses need to be deleted choice_delete_responses($attemptids); //delete responses. redirect("view.php?id={$cm->id}"); } } $answer = optional_param('answer', '', PARAM_INT); if (empty($answer)) { redirect("view.php?id={$cm->id}", get_string('mustchooseone', 'choice')); } else { choice_user_submit_response($answer, $choice, $USER->id, $course->id, $cm); } redirect("view.php?id={$cm->id}"); exit; } /// Display the choice and possibly results $navigation = build_navigation('', $cm);
/** * Test to ensure that event data is being stored correctly. */ public function test_answer_deleted() { global $DB, $USER; // Generate user data. $user = $this->getDataGenerator()->create_user(); $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id))); // Create the first answer. choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm); // Get the users response. $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id), '*', $strictness = IGNORE_MULTIPLE); // Redirect event. $sink = $this->redirectEvents(); // Now delete the answer. choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course); // Get our event event. $events = $sink->get_events(); $event = reset($events); // Data checking. $this->assertInstanceOf('\\mod_choice\\event\\answer_deleted', $event); $this->assertEquals($USER->id, $event->userid); $this->assertEquals($user->id, $event->relateduserid); $this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context()); $this->assertEquals($this->choice->id, $event->other['choiceid']); $this->assertEquals($answer->optionid, $event->other['optionid']); $this->assertEventContextNotUsed($event); $sink->close(); }
} if (!($course = get_record("course", "id", $cm->course))) { error("Course module is misconfigured"); } require_login($course->id, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); require_capability('mod/choice:readresponses', $context); if (!($choice = choice_get_choice($cm->instance))) { error("Course module is incorrect"); } $strchoice = get_string("modulename", "choice"); $strchoices = get_string("modulenameplural", "choice"); $strresponses = get_string("responses", "choice"); add_to_log($course->id, "choice", "report", "report.php?id={$cm->id}", "{$choice->id}", $cm->id); if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses', $context) && confirm_sesskey()) { choice_delete_responses($attemptids, $choice->id); //delete responses. redirect("report.php?id={$cm->id}"); } if (!$download) { $navigation = build_navigation($strresponses, $cm); print_header_simple(format_string($choice->name) . ": {$strresponses}", "", $navigation, "", '', true, update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm)); /// Check to see if groups are being used in this choice $groupmode = groups_get_activity_groupmode($cm); if ($groupmode) { groups_get_activity_group($cm, true); groups_print_activity_menu($cm, 'report.php?id=' . $id); } } else { $groupmode = groups_get_activity_groupmode($cm); }
/** * Delete the given submitted responses in a choice * * @param int $choiceid the choice instance id * @param array $responses the response ids, empty for deleting all the user responses * @return array status information and warnings * @throws moodle_exception * @since Moodle 3.0 */ public static function delete_choice_responses($choiceid, $responses = array()) { $status = false; $warnings = array(); $params = self::validate_parameters(self::delete_choice_responses_parameters(), array('choiceid' => $choiceid, 'responses' => $responses)); if (!($choice = choice_get_choice($params['choiceid']))) { throw new moodle_exception("invalidcoursemodule", "error"); } list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice'); $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/choice:choose', $context); // If we have the capability, delete all the passed responses. if (has_capability('mod/choice:deleteresponses', $context)) { if (empty($params['responses'])) { // Get all the responses for the choice. $params['responses'] = array_keys(choice_get_all_responses($choice)); } $status = choice_delete_responses($params['responses'], $choice, $cm, $course); } else { if ($choice->allowupdate) { // Check if we can delate our own responses. $timenow = time(); if ($choice->timeclose != 0) { if ($timenow > $choice->timeclose) { throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); } } // Delete only our responses. $myresponses = array_keys(choice_get_my_response($choice)); if (empty($params['responses'])) { $todelete = $myresponses; } else { $todelete = array(); foreach ($params['responses'] as $response) { if (!in_array($response, $myresponses)) { $warnings[] = array('item' => 'response', 'itemid' => $response, 'warningcode' => 'nopermissions', 'message' => 'No permission to delete this response'); } else { $todelete[] = $response; } } } $status = choice_delete_responses($todelete, $choice, $cm, $course); } else { // The user requires the capability to delete responses. throw new required_capability_exception($context, 'mod/choice:deleteresponses', 'nopermissions', ''); } } return array('status' => $status, 'warnings' => $warnings); }