Skip to content

Commit

Permalink
MDL-63062 block_recentlyaccessedcourses: add block and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
vmdef committed Nov 5, 2018
1 parent 98a52c8 commit 41f6129
Show file tree
Hide file tree
Showing 25 changed files with 940 additions and 128 deletions.
1 change: 1 addition & 0 deletions blocks/recentlyaccessedcourses/amd/build/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions blocks/recentlyaccessedcourses/amd/src/main.js
@@ -0,0 +1,105 @@
// 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/>.

/**
* Javascript to initialise the Recently accessed courses block.
*
* @module block_recentlyaccessedcourses/main.js
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define(
[
'jquery',
'core_course/repository',
'core/templates',
'core/notification'
],
function(
$,
CoursesRepository,
Templates,
Notification
) {

var SELECTORS = {
COURSES_VIEW: '[data-region="recentlyaccessedcourses-view"]',
COURSES_VIEW_CONTENT: '[data-region="recentlyaccessedcourses-view-content"]'
};

var NUM_COURSES_TOTAL = 10;

/**
* Get enrolled courses from backend.
*
* @method getRecentCourses
* @param {int} userid User from which the courses will be obtained
* @param {int} limit Only return this many results
* @return {array} Courses user has accessed
*/
var getRecentCourses = function(userid, limit) {
return CoursesRepository.getLastAccessedCourses(userid, limit);
};

/**
* Render the dashboard courses.
*
* @method renderCourses
* @param {object} root The root element for the courses view.
* @param {array} courses containing array of returned courses.
* @return {promise} Resolved with HTML and JS strings
*/
var renderCourses = function(root, courses) {
if (courses.length > 0) {
return Templates.render('block_recentlyaccessedcourses/view-cards', {
courses: courses
});
} else {
var nocoursesimgurl = root.attr('data-nocoursesimgurl');
return Templates.render('block_recentlyaccessedcourses/no-courses', {
nocoursesimgurl: nocoursesimgurl
});
}
};

/**
* Get and show the recent courses into the block.
*
* @param {int} userid User from which the courses will be obtained
* @param {object} root The root element for the recentlyaccessedcourses block.
*/
var init = function(userid, root) {
root = $(root);
var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);

var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);

coursesPromise.then(function(courses) {
var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);

pagedContentPromise.then(function(html, js) {
return Templates.replaceNodeContents(recentcoursesViewContent, html, js);
}).catch(Notification.exception);
return coursesPromise;
}).catch(Notification.exception);
};

return {
init: init
};
});
70 changes: 70 additions & 0 deletions blocks/recentlyaccessedcourses/block_recentlyaccessedcourses.php
@@ -0,0 +1,70 @@
<?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/>.

/**
* Class definition for the Recently accessed courses block.
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Recently accessed courses block class.
*
* @package block_recentlyaccessedcourses
* @copyright Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_recentlyaccessedcourses extends block_base {
/**
* Initialize class member variables
*/
public function init() {
$this->title = get_string('pluginname', 'block_recentlyaccessedcourses');
}

/**
* Returns the contents.
*
* @return stdClass contents of block
*/
public function get_content() {
if (isset($this->content)) {
return $this->content;
}

$renderable = new block_recentlyaccessedcourses\output\main();
$renderer = $this->page->get_renderer('block_recentlyaccessedcourses');

$this->content = new stdClass();
$this->content->text = $renderer->render($renderable);
$this->content->footer = '';

return $this->content;
}

/**
* Locations where block can be displayed.
*
* @return array
*/
public function applicable_formats() {
return array('my' => true);
}
}
55 changes: 55 additions & 0 deletions blocks/recentlyaccessedcourses/classes/output/main.php
@@ -0,0 +1,55 @@
<?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/>.

/**
* Class containing data for the Recently accessed courses block.
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccessedcourses\output;
defined('MOODLE_INTERNAL') || die();

use renderable;
use renderer_base;
use templatable;

/**
* Class containing data for Recently accessed courses block.
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class main implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output
* @return \stdClass|array
*/
public function export_for_template(renderer_base $output) {
global $USER;

$nocoursesurl = $output->image_url('courses', 'block_recentlyaccessedcourses')->out();

return [
'userid' => $USER->id,
'nocoursesimgurl' => $nocoursesurl
];
}
}
47 changes: 47 additions & 0 deletions blocks/recentlyaccessedcourses/classes/output/renderer.php
@@ -0,0 +1,47 @@
<?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/>.

/**
* Recently accessed courses block renderer
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recentlyaccessedcourses\output;
defined('MOODLE_INTERNAL') || die;

use plugin_renderer_base;

/**
* Recently accessed courses block renderer
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {

/**
* Return the main content for the Recently accessed courses block.
*
* @param main $main The main renderable
* @return string HTML string
*/
public function render_recentcourses(main $main) {
return $this->render_from_template('block_recentlyaccessedcourses/main', $main->export_for_template($this));
}
}
46 changes: 46 additions & 0 deletions blocks/recentlyaccessedcourses/classes/privacy/provider.php
@@ -0,0 +1,46 @@
<?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 Subsystem implementation for Recently accessed courses block.
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_recentlyaccessedcourses\privacy;

defined('MOODLE_INTERNAL') || die();

/**
* Privacy Subsystem for Recently accessed courses block.
*
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason() : string {
return 'privacy:metadata';
}
}
48 changes: 48 additions & 0 deletions blocks/recentlyaccessedcourses/db/access.php
@@ -0,0 +1,48 @@
<?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/>.

/**
* Capabilities for the Recently accessed courses block.
*
* @package block_recentlyaccessedcourses
* @copyright 2018 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

'block/recentlyaccessedcourses:myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'user' => CAP_ALLOW
),

'clonepermissionsfrom' => 'moodle/my:manageblocks'
),

'block/recentlyaccessedcourses:addinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'manager' => CAP_ALLOW
),

'clonepermissionsfrom' => 'moodle/site:manageblocks'
)
);

0 comments on commit 41f6129

Please sign in to comment.