Choice: MDL-20440 Added completion support to Choice (mark completed when user makes...
authorSam Marshall <s.marshall@open.ac.uk>
Thu, 14 Oct 2010 14:58:43 +0000 (14:58 +0000)
committerSam Marshall <s.marshall@open.ac.uk>
Thu, 14 Oct 2010 14:58:43 +0000 (14:58 +0000)
mod/choice/backup/moodle2/backup_choice_stepslib.php
mod/choice/db/install.xml
mod/choice/db/upgrade.php
mod/choice/lang/en/choice.php
mod/choice/lib.php
mod/choice/mod_form.php
mod/choice/report.php
mod/choice/version.php
mod/choice/view.php

index b90b584..d211a2d 100644 (file)
@@ -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');
 
index 3f66472..b60d4f4 100644 (file)
@@ -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"
 >
@@ -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"/>
@@ -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"/>
@@ -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"/>
@@ -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>
@@ -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>
@@ -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>
index 4d664d6..2d50f27 100644 (file)
@@ -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;
 }
 
index c5bb52d..3d7f0a6 100644 (file)
@@ -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';
index ac78606..b3c8d9f 100644 (file)
@@ -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;
+    }
+}
index 489e33d..1f65611 100644 (file)
@@ -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']);
+    }
 }
 
index f2bc269..bbd673b 100644 (file)
@@ -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");
     }
 
index 3b5acd4..792bdcb 100644 (file)
@@ -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;
 
index 405f307..019dce6 100644 (file)
     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');