|
| 1 | +<?php |
| 2 | +// ------------------------ |
| 3 | +// @FUNC F030 AT: Block announcements. |
| 4 | +// Block que mostra noticies. |
| 5 | +// ---Fi |
| 6 | +// |
| 7 | +// This file is part of Moodle - http://moodle.org/ |
| 8 | +// |
| 9 | +// Moodle is free software: you can redistribute it and/or modify |
| 10 | +// it under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 3 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// Moodle is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +defined('MOODLE_INTERNAL') || die(); |
| 23 | + |
| 24 | +/** |
| 25 | + * Displays the announcements information. |
| 26 | + * |
| 27 | + * @copyright 2017 UPCnet |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class block_announcements extends block_base { |
| 31 | + var $maxelements = 3; |
| 32 | + |
| 33 | + /** |
| 34 | + * block initializations |
| 35 | + */ |
| 36 | + public function init() { |
| 37 | + $this->title = get_string('pluginname', 'block_announcements'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * block contents |
| 42 | + * |
| 43 | + * @return object |
| 44 | + */ |
| 45 | + public function get_content() { |
| 46 | + global $CFG, $USER, $DB, $OUTPUT, $PAGE; |
| 47 | + |
| 48 | + if ($this->content !== NULL) { |
| 49 | + return $this->content; |
| 50 | + } |
| 51 | + |
| 52 | + /*if (!isloggedin() or isguestuser()) { |
| 53 | + return ''; // Never useful unless you are logged in as real users |
| 54 | + }*/ |
| 55 | + |
| 56 | + $this->content = new stdClass; |
| 57 | + $this->content->text = ''; |
| 58 | + $this->content->footer = ''; |
| 59 | + $this->layout = ''; |
| 60 | + $this->position = ''; |
| 61 | + |
| 62 | + if (!isset($this->config)) { |
| 63 | + $this->config = new stdClass; |
| 64 | + } |
| 65 | + if (!isset($this->config->category)) $this->config->category = ''; |
| 66 | + if (!isset($this->config->view_link_other_announcements)) $this->config->view_link_other_announcements = 0; |
| 67 | + if (isset($this->config->maxelements)) $this->maxelements = $this->config->maxelements; |
| 68 | + if (isset($this->config->position)) $this->position = $this->config->position; |
| 69 | + if (isset($this->config->layout)) $this->layout = $this->config->layout; |
| 70 | + |
| 71 | + // Get announcements |
| 72 | + $announcements = \block_announcements\manager::get_enabled_blocks_by_category($this->config->category); |
| 73 | + //$announcements = array_slice($announcements, 0, $this->maxelements); |
| 74 | + |
| 75 | + // Fill template |
| 76 | + $data = new stdClass; |
| 77 | + $data->layout = ''; |
| 78 | + $data->announcementsid = 'Moodleannouncements'; |
| 79 | + |
| 80 | + $data->position = $this->position; |
| 81 | + if ($this->layout == 2) $data->layout = $this->layout; |
| 82 | + |
| 83 | + $data->link_other_announcements = null; |
| 84 | + if ($this->config->view_link_other_announcements |
| 85 | + && ( |
| 86 | + isloggedin() || $this->instance->parentcontextid == 2 |
| 87 | + ) |
| 88 | + ) { |
| 89 | + $link_other_announcements_url = new moodle_url('/blocks/announcements/viewall.php', array('category' => $this->config->category)); |
| 90 | + $data->link_other_announcements = $link_other_announcements_url->out(); |
| 91 | + } |
| 92 | + |
| 93 | + $data->button_announcements = null; |
| 94 | + if ($this->page->user_is_editing() && has_capability('block/announcements:addinstance', $this->context)) { |
| 95 | + $button_announcements_url = new moodle_url('/blocks/announcements/manager/index.php?filter_category='.$this->config->category, array()); |
| 96 | + $data->button_announcements = $button_announcements_url->out(); |
| 97 | + } |
| 98 | + |
| 99 | + $data->elements = array(); |
| 100 | + |
| 101 | + // Get user roles in site context |
| 102 | + $userrolesatsystemcontext = get_user_roles(\context_system::instance()); |
| 103 | + foreach ($announcements as $announcement) { |
| 104 | + |
| 105 | + if (count($data->elements) == $this->maxelements){ |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + if (!$announcement->is_visible_for_user_with_roles($userrolesatsystemcontext)){ |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + $button_edit_announcement = null; |
| 114 | + if ($this->page->user_is_editing() && has_capability('block/announcements:addinstance', $this->context)) { |
| 115 | + $button_edit_announcement_url = new moodle_url('/blocks/announcements/manager/edit.php', array('id' => $announcement->get_id(), 'filter_category' => $announcement->get_category())); |
| 116 | + $button_edit_announcement = $button_edit_announcement_url->out(); |
| 117 | + } |
| 118 | + $element = (object) [ |
| 119 | + 'imageurl' => $announcement->get_img_url(), |
| 120 | + 'title' => format_text($announcement->get_title(), FORMAT_HTML, array('trusted' => true, 'noclean' => true)), |
| 121 | + 'content' => format_text($announcement->get_content(), FORMAT_HTML, array('trusted' => true, 'noclean' => true)), |
| 122 | + 'url' => format_text($announcement->get_url(), FORMAT_PLAIN, array('trusted' => true, 'noclean' => true)), |
| 123 | + 'target' => $announcement->get_link_target(), |
| 124 | + 'alt'=>format_text($announcement->get_alt(), FORMAT_HTML), |
| 125 | + 'button_edit_announcement' => $button_edit_announcement, |
| 126 | + 'fontawesome_icon' => format_text($announcement->get_icon(), FORMAT_HTML, array('trusted' => true, 'noclean' => true)), |
| 127 | + 'timecreated' => userdate($announcement->timecreated, get_string('strftimedatefullshort', 'core_langconfig')), |
| 128 | + 'timemodified' => userdate($announcement->timemodified, get_string('strftimedatefullshort', 'core_langconfig')), |
| 129 | + ]; |
| 130 | + $data->elements[] = $element; |
| 131 | + } |
| 132 | + |
| 133 | + if (count($data->elements)) { |
| 134 | + $data->colsperrow = round(12/count($data->elements)); |
| 135 | + } else { |
| 136 | + $data->colsperrow = 12; |
| 137 | + } |
| 138 | + |
| 139 | + //midem si tenim un inclusion o no |
| 140 | + if (function_exists('atenea_include_inclusion')) { |
| 141 | + ob_start(); |
| 142 | + $result = atenea_include_inclusion ('block_announcements', array('config'=> $this->config, 'data' => $data)); |
| 143 | + $output = ob_get_contents(); |
| 144 | + ob_end_clean(); |
| 145 | + if (!empty($output)) { |
| 146 | + $this->content->text .= $output; |
| 147 | + return $this->content; |
| 148 | + } |
| 149 | + } |
| 150 | + // si no tneim res a posar deixem el text en blanc |
| 151 | + if (!empty($data->elements) || $data->button_announcements || $data->link_other_announcements) { |
| 152 | + if($data->position == 1){ |
| 153 | + $this->content->text .= $OUTPUT->render_from_template('block_announcements/announcement_text_top', $data); |
| 154 | + }else{ |
| 155 | + if($data->position == 2 || $data->layout == 2){ |
| 156 | + $this->content->text .= $OUTPUT->render_from_template('block_announcements/announcement_img_top', $data); |
| 157 | + }else{ |
| 158 | + $this->content->text .= $OUTPUT->render_from_template('block_announcements/announcements', $data); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + // End fill template |
| 163 | + return $this->content; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * allow the block to have a configuration page |
| 168 | + * |
| 169 | + * @return boolean |
| 170 | + */ |
| 171 | + public function has_config() { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * allow more than one instance of the block on a page |
| 177 | + * |
| 178 | + * @return boolean |
| 179 | + */ |
| 180 | + public function instance_allow_multiple() { |
| 181 | + //allow more than one instance on a page |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * allow instances to have their own configuration |
| 187 | + * |
| 188 | + * @return boolean |
| 189 | + */ |
| 190 | + function instance_allow_config() { |
| 191 | + //allow instances to have their own configuration |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * instance specialisations (must have instance allow config true) |
| 197 | + * |
| 198 | + */ |
| 199 | + public function specialization() { |
| 200 | + if (!$this->page->user_is_editing()) $this->title = ''; |
| 201 | + } |
| 202 | + |
| 203 | + public function applicable_formats() { |
| 204 | + return array( |
| 205 | + 'admin' => false, |
| 206 | + 'site-index' => true, |
| 207 | + 'course-view' => false, |
| 208 | + 'mod' => false, |
| 209 | + 'my' => true |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * post install configurations |
| 215 | + * |
| 216 | + */ |
| 217 | + public function after_install() { |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * post delete configurations |
| 222 | + * |
| 223 | + */ |
| 224 | + public function before_delete() { |
| 225 | + } |
| 226 | + |
| 227 | + /* |
| 228 | + * Add custom html attributes to aid with theming and styling |
| 229 | + * |
| 230 | + * @return array |
| 231 | + */ |
| 232 | + function html_attributes() { |
| 233 | + global $CFG; |
| 234 | + |
| 235 | + $attributes = parent::html_attributes(); |
| 236 | + |
| 237 | + if (!empty($this->config->classes)) { |
| 238 | + $attributes['class'] .= ' '.$this->config->classes; |
| 239 | + } |
| 240 | + |
| 241 | + return $attributes; |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments