Skip to content

Commit

Permalink
Choice: MDL-20440 Added completion support to Choice (mark completed …
Browse files Browse the repository at this point in the history
…when user makes choice)
  • Loading branch information
sammarshallou committed Oct 14, 2010
1 parent 14333c1 commit bc49973
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 15 deletions.
3 changes: 2 additions & 1 deletion mod/choice/backup/moodle2/backup_choice_stepslib.php
Expand Up @@ -40,7 +40,8 @@ protected function define_structure() {
$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');

Expand Down
11 changes: 9 additions & 2 deletions mod/choice/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/choice/db" VERSION="20090420" COMMENT="XMLDB file for Moodle mod/choice"
<XMLDB PATH="mod/choice/db" VERSION="20101013" COMMENT="XMLDB file for Moodle mod/choice"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -11,6 +11,7 @@
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="publish"/>

<FIELD NAME="publish" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="showresults"/>
<FIELD NAME="showresults" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="publish" NEXT="display"/>
<FIELD NAME="display" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showresults" NEXT="allowupdate"/>
Expand All @@ -19,7 +20,9 @@
<FIELD NAME="limitanswers" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showunanswered" NEXT="timeopen"/>
<FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="limitanswers" NEXT="timeclose"/>
<FIELD NAME="timeclose" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeopen" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeclose"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeclose" NEXT="completionsubmit"/>

<FIELD NAME="completionsubmit" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="If this field is set to 1, then the activity will be automatically marked as 'complete' once the user submits their choice." PREVIOUS="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand All @@ -28,6 +31,7 @@
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>

<TABLE NAME="choice_options" COMMENT="available options to choice" PREVIOUS="choice" NEXT="choice_answers">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/>
Expand All @@ -37,6 +41,7 @@
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="maxanswers"/>
</FIELDS>
<KEYS>

<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="choiceid"/>
<KEY NAME="choiceid" TYPE="foreign" FIELDS="choiceid" REFTABLE="choice" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
Expand All @@ -46,6 +51,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choiceid"/>
<FIELD NAME="choiceid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="choiceid" NEXT="optionid"/>

<FIELD NAME="optionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="optionid"/>
</FIELDS>
Expand All @@ -55,6 +61,7 @@
<KEY NAME="optionid" TYPE="foreign" FIELDS="optionid" REFTABLE="choice_options" REFFIELDS="id" PREVIOUS="choiceid"/>
</KEYS>
<INDEXES>

<INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
</INDEXES>
</TABLE>
Expand Down
15 changes: 15 additions & 0 deletions mod/choice/db/upgrade.php
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions mod/choice/lang/en/choice.php
Expand Up @@ -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';
Expand Down
57 changes: 49 additions & 8 deletions mod/choice/lib.php
Expand Up @@ -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);
Expand Down Expand Up @@ -291,15 +291,21 @@ function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $
$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;
$newanswer->userid = $userid;
$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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
22 changes: 22 additions & 0 deletions mod/choice/mod_form.php
Expand Up @@ -137,5 +137,27 @@ function validation($data, $files) {
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']);
}
}

2 changes: 1 addition & 1 deletion mod/choice/report.php
Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion mod/choice/version.php
Expand Up @@ -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;

Expand Down
10 changes: 8 additions & 2 deletions mod/choice/view.php
Expand Up @@ -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);
}
}
}

Expand All @@ -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");
}
}
Expand All @@ -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');
Expand Down

0 comments on commit bc49973

Please sign in to comment.