diff --git a/mod/choice/backup/moodle2/backup_choice_stepslib.php b/mod/choice/backup/moodle2/backup_choice_stepslib.php index b90b584d4a3..d211a2dbd8e 100644 --- a/mod/choice/backup/moodle2/backup_choice_stepslib.php +++ b/mod/choice/backup/moodle2/backup_choice_stepslib.php @@ -40,7 +40,8 @@ class backup_choice_activity_structure_step extends backup_activity_structure_st $choice = new backup_nested_element('choice', array('id'), array( 'name', 'intro', 'introformat', 'publish', 'showresults', 'display', 'allowupdate', 'allowunanswered', - 'limitanswers', 'timeopen', 'timeclose', 'timemodified')); + 'limitanswers', 'timeopen', 'timeclose', 'timemodified', + 'completionsubmit')); $options = new backup_nested_element('options'); diff --git a/mod/choice/db/install.xml b/mod/choice/db/install.xml index 3f66472dd48..b60d4f44ae3 100644 --- a/mod/choice/db/install.xml +++ b/mod/choice/db/install.xml @@ -1,5 +1,5 @@ - @@ -11,6 +11,7 @@ + @@ -19,7 +20,9 @@ - + + + @@ -28,6 +31,7 @@ + @@ -37,6 +41,7 @@ + @@ -46,6 +51,7 @@ + @@ -55,6 +61,7 @@ +
diff --git a/mod/choice/db/upgrade.php b/mod/choice/db/upgrade.php index 4d664d627af..2d50f277179 100644 --- a/mod/choice/db/upgrade.php +++ b/mod/choice/db/upgrade.php @@ -53,6 +53,21 @@ function xmldb_choice_upgrade($oldversion) { upgrade_mod_savepoint(true, 2009042001, 'choice'); } + if ($oldversion < 2010101300) { + + // Define field completionsubmit to be added to choice + $table = new xmldb_table('choice'); + $field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timemodified'); + + // Conditionally launch add field completionsubmit + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // choice savepoint reached + upgrade_mod_savepoint(true, 2010101300, 'choice'); + } + return true; } diff --git a/mod/choice/lang/en/choice.php b/mod/choice/lang/en/choice.php index c5bb52d0b39..3d7f0a6f509 100644 --- a/mod/choice/lang/en/choice.php +++ b/mod/choice/lang/en/choice.php @@ -26,6 +26,7 @@ $string['addmorechoices'] = 'Add more choices'; $string['allowupdate'] = 'Allow choice to be updated'; $string['answered'] = 'Answered'; +$string['completionsubmit'] = 'Show as complete when user makes a choice'; $string['displayhorizontal'] = 'Display horizontally'; $string['displaymode'] = 'Display Mode'; $string['displayvertical'] = 'Display vertically'; diff --git a/mod/choice/lib.php b/mod/choice/lib.php index ac786065e8d..b3c8d9f3058 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -240,10 +240,10 @@ function choice_prepare_options($choice, $user, $coursemodule, $allresponses) { * @param int $formanswer * @param object $choice * @param int $userid - * @param int $courseid + * @param object $course Course object * @param object $cm */ -function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) { +function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) { global $DB; $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid)); $context = get_context_instance(CONTEXT_MODULE, $cm->id); @@ -291,7 +291,7 @@ WHERE $newanswer->optionid = $formanswer; $newanswer->timemodified = time(); $DB->update_record("choice_answers", $newanswer); - add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); + add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); } else { $newanswer = NULL; $newanswer->choiceid = $choice->id; @@ -299,7 +299,13 @@ WHERE $newanswer->optionid = $formanswer; $newanswer->timemodified = time(); $DB->insert_record("choice_answers", $newanswer); - add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); + + // Update completion state + $completion = new completion_info($course); + if ($completion->is_enabled($cm) && $choice->completionsubmit) { + $completion->update_state($cm, COMPLETION_COMPLETE); + } + add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); } } else { if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error @@ -513,10 +519,12 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forc /** * @global object * @param array $attemptids - * @param int $choiceid + * @param object $choice Choice main table row + * @param object $cm Course-module object + * @param object $course Course object * @return bool */ -function choice_delete_responses($attemptids, $choiceid) { +function choice_delete_responses($attemptids, $choice, $cm, $course) { global $DB; if(!is_array($attemptids) || empty($attemptids)) { return false; @@ -528,9 +536,14 @@ function choice_delete_responses($attemptids, $choiceid) { } } + $completion = new completion_info($course); foreach($attemptids as $attemptid) { - if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid))) { - $DB->delete_records('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid)); + if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid))) { + $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid)); + // Update completion state + if ($completion->is_enabled($cm) && $choice->completionsubmit) { + $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid); + } } } return true; @@ -772,6 +785,7 @@ function choice_supports($feature) { case FEATURE_GROUPMEMBERSONLY: return true; case FEATURE_MOD_INTRO: return true; case FEATURE_COMPLETION_TRACKS_VIEWS: return true; + case FEATURE_COMPLETION_HAS_RULES: return true; case FEATURE_GRADE_HAS_GRADE: return false; case FEATURE_GRADE_OUTCOMES: return false; case FEATURE_BACKUP_MOODLE2: return true; @@ -809,3 +823,30 @@ function choice_extend_settings_navigation(settings_navigation $settings, naviga $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id))); } } + +/** + * Obtains the automatic completion state for this choice based on any conditions + * in forum settings. + * + * @param object $course Course + * @param object $cm Course-module + * @param int $userid User ID + * @param bool $type Type of comparison (or/and; can be used as return value if no conditions) + * @return bool True if completed, false if not, $type if conditions not set. + */ +function choice_get_completion_state($course, $cm, $userid, $type) { + global $CFG,$DB; + + // Get choice details + $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*', + MUST_EXIST); + + // If completion option is enabled, evaluate it and return true/false + if($choice->completionsubmit) { + return $DB->record_exists('choice_answers', array( + 'choiceid'=>$choice->id, 'userid'=>$userid)); + } else { + // Completion option is not enabled so just return $type + return $type; + } +} diff --git a/mod/choice/mod_form.php b/mod/choice/mod_form.php index 489e33d65e1..1f65611f953 100644 --- a/mod/choice/mod_form.php +++ b/mod/choice/mod_form.php @@ -137,5 +137,27 @@ class mod_choice_mod_form extends moodleform_mod { return $errors; } + function get_data() { + $data = parent::get_data(); + if (!$data) { + return false; + } + // Set up completion section even if checkbox is not ticked + if (empty($data->completionsection)) { + $data->completionsection=0; + } + return $data; + } + + function add_completion_rules() { + $mform =& $this->_form; + + $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice')); + return array('completionsubmit'); + } + + function completion_rule_enabled($data) { + return !empty($data['completionsubmit']); + } } diff --git a/mod/choice/report.php b/mod/choice/report.php index f2bc2698ba7..bbd673b0010 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -46,7 +46,7 @@ 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. + choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses. redirect("report.php?id=$cm->id"); } diff --git a/mod/choice/version.php b/mod/choice/version.php index 3b5acd46495..792bdcb670d 100644 --- a/mod/choice/version.php +++ b/mod/choice/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2010080300; +$module->version = 2010101300; $module->requires = 2010080300; // Requires this Moodle version $module->cron = 0; diff --git a/mod/choice/view.php b/mod/choice/view.php index 405f307e685..019dce6c3ca 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -38,6 +38,12 @@ if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) { if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) { $DB->delete_records('choice_answers', array('id' => $answer->id)); + + // Update completion state + $completion = new completion_info($course); + if ($completion->is_enabled($cm) && $choice->completionsubmit) { + $completion->update_state($cm, COMPLETION_INCOMPLETE); + } } } @@ -49,7 +55,7 @@ $timenow = time(); if (has_capability('mod/choice:deleteresponses', $context)) { if ($action == 'delete') { //some responses need to be deleted - choice_delete_responses($attemptids, $choice->id); //delete responses. + choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses. redirect("view.php?id=$cm->id"); } } @@ -58,7 +64,7 @@ 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); + choice_user_submit_response($answer, $choice, $USER->id, $course, $cm); } echo $OUTPUT->header(); echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');