MDL-20131 Glossary: Global glossary can be added in Random glossary entry block.
authorRajesh Taneja <rajesh@moodle.com>
Fri, 1 Feb 2013 09:02:39 +0000 (17:02 +0800)
committerRajesh Taneja <rajesh@moodle.com>
Mon, 11 Feb 2013 03:55:26 +0000 (11:55 +0800)
blocks/glossary_random/block_glossary_random.php
blocks/glossary_random/edit_form.php
blocks/glossary_random/version.php

index 884c909..c0f1a7a 100644 (file)
@@ -43,8 +43,18 @@ class block_glossary_random extends block_base {
                 $this->instance_config_commit();
             }
 
+            // Get glossary instance, if not found then return without error, as this will be handled in get_content.
+            if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
+                return false;
+            }
+
+            $this->config->globalglossary = $glossary->globalglossary;
+
+            // Save course id in config, so we can get correct course module.
+            $this->config->courseid = $glossary->course;
+
             // Get module and context, to be able to rewrite urls
-            if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
+            if (! $cm = get_coursemodule_from_instance('glossary', $glossary->id, $this->config->courseid)) {
                 return false;
             }
             $glossaryctx = context_module::instance($cm->id);
@@ -144,13 +154,22 @@ class block_glossary_random extends block_base {
         }
 
         require_once($CFG->dirroot.'/course/lib.php');
-        $course = $this->page->course;
-        $modinfo = get_fast_modinfo($course);
-        $glossaryid = $this->config->glossary;
 
-        if (!isset($modinfo->instances['glossary'][$glossaryid])) {
-            // we can get here if the glossary has been deleted, so
-            // unconfigure the glossary from the block..
+        // If $this->config->globalglossary is not set then get glossary info from db.
+        if (!isset($this->config->globalglossary)) {
+            if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
+                return '';
+            } else {
+                $this->config->courseid = $glossary->course;
+                $this->config->globalglossary = $glossary->globalglossary;
+                $this->instance_config_commit();
+            }
+        }
+
+        $modinfo = get_fast_modinfo($this->config->courseid);
+        // If deleted glossary or non-global glossary on different course page, then reset.
+        if (!isset($modinfo->instances['glossary'][$this->config->glossary])
+                || ((empty($this->config->globalglossary) && ($this->config->courseid != $this->page->course)))) {
             $this->config->glossary = 0;
             $this->config->cache = '';
             $this->instance_config_commit();
@@ -161,8 +180,7 @@ class block_glossary_random extends block_base {
             return $this->content;
         }
 
-        $cm = $modinfo->instances['glossary'][$glossaryid];
-
+        $cm = $modinfo->instances['glossary'][$this->config->glossary];
         if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
             return '';
         }
@@ -176,12 +194,10 @@ class block_glossary_random extends block_base {
         }
 
         $this->content = new stdClass();
-        $this->content->text = $this->config->cache;
-
-        // place link to glossary in the footer if the glossary is visible
 
-        //Obtain the visible property from the instance
-        if ($cm->uservisible) {
+        // Show glossary if visible and place links in footer.
+        if ($cm->visible) {
+            $this->content->text = $this->config->cache;
             if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
                 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
                 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
@@ -192,7 +208,7 @@ class block_glossary_random extends block_base {
             $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
                 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
 
-        // otherwise just place some text, no link
+        // Otherwise just place some text, no link.
         } else {
             $this->content->footer = $this->config->invisible;
         }
index d9c613c..24b2df1 100644 (file)
@@ -41,7 +41,7 @@ class block_glossary_random_edit_form extends block_edit_form {
         $mform->setType('config_title', PARAM_TEXT);
 
         // Select glossaries to put in dropdown box ...
-        $glossaries = $DB->get_records_menu('glossary', array('course' => $this->block->course->id), 'name', 'id,name');
+        $glossaries = $DB->get_records_select_menu('glossary', 'course = ? OR globalglossary = ?', array($this->block->course->id, 1), 'name', 'id,name');
         foreach($glossaries as $key => $value) {
             $glossaries[$key] = strip_tags(format_string($value, true));
         }
index a5d21fa..67280cc 100644 (file)
@@ -25,6 +25,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2012112902;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2013020400;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2012112900;        // Requires this Moodle version
 $plugin->component = 'block_glossary_random'; // Full name of the plugin (used for diagnostics)