MDL-40121 Support old activities with subtypes
authorMarina Glancy <marina@moodle.com>
Mon, 15 Jul 2013 11:15:58 +0000 (21:15 +1000)
committerMarina Glancy <marina@moodle.com>
Mon, 15 Jul 2013 11:42:04 +0000 (21:42 +1000)
course/format/singleactivity/lib.php
course/format/singleactivity/renderer.php

index 957519c..02795d1 100644 (file)
@@ -291,6 +291,47 @@ class format_singleactivity extends format_base {
         return $this->activity;
     }
 
+    /**
+     * Checks if the current user can add the activity of the specified type to this course.
+     *
+     * @return bool
+     */
+    protected function can_add_activity() {
+        global $CFG;
+        if (!($modname = $this->get_activitytype())) {
+            return false;
+        }
+        if (!has_capability('moodle/course:manageactivities', context_course::instance($this->courseid))) {
+            return false;
+        }
+        if (!course_allowed_module($this->get_course(), $modname)) {
+            return false;
+        }
+        $libfile = "$CFG->dirroot/mod/$modname/lib.php";
+        if (!file_exists($libfile)) {
+            return null;
+        }
+        return true;
+    }
+
+    /**
+     * Checks if the activity type requires subtypes.
+     *
+     * @return bool|null (null if the check is not possible)
+     */
+    public function activity_has_subtypes() {
+        global $CFG;
+        if (!($modname = $this->get_activitytype())) {
+            return null;
+        }
+        $libfile = "$CFG->dirroot/mod/$modname/lib.php";
+        if (!file_exists($libfile)) {
+            return null;
+        }
+        include_once($libfile);
+        return function_exists($modname. '_get_types');
+    }
+
     /**
      * Allows course format to execute code on moodle_page::set_course()
      *
@@ -335,10 +376,20 @@ class format_singleactivity extends format_base {
                 }
             }
             if ($cm === null) {
-                if (has_capability('moodle/course:manageactivities', context_course::instance($this->courseid))) {
-                    // Teacher is redirected to create a new activity.
-                    $url = new moodle_url('/course/modedit.php',
-                            array('course' => $this->courseid, 'section' => 0, 'add' => $this->get_activitytype()));
+                if ($this->can_add_activity()) {
+                    // This is a user who has capability to create an activity.
+                    if ($this->activity_has_subtypes()) {
+                        // Activity that requires subtype can not be added automatically.
+                        if (optional_param('addactivity', 0, PARAM_INT)) {
+                            return;
+                        } else {
+                            $url = new moodle_url('/course/view.php', array('id' => $this->courseid, 'addactivity' => 1));
+                            redirect($url);
+                        }
+                    }
+                    // Redirect to the add activity form.
+                    $url = new moodle_url('/course/mod.php', array('id' => $this->courseid,
+                        'section' => 0, 'sesskey' => sesskey(), 'add' => $this->get_activitytype()));
                     redirect($url);
                 } else {
                     // Student views an empty course page.
index 31aa866..e9961a0 100644 (file)
@@ -43,18 +43,21 @@ class format_singleactivity_renderer extends plugin_renderer_base {
      */
     public function display($course, $orphaned) {
         $courserenderer = $this->page->get_renderer('core', 'course');
+        $output = '';
+        $modinfo = get_fast_modinfo($course);
         if ($orphaned) {
-            $modinfo = get_fast_modinfo($course);
-
-            $output = '';
             if (!empty($modinfo->sections[1])) {
                 $output .= $this->output->heading(get_string('orphaned', 'format_singleactivity'), 3, 'sectionname');
                 $output .= $this->output->box(get_string('orphanedwarning', 'format_singleactivity'));
                 $output .= $courserenderer->course_section_cm_list($course, 1, 1);
             }
-            return $output;
         } else {
-            return $courserenderer->course_section_cm_list($course, 0, 0);
+            $output .= $courserenderer->course_section_cm_list($course, 0, 0);
+            if (empty($modinfo->sections[0]) && course_get_format($course)->activity_has_subtypes()) {
+                // Course format was unable to automatically redirect to add module page.
+                $output .= $courserenderer->course_section_add_cm_control($course, 0, 0);
+            }
         }
+        return $output;
     }
 }