first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\block
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Class to help display an RSS Feeds block
*
* @package block_rss_client
* @copyright 2016 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block implements \renderable, \templatable {
/**
* An array of renderable feeds
*
* @var array
*/
protected $feeds;
/**
* Contruct
*
* @param array $feeds An array of renderable feeds
*/
public function __construct(array $feeds = array()) {
$this->feeds = $feeds;
}
/**
* Prepare data for use in a template
*
* @param \renderer_base $output
* @return array
*/
public function export_for_template(\renderer_base $output) {
$data = array('feeds' => array());
foreach ($this->feeds as $feed) {
$data['feeds'][] = $feed->export_for_template($output);
}
return $data;
}
/**
* Add a feed
*
* @param \block_rss_client\output\feed $feed
* @return \block_rss_client\output\block
*/
public function add_feed(feed $feed) {
$this->feeds[] = $feed;
return $this;
}
/**
* Set the feeds
*
* @param array $feeds
* @return \block_rss_client\output\block
*/
public function set_feeds(array $feeds) {
$this->feeds = $feeds;
return $this;
}
/**
* Get feeds
*
* @return array
*/
public function get_feeds() {
return $this->feeds;
}
}
@@ -0,0 +1,151 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\channel_image
*
* @package block_rss_client
* @copyright 2016 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Class to display RSS channel images
*
* @package block_rss_client
* @copyright 2016 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class channel_image implements \renderable, \templatable {
/**
* The URL location of the image
*
* @var string
*/
protected $url;
/**
* The title of the image
*
* @var string
*/
protected $title;
/**
* The URL of the image link
*
* @var string
*/
protected $link;
/**
* Contructor
*
* @param \moodle_url $url The URL location of the image
* @param string $title The title of the image
* @param \moodle_url $link The URL of the image link
*/
public function __construct(\moodle_url $url, $title, \moodle_url $link = null) {
$this->url = $url;
$this->title = $title;
$this->link = $link;
}
/**
* Export this for use in a mustache template context.
*
* @see templatable::export_for_template()
* @param renderer_base $output
* @return array The data for the template
*/
public function export_for_template(\renderer_base $output) {
return array(
'url' => clean_param($this->url, PARAM_URL),
'title' => $this->title,
'link' => clean_param($this->link, PARAM_URL),
);
}
/**
* Set the URL
*
* @param \moodle_url $url
* @return \block_rss_client\output\channel_image
*/
public function set_url(\moodle_url $url) {
$this->url = $url;
return $this;
}
/**
* Get the URL
*
* @return \moodle_url
*/
public function get_url() {
return $this->url;
}
/**
* Set the title
*
* @param string $title
* @return \block_rss_client\output\channel_image
*/
public function set_title($title) {
$this->title = $title;
return $this;
}
/**
* Get the title
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Set the link
*
* @param \moodle_url $link
* @return \block_rss_client\output\channel_image
*/
public function set_link($link) {
$this->link = $link;
return $this;
}
/**
* Get the link
*
* @return \moodle_url
*/
public function get_link() {
return $this->link;
}
}
+224
View File
@@ -0,0 +1,224 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\feed
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Class to help display an RSS Feed
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feed implements \renderable, \templatable {
/**
* The feed's title
*
* @var string
*/
protected $title = null;
/**
* An array of renderable feed items
*
* @var array
*/
protected $items = array();
/**
* The channel image
*
* @var channel_image
*/
protected $image = null;
/**
* Whether or not to show the title
*
* @var boolean
*/
protected $showtitle;
/**
* Whether or not to show the channel image
*
* @var boolean
*/
protected $showimage;
/**
* Contructor
*
* @param string $title The title of the RSS feed
* @param boolean $showtitle Whether to show the title
* @param boolean $showimage Whether to show the channel image
*/
public function __construct($title, $showtitle = true, $showimage = true) {
$this->title = $title;
$this->showtitle = $showtitle;
$this->showimage = $showimage;
}
/**
* Export this for use in a mustache template context.
*
* @see templatable::export_for_template()
* @param \renderer_base $output
* @return array
*/
public function export_for_template(\renderer_base $output) {
$data = array(
'title' => $this->showtitle ? $this->title : null,
'image' => null,
'items' => array(),
);
if ($this->showimage && $this->image) {
$data['image'] = $this->image->export_for_template($output);
}
foreach ($this->items as $item) {
$data['items'][] = $item->export_for_template($output);
}
return $data;
}
/**
* Set the feed title
*
* @param string $title
* @return \block_rss_client\output\feed
*/
public function set_title($title) {
$this->title = $title;
return $this;
}
/**
* Get the feed title
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Add an RSS item
*
* @param \block_rss_client\output\item $item
*/
public function add_item(item $item) {
$this->items[] = $item;
return $this;
}
/**
* Set the RSS items
*
* @param array $items An array of renderable RSS items
*/
public function set_items(array $items) {
$this->items = $items;
return $this;
}
/**
* Get the RSS items
*
* @return array An array of renderable RSS items
*/
public function get_items() {
return $this->items;
}
/**
* Set the channel image
*
* @param \block_rss_client\output\channel_image $image
*/
public function set_image(channel_image $image) {
$this->image = $image;
}
/**
* Get the channel image
*
* @return channel_image
*/
public function get_image() {
return $this->image;
}
/**
* Set showtitle
*
* @param boolean $showtitle
* @return \block_rss_client\output\feed
*/
public function set_showtitle($showtitle) {
$this->showtitle = boolval($showtitle);
return $this;
}
/**
* Get showtitle
*
* @return boolean
*/
public function get_showtitle() {
return $this->showtitle;
}
/**
* Set showimage
*
* @param boolean $showimage
* @return \block_rss_client\output\feed
*/
public function set_showimage($showimage) {
$this->showimage = boolval($showimage);
return $this;
}
/**
* Get showimage
*
* @return boolean
*/
public function get_showimage() {
return $this->showimage;
}
}
+111
View File
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\footer
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Class to help display an RSS Block footer
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class footer implements \renderable, \templatable {
/**
* The link provided in the RSS channel
*
* @var \moodle_url|null
*/
protected $channelurl;
/**
* Link to manage feeds, only provided if a feed has failed.
*
* @var \moodle_url|null
*/
protected $manageurl = null;
/**
* Constructor
*
* @param \moodle_url $channelurl (optional) The link provided in the RSS channel
*/
public function __construct($channelurl = null) {
$this->channelurl = $channelurl;
}
/**
* Set the channel url
*
* @param \moodle_url $channelurl
* @return \block_rss_client\output\footer
*/
public function set_channelurl(\moodle_url $channelurl) {
$this->channelurl = $channelurl;
return $this;
}
/**
* Record the fact that there is at least one failed feed (and the URL for viewing
* these failed feeds).
*
* @param \moodle_url $manageurl the URL to link to for more information
*/
public function set_failed(\moodle_url $manageurl) {
$this->manageurl = $manageurl;
}
/**
* Get the channel url
*
* @return \moodle_url
*/
public function get_channelurl() {
return $this->channelurl;
}
/**
* Export context for use in mustache templates
*
* @see templatable::export_for_template()
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->channellink = clean_param($this->channelurl, PARAM_URL);
if ($this->manageurl) {
$data->hasfailedfeeds = true;
$data->manageurl = clean_param($this->manageurl, PARAM_URL);
}
return $data;
}
}
+286
View File
@@ -0,0 +1,286 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\feed
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Class to help display an RSS Item
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item implements \renderable, \templatable {
/**
* The unique id of the item
*
* @var string
*/
protected $id;
/**
* The link to the item
*
* @var \moodle_url
*/
protected $link;
/**
* The title of the item
*
* @var string
*/
protected $title;
/**
* The description of the item
*
* @var string
*/
protected $description;
/**
* The item's permalink
*
* @var \moodle_url
*/
protected $permalink;
/**
* The publish date of the item in Unix timestamp format
*
* @var int
*/
protected $timestamp;
/**
* Whether or not to show the item's description
*
* @var string
*/
protected $showdescription;
/**
* Contructor
*
* @param string $id The id of the RSS item
* @param \moodle_url $link The URL of the RSS item
* @param string $title The title pf the RSS item
* @param string $description The description of the RSS item
* @param \moodle_url $permalink The permalink of the RSS item
* @param int $timestamp The Unix timestamp that represents the published date
* @param boolean $showdescription Whether or not to show the description
*/
public function __construct($id, \moodle_url $link, $title, $description, \moodle_url $permalink, $timestamp,
$showdescription = true) {
$this->id = $id;
$this->link = $link;
$this->title = $title;
$this->description = $description;
$this->permalink = $permalink;
$this->timestamp = $timestamp;
$this->showdescription = $showdescription;
}
/**
* Export context for use in mustache templates
*
* @see templatable::export_for_template()
* @param \renderer_base $output
* @return array
*/
public function export_for_template(\renderer_base $output) {
$data = array(
'id' => $this->id,
'permalink' => clean_param($this->permalink, PARAM_URL),
'datepublished' => $output->format_published_date($this->timestamp),
'link' => clean_param($this->link, PARAM_URL),
);
// If the item does not have a title, create one from the description.
$title = $this->title;
if (!$title) {
$title = strip_tags($this->description);
$title = \core_text::substr($title, 0, 20) . '...';
}
// Allow the renderer to format the title and description.
$data['title'] = strip_tags($output->format_title($title));
$data['description'] = $this->showdescription ? $output->format_description($this->description) : null;
return $data;
}
/**
* Set id
*
* @param string $id
* @return \block_rss_client\output\item
*/
public function set_id($id) {
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* Set link
*
* @param \moodle_url $link
* @return \block_rss_client\output\item
*/
public function set_link(\moodle_url $link) {
$this->link = $link;
return $this;
}
/**
* Get link
*
* @return \moodle_url
*/
public function get_link() {
return $this->link;
}
/**
* Set title
*
* @param string $title
* @return \block_rss_client\output\item
*/
public function set_title($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return \block_rss_client\output\item
*/
public function set_description($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function get_description() {
return $this->description;
}
/**
* Set permalink
*
* @param string $permalink
* @return \block_rss_client\output\item
*/
public function set_permalink($permalink) {
$this->permalink = $permalink;
return $this;
}
/**
* Get permalink
*
* @return string
*/
public function get_permalink() {
return $this->permalink;
}
/**
* Set timestamp
*
* @param int $timestamp
* @return \block_rss_client\output\item
*/
public function set_timestamp($timestamp) {
$this->timestamp = $timestamp;
return $this;
}
/**
* Get timestamp
*
* @return string
*/
public function get_timestamp() {
return $this->timestamp;
}
/**
* Set showdescription
*
* @param boolean $showdescription
* @return \block_rss_client\output\item
*/
public function set_showdescription($showdescription) {
$this->showdescription = boolval($showdescription);
return $this;
}
/**
* Get showdescription
*
* @return boolean
*/
public function get_showdescription() {
return $this->showdescription;
}
}
@@ -0,0 +1,124 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class block_rss_client\output\block_renderer_html
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\output;
defined('MOODLE_INTERNAL') || die();
/**
* Renderer for RSS Client block
*
* @package block_rss_client
* @copyright 2015 Howard County Public School System
* @author Brendan Anderson <brendan_anderson@hcpss.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/**
* Render an RSS Item
*
* @param \templatable $item
* @return string
*/
public function render_item(\templatable $item) {
$data = $item->export_for_template($this);
return $this->render_from_template('block_rss_client/item', $data);
}
/**
* Render an RSS Feed
*
* @param \templatable $feed
* @return string
*/
public function render_feed(\templatable $feed) {
$data = $feed->export_for_template($this);
return $this->render_from_template('block_rss_client/feed', $data);
}
/**
* Render an RSS feeds block
*
* @param \templatable $block
* @return string
*/
public function render_block(\templatable $block) {
$data = $block->export_for_template($this);
return $this->render_from_template('block_rss_client/block', $data);
}
/**
* Render the block footer
*
* @param \templatable $footer
* @return string
*/
public function render_footer(\templatable $footer) {
$data = $footer->export_for_template($this);
return $this->render_from_template('block_rss_client/footer', $data);
}
/**
* Format a timestamp to use as a published date
*
* @param int $timestamp Unix timestamp
* @return string
*/
public function format_published_date($timestamp) {
if (empty($timestamp)) {
return '';
} else {
return \core_date::strftime(get_string('strftimerecentfull', 'langconfig'), $timestamp);
}
}
/**
* Format an RSS item title
*
* @param string $title
* @return string
*/
public function format_title($title) {
return break_up_long_words($title, 30);
}
/**
* Format an RSS item description
*
* @param string $description
* @return string
*/
public function format_description($description) {
$description = format_text($description, FORMAT_HTML, array('para' => false));
$description = break_up_long_words($description, 30);
return $description;
}
}
@@ -0,0 +1,189 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy class for requesting user data.
*
* @package block_rss_client
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\userlist;
use \core_privacy\local\request\approved_userlist;
/**
* Privacy class for requesting user data.
*
* @package block_rss_client
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_database_table('block_rss_client', [
'userid' => 'privacy:metadata:block_rss_client:userid',
'title' => 'privacy:metadata:block_rss_client:title',
'preferredtitle' => 'privacy:metadata:block_rss_client:preferredtitle',
'description' => 'privacy:metadata:block_rss_client:description',
'shared' => 'privacy:metadata:block_rss_client:shared',
'url' => 'privacy:metadata:block_rss_client:url',
'skiptime' => 'privacy:metadata:block_rss_client:skiptime',
'skipuntil' => 'privacy:metadata:block_rss_client:skipuntil',
], 'privacy:metadata:block_rss_client:tableexplanation');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$sql = "SELECT ctx.id
FROM {block_rss_client} brc
JOIN {user} u
ON brc.userid = u.id
JOIN {context} ctx
ON ctx.instanceid = u.id
AND ctx.contextlevel = :contextlevel
WHERE brc.userid = :userid";
$params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users within a specific context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!$context instanceof \context_user) {
return;
}
$sql = "SELECT userid
FROM {block_rss_client}
WHERE userid = ?";
$params = [$context->instanceid];
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
$rssdata = [];
$results = static::get_records($contextlist->get_user()->id);
foreach ($results as $result) {
$rssdata[] = (object) [
'title' => $result->title,
'preferredtitle' => $result->preferredtitle,
'description' => $result->description,
'shared' => \core_privacy\local\request\transform::yesno($result->shared),
'url' => $result->url
];
}
if (!empty($rssdata)) {
$data = (object) [
'feeds' => $rssdata,
];
\core_privacy\local\request\writer::with_context($contextlist->current())->export_data([
get_string('pluginname', 'block_rss_client')], $data);
}
}
/**
* Delete all use data which matches the specified deletion_criteria.
*
* @param \context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
if ($context instanceof \context_user) {
static::delete_data($context->instanceid);
}
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
$context = $userlist->get_context();
if ($context instanceof \context_user) {
static::delete_data($context->instanceid);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
static::delete_data($contextlist->get_user()->id);
}
/**
* Delete data related to a userid.
*
* @param int $userid The user ID
*/
protected static function delete_data($userid) {
global $DB;
$DB->delete_records('block_rss_client', ['userid' => $userid]);
}
/**
* Get records related to this plugin and user.
*
* @param int $userid The user ID
* @return array An array of records.
*/
protected static function get_records($userid) {
global $DB;
return $DB->get_records('block_rss_client', ['userid' => $userid]);
}
}
@@ -0,0 +1,151 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task for updating RSS feeds for rss client block
*
* @package block_rss_client
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\task;
defined('MOODLE_INTERNAL') || die();
/**
* Task for updating RSS feeds for rss client block
*
* @package block_rss_client
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class refreshfeeds extends \core\task\scheduled_task {
/** The maximum time in seconds that cron will wait between attempts to retry failing RSS feeds. */
const CLIENT_MAX_SKIPTIME = HOURSECS * 12;
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('refreshfeedstask', 'block_rss_client');
}
/**
* This task goes through all the feeds. If the feed has a skipuntil value
* that is less than the current time cron will attempt to retrieve it
* with the cache duration set to 0 in order to force the retrieval of
* the item and refresh the cache.
*
* If a feed fails then the skipuntil time of that feed is set to be
* later than the next expected task time. The amount of time will
* increase each time the fetch fails until the maximum is reached.
*
* If a feed that has been failing is successfully retrieved it will
* go back to being handled as though it had never failed.
*
* Task should therefore process requests for permanently broken RSS
* feeds infrequently, and temporarily unavailable feeds will be tried
* less often until they become available again.
*/
public function execute() {
global $CFG, $DB;
require_once("{$CFG->libdir}/simplepie/moodle_simplepie.php");
// We are going to measure execution times.
$starttime = microtime();
$starttimesec = time();
// Fetch all site feeds.
$rs = $DB->get_recordset('block_rss_client');
$counter = 0;
mtrace('');
foreach ($rs as $rec) {
mtrace(' ' . $rec->url . ' ', '');
// Skip feed if it failed recently.
if ($starttimesec < $rec->skipuntil) {
mtrace('skipping until ' . userdate($rec->skipuntil));
continue;
}
$feed = $this->fetch_feed($rec->url);
if ($feed->error()) {
// Skip this feed (for an ever-increasing time if it keeps failing).
$rec->skiptime = $this->calculate_skiptime($rec->skiptime);
$rec->skipuntil = time() + $rec->skiptime;
$DB->update_record('block_rss_client', $rec);
mtrace("Error: could not load/find the RSS feed - skipping for {$rec->skiptime} seconds.");
} else {
mtrace ('ok');
// It worked this time, so reset the skiptime.
if ($rec->skiptime > 0) {
$rec->skiptime = 0;
$rec->skipuntil = 0;
$DB->update_record('block_rss_client', $rec);
}
// Only increase the counter when a feed is sucesfully refreshed.
$counter ++;
}
}
$rs->close();
// Show times.
mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
}
/**
* Fetch a feed for the specified URL.
*
* @param string $url The URL to fetch
* @return \moodle_simplepie
*/
protected function fetch_feed(string $url): \moodle_simplepie {
// Fetch the rss feed, using standard simplepie caching so feeds will be renewed only if cache has expired.
\core_php_time_limit::raise(60);
$feed = new \moodle_simplepie();
// Set timeout for longer than normal to be agressive at fetching feeds if possible..
$feed->set_timeout(40);
$feed->set_cache_duration(0);
$feed->set_feed_url($url);
$feed->init();
return $feed;
}
/**
* Calculates a new skip time for a record based on the current skip time.
*
* @param int $currentskip The current skip time of a record.
* @return int The newly calculated skip time.
*/
protected function calculate_skiptime(int $currentskip): int {
// If the feed has never failed, then the initial skiptime will be 0. We use a default of 5 minutes in this case.
// If the feed has previously failed then we double that time.
$newskiptime = max(MINSECS * 5, ($currentskip * 2));
// Max out at the CLIENT_MAX_SKIPTIME.
return min($newskiptime, self::CLIENT_MAX_SKIPTIME);
}
}