Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/glossary_random/block_glossary_random.php
Go to the documentation of this file.
00001 <?php
00002 
00003 define('BGR_RANDOMLY',     '0');
00004 define('BGR_LASTMODIFIED', '1');
00005 define('BGR_NEXTONE',      '2');
00006 
00007 class block_glossary_random extends block_base {
00008     function init() {
00009         $this->title = get_string('pluginname','block_glossary_random');
00010     }
00011 
00012     function specialization() {
00013         global $CFG, $DB;
00014 
00015         require_once($CFG->libdir . '/filelib.php');
00016 
00017         $this->course = $this->page->course;
00018 
00019         // load userdefined title and make sure it's never empty
00020         if (empty($this->config->title)) {
00021             $this->title = get_string('pluginname','block_glossary_random');
00022         } else {
00023             $this->title = $this->config->title;
00024         }
00025 
00026         if (empty($this->config->glossary)) {
00027             return false;
00028         }
00029 
00030         if (!isset($this->config->nexttime)) {
00031             $this->config->nexttime = 0;
00032         }
00033 
00034         //check if it's time to put a new entry in cache
00035         if (time() > $this->config->nexttime) {
00036 
00037             // place glossary concept and definition in $pref->cache
00038             if (!$numberofentries = $DB->count_records('glossary_entries',
00039                                                        array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
00040                 $this->config->cache = get_string('noentriesyet','block_glossary_random');
00041                 $this->instance_config_commit();
00042             }
00043 
00044             // Get module and context, to be able to rewrite urls
00045             if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
00046                 return false;
00047             }
00048             $glossaryctx = get_context_instance(CONTEXT_MODULE, $cm->id);
00049 
00050             $limitfrom = 0;
00051             $limitnum = 1;
00052 
00053             switch ($this->config->type) {
00054 
00055                 case BGR_RANDOMLY:
00056                     $i = rand(1,$numberofentries);
00057                     $limitfrom = $i-1;
00058                     $SORT = 'ASC';
00059                     break;
00060 
00061                 case BGR_NEXTONE:
00062                     if (isset($this->config->previous)) {
00063                         $i = $this->config->previous + 1;
00064                     } else {
00065                         $i = 1;
00066                     }
00067                     if ($i > $numberofentries) {  // Loop back to beginning
00068                         $i = 1;
00069                     }
00070                     $limitfrom = $i-1;
00071                     $SORT = 'ASC';
00072                     break;
00073 
00074                 default:  // BGR_LASTMODIFIED
00075                     $i = $numberofentries;
00076                     $limitfrom = 0;
00077                     $SORT = 'DESC';
00078                     break;
00079             }
00080 
00081             if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
00082                                                  FROM {glossary_entries}
00083                                                 WHERE glossaryid = ? AND approved = 1
00084                                              ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
00085 
00086                 $entry = reset($entry);
00087 
00088                 if (empty($this->config->showconcept)) {
00089                     $text = '';
00090                 } else {
00091                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
00092                 }
00093 
00094                 $options = new stdClass();
00095                 $options->trusted = $entry->definitiontrust;
00096                 $options->overflowdiv = true;
00097                 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
00098                 $text .= format_text($entry->definition, $entry->definitionformat, $options);
00099 
00100                 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
00101                 $this->config->previous = $i;
00102 
00103             } else {
00104                 $text = get_string('noentriesyet','block_glossary_random');
00105             }
00106             // store the text
00107             $this->config->cache = $text;
00108             $this->instance_config_commit();
00109         }
00110     }
00111 
00112     function instance_allow_multiple() {
00113     // Are you going to allow multiple instances of each block?
00114     // If yes, then it is assumed that the block WILL USE per-instance configuration
00115         return true;
00116     }
00117 
00118     function get_content() {
00119         global $USER, $CFG, $DB;
00120 
00121         if (empty($this->config->glossary)) {
00122             $this->content = new stdClass();
00123             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
00124             $this->content->footer = '';
00125             return $this->content;
00126         }
00127 
00128         $glossaryid = $this->config->glossary;
00129 
00130         $course = $this->page->course;
00131 
00132         require_once($CFG->dirroot.'/course/lib.php');
00133         $modinfo = get_fast_modinfo($course);
00134 
00135         if (!isset($modinfo->instances['glossary'][$glossaryid])) {
00136             // we can get here if the glossary has been deleted, so
00137             // unconfigure the glossary from the block..
00138             $this->config->glossary = 0;
00139             $this->config->cache = '';
00140             $this->instance_config_commit();
00141 
00142             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
00143             $this->content->footer = '';
00144             return $this->content;
00145         }
00146 
00147         $cm = $modinfo->instances['glossary'][$glossaryid];
00148 
00149         if (empty($this->config->cache)) {
00150             $this->config->cache = '';
00151         }
00152 
00153         if ($this->content !== NULL) {
00154             return $this->content;
00155         }
00156 
00157         $this->content = new stdClass();
00158         $this->content->text = $this->config->cache;
00159 
00160         // place link to glossary in the footer if the glossary is visible
00161 
00162         //Obtain the visible property from the instance
00163         if ($cm->uservisible) {
00164             if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
00165                 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
00166                 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
00167             } else {
00168                 $this->content->footer = '';
00169             }
00170 
00171             $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
00172                 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
00173 
00174         // otherwise just place some text, no link
00175         } else {
00176             $this->content->footer = $this->config->invisible;
00177         }
00178 
00179         return $this->content;
00180     }
00181 
00182     function hide_header() {
00183         if (empty($this->config->title)) {
00184             return true;
00185         }
00186         return false;
00187     }
00188 }
00189 
 All Data Structures Namespaces Files Functions Variables Enumerations