92
if (!empty($CFG->useblogassociations)) {
93
$associations = $DB->get_records('blog_association', array('blogid' => $this->id));
94
foreach ($associations as $association) {
95
$context = context::instance_by_id($association->contextid);
96
if ($context->contextlevel == CONTEXT_COURSE) {
97
$this->courseassoc = $association->contextid;
98
} else if ($context->contextlevel == CONTEXT_MODULE) {
99
$this->modassoc = $association->contextid;
89
104
$this->form = $form;
93
* Prints or returns the HTML for this blog entry.
109
* Gets the required data to print the entry
98
public function print_html($return=false) {
100
global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE;
102
$user = $DB->get_record('user', array('id'=>$this->userid));
111
public function prepare_render() {
113
global $DB, $CFG, $PAGE;
115
$this->renderable = new StdClass();
117
$this->renderable->user = $DB->get_record('user', array('id'=>$this->userid));
104
120
if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
105
121
require_once($CFG->dirroot . '/comment/lib.php');
107
123
$cmt = new stdClass();
108
$cmt->context = get_context_instance(CONTEXT_USER, $user->id);
124
$cmt->context = context_user::instance($this->userid);
109
125
$cmt->courseid = $PAGE->course->id;
110
126
$cmt->area = 'format_blog';
111
127
$cmt->itemid = $this->id;
112
128
$cmt->showcount = $CFG->blogshowcommentscount;
113
129
$cmt->component = 'blog';
114
$comment = new comment($cmt);
115
$cmttext = $comment->output(true);
130
$this->renderable->comment = new comment($cmt);
117
133
$this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
119
$options = array('overflowdiv'=>true);
120
$template['body'] = format_text($this->summary, $this->summaryformat, $options);
121
$template['title'] = format_string($this->subject);
122
$template['userid'] = $user->id;
123
$template['author'] = fullname($user);
124
$template['created'] = userdate($this->created);
126
if ($this->created != $this->lastmodified) {
127
$template['lastmod'] = userdate($this->lastmodified);
130
$template['publishstate'] = $this->publishstate;
132
$stredit = get_string('edit');
133
$strdelete = get_string('delete');
135
// Check to see if the entry is unassociated with group/course level access
136
$unassociatedentry = false;
137
if (!empty($CFG->useblogassociations) && ($this->publishstate == 'group' || $this->publishstate == 'course')) {
138
if (!$DB->record_exists('blog_association', array('blogid' => $this->id))) {
139
$unassociatedentry = true;
143
// Start printing of the blog
144
$table = new html_table();
145
$table->cellspacing = 0;
146
$table->attributes['class'] = 'forumpost blog_entry blog'. ($unassociatedentry ? 'draft' : $template['publishstate']);
147
$table->attributes['id'] = 'b'.$this->id;
148
$table->width = '100%';
150
$picturecell = new html_table_cell();
151
$picturecell->attributes['class'] = 'picture left';
152
$picturecell->text = $OUTPUT->user_picture($user);
154
$table->head[] = $picturecell;
156
$topiccell = new html_table_cell();
157
$topiccell->attributes['class'] = 'topic starter';
158
$titlelink = html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), $template['title']);
159
$topiccell->text = $OUTPUT->container($titlelink, 'subject');
160
$topiccell->text .= $OUTPUT->container_start('author');
162
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $PAGE->course->id)));
163
$by = new stdClass();
164
$by->name = html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $PAGE->course->id)), $fullname);
165
$by->date = $template['created'];
167
$topiccell->text .= get_string('bynameondate', 'forum', $by);
168
$topiccell->text .= $OUTPUT->container_end();
135
// External blog link.
170
136
if ($this->uniquehash && $this->content) {
171
137
if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {
172
138
$urlparts = parse_url($externalblog->url);
173
$topiccell->text .= $OUTPUT->container(get_string('retrievedfrom', 'blog').get_string('labelsep', 'langconfig').html_writer::link($urlparts['scheme'].'://'.$urlparts['host'], $externalblog->name), 'externalblog');
177
$topiccell->header = false;
178
$table->head[] = $topiccell;
181
$mainrow = new html_table_row();
183
$leftsidecell = new html_table_cell();
184
$leftsidecell->attributes['class'] = 'left side';
185
$mainrow->cells[] = $leftsidecell;
187
$contentcell = new html_table_cell();
188
$contentcell->attributes['class'] = 'content';
190
$attachedimages = $OUTPUT->container($this->print_attachments(), 'attachments');
192
// retrieve associations in case they're needed early
193
$blogassociations = $DB->get_records('blog_association', array('blogid' => $this->id));
195
// determine text for publish state
196
switch ($template['publishstate']) {
198
$blogtype = get_string('publishtonoone', 'blog');
201
$blogtype = get_string('publishtosite', 'blog');
204
$blogtype = get_string('publishtoworld', 'blog');
212
$contentcell->text .= $OUTPUT->container($blogtype, 'audience');
214
$contentcell->text .= $template['body'];
215
$contentcell->text .= $attachedimages;
217
// Uniquehash is used as a link to an external blog
218
if (!empty($this->uniquehash)) {
219
$contentcell->text .= $OUTPUT->container_start('externalblog');
220
$contentcell->text .= html_writer::link($this->uniquehash, get_string('linktooriginalentry', 'blog'));
221
$contentcell->text .= $OUTPUT->container_end();
225
$officialtags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'official');
226
$defaulttags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'default');
228
if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
229
$contentcell->text .= $OUTPUT->container_start('tags');
232
$contentcell->text .= get_string('tags', 'tag') .': '. $OUTPUT->container($officialtags, 'officialblogtags');
234
$contentcell->text .= ', ';
237
$contentcell->text .= $defaulttags;
238
$contentcell->text .= $OUTPUT->container_end();
242
if (!empty($CFG->useblogassociations) && $blogassociations) {
243
$contentcell->text .= $OUTPUT->container_start('tags');
245
$hascourseassocs = false;
248
// First find and show the associated course
249
foreach ($blogassociations as $assocrec) {
250
$context = get_context_instance_by_id($assocrec->contextid);
251
if ($context->contextlevel == CONTEXT_COURSE) {
252
$assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid));
253
$text = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
254
$assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('i/course', $text), null, array(), true);
255
$hascourseassocs = true;
256
$assoctype = get_string('course');
260
// Now show mod association
261
foreach ($blogassociations as $assocrec) {
262
$context = get_context_instance_by_id($assocrec->contextid);
264
if ($context->contextlevel == CONTEXT_MODULE) {
265
if ($hascourseassocs) {
267
$hascourseassocs = false;
270
$modinfo = $DB->get_record('course_modules', array('id' => $context->instanceid));
271
$modname = $DB->get_field('modules', 'name', array('id' => $modinfo->module));
273
$assocurl = new moodle_url('/mod/'.$modname.'/view.php', array('id' => $modinfo->id));
274
$text = $DB->get_field($modname, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!!
275
$assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('icon', $text, $modname), null, array(), true);
277
$assoctype = get_string('modulename', $modname);
281
$assocstr = substr($assocstr, 0, -2);
282
$contentcell->text .= get_string('associated', 'blog', $assoctype) . ': '. $assocstr;
284
$contentcell->text .= $OUTPUT->container_end();
287
if ($unassociatedentry) {
288
$contentcell->text .= $OUTPUT->container(get_string('associationunviewable', 'blog'), 'noticebox');
293
$contentcell->text .= $OUTPUT->container_start('commands');
295
if (blog_user_can_edit_entry($this)) {
296
if (empty($this->uniquehash)) {
297
//External blog entries should not be edited
298
$contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
299
array('action' => 'edit', 'entryid' => $this->id)),
302
$contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
303
array('action' => 'delete', 'entryid' => $this->id)),
307
$contentcell->text .= html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), get_string('permalink', 'blog'));
309
$contentcell->text .= $OUTPUT->container_end();
311
if (isset($template['lastmod']) ){
312
$contentcell->text .= '<div style="font-size: 55%;">';
313
$contentcell->text .= ' [ '.get_string('modified').': '.$template['lastmod'].' ]';
314
$contentcell->text .= '</div>';
317
//add comments under everything
318
$contentcell->text .= $cmttext;
320
$mainrow->cells[] = $contentcell;
321
$table->data = array($mainrow);
324
return html_writer::table($table);
326
echo html_writer::table($table);
139
$this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig');
140
$this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name);
144
// Retrieve associations
145
$this->renderable->unassociatedentry = false;
146
if (!empty($CFG->useblogassociations)) {
148
// Adding the entry associations data.
149
if ($associations = $associations = $DB->get_records('blog_association', array('blogid' => $this->id))) {
151
// Check to see if the entry is unassociated with group/course level access.
152
if ($this->publishstate == 'group' || $this->publishstate == 'course') {
153
$this->renderable->unassociatedentry = true;
156
foreach ($associations as $key => $assocrec) {
158
if (!$context = context::instance_by_id($assocrec->contextid, IGNORE_MISSING)) {
159
unset($associations[$key]);
163
// The renderer will need the contextlevel of the association.
164
$associations[$key]->contextlevel = $context->contextlevel;
166
// Course associations.
167
if ($context->contextlevel == CONTEXT_COURSE) {
168
$instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
170
$associations[$key]->url = $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid));
171
$associations[$key]->text = $instancename;
172
$associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text);
176
if ($context->contextlevel == CONTEXT_MODULE) {
178
// Getting the activity type and the activity instance id
179
$sql = 'SELECT cm.instance, m.name FROM {course_modules} cm
180
JOIN {modules} m ON m.id = cm.module
181
WHERE cm.id = :cmid';
182
$modinfo = $DB->get_record_sql($sql, array('cmid' => $context->instanceid));
183
$instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!!
185
$associations[$key]->type = get_string('modulename', $modinfo->name);
186
$associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', array('id' => $context->instanceid));
187
$associations[$key]->text = $instancename;
188
$associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name);
192
$this->renderable->blogassociations = $associations;
195
// Entry attachments.
196
$this->renderable->attachments = $this->get_attachments();
198
$this->renderable->usercanedit = blog_user_can_edit_entry($this);
203
* Gets the entry attachments list
204
* @return array List of blog_entry_attachment instances
206
function get_attachments() {
210
require_once($CFG->libdir.'/filelib.php');
212
$syscontext = context_system::instance();
214
$fs = get_file_storage();
215
$files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
217
// Adding a blog_entry_attachment for each non-directory file.
218
$attachments = array();
219
foreach ($files as $file) {
220
if ($file->is_directory()) {
223
$attachments[] = new blog_entry_attachment($file, $this->id);