block_glossary_random.php 6.03 KB
Newer Older
1
<?php
2
3
4
5

define('BGR_RANDOMLY',     '0');
define('BGR_LASTMODIFIED', '1');
define('BGR_NEXTONE',      '2');
jpuntd's avatar
jpuntd committed
6
7
8

class block_glossary_random extends block_base {
    function init() {
9
        $this->title = get_string('pluginname','block_glossary_random');
defacer's avatar
   
defacer committed
10
11
    }

jpuntd's avatar
jpuntd committed
12
    function specialization() {
13
14
        global $CFG, $DB;
        $this->course = $this->page->course;
defacer's avatar
   
defacer committed
15

jpuntd's avatar
jpuntd committed
16
17
        // load userdefined title and make sure it's never empty
        if (empty($this->config->title)) {
18
            $this->title = get_string('pluginname','block_glossary_random');
defacer's avatar
   
defacer committed
19
        } else {
jpuntd's avatar
jpuntd committed
20
            $this->title = $this->config->title;
defacer's avatar
   
defacer committed
21
        }
moodler's avatar
moodler committed
22

defacer's avatar
   
defacer committed
23
        if (empty($this->config->glossary)) {
moodler's avatar
moodler committed
24
25
26
            return false;
        }

defacer's avatar
   
defacer committed
27
        if (!isset($this->config->nexttime)) {
moodler's avatar
moodler committed
28
29
            $this->config->nexttime = 0;
        }
defacer's avatar
   
defacer committed
30

jpuntd's avatar
jpuntd committed
31
        //check if it's time to put a new entry in cache
defacer's avatar
   
defacer committed
32
33
34
        if (time() > $this->config->nexttime) {

            // place glossary concept and definition in $pref->cache
skodak's avatar
skodak committed
35
36
            if (!$numberofentries = $DB->count_records('glossary_entries',
                                                       array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
defacer's avatar
   
defacer committed
37
                $this->config->cache = get_string('noentriesyet','block_glossary_random');
defacer's avatar
   
defacer committed
38
                $this->instance_config_commit();
39
40
            }

41
42
43
            $limitfrom = 0;
            $limitnum = 1;

defacer's avatar
   
defacer committed
44
45
46
47
            switch ($this->config->type) {

                case BGR_RANDOMLY:
                    $i = rand(1,$numberofentries);
48
                    $limitfrom = $i-1;
49
                    $SORT = 'ASC';
defacer's avatar
   
defacer committed
50
51
                    break;

52
53
                case BGR_NEXTONE:
                    if (isset($this->config->previous)) {
defacer's avatar
   
defacer committed
54
                        $i = $this->config->previous + 1;
55
56
57
                    } else {
                        $i = 1;
                    }
defacer's avatar
   
defacer committed
58
                    if ($i > $numberofentries) {  // Loop back to beginning
59
                        $i = 1;
defacer's avatar
   
defacer committed
60
                    }
61
                    $limitfrom = $i-1;
62
63
                    $SORT = 'ASC';
                    break;
defacer's avatar
   
defacer committed
64

65
                default:  // BGR_LASTMODIFIED
defacer's avatar
   
defacer committed
66
                    $i = $numberofentries;
67
                    $limitfrom = 0;
68
                    $SORT = 'DESC';
defacer's avatar
   
defacer committed
69
70
                    break;
            }
71

72
            if ($entry = $DB->get_records_sql("SELECT concept, definition, definitionformat, definitiontrust
skodak's avatar
skodak committed
73
74
75
                                                 FROM {glossary_entries}
                                                WHERE glossaryid = ? AND approved = 1
                                             ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
76

defacer's avatar
   
defacer committed
77
78
                $entry = reset($entry);

79
                if (empty($this->config->showconcept)) {
80
                    $text = '';
81
                } else {
82
                    $text = "<h3>".format_string($entry->concept,true)."</h3>";
83
                }
moodler's avatar
moodler committed
84

85
                $options = new stdClass();
86
                $options->trusted = $entry->definitiontrust;
87
                $options->overflowdiv = true;
88
                $text .= format_text($entry->definition, $entry->definitionformat, $options);
defacer's avatar
   
defacer committed
89

defacer's avatar
   
defacer committed
90
                $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
defacer's avatar
   
defacer committed
91
92
93
94
95
                $this->config->previous = $i;

            } else {
                $text = get_string('noentriesyet','block_glossary_random');
            }
jpuntd's avatar
jpuntd committed
96
            // store the text
defacer's avatar
   
defacer committed
97
98
            $this->config->cache = $text;
            $this->instance_config_commit();
defacer's avatar
   
defacer committed
99
        }
jpuntd's avatar
jpuntd committed
100
    }
defacer's avatar
   
defacer committed
101

jpuntd's avatar
jpuntd committed
102
103
104
105
106
    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;
    }
defacer's avatar
   
defacer committed
107

jpuntd's avatar
jpuntd committed
108
    function get_content() {
109
        global $USER, $CFG, $DB;
moodler's avatar
moodler committed
110

defacer's avatar
   
defacer committed
111
112
113
114
        if (empty($this->config->glossary)) {
            $this->content->text   = get_string('notyetconfigured','block_glossary_random');
            $this->content->footer = '';
            return $this->content;
115
116
        }

117
118
        $glossaryid = $this->config->glossary;

119
        $course = $this->page->course;
120
121
122
123
124

        require_once($CFG->dirroot.'/course/lib.php');
        $modinfo = get_fast_modinfo($course);

        if (!isset($modinfo->instances['glossary'][$glossaryid])) {
125
126
127
128
129
130
131
132
133
134
135
            // we can get here if the glossary has been deleted, so
            // unconfigure the glossary from the block..
            $this->config->glossary = 0;
            $this->config->cache = '';
            $this->instance_config_commit();

            $this->content->text   = get_string('notyetconfigured','block_glossary_random');
            $this->content->footer = '';
            return $this->content;
        }

136
        $cm = $modinfo->instances['glossary'][$glossaryid];
137

defacer's avatar
   
defacer committed
138
        if (empty($this->config->cache)) {
139
140
            $this->config->cache = '';
        }
defacer's avatar
   
defacer committed
141

142
        if ($this->content !== NULL) {
moodler's avatar
moodler committed
143
144
            return $this->content;
        }
145

defacer's avatar
   
defacer committed
146
        $this->content = new stdClass;
147
        $this->content->text = $this->config->cache;
defacer's avatar
   
defacer committed
148
149
150
151

        // place link to glossary in the footer if the glossary is visible

        //Obtain the visible property from the instance
152
        if ($cm->uservisible) {
153
            if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
154
                $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
155
                .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
156
            } else {
157
                $this->content->footer = '';
158
159
            }

160
161
            $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
                .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
defacer's avatar
   
defacer committed
162

jpuntd's avatar
jpuntd committed
163
        // otherwise just place some text, no link
defacer's avatar
   
defacer committed
164
165
        } else {
            $this->content->footer = $this->config->invisible;
jpuntd's avatar
jpuntd committed
166
        }
defacer's avatar
   
defacer committed
167
168
169
170

        return $this->content;
    }

jpuntd's avatar
jpuntd committed
171
    function hide_header() {
defacer's avatar
   
defacer committed
172
        if (empty($this->config->title)) {
jpuntd's avatar
jpuntd committed
173
            return true;
moodler's avatar
moodler committed
174
175
        }
        return false;
jpuntd's avatar
jpuntd committed
176
177
    }
}
178