MDL-58454 badges: Fix some minor errors for Open Badges v2
[moodle.git] / blocks / community / block_community.php
blobfc345caa52115ccc032c74c87f271f73ec451f4d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * @package block_community
19 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
21 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
23 * The community block
26 class block_community extends block_list {
28 function init() {
29 $this->title = get_string('pluginname', 'block_community');
32 function user_can_addto($page) {
33 // Don't allow people to add the block if they can't even use it
34 if (!has_capability('moodle/community:add', $page->context)) {
35 return false;
38 return parent::user_can_addto($page);
41 function user_can_edit() {
42 // Don't allow people to edit the block if they can't even use it
43 if (!has_capability('moodle/community:add',
44 context::instance_by_id($this->instance->parentcontextid))) {
45 return false;
47 return parent::user_can_edit();
50 function get_content() {
51 global $CFG, $OUTPUT, $USER;
53 $coursecontext = context::instance_by_id($this->instance->parentcontextid);
55 if (!has_capability('moodle/community:add', $coursecontext)
56 or $this->content !== NULL) {
57 return $this->content;
60 $this->content = new stdClass();
61 $this->content->items = array();
62 $this->content->icons = array();
63 $this->content->footer = '';
65 if (!isloggedin()) {
66 return $this->content;
69 $icon = $OUTPUT->pix_icon('i/group', get_string('group'));
70 $addcourseurl = new moodle_url('/blocks/community/communitycourse.php',
71 array('add' => true, 'courseid' => $this->page->course->id));
72 $searchlink = html_writer::tag('a', $icon . get_string('addcourse', 'block_community'),
73 array('href' => $addcourseurl->out(false)));
74 $this->content->items[] = $searchlink;
76 require_once($CFG->dirroot . '/blocks/community/locallib.php');
77 $communitymanager = new block_community_manager();
78 $courses = $communitymanager->block_community_get_courses($USER->id);
79 if ($courses) {
80 $this->content->items[] = html_writer::empty_tag('hr');
81 $this->content->icons[] = '';
82 $this->content->items[] = get_string('mycommunities', 'block_community');
83 $this->content->icons[] = '';
84 foreach ($courses as $course) {
85 //delete link
86 $deleteicon = $OUTPUT->pix_icon('t/delete', get_string('removecommunitycourse', 'block_community'));
87 $deleteurl = new moodle_url('/blocks/community/communitycourse.php',
88 array('remove' => true,
89 'courseid' => $this->page->course->id,
90 'communityid' => $course->id, 'sesskey' => sesskey()));
91 $deleteatag = html_writer::tag('a', $deleteicon, array('href' => $deleteurl));
93 $courselink = html_writer::tag('a', $course->coursename,
94 array('href' => $course->courseurl));
95 $this->content->items[] = $courselink . ' ' . $deleteatag;
96 $this->content->icons[] = '';
100 return $this->content;