MDL-25072 Stop abusing $CFG->ajaxcapable and $CFG->ajaxtestedbrowsers
authorDavid Mudrak <david.mudrak@gmail.com>
Tue, 9 Nov 2010 11:54:58 +0000 (11:54 +0000)
committerDavid Mudrak <david.mudrak@gmail.com>
Tue, 9 Nov 2010 11:54:58 +0000 (11:54 +0000)
course/format/topics/ajax.php [deleted file]
course/format/topics/lib.php
course/format/weeks/ajax.php [deleted file]
course/format/weeks/lib.php
course/lib.php
course/view.php

diff --git a/course/format/topics/ajax.php b/course/format/topics/ajax.php
deleted file mode 100644 (file)
index af7a527..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-/**
- * This file is required if the course format is to support AJAX.
- */
-
-
-$CFG->ajaxcapable = true;
-$CFG->ajaxtestedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari'=>531, 'Chrome'=>6.0);
index 418522c..c80b518 100644 (file)
@@ -76,4 +76,17 @@ function callback_topics_get_section_name($course, $section) {
     } else {
         return get_string('topic').' '.$section->section;
     }
-}
\ No newline at end of file
+}
+
+/**
+ * Declares support for course AJAX features
+ *
+ * @see course_format_ajax_support()
+ * @return stdClass
+ */
+function callback_topics_ajax_support() {
+    $ajaxsupport = new stdClass();
+    $ajaxsupport->capable = true;
+    $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
+    return $ajaxsupport;
+}
diff --git a/course/format/weeks/ajax.php b/course/format/weeks/ajax.php
deleted file mode 100644 (file)
index af7a527..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-/**
- * This file is required if the course format is to support AJAX.
- */
-
-
-$CFG->ajaxcapable = true;
-$CFG->ajaxtestedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari'=>531, 'Chrome'=>6.0);
index ba4462e..38aa940 100644 (file)
@@ -99,3 +99,16 @@ function callback_weeks_get_section_name($course, $section) {
         return $weekday.' - '.$endweekday;
     }
 }
+
+/**
+ * Declares support for course AJAX features
+ *
+ * @see course_format_ajax_support()
+ * @return stdClass
+ */
+function callback_weeks_ajax_support() {
+    $ajaxsupport = new stdClass();
+    $ajaxsupport->capable = true;
+    $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
+    return $ajaxsupport;
+}
index 428492f..a32bde4 100644 (file)
@@ -3396,6 +3396,43 @@ function course_format_uses_sections($format) {
     return false;
 }
 
+/**
+ * Returns the information about the ajax support in the given source format
+ *
+ * The returned object's property (boolean)capable indicates that
+ * the course format supports Moodle course ajax features.
+ * The property (array)testedbrowsers can be used as a parameter for {@see ajaxenabled()}.
+ *
+ * @param string $format
+ * @return stdClass
+ */
+function course_format_ajax_support($format) {
+    global $CFG;
+
+    // set up default values
+    $ajaxsupport = new stdClass();
+    $ajaxsupport->capable = false;
+    $ajaxsupport->testedbrowsers = array();
+
+    // get the information from the course format library
+    $featurefile = $CFG->dirroot.'/course/format/'.$format.'/lib.php';
+    $featurefunction = 'callback_'.$format.'_ajax_support';
+    if (!function_exists($featurefunction) && file_exists($featurefile)) {
+        require_once $featurefile;
+    }
+    if (function_exists($featurefunction)) {
+        $formatsupport = $featurefunction();
+        if (isset($formatsupport->capable)) {
+            $ajaxsupport->capable = $formatsupport->capable;
+        }
+        if (is_array($formatsupport->testedbrowsers)) {
+            $ajaxsupport->testedbrowsers = $formatsupport->testedbrowsers;
+        }
+    }
+
+    return $ajaxsupport;
+}
+
 /**
  * Can the current user delete this course?
  * Course creators have exception,
index 25bebe5..6862477 100644 (file)
         redirect($CFG->wwwroot .'/');
     }
 
-
     // AJAX-capable course format?
     $useajax = false;
-    $ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
-    $bodytags = '';
-
-    if (empty($CFG->disablecourseajax) and file_exists($ajaxformatfile) and !$PAGE->theme->disablecourseajax) {      // Needs to exist otherwise no AJAX by default
-
-        // TODO: stop abusing CFG global here
-        $CFG->ajaxcapable = false;           // May be overridden later by ajaxformatfile
-        $CFG->ajaxtestedbrowsers = array();  // May be overridden later by ajaxformatfile
-
-        require_once($ajaxformatfile);
-
-        if (!empty($USER->editing) && $CFG->ajaxcapable && has_capability('moodle/course:manageactivities', $context)) {
-                                                             // Course-based switches
-
-            if (ajaxenabled($CFG->ajaxtestedbrowsers)) {     // Browser, user and site-based switches
-                $PAGE->requires->yui2_lib('dragdrop');
-                $PAGE->requires->yui2_lib('connection');
-                $PAGE->requires->yui2_lib('selector');
-                $PAGE->requires->js('/lib/ajax/block_classes.js', true);
-                $PAGE->requires->js('/lib/ajax/section_classes.js', true);
-
-                // Okay, global variable alert. VERY UGLY. We need to create
-                // this object here before the <blockname>_print_block()
-                // function is called, since that function needs to set some
-                // stuff in the javascriptportal object.
-                $COURSE->javascriptportal = new jsportal();
-                $useajax = true;
-            }
-        }
+    $formatajax = course_format_ajax_support($course->format);
+
+    if (empty($CFG->disablecourseajax)
+            and $formatajax->capable
+            and !empty($USER->editing)
+            and ajaxenabled($formatajax->testedbrowsers)
+            and $PAGE->theme->disablecourseajax
+            and has_capability('moodle/course:manageactivities', $context)) {
+        $PAGE->requires->yui2_lib('dragdrop');
+        $PAGE->requires->yui2_lib('connection');
+        $PAGE->requires->yui2_lib('selector');
+        $PAGE->requires->js('/lib/ajax/block_classes.js', true);
+        $PAGE->requires->js('/lib/ajax/section_classes.js', true);
+
+        // Okay, global variable alert. VERY UGLY. We need to create
+        // this object here before the <blockname>_print_block()
+        // function is called, since that function needs to set some
+        // stuff in the javascriptportal object.
+        $COURSE->javascriptportal = new jsportal();
+        $useajax = true;
     }
 
     $CFG->blocksdrag = $useajax;   // this will add a new class to the header so we can style differently