moodle/blocks/glossary_random/block_glossary_random.php

195 lines
7.0 KiB
PHP
Raw Normal View History

<?php // $Id$
define('BGR_RANDOMLY', '0');
define('BGR_LASTMODIFIED', '1');
define('BGR_NEXTONE', '2');
2005-02-20 07:23:10 +00:00
class block_glossary_random extends block_base {
function init() {
$this->title = get_string('blockname','block_glossary_random');
2005-02-20 07:23:10 +00:00
$this->version = 2005010300;
}
2005-02-20 07:23:10 +00:00
function specialization() {
global $CFG;
$this->course = get_record('course', 'id', $this->instance->pageid);
2005-02-20 07:23:10 +00:00
// load userdefined title and make sure it's never empty
if (empty($this->config->title)) {
$this->title = get_string('blockname','block_glossary_random');
} else {
2005-02-20 07:23:10 +00:00
$this->title = $this->config->title;
}
2005-02-26 08:11:01 +00:00
if (empty($this->config->glossary)) {
2005-02-26 08:11:01 +00:00
return false;
}
if (!isset($this->config->nexttime)) {
2005-02-26 08:11:01 +00:00
$this->config->nexttime = 0;
}
2005-02-20 07:23:10 +00:00
//check if it's time to put a new entry in cache
if (time() > $this->config->nexttime) {
// place glossary concept and definition in $pref->cache
if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config->glossary,
'approved',1)) {
$this->config->cache = get_string('noentriesyet','block_glossary_random');
parent::instance_config_save($this->config);
}
switch ($this->config->type) {
case BGR_RANDOMLY:
$i = rand(1,$numberofentries);
$LIMIT = sql_paging_limit($i-1, 1);
$SORT = 'ASC';
break;
case BGR_NEXTONE:
if (isset($this->config->previous)) {
$i = $this->config->previous + 1;
} else {
$i = 1;
}
if ($i > $numberofentries) { // Loop back to beginning
$i = 1;
}
$LIMIT = sql_paging_limit($i-1, 1);
$SORT = 'ASC';
break;
default: // BGR_LASTMODIFIED
$i = $numberofentries;
$LIMIT = 'LIMIT 1'; // The last one
$SORT = 'DESC';
break;
}
if ($entry = get_record_sql(' SELECT concept, definition, format '.
' FROM '.$CFG->prefix.'glossary_entries'.
' WHERE glossaryid = '.$this->config->glossary.
' AND approved = 1 '.
'ORDER BY timemodified '.$SORT.' '.$LIMIT)) {
$text = "<b>$entry->concept</b><br />";
$text .= format_text($entry->definition, $entry->format);
2005-02-20 07:23:10 +00:00
$this->config->nexttime = usergetmidnight(time())+60*60*24*$this->config->refresh;
$this->config->previous = $i;
} else {
$text = get_string('noentriesyet','block_glossary_random');
}
2005-02-20 07:23:10 +00:00
// store the text
$this->config->cache= $text;
parent::instance_config_save($this->config);
}
2005-02-20 07:23:10 +00:00
}
2005-02-20 07:23:10 +00:00
function instance_allow_multiple() {
// Are you going to allow multiple instances of each block?
// If yes, then it is assumed that the block WILL USE per-instance configuration
return true;
}
2005-02-20 07:23:10 +00:00
function instance_config_print() {
global $CFG;
2005-02-20 07:23:10 +00:00
if (empty($this->config->nexttime)) {
// ... teacher has not yet configured the block, let's put some default values here to explain things
$this->config->title = get_string('blockname','block_glossary_random');
$this->config->refresh = 0;
$this->config->cache= get_string('notyetconfigured','block_glossary_random');
$this->config->addentry=get_string('addentry', 'block_glossary_random');
$this->config->viewglossary=get_string('viewglossary', 'block_glossary_random');
$this->config->invisible=get_string('invisible', 'block_glossary_random');
2005-02-20 07:23:10 +00:00
}
2005-02-20 07:23:10 +00:00
// select glossaries to put in dropdown box ...
$glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name');
2005-02-20 07:23:10 +00:00
// and select quotetypes to put in dropdown box
$type[0] = get_string('random','block_glossary_random');
$type[1] = get_string('lastmodified','block_glossary_random');
$type[2] = get_string('nextone','block_glossary_random');
$this->config->nexttime = usergetmidnight(time())+24*60*60*$this->config->refresh;
2005-02-20 07:23:10 +00:00
// display the form
2005-02-20 07:23:10 +00:00
if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
print_simple_box_end();
} else {
notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
}
return true;
2005-02-20 07:23:10 +00:00
}
2005-02-20 07:23:10 +00:00
function get_content() {
global $USER, $CFG;
2005-02-26 08:11:01 +00:00
if (empty($this->config->glossary)) {
$this->content->text = get_string('notyetconfigured','block_glossary_random');
$this->content->footer = '';
return $this->content;
}
if (empty($this->config->cache)) {
$this->config->cache = '';
}
if ($this->content !== NULL) {
2005-02-26 08:11:01 +00:00
return $this->content;
}
$this->content = new stdClass;
$this->content->text = $this->config->cache;
// place link to glossary in the footer if the glossary is visible
2005-02-20 07:23:10 +00:00
$glossaryid = $this->config->glossary;
$glossary=get_record('glossary', 'id', $glossaryid);
2005-02-20 07:23:10 +00:00
$studentcanpost = $glossary->studentcanpost; //needed to decide on which footer
2005-02-20 07:23:10 +00:00
//Create a temp valid module structure (course,id)
$tempmod->course = $this->course->id;
$tempmod->id = $glossaryid;
//Obtain the visible property from the instance
2005-02-20 07:23:10 +00:00
if (instance_is_visible('glossary', $tempmod)) {
2005-02-20 07:23:10 +00:00
$cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
if ($studentcanpost) {
$footertext = $this->config->addentry;
} else {
$footertext = $this->config->viewglossary;
}
2005-02-20 07:23:10 +00:00
$this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/'
.(($studentcanpost == 1)?'edit':'view').'.php?id='.$cm->id
.'" title="'.$footertext.'">'.$footertext.'</a>';
2005-02-20 07:23:10 +00:00
// otherwise just place some text, no link
} else {
$this->content->footer = $this->config->invisible;
2005-02-20 07:23:10 +00:00
}
return $this->content;
}
2005-02-20 07:23:10 +00:00
function hide_header() {
if (empty($this->config->title)) {
2005-02-20 07:23:10 +00:00
return true;
2005-02-26 08:11:01 +00:00
}
return false;
2005-02-20 07:23:10 +00:00
}
2005-02-20 07:23:10 +00:00
}
?>