]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16234 defaults support in plugin_supports()
authorskodak <skodak>
Wed, 27 Aug 2008 20:22:33 +0000 (20:22 +0000)
committerskodak <skodak>
Wed, 27 Aug 2008 20:22:33 +0000 (20:22 +0000)
course/moodleform_mod.php
lib/moodlelib.php

index eedc6d67b825468414c4893da43c6a91438d7171..03f7f84b19599ba46867983375e2bc997ed13c44 100644 (file)
@@ -1,5 +1,7 @@
-<?php
+<?php  //$Id$
 require_once ($CFG->libdir.'/formslib.php');
+require_once($CFG->libdir.'/completionlib.php');
+
 /**
  * This class adds extra methods to form wrapper specific to be used for module
  * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod_form.php
@@ -124,26 +126,26 @@ class moodleform_mod extends moodleform {
         }
 
         // Completion: If necessary, freeze fields
-        $completion=new completion_info($COURSE);
-        if($completion->is_enabled()) {
+        $completion = new completion_info($COURSE);
+        if ($completion->is_enabled()) {
             // If anybody has completed the activity, these options will be 'locked'
             $completedcount = empty($this->_cm)
                 ? 0
                 : $completion->count_user_data($this->_cm);
 
-            $freeze=false;
-            if(!$completedcount) {
-                if($mform->elementExists('unlockcompletion')) {
+            $freeze = false;
+            if (!$completedcount) {
+                if ($mform->elementExists('unlockcompletion')) {
                     $mform->removeElement('unlockcompletion');
                 }
             } else {
                 // Has the element been unlocked?
-                if($mform->exportValue('unlockcompletion')) {
+                if ($mform->exportValue('unlockcompletion')) {
                     // Yes, add in warning text and set the hidden variable
                     $mform->insertElementBefore(
-                        $mform->createElement('static','completedunlocked',
-                            get_string('completedunlocked','completion'),
-                            get_string('completedunlockedtext','completion')),
+                        $mform->createElement('static', 'completedunlocked',
+                            get_string('completedunlocked', 'completion'),
+                            get_string('completedunlockedtext', 'completion')),
                         'unlockcompletion');
                     $mform->removeElement('unlockcompletion');
                     $mform->getElement('completionunlocked')->setValue(1);
@@ -151,22 +153,22 @@ class moodleform_mod extends moodleform {
                     // No, add in the warning text with the count (now we know
                     // it) before the unlock button
                     $mform->insertElementBefore(
-                        $mform->createElement('static','completedwarning',
-                            get_string('completedwarning','completion'),
-                            get_string('completedwarningtext','completion',$completedcount)),
+                        $mform->createElement('static', 'completedwarning',
+                            get_string('completedwarning', 'completion'),
+                            get_string('completedwarningtext', 'completion', $completedcount)),
                         'unlockcompletion');
                     $mform->setHelpButton('completedwarning', array('completionlocked', get_string('help_completionlocked', 'completion'), 'completion'));
                             
-                    $freeze=true;
+                    $freeze = true;
                 }
             } 
 
-            if($freeze) {
+            if ($freeze) {
                 $mform->freeze('completion');
-                if($mform->elementExists('completionview')) {
+                if ($mform->elementExists('completionview')) {
                     $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
                 }
-                if($mform->elementExists('completionusegrade')) {
+                if ($mform->elementExists('completionusegrade')) {
                     $mform->freeze('completionusegrade');
                 }
                 $mform->freeze($this->_customcompletionelements);
@@ -207,10 +209,10 @@ class moodleform_mod extends moodleform {
         
         // Completion: Don't let them choose automatic completion without turning
         // on some conditions
-        if(array_key_exists('completion',$data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
-            if(empty($data['completionview']) && empty($data['completionusegrade']) &&
+        if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
+            if (empty($data['completionview']) && empty($data['completionusegrade']) &&
                 !$this->completion_rule_enabled($data)) {
-                $errors['completion']=get_string('badautocompletion','completion');
+                $errors['completion'] = get_string('badautocompletion', 'completion');
             }
         }
 
@@ -238,14 +240,14 @@ class moodleform_mod extends moodleform {
      * @param mixed $features array or object describing supported features - groups, groupings, groupmembersonly, etc.
      * @param string $modname Name of module e.g. 'label'
      */
-    function standard_coursemodule_elements($features=null,$modname=null){
+    function standard_coursemodule_elements($features=null, $modname=null){
         global $COURSE, $CFG, $DB;
         $mform =& $this->_form;
 
         // Guess module name if not supplied
-        if(!$modname) {
+        if (!$modname) {
             $matches=array();
-            if(!preg_match('/^mod_([^_]+)_mod_form$/',$this->_formname,$matches)) {
+            if (!preg_match('/^mod_([^_]+)_mod_form$/', $this->_formname, $matches)) {
                 debugging('Use $modname parameter or rename form to mod_xx_mod_form, where xx is name of your module');
                 error('Unknown module name for form');
             }
@@ -292,7 +294,7 @@ class moodleform_mod extends moodleform {
             $this->_features->idnumber = true;
         }
         
-        if(!isset($this->_features->defaultcompletion)) {
+        if (!isset($this->_features->defaultcompletion)) {
             $this->_features->defaultcompletion = true;
         }
 
@@ -349,64 +351,63 @@ class moodleform_mod extends moodleform {
         }
 
         // Conditional activities: completion tracking section 
-        require_once($CFG->libdir.'/completionlib.php');
-        $completion=new completion_info($COURSE);
-        if($completion->is_enabled()) {
+        $completion = new completion_info($COURSE);
+        if ($completion->is_enabled()) {
             $mform->addElement('header', '', get_string('activitycompletion', 'completion'));
 
             // Unlock button for if people have completed it (will
             // be removed in definition_after_data if they haven't)
-            $mform->addElement('submit','unlockcompletion',get_string('unlockcompletion','completion'));
+            $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
             $mform->registerNoSubmitButton('unlockcompletion');
-            $mform->addElement('hidden','completionunlocked',0);
+            $mform->addElement('hidden', 'completionunlocked', 0);
             
-            $mform->addElement('select', 'completion', get_string('completion','completion'), 
-                array(COMPLETION_TRACKING_NONE=>get_string('completion_none','completion'), 
-                COMPLETION_TRACKING_MANUAL=>get_string('completion_manual','completion')));
+            $mform->addElement('select', 'completion', get_string('completion', 'completion'), 
+                array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'), 
+                COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
             $mform->setHelpButton('completion', array('completion', get_string('help_completion', 'completion'), 'completion'));
-            $mform->setDefault('completion',$this->_features->defaultcompletion
+            $mform->setDefault('completion', $this->_features->defaultcompletion
                 ? COMPLETION_TRACKING_MANUAL
                 : COMPLETION_TRACKING_NONE);
 
             // Automatic completion once you view it
-            $gotcompletionoptions=false;
-            if(plugin_supports('mod',$modname,FEATURE_COMPLETION_TRACKS_VIEWS)) {
-                $mform->addElement('checkbox', 'completionview', get_string('completionview','completion'),
-                    get_string('completionview_text','completion'));
+            $gotcompletionoptions = false;
+            if (plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
+                $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
+                    get_string('completionview_text', 'completion'));
                 $mform->setHelpButton('completionview', array('completionview', get_string('help_completionview', 'completion'), 'completion'));
-                $mform->disabledIf('completionview','completion','ne',COMPLETION_TRACKING_AUTOMATIC);
-                $gotcompletionoptions=true;
+                $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
+                $gotcompletionoptions = true;
             }
 
             // Automatic completion once it's graded
-            if(plugin_supports('mod',$modname,FEATURE_GRADE_HAS_GRADE)) {
-                $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade','completion'),
-                    get_string('completionusegrade_text','completion'));
+            if (plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
+                $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
+                    get_string('completionusegrade_text', 'completion'));
                 $mform->setHelpButton('completionusegrade', array('completionusegrade', get_string('help_completionusegrade', 'completion'), 'completion'));
-                $mform->disabledIf('completionusegrade','completion','ne',COMPLETION_TRACKING_AUTOMATIC);
-                $gotcompletionoptions=true;
+                $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
+                $gotcompletionoptions = true;
             }
 
             // Automatic completion according to module-specific rules
             $this->_customcompletionelements = $this->add_completion_rules();
-            foreach($this->_customcompletionelements as $element) {
-                $mform->disabledIf($element,'completion','ne',COMPLETION_TRACKING_AUTOMATIC);                
+            foreach ($this->_customcompletionelements as $element) {
+                $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);                
             }
 
             $gotcompletionoptions = $gotcompletionoptions ||
                 count($this->_customcompletionelements)>0;
 
             // Automatic option only appears if possible
-            if($gotcompletionoptions) {
+            if ($gotcompletionoptions) {
                 $mform->getElement('completion')->addOption(
-                    get_string('completion_automatic','completion'),
+                    get_string('completion_automatic', 'completion'),
                     COMPLETION_TRACKING_AUTOMATIC);
             } 
 
             // Completion expected at particular date? (For progress tracking)
-            $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected','completion'), array('optional'=>true));
+            $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
             $mform->setHelpButton('completionexpected', array('completionexpected', get_string('help_completionexpected', 'completion'), 'completion'));
-            $mform->disabledIf('completionexpected','completion','eq',COMPLETION_TRACKING_NONE);    
+            $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);    
         }
 
         $this->standard_hidden_coursemodule_elements();
index b8ac39df34af615f09bd2383e37b52249e42eb5b..4a9fe3a27198812a45163dee9646e30a72618761 100644 (file)
@@ -286,11 +286,11 @@ define ('PASSWORD_NONALPHANUM', '.,;:!?_-+/*@#&$');
 // Used for plugin_supports() to report features that are, or are not, supported by a module.
 
 /** True if module can provide a grade */
-define('FEATURE_GRADE_HAS_GRADE','grade_has_grade');
+define('FEATURE_GRADE_HAS_GRADE', 'grade_has_grade');
 /** True if module has code to track whether somebody viewed it */
-define('FEATURE_COMPLETION_TRACKS_VIEWS','completion_tracks_views');
+define('FEATURE_COMPLETION_TRACKS_VIEWS', 'completion_tracks_views');
 /** True if module has custom completion rules */
-define('FEATURE_COMPLETION_HAS_RULES','completion_has_rules');
+define('FEATURE_COMPLETION_HAS_RULES', 'completion_has_rules');
 
 
 
@@ -6100,31 +6100,41 @@ function get_list_of_plugins($plugin='mod', $exclude='', $basedir='') {
  * @param string $type Plugin type e.g. 'mod'
  * @param string $name Plugin name
  * @param string $feature Feature code (FEATURE_xx constant)
- * @return Feature result (false if not supported, null if feature is unknown 
- *   [=not supported, usually]; otherwise usually true but may have
- *   other feature-specific value)
+ * @param mixed $default default value if feature support unknown
+ * @return mixed Feature result (false if not supported, null if feature is unknown,
+ *         otherwise usually true but may have other feature-specific value)
  */
-function plugin_supports($type,$name,$feature) {
+function plugin_supports($type, $name, $feature, $default=null) {
     global $CFG;
+
+    $name = clean_param($name, PARAM_SAFEDIR);
+
     switch($type) {
         case 'mod' :
-            $file=$CFG->dirroot.'/mod/'.$name.'/lib.php';
-            $function=$name.'_supports';
+            $file = $CFG->dirroot.'/mod/'.$name.'/lib.php';
+            $function = $name.'_supports';
             break;
         default:
             throw new Exception('Unsupported plugin type ('.$type.')');
     }
 
     // Load library and look for function
-    require_once($file);
-    if(function_exists($function)) {
+    if (file_exists($file)) {
+        require_once($file);
+    }
+    if (function_exists($function)) {
         // Function exists, so just return function result
-        return $function($feature);
+        $supports = $function($feature);
+        if (is_null($supports)) {
+            return $default;
+        } else {
+            return $supports;
+        }
     } else {
         switch($feature) {
             // If some features can also be checked in other ways
             // for legacy support, this could be added here
-            default: return null;
+            default: return $default;
         }
     }
 }