From 8a1b7f600811f8284e490e8d698d5c0132f3d6a1 Mon Sep 17 00:00:00 2001 From: Victor Cabral <87657291+victordscabral@users.noreply.github.com> Date: Thu, 7 Dec 2023 10:13:19 -0300 Subject: [PATCH] Add files via upload --- README.md | 68 +++++++ block_fbcomments.php | 238 +++++++++++++++++++++++++ db/access.php | 61 +++++++ db/upgrade.php | 25 +++ edit_form.php | 95 ++++++++++ lang/en/block_fbcomments.php | 64 +++++++ styles.css | 7 + tests/behat/behat_block_fbcomments.php | 55 ++++++ tests/behat/block_fbcomments.feature | 63 +++++++ version.php | 30 ++++ 10 files changed, 706 insertions(+) create mode 100644 README.md create mode 100644 block_fbcomments.php create mode 100644 db/access.php create mode 100644 db/upgrade.php create mode 100644 edit_form.php create mode 100644 lang/en/block_fbcomments.php create mode 100644 styles.css create mode 100644 tests/behat/behat_block_fbcomments.php create mode 100644 tests/behat/block_fbcomments.feature create mode 100644 version.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..b479dfc --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +Fbcomments +===================== + +Facebook like and comment block for Moodle + +Installation Instruction +===================== + +* Make sure you have all the required versions. +* Download and unpack the block folder. +* Place the folder (eg "myblock") in the "blocks" subdirectory. +* Visit http://yoursite.com/admin to complete the installation +* Turn editing on in any home or course page or any other page. +* Add the block to the page +* Visit the config link in the block for more options. +* Please note due to the way FB works, it probabily wont work properly on local installs. + +Moodle compatibility +===================== +* Tested with Moodle 2.5, 2.6, 2.7 and 2.8 + +Things to do +===================== +* Add more customisation options + +Maturity +==================== +STABLE + +Features +==================== +* Facebook comment box, with custom number of comments. +* Facebook like box. +* Display indivisual box for each page or a single one through out the site. +* Ability to manage all comments posted using a Fb app id. +* Supports a dark and light colored theme. +* Supports a recommend button. + +Change log +===================== +* 2012070801 - First beta release - 1.0 +* 2013041200 - First public release - 1.1 +* 2013041500 - Added course/mod level urls - 1.2 +* 2013041800 - Support for color themes and Moodle 2.3 -1.3 +* 2013071800 - Added appid support, code cleanup - 1.4 +* 2013071800 - code cleanup - 1.5 +* 2013071800 - Fix missing capabilities strings - 1.6 +* 2013071800 - Add support for ordering of comments - 1.7 +* 2014032000 - Add support for Moodle 2.7 and drop support for Moodle 2.2 - 1.8 +* 2014072100 - Minor clean up and drop support Moodle 2.3 - 1.9 +* 2015011200 - Drop 2.4/2.5, support 2.8, new recommend button, behat tests, travis-ci support - 2.0 + +About Author +===================== +Ankit Kumar Agarwal + +Moodle HQ developer + +https://github.com/ankitagarwal + +http://ankitkumaragarwal.com + +ankit@moodle.com + +License +===================== + +GPL 3 or later diff --git a/block_fbcomments.php b/block_fbcomments.php new file mode 100644 index 0000000..4d5065c --- /dev/null +++ b/block_fbcomments.php @@ -0,0 +1,238 @@ +. + +/** + * Fbcomments block class. + * + * @package block_fbcomments + * @copyright 2013 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_fbcomments extends block_base { + + /** @const string order by constants. (used for comments ordering) */ + const ORDER_SOCIAL = 'social'; + + /** @const string order by constants. (used for comments ordering) */ + const ORDER_TIME = 'time'; + + /** @const string order by constants. (used for comments ordering) */ + const ORDER_REVERSE_TIME = 'reverse_time'; + + /** @const int enable like button. */ + const BUTTON_ENABLE_LIKE = 1; + + /** @const int enable recommend button.*/ + const BUTTON_ENABLE_RECOMMEND = 2; + + /** @const int disable all buttons. */ + const BUTTON_DISABLE_ALL = 0; + + public function __construct() { + $this->title = get_string('pluginname', 'block_fbcomments'); + } + + public function has_config() { + return false; + } + + public function applicable_formats() { + return array('all' => true); + } + + public function specialization() { + $this->title = isset($this->config->title) ? + format_string($this->config->title) : format_string(get_string('newfbblock', 'block_fbcomments')); + } + + public function instance_allow_multiple() { + return false; + } + + public function get_content() { + global $CFG; + + if (!empty($this->config->appid) && !empty($this->config->enablecomment)) { + $meta = html_writer::tag("meta", "", array("property" => "fb:app_id", "content" => $this->config->appid)); + if (empty ($CFG->additionalhtmlhead)) { + $CFG->additionalhtmlhead = ''; + } + // Moodle creates a instance of block multiple times in every page, not sure why. + if (strpos($CFG->additionalhtmlhead, $meta) === false) { + $CFG->additionalhtmlhead .= $meta; + } + } + + if ($this->content !== null) { + return $this->content; + } + $fblike = null; + $fbcomment = null; + $this->content = new stdClass(); + // Config wont be set if block is just added. + if (!empty($this->config->urltype)) { + switch ($this->config->urltype) { + case 4 : $url = $this->get_mod_url(); + break; + case 3 : $url = $this->get_course_url(); + break; + case 2 : $url = new moodle_url($CFG->wwwroot); + break; + case 1 : + default : $url = $this->page->url; + break; + } + } else { + // No point going any further. + return ''; + } + $url = $url->out(true); + + $this->content->text = '
'; + $jscode = $this->get_fb_js(); + $this->page->requires->js_init_code($jscode); + + $color = $this->config->colorscheme; + $attr = 'data-href="'.$url.'" colorscheme="'.$color.'"'; + $this->content->text .= $this->get_like_share_button($attr); // Add like/share button if needed. + $this->content->text .= $this->get_comments_box($attr); // Add comments block. + + return $this->content; + } + + /** + * Return course url, which current page belongs to. + * + * @return moodle_url + */ + public function get_course_url() { + $context = $this->context->get_course_context(); + return $url = new moodle_url($context->get_url()); + } + + /** + * Return mod url, which current page belongs to. + * + * @return moodle_url + */ + public function get_mod_url() { + // Context of the page holding the block. + $context = $this->page->context; + return $url = new moodle_url($context->get_url()); + } + + /** + * The block should only be dockable when the title of the block is not empty + * and when parent allows docking. + * + * @return bool + */ + public function instance_can_be_docked() { + return (!empty($this->config->title) && parent::instance_can_be_docked()); + } + + /** + * Returns a list of allowed order-by parameters. + * + * @return array + */ + public static function get_order_by_options() { + $arr = array( + self::ORDER_SOCIAL => get_string(self::ORDER_SOCIAL, 'block_fbcomments'), + self::ORDER_TIME => get_string(self::ORDER_TIME, 'block_fbcomments'), + self::ORDER_REVERSE_TIME => get_string(self::ORDER_REVERSE_TIME, 'block_fbcomments') + ); + return $arr; + } + + /** + * Returns a list of allowed order-by parameters. + * + * @return array + */ + public static function get_button_options() { + $arr = array( + self::BUTTON_DISABLE_ALL => get_string('disable'), + self::BUTTON_ENABLE_LIKE => get_string('buttonlike', 'block_fbcomments'), + self::BUTTON_ENABLE_RECOMMEND => get_string('buttonrecommend', 'block_fbcomments') + ); + return $arr; + } + + /** + * Get Facebook js code. + * + * @return string + */ + protected function get_fb_js() { + return '(function(d, s, id) { + var js, fjs = d.getElementsByTagName(s)[0]; + if (d.getElementById(id)) return; + js = d.createElement(s); js.id = id; + js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; + fjs.parentNode.insertBefore(js, fjs); + }(document, "script", "facebook-jssdk"));'; + } + + /** + * Get html for Facebook like and share button if enabled. + * + * @param string $attr Attributes to add to the like and share button. + * + * @return string HTML code for the like button. + */ + protected function get_like_share_button($attr = '') { + if (!empty($this->config->enablelike)) { + $likeattr = 'data-send="false" data-show-faces="false"'; + + if ($this->config->enablelike == 1) { + // Like button. + $likeattr .= ' data-action="like"'; + } else { + // Recommend button. + $likeattr .= ' data-action="recommend"'; + } + + // Add a share button if specified. + if (!empty($this->config->enableshare)) { + $likeattr .= ' data-share="true"'; + } + return "
"; + } + return ''; + } + + /** + * Get html for Facebook comments box if enabled. + * + * @param string $attr Attributes to add to the comments box. + * + * @return string HTML code for the comments box. + */ + protected function get_comments_box($attr = '') { + if (!empty($this->config->enablecomment)) { + if (empty($this->config->numposts)) { + $this->config->numposts = 10; + } + $commentattr = 'data-num-posts="'.$this->config->numposts.'"'; + if (empty($this->config->commentorder)) { + $this->config->commentorder = self::ORDER_SOCIAL; + } + $commentattr .= 'data-order-by="'.$this->config->commentorder.'"'; + return "
"; + } + return ''; + } +} diff --git a/db/access.php b/db/access.php new file mode 100644 index 0000000..61b8788 --- /dev/null +++ b/db/access.php @@ -0,0 +1,61 @@ +. + +/** + * Caps for fbcomments block instances. + * + * @package block_fbcomments + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); + +$capabilities = array( + + 'block/fbcomments:myaddinstance' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => array( + 'user' => CAP_ALLOW + ), + + 'clonepermissionsfrom' => 'moodle/my:manageblocks' + ), + + 'block/fbcomments:addinstance' => array( + 'riskbitmask' => RISK_SPAM | RISK_XSS, + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + + 'clonepermissionsfrom' => 'moodle/site:manageblocks' + ), + // Override at lower contexts. + 'block/fbcomments:manageurl' => array( + 'riskbitmask' => RISK_SPAM, + + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'moodle/course:update' + ), +); diff --git a/db/upgrade.php b/db/upgrade.php new file mode 100644 index 0000000..ece9d7e --- /dev/null +++ b/db/upgrade.php @@ -0,0 +1,25 @@ +. + +/** + * Upgrade code for fbcomments block instances. + * + * @package block_fbcomments + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +function xmldb_block_fbcomments_upgrade($oldversion) { + return true; +} diff --git a/edit_form.php b/edit_form.php new file mode 100644 index 0000000..eb7c6ef --- /dev/null +++ b/edit_form.php @@ -0,0 +1,95 @@ +. + +/** + * Form for editing fbcomments block instances. + * + * @package block_fbcomments + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Form for editing fbcomments block instances. + * + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_fbcomments_edit_form extends block_edit_form { + protected function specific_definition($mform) { + + // Fields for editing HTML block title and contents. + $mform->addElement('header', 'configheader', get_string('blocksettings', 'block')); + + $mform->addElement('text', 'config_title', get_string('configtitle', 'block_fbcomments')); + $mform->setType('config_title', PARAM_TEXT); + $mform->setDefault('config_title', get_string('newfbblock', 'block_fbcomments')); + + $options = $this->get_page_options(); + $mform->addElement('select', 'config_urltype', get_string('urltype', 'block_fbcomments'), $options); + $options = array('light' => get_string('lightcolor', 'block_fbcomments'), 'dark' => get_string('darkcolor', 'block_fbcomments')); + $mform->addElement('select', 'config_colorscheme', get_string('colorscheme', 'block_fbcomments'), $options); + $mform->setDefault('config_colorscheme', 'light'); + + $options = block_fbcomments::get_button_options(); + $mform->addElement('select', 'config_enablelike', get_string('enablelike', 'block_fbcomments'), $options); + $mform->addElement('advcheckbox', 'config_enableshare', get_string('enableshare', 'block_fbcomments')); + $mform->addElement('advcheckbox', 'config_enablecomment', get_string('enablecomment', 'block_fbcomments')); + $mform->setType('config_enablelike', PARAM_INT); + $mform->setType('config_enablecomment', PARAM_BOOL); + $mform->setType('config_enableshare', PARAM_BOOL); + $mform->setDefault('config_enablelike', 1); + $mform->setDefault('config_enablecomment', 0); + $mform->setDefault('config_enableshare', 0); + $mform->disabledIf('config_enableshare', 'config_enablelike', 'eq', 0); + + $mform->addElement('text', 'config_numposts', get_string('numposts', 'block_fbcomments')); + $mform->setType('config_numposts', PARAM_INT); + $mform->addRule('config_numposts', get_string("notnumeric", "block_fbcomments"), "numeric", null, "client"); + $mform->setDefault('config_numposts', 10); + $mform->disabledIf('config_numposts', 'config_enablecomment', 'notchecked'); + + $options = block_fbcomments::get_order_by_options(); + $mform->addElement('select', 'config_commentorder', get_string('commentorder', 'block_fbcomments'), $options); + $mform->setType('config_commentorder', PARAM_ALPHA); + $mform->addHelpButton('config_commentorder', 'commentorder', 'block_fbcomments'); + $mform->disabledIf('config_commentorder', 'config_enablecomment', 'notchecked'); + + $mform->addElement('text', 'config_appid', get_string('appid', 'block_fbcomments')); + $mform->addHelpButton('config_appid', 'appid', 'block_fbcomments'); + $mform->addRule('config_appid', get_string("notnumeric", "block_fbcomments"), "numeric", null, "client"); + $mform->setType('config_appid', PARAM_INT); + $mform->disabledIf('config_appid', 'config_enablecomment', 'notchecked'); + + } + // No security checks needed as moodle automatically checks if submitted value for a select is in $options. + + private function get_page_options() { + $options = array(1 => get_string('thispage', 'block_fbcomments')); + $pagecontext = $this->page->context; + $coursecontext = $pagecontext->get_course_context(IGNORE_MISSING); + if (has_capability('block/fbcomments:manageurl', context_system::instance())) { + // Most probably an admin. + $options[2] = get_string('siteroot', 'block_fbcomments'); + } + if ($coursecontext && has_capability('block/fbcomments:manageurl', $coursecontext)) { + $options[3] = get_string('coursepage', 'block_fbcomments'); + } + if ($pagecontext->contextlevel == CONTEXT_MODULE && has_capability('block/fbcomments:manageurl', $pagecontext)) { + $options[4] = get_string('modpage', 'block_fbcomments', $pagecontext->get_context_name(false)); + } + return $options; + } +} diff --git a/lang/en/block_fbcomments.php b/lang/en/block_fbcomments.php new file mode 100644 index 0000000..6303201 --- /dev/null +++ b/lang/en/block_fbcomments.php @@ -0,0 +1,64 @@ +. + +/** + * Lang strings for fbcomments block instances. + * + * @package block_fbcomments + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['appid'] = 'Facebook App id'; +$string['appid_help'] = 'Facebook App lets you manage comments posted on various pages using this block. It also gives you various other related stats. +Visit Get a facebook app to get one. Setting this is optional.'; +$string['buttonlike'] = "Like"; +$string['buttonrecommend'] = "Recommend"; +$string['commentorder'] = 'Order of comments'; +$string['commentorder_help'] = 'Comments can be ordered by following order:- + +social aka "Social Relevance": The Comments plugin uses social signals to surface the highest quality comments. +Comments are ordered to display the most relevant comments from friends and friends of friends as well as the most-liked or active discussion threads.Comments marked as spam are hidden from view. + +Time commented: Comments are shown in the order in which they were posted, with the oldest comments at the top, +and the newest at the bottom. + +Reverse time commented: Comments are shown in the opposite order from which they were posted, +with the newest comments at the top,and the oldest at the bottom.'; +$string['configtitle'] = 'Block title'; +$string['colorscheme'] = 'Theme to use'; +$string['coursepage'] = 'Content for the course'; +$string['darkcolor'] = 'Dark color theme'; +$string['enablecomment'] = 'Enable comments'; +$string['enablelike'] = 'Enable Like/recommend button'; +$string['enableshare'] = 'Enable Share button'; +$string['fbcomments:addinstance'] = 'Add a new Facebook comments block'; +$string['fbcomments:manageurl'] = 'Manage Facebook comments block url'; +$string['fbcomments:myaddinstance'] = 'Add a new Facebook comments block'; +$string['invalidvalue'] = 'That is an invalid value'; +$string['leaveblanktohide'] = 'leave blank to hide the title'; +$string['lightcolor'] = 'Light color theme'; +$string['modpage'] = 'Content for the module:{$a}'; +$string['newfbblock'] = 'Facebook comments'; +$string['notnumeric'] = 'This field must be numeric'; +$string['numposts'] = 'Number of comments to show'; +$string['pluginname'] = 'Facebook comments'; +$string['reverse_time'] = 'Reverse time commented'; +$string['siteroot'] = 'Content for whole site'; +$string['social'] = 'Social'; +$string['thispage'] = 'Content for the block page only'; +$string['time'] = 'Time commented'; +$string['urltype'] = 'Block represents'; + diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..e35a31f --- /dev/null +++ b/styles.css @@ -0,0 +1,7 @@ +.block_fbcomments .fb-comments, +.block_fbcomments .fb-comments span, +.block_fbcomments .fb-comments iframe[style] {width: 100% !important;} + +.block_fbcomments .fb-like, +.block_fbcomments .fb-like span, +.block_fbcomments .fb-like iframe[style] {width: 100% !important;} \ No newline at end of file diff --git a/tests/behat/behat_block_fbcomments.php b/tests/behat/behat_block_fbcomments.php new file mode 100644 index 0000000..aa8cd9e --- /dev/null +++ b/tests/behat/behat_block_fbcomments.php @@ -0,0 +1,55 @@ +. + +/** + * Fbcomments behat custom steps. + * + * @package block_fbcomments + * @copyright 2015 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); + +use Behat\Behat\Context\Step\Given as Given, + Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; + +/** + * Fbcomments behat custom steps. + * + * @package block_fbcomments + * @copyright 2015 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_block_fbcomments extends behat_base { + + /** + * Helper used to find and switch to behat frame. This is currently not used as facebook doesn't like opening and parsing + * this iframe. + * + * @Given /^I switch to fbcomments iframe$/ + * @throws ElementNotFoundException + * @return Given[] the steps. + */ + public function switch_to_fbcomments_iframe() { + $iframe = $this->find('css' ,'iframe[title~="Facebook"]'); + if (empty($iframe)) { + // Iframe not found. + throw new ElementNotFoundException($this->getSession(), 'Facebook iframe '); + } + $name = $iframe->getAttribute('name'); + return new Given('I switch to "' . $name . '" iframe'); + } +} diff --git a/tests/behat/block_fbcomments.feature b/tests/behat/block_fbcomments.feature new file mode 100644 index 0000000..c18a855 --- /dev/null +++ b/tests/behat/block_fbcomments.feature @@ -0,0 +1,63 @@ +@block @block_fbcomments +Feature: Configure and setup Fbcomments block + In order to use the features of fbcomments block + As a teacher + I need to be able to set it up and configure it. + + Background: + Given the following "courses" exist: + | fullname | shortname | summary | category | + | Course 1 | C101 | Proved the course summary block works! |0 | + And the following "users" exist: + | username | firstname | lastname | email | + | student1 | Sam | Student | student1@asd.com | + | teacher1 | Teacher | One | teacher1@asd.com | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C101 | student | + | teacher1 | C101 | editingteacher | + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + And I add the "Facebook comments" block + And I configure the "Facebook comments" block + And I press "Save changes" + And I log out + + @javascript + Scenario: Student can view Facebook comments + When I log in as "student1" + And I follow "Course 1" + Then "Facebook comments" "block" should exist + + @javascript + Scenario: Teacher can configure the block. + When I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + And I configure the "Facebook comments" block + And I set the following fields to these values: + | Block title | Fbcomments | + | Block represents | Content for the course | + | Theme to use | Dark color theme | + | Enable Like/recommend button | Recommend | + | Enable Share button | 1 | + | Enable comments | 1 | + | Number of comments to show | 20 | + | Order of comments | Social | + | Facebook App id | 25 | + And I press "Save changes" + Then I configure the "Fbcomments" block + And the field "Block title" matches value "Fbcomments" + And the field "Block represents" matches value "Content for the course" + And the field "Theme to use" matches value "Dark color theme" + And the field "Enable Like/recommend button" matches value "Recommend" + And the field "Enable Share button" matches value "1" + And the field "Enable comments" matches value "1" + And the field "Number of comments to show" matches value "20" + And the field "Order of comments" matches value "Social" + And the field "Facebook App id" matches value "25" + And I set the field "Enable Like/recommend button" to "Disable" + And the "Enable Share button" "checkbox" should be disabled + And I set the field "Enable Like/recommend button" to "Like" + And the "Enable Share button" "checkbox" should be enabled diff --git a/version.php b/version.php new file mode 100644 index 0000000..9808742 --- /dev/null +++ b/version.php @@ -0,0 +1,30 @@ +. + +/** + * Version files for fbcomments block instances. + * + * @package block_fbcomments + * @copyright 2013 Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2015011200; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2013111800; // Requires this Moodle version +$plugin->component = 'block_fbcomments'; // Full name of the plugin (used for diagnostics) +$plugin->release = '2.0'; +$plugin->maturity = MATURITY_STABLE;