--- /dev/null
+<?PHP
+define("RANDOMLY", "0");
+define("LASTMODIFIED", "1");
+define("NEXTONE","2");
+
+class CourseBlock_glossary_random extends MoodleBlock {
+ function CourseBlock_glossary_random ($course) {
+
+ $this->title = get_string('blockname','block_glossary_random');
+ $this->content_type = BLOCK_TYPE_TEXT;
+ $this->course = $course;
+ $this->version = 2004100700;
+ if(!empty($course)) {
+ if ($prefs=get_record("block_glossary_random","course",$course->id)) {
+ $this->title = $prefs->title;
+ }
+ }
+ }
+ function get_content() {
+ global $USER, $CFG, $THEME;
+
+ if($this->content !== NULL) {
+ return $this->content;
+ }
+
+
+ $this->content = New object;
+ $this->content->text = '';
+ $this->content->footer = '';
+
+ if ($prefs=get_record("block_glossary_random","course",$this->course->id)) {
+
+ $glossaryid = $prefs->glossary;
+ $glossary=get_record("glossary", "id", $glossaryid);
+ $studentcanpost = $glossary->studentcanpost; //needed to decide on which footer
+
+ //check if it's time to put a new entry in cache
+ if (time() > $prefs->nexttime)
+ {
+ // place glossary concept and definition in $pref->cache
+ $numberofentries = count_records("glossary_entries","glossaryid",$glossaryid,"approved",1)-1;
+ switch ($prefs->type) {
+ case RANDOMLY:
+ $i = rand(0,$numberofentries);
+ break;
+ case NEXTONE:
+ $i = 1 + $prefs->previous;
+ if ($i < $numberofentries) {
+ break;
+ }
+ //otherwise fall through
+ case LASTMODIFIED:
+ $i=$numberofentries;
+ break;
+
+ }
+
+ if ($entries = get_records_sql("SELECT concept, definition, format
+ FROM {$CFG->prefix}glossary_entries
+ WHERE glossaryid = {$glossaryid} and approved = 1
+ ORDER BY timemodified LIMIT {$i},1")) {
+
+ foreach ($entries as $entry) { //normally only on entry
+ $text = "<b> $entry->concept</b><br>";
+ $text .= clean_text($entry->definition, $entry->format);
+ }
+
+
+ $prefs->nexttime = usergetmidnight(time())+60*60*24*$prefs->refresh;
+ $prefs->cache = addslashes($text);
+ $prefs->previous = $i;
+ if (!(update_record("block_glossary_random", $prefs))) {
+ error("Could not update the database");
+ }
+ } else {
+ $text = get_string('notyetconfigured','block_glossary_random');
+ }
+ }
+
+ //otherwise just return the cached text
+ $this->content->text = stripslashes($prefs->cache);
+
+ // place link to glossary in the footer if the glossary is visible
+
+ //Create a temp valid module structure (course,id)
+ $tempmod->course = $this->course->id;
+ $tempmod->id = $glossaryid;
+
+ //Obtain the visible property from the instance
+ if (instance_is_visible('glossary', $tempmod)) {
+
+ $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
+ if ($studentcanpost) {
+ $footertext = $prefs->addentry;
+ } else {
+ $footertext = $prefs->viewglossary;
+ }
+ $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/'
+ .(($studentcanpost == 1)?'edit':'view').'.php?id='.$cm->id
+ .'" title="'.$footertext.'">'.$footertext.'</a>';
+
+ // otherwise just place some text, no link
+ } else {
+ $this->content->footer = $prefs->invisible;
+ }
+
+
+ } else { //nothing in the database, this block needs configuration
+ $this->content->text = get_string("notyetconfigured","block_glossary_random");
+ }
+
+ if (isteacheredit($this->course->id)) { //add the option to configure this block
+ $this->content->footer .= '<br><a href="'
+ .$CFG->wwwroot."/blocks/glossary_random/prefs.php?id="
+ .$this->course->id."\">"
+ .get_string("configureblock","block_glossary_random")."</a>";
+ }
+
+ return $this->content;
+ }
+
+
+ }
+?>
--- /dev/null
+<?PHP
+//
+// This file keeps track of upgrades to Moodle's
+// blocks system.
+//
+// Sometimes, changes between versions involve
+// alterations to database structures and other
+// major things that may break installations.
+//
+// The upgrade function in this file will attempt
+// to perform all the necessary actions to upgrade
+// your older installtion to the current version.
+//
+// If there's something it cannot do itself, it
+// will tell you what you need to do.
+//
+// Versions are defined by backup_version.php
+//
+// This file is tailored to MySQL
+
+function glossary_random_upgrade($oldversion=0) {
+
+ global $CFG;
+
+ $result = true;
+
+ if ($oldversion < 2004041000 and $result) {
+ $result = true; //Nothing to do
+ }
+
+ //Finally, return result
+ return $result;
+}
--- /dev/null
+#
+# Table structure for table `block_glossary_random`
+#
+
+CREATE TABLE prefix_block_glossary_random (
+ id int(10) unsigned NOT NULL auto_increment,
+ course int(10) unsigned NOT NULL default '0',
+ title varchar(50) default NULL,
+ glossary int(10) unsigned NOT NULL default '0',
+ previous int(10) unsigned NOT NULL default '0',
+ type tinyint(4) NOT NULL default '0',
+ addentry varchar(255) default NULL,
+ viewglossary varchar(255) default NULL,
+ invisible varchar(255) default NULL,
+ cache text NOT NULL,
+ refresh int(10) unsigned NOT NULL default'0',
+ nexttime int(10) unsigned NOT NULL default '0',
+ PRIMARY KEY (id),
+ UNIQUE KEY id (id)
+) TYPE=MyISAM;
--- /dev/null
+<table cellpadding="20" width=75% align="center"> <tr> <td bgcolor="<?php p($THEME->cellcontent2) ?>" class="generalbox">
+
+<form action="prefs.php" method="post" name="form" id="form">
+<p><b><?php print_string("intro","block_glossary_random") ?>:</b></p>
+
+<table>
+<tr valign=top>
+<td align=right><p><?php print_string("title","block_glossary_random") ?>:</p></td>
+<td><input type="text" name="title" size="50" value="<?php p($prefs->title) ?>" /></td>
+</tr>
+
+<tr valign=top>
+<td align=right><p><?php print_string("select_glossary","block_glossary_random") ?>:</p></td>
+<td><?php choose_from_menu($glossaries,"glossary",$prefs->glossary) ?></td>
+</tr>
+
+<tr valign=top>
+<td align=right><p><?php print_string("refresh","block_glossary_random") ?>:</p></td>
+<td><input type="text" name="refresh" size="5" value="<?php p($prefs->refresh) ?>" /></td>
+</tr>
+
+<tr valign=top>
+<td align=right><p><?php print_string("type","block_glossary_random") ?>:</p></td>
+<td><?php
+ choose_from_menu($type, "type", $prefs->type, "");
+ helpbutton("",get_string("type","block_glossary_random"),"moodle",true,false,get_string("typehelp","block_glossary_random"));
+ ?></td>
+</tr>
+</table>
+
+ <p><b><?php print_string("aboutfooter","block_glossary_random") ?>:</b></p>
+ <p><?php print_string("askaddentry", "block_glossary_random") ?></p>
+ <input type="text" name="addentry" size="80" value="<?php p($prefs->addentry) ?>" /><br />
+<p><?php print_string("askviewglossary", "block_glossary_random") ?></p>
+ <input type="text" name="viewglossary" size="80" value="<?php p($prefs->viewglossary) ?>" /><br />
+ <p><?php print_string("askinvisible", "block_glossary_random") ?></p>
+ <input type="text" name="invisible" size="80" value="<?php p($prefs->invisible) ?>" /><br />
+
+<input type="hidden" name=course value="<?php p($course) ?>">
+<input type="hidden" name=id value="<?php p($prefs->id) ?>">
+
+<br /><center><input type="submit" value="<?php print_string("savechanges") ?>"></center>
+</form>
+
+</td></tr></table>
--- /dev/null
+<?PHP
+
+ require_once("../../config.php");
+ require_variable($id);
+ $course = $id;// is there another way of getting the course id?
+ require_login($course);
+ if (!(isteacheredit($course))) {
+ error("You're not allowed to edit this course");
+ }
+
+ // process data
+
+ if ($prefs = data_submitted()) {
+ validate_form($prefs, $err); //might add this later
+ if (count((array)$err) == 0) {
+ $prefs->nexttime = usergetmidnight(time())+24*60*60*$prefs->refresh;
+ if (!(update_record("block_glossary_random", $prefs))) {
+ error("Could not update record in the database.");
+ }
+ redirect("$CFG->wwwroot/course/view.php?id=$prefs->course");
+ }
+ }
+
+ // print form
+
+ if (!empty($err)) {
+ $focus = "form.".array_shift(array_flip(get_object_vars($err)));
+ } else {
+ $focus = "";
+ }
+
+ $prefs = get_record("block_glossary_random", "course", $course);
+ if (!$prefs) {
+ $prefs->course = $course;
+ $prefs->title = get_string('blockname','block_glossary_random');
+ $prefs->refresh = 0;
+ $prefs->text= get_string("notyetconfigured","block_glossary_random");
+ $prefs->addentry=get_string("addentry", "block_glossary_random");
+ $prefs->viewglossary=get_string("viewglossary", "block_glossary_random");
+ $prefs->invisible=get_string("invisible", "block_glossary_random");
+
+ if (!(insert_record("block_glossary_random",$prefs))) {
+ error("Could not insert new record in database");
+ }
+ }
+
+ // select glossaries to put in dropdown box ...
+
+ $glossaries = get_records_select_menu("glossary", "course= $course","name","id,name");
+ // and 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");
+
+ print_header(get_string("blockname","block_glossary_random"),
+ get_string("change_configuration","block_glossary_random"));
+ include("prefs.html");
+ print_footer();
+
+/******************************************************************************
+ * FUNCTIONS
+ *****************************************************************************/
+
+function validate_form($user, &$err) {
+ //we might add a check for glossary selected
+ return;
+}
+
+
+?>