MDL-25178 glossary - add completionentries rule
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 15 Nov 2010 19:16:45 +0000 (20:16 +0100)
committerPetr Skoda <commits@skodak.org>
Mon, 20 Dec 2010 19:02:04 +0000 (20:02 +0100)
mod/glossary/approve.php
mod/glossary/backup/moodle2/backup_glossary_stepslib.php
mod/glossary/db/install.xml [changed mode: 0644->0755]
mod/glossary/db/upgrade.php
mod/glossary/deleteentry.php
mod/glossary/edit.php
mod/glossary/lang/en/glossary.php
mod/glossary/lib.php
mod/glossary/mod_form.php
mod/glossary/version.php

index 579ea45..67fc212 100644 (file)
@@ -27,6 +27,13 @@ if (!$entry->approved and confirm_sesskey()) {
     $newentry->approved     = 1;
     $newentry->timemodified = time(); // wee need this date here to speed up recent activity, TODO: use timestamp in approved field instead in 2.0
     $DB->update_record("glossary_entries", $newentry);
+
+    // Update completion state
+    $completion = new completion_info($course);
+    if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
+        $completion->update_state($cm, COMPLETION_COMPLETE, $entry->userid);
+    }
+
     add_to_log($course->id, "glossary", "approve entry", "showentry.php?id=$cm->id&amp;eid=$eid", "$eid", $cm->id);
 }
 
index 585250a..e0186e0 100644 (file)
@@ -43,7 +43,7 @@ class backup_glossary_activity_structure_step extends backup_activity_structure_
             'allowcomments', 'allowprintview', 'usedynalink', 'defaultapproval',
             'globalglossary', 'entbypage', 'editalways', 'rsstype',
             'rssarticles', 'assessed', 'assesstimestart', 'assesstimefinish',
-            'scale', 'timecreated', 'timemodified'));
+            'scale', 'timecreated', 'timemodified', 'completionentries'));
 
         $entries = new backup_nested_element('entries');
 
old mode 100644 (file)
new mode 100755 (executable)
index b7f0aba..e0502e8
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="mod/glossary/db" VERSION="20100428" COMMENT="XMLDB file for Moodle mod/glossary"
+<XMLDB PATH="mod/glossary/db" VERSION="20101115" COMMENT="XMLDB file for Moodle mod/glossary"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
 >
@@ -31,7 +31,8 @@
         <FIELD NAME="assesstimefinish" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="assesstimestart" NEXT="scale"/>
         <FIELD NAME="scale" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="assesstimefinish" NEXT="timecreated"/>
         <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="scale" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated" NEXT="completionentries"/>
+        <FIELD NAME="completionentries" TYPE="int" LENGTH="9" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="Non zero if a certain number of entries are required to mark this glossary complete for a user." PREVIOUS="timemodified"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
index 7d155b6..f6507cf 100644 (file)
@@ -309,6 +309,22 @@ function xmldb_glossary_upgrade($oldversion) {
 
         upgrade_mod_savepoint(true, 2010111500, 'glossary');
     }
+
+    if ($oldversion < 2010111501) {
+
+        // Define field completionentries to be added to glossary
+        $table = new xmldb_table('glossary');
+        $field = new xmldb_field('completionentries', XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
+
+        // Conditionally launch add field completionentries
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        // glossary savepoint reached
+        upgrade_mod_savepoint(true, 2010111501, 'glossary');
+    }
+
     return true;
 }
 
index dbaccf4..fe1b709 100644 (file)
@@ -96,6 +96,12 @@ if ($confirm and confirm_sesskey()) { // the operation was confirmed.
         $DB->delete_records("glossary_alias", array("entryid"=>$entry->id));
         $DB->delete_records("glossary_entries", array("id"=>$entry->id));
 
+        // Update completion state
+        $completion = new completion_info($course);
+        if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
+            $completion->update_state($cm, COMPLETION_INCOMPLETE, $entry->userid);
+        }
+
         //delete glossary entry ratings
         require_once($CFG->dirroot.'/rating/lib.php');
         $delopt = new stdclass();
index c59d6c7..d69ae54 100644 (file)
@@ -119,6 +119,13 @@ if ($mform->is_cancelled()){
     if (empty($entry->id)) {
         //new entry
         $entry->id = $DB->insert_record('glossary_entries', $entry);
+
+        // Update completion state
+        $completion = new completion_info($course);
+        if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) {
+            $completion->update_state($cm, COMPLETION_COMPLETE);
+        }
+
         add_to_log($course->id, "glossary", "add entry",
                    "view.php?id=$cm->id&amp;mode=entry&amp;hook=$entry->id", $entry->id, $cm->id);
 
index f060839..278ecc1 100644 (file)
@@ -79,6 +79,8 @@ $string['commentdeleted'] = 'The comment has been deleted.';
 $string['comments'] = 'Comments';
 $string['commentson'] = 'Comments on';
 $string['commentupdated'] = 'The comment has been updated.';
+$string['completionentries'] = 'Student must create entries:';
+$string['completionentriesgroup'] = 'Require entries';
 $string['concept'] = 'Concept';
 $string['concepts'] = 'Concepts';
 $string['configenablerssfeeds'] = 'This switch will enable the possibility of RSS feeds for all glossaries.  You will still need to turn feeds on manually in the settings for each glossary.';
index a0e1371..0f58ef0 100644 (file)
@@ -24,6 +24,7 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 require_once($CFG->dirroot . '/rating/lib.php');
+require_once($CFG->libdir . '/completionlib.php');
 
 define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
 define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
@@ -2537,6 +2538,7 @@ function glossary_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 true;
         case FEATURE_GRADE_OUTCOMES:          return true;
         case FEATURE_RATE:                    return true;
@@ -2546,6 +2548,42 @@ function glossary_supports($feature) {
     }
 }
 
+/**
+ * Obtains the automatic completion state for this glossary based on any conditions
+ * in glossary settings.
+ *
+ * @global object
+ * @global object
+ * @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. (If no conditions, then return
+ *   value depends on comparison type)
+ */
+function glossary_get_completion_state($course,$cm,$userid,$type) {
+    global $CFG, $DB;
+
+    // Get glossary details
+    if (!($glossary=$DB->get_record('glossary',array('id'=>$cm->instance)))) {
+        throw new Exception("Can't find glossary {$cm->instance}");
+    }
+
+    $result=$type; // Default return value
+
+    if ($glossary->completionentries) {
+        $value = $glossary->completionentries <=
+                 $DB->count_records('glossary_entries',array('glossaryid'=>$glossary->id, 'userid'=>$userid, 'approved'=>1));
+        if ($type == COMPLETION_AND) {
+            $result = $result && $value;
+        } else {
+            $result = $result || $value;
+        }
+    }
+
+    return $result;
+}
+
 function glossary_extend_navigation($navigation, $course, $module, $cm) {
     global $CFG;
     $navigation->add(get_string('standardview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id, 'mode'=>'letter')));
index fe71488..f5c21bd 100644 (file)
@@ -156,6 +156,45 @@ class mod_glossary_mod_form extends moodleform_mod {
 
     function data_preprocessing(&$default_values){
         parent::data_preprocessing($default_values);
+
+        // Set up the completion checkboxes which aren't part of standard data.
+        // We also make the default value (if you turn on the checkbox) for those
+        // numbers to be 1, this will not apply unless checkbox is ticked.
+        $default_values['completionentriesenabled']=
+            !empty($default_values['completionentries']) ? 1 : 0;
+        if (empty($default_values['completionentries'])) {
+            $default_values['completionentries']=1;
+        }
+    }
+
+    function add_completion_rules() {
+        $mform =& $this->_form;
+
+        $group=array();
+        $group[] =& $mform->createElement('checkbox', 'completionentriesenabled', '', get_string('completionentries','glossary'));
+        $group[] =& $mform->createElement('text', 'completionentries', '', array('size'=>3));
+        $mform->setType('completionentries', PARAM_INT);
+        $mform->addGroup($group, 'completionentriesgroup', get_string('completionentriesgroup','glossary'), array(' '), false);
+        $mform->disabledIf('completionentries','completionentriesenabled','notchecked');
+
+        return array('completionentriesgroup');
+    }
+
+    function completion_rule_enabled($data) {
+        return (!empty($data['completionentriesenabled']) && $data['completionentries']!=0);
+    }
+
+    function get_data() {
+        $data = parent::get_data();
+        if (!$data) {
+            return false;
+        }
+        // Turn off completion settings if the checkboxes aren't ticked
+        $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
+        if (empty($data->completionentriesenabled) || !$autocompletion) {
+            $data->completionentries = 0;
+        }
+        return $data;
     }
 
 }
index 440ac75..5f7050f 100644 (file)
@@ -5,7 +5,7 @@
 ///  This fragment is called by moodle_needs_upgrading() and /admin/index.php
 /////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2010111500;
+$module->version  = 2010111501;
 $module->requires = 2010080300;  // Requires this Moodle version
 $module->cron     = 0;           // Period for cron to check this module (secs)