Skip to content

Commit

Permalink
Merge pull request #66 from UniversityofPortland/4.x
Browse files Browse the repository at this point in the history
4.x version with speed improvements
  • Loading branch information
Syxton authored Sep 14, 2023
2 parents 777c821 + 321affe commit 7866750
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 151 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,38 @@ on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest

services:
postgres:
image: postgres:10
image: postgres:13
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
mariadb:
image: mariadb:10.5
image: mariadb:10
env:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3

strategy:
fail-fast: false
matrix:
php: ['7.3', '7.4', '8.0']
moodle-branch: ['MOODLE_311_STABLE']
php: ['8.1']
moodle-branch: ['MOODLE_401_STABLE', 'MOODLE_402_STABLE']
database: [pgsql, mariadb]

steps:
- name: Check out repository code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
path: plugin

Expand Down
12 changes: 3 additions & 9 deletions block_custom_course_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function has_config() {
* @return stdClass the content
*/
public function get_content() {
global $CFG, $USER, $OUTPUT;
global $CFG, $USER;

if ($this->content !== null) {
return $this->content;
Expand All @@ -68,14 +68,6 @@ public function get_content() {
$this->content = new stdClass;
$this->content->footer = ' ';

$adminseesall = true;

if (isset($CFG->block_course_list_adminview)) {
if ($CFG->block_course_list_adminview == 'own') {
$adminseesall = false;
}
}

$html = '<div id="custom_course_menu_application">'
. '<div id="custom_course_menu_dynamic">'
. '<span class="interface">' . get_string('loading', 'block_custom_course_menu') . '</span>'
Expand Down Expand Up @@ -115,7 +107,9 @@ public function get_content() {

$this->content->footer = $footer;

// Takes into account hidden courses and permissions to get viewable count.
$courses = enrol_get_my_courses();

$hidelink = array();
if (empty($courses) && empty($CFG->block_custom_course_menu_enablelastviewed)) {
$hidelink = array('class' => 'hidden');
Expand Down
107 changes: 58 additions & 49 deletions interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,36 +222,38 @@
* @return array
*/
function get_category_tree() {
global $DB;
global $DB, $USER;

$categorymeta = get_meta_for('category');
$coursemeta = get_meta_for('course');

$courses = enrol_get_my_courses();
$courses = enrol_get_all_users_courses($USER->id, true);

$categories = array();
foreach ($courses as $course) {
if (!isset($categories[$course->category])) {
$params = array('id' => $course->category);
$category = $DB->get_record('course_categories', $params);
$category->courses = array();
if ($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
if (!isset($categories[$course->category])) {
$params = array('id' => $course->category);
$category = $DB->get_record('course_categories', $params);
$category->courses = array();

if (isset($categorymeta[$category->id])) {
$category->meta = $categorymeta[$category->id];
} else {
$category->meta = (object) array('hide' => 0);
if (isset($categorymeta[$category->id])) {
$category->meta = $categorymeta[$category->id];
} else {
$category->meta = (object) array('hide' => 0);
}

$categories[$course->category] = $category;
}

$categories[$course->category] = $category;
}
if (isset($coursemeta[$course->id])) {
$course->meta = $coursemeta[$course->id];
} else {
$course->meta = (object) array('hide' => 0, 'fav' => 0);
}

if (isset($coursemeta[$course->id])) {
$course->meta = $coursemeta[$course->id];
} else {
$course->meta = (object) array('hide' => 0, 'fav' => 0);
$categories[$course->category]->courses[$course->id] = $course;
}

$categories[$course->category]->courses[$course->id] = $course;
}

return $categories;
Expand Down Expand Up @@ -300,43 +302,16 @@ function sort_my_categories($categories) {
*/
function get_last_viewed() {
global $CFG, $DB, $USER;
require_once($CFG->dirroot.'/course/lib.php');
$categorymeta = get_meta_for('category');
$lva = get_config('block_custom_course_menu')->lastviewedamount;
if ($CFG->version < 2014051200) { // Moodle < 2.7.
$sql = "SELECT *
FROM {log} a
INNER JOIN (SELECT c.*, course, MAX(time) as time
FROM {log} l
JOIN {course} c
ON c.id = l.course
WHERE userid = '$USER->id'
AND course != 1
AND module = 'course'
GROUP BY course) b
ON a.course = b.course
AND a.time = b.time
GROUP BY a.course
ORDER BY b.time DESC
LIMIT $lva";
} else { // Moodle 2.7+.
$sql = "SELECT a.courseid, max(a.timecreated) as date, a.userid
FROM (SELECT *
FROM {logstore_standard_log}
WHERE courseid !=0
AND courseid !=1) AS a
WHERE a.userid = '$USER->id'
AND a.origin != 'cli'
GROUP BY a.userid, a.courseid
ORDER BY date DESC
LIMIT $lva";
}

$latestcourses = $DB->get_records_sql($sql);
$courses = get_last_viewed_courses($USER->id, $lva);

$categories = array();
$order = 1;
foreach ($latestcourses as $latest) {
if ($course = $DB->get_record('course', array('id' => $latest->courseid))) {
foreach ($courses as $course) {
if ($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
if (!isset($categories[-2])) {
$category = new stdClass();
$category->name = get_string('lastxviewed', 'block_custom_course_menu', $lva);
Expand All @@ -359,6 +334,40 @@ function get_last_viewed() {
return $categories;
}

/**
* Returns a list of the most recently courses accessed by a user
*
* @param int $userid User id from which the courses will be obtained
* @param int $limit Restrict result set to this amount
* @return array
*/
function get_last_viewed_courses(int $userid = null, int $limit = 0) {
global $CFG, $USER, $DB;

if (empty($userid)) {
$userid = $USER->id;
}

$basefields = [
'id', 'idnumber', 'category',
'shortname', 'fullname', 'timeaccess', 'visible'];

$coursefields = 'c.' . join(',', $basefields);

$sql = "SELECT $coursefields
FROM {course} c
JOIN {user_lastaccess} ul
ON ul.courseid = c.id
AND ul.userid = :userid
ORDER BY timeaccess DESC";

$params = ['userid' => $userid];

$recentcourses = $DB->get_records_sql($sql, $params, 0, $limit);

return $recentcourses;
}

/**
* Helper method to pull get the users favorite courses
*
Expand Down
73 changes: 0 additions & 73 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,76 +101,3 @@
.block_custom_course_menu .searchfield {
margin: 0;
}
/* Layout helpers
----------------------------------*/
.ui-helper-hidden {
display: none;
}
.ui-helper-hidden-accessible {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
.ui-helper-reset {
border: 0;
font-size: 100%;
line-height: 1.3;
list-style: none;
margin: 0;
outline: 0;
padding: 0;
text-decoration: none;
}
.ui-helper-clearfix:before,
.ui-helper-clearfix:after {
border-collapse: collapse;
content: "";
display: table;
}
.ui-helper-clearfix:after {
clear: both;
}
.ui-helper-clearfix {
min-height: 0; /* support: IE7 */
}
.ui-helper-zfix {
filter: alpha(Opacity=0);
height: 100%;
left: 0;
opacity: 0;
position: absolute;
top: 0;
width: 100%;
}
.ui-front {
z-index: 100;
}
/* Interaction Cues
----------------------------------*/
.ui-state-disabled {
cursor: default;
}
/* Icons
----------------------------------*/
/* states and images */
.ui-icon {
background-repeat: no-repeat;
display: block;
overflow: hidden;
text-indent: -99999px;
}
/* Misc visuals
----------------------------------*/
/* Overlays */
.ui-widget-overlay {
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
}
17 changes: 3 additions & 14 deletions tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
* @category test
* @copyright 2015 onwards University of Portland (www.up.edu)
* @copyright 2020 unistra {@link http://unistra.fr}
* @author Céline Pervès <[email protected]>
* @author Céline Pervès <[email protected]>
* @covers \block_custom_course_menu\provider
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \core_privacy\tests\provider_testcase {
Expand All @@ -45,14 +46,12 @@ public function setUp(): void {
/**
* Test get_contexts_for_userid function.
* Function that get the list of contexts that contain user information for the specified user.
* @throws coding_exception
*/
public function test_get_contexts_for_userid() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$this->setUser($user);
$usercontext = \context_user::instance($user->id);

// Create block.
$this->create_block_instance();
// Create datas.
Expand All @@ -68,12 +67,10 @@ public function test_get_contexts_for_userid() {
/**
* Test expert_user_data function.
* Function that get the list of users who have data within a context.
* @throws coding_exception
*/
public function test_export_user_data() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$this->setUser($user);
$usercontext = \context_user::instance($user->id);
// Create block.
Expand Down Expand Up @@ -112,12 +109,10 @@ public function test_export_user_data() {
/**
* Test delete_data_for_all_users_in_context function.
* Function that delete all data for all users in the specified context
* @throws coding_exception
*/
public function test_delete_data_for_all_users_in_context() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$this->setUser($user);
$usercontext = \context_user::instance($user->id);
// Create block.
Expand All @@ -131,13 +126,10 @@ public function test_delete_data_for_all_users_in_context() {
/**
* Test delete_data_for_user function.
* Function that delete all user data for the specified user, in the specified contexts.
* @throws coding_exception
* @throws dml_exception
*/
public function test_delete_data_for_user() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$this->setUser($user);
$usercontext = \context_user::instance($user->id);
// Create block.
Expand All @@ -156,12 +148,10 @@ public function test_delete_data_for_user() {
/**
* Test delete_data_for_users function.
* Function that Delete multiple users within a single context.
* @throws coding_exception
*/
public function test_delete_data_for_users() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$this->setUser($user);
$usercontext = \context_user::instance($user->id);
// Create block.
Expand All @@ -175,7 +165,6 @@ public function test_delete_data_for_users() {

/**
* Create a block instance
* @throws coding_exception
*/
private function create_block_instance() {
global $SITE;
Expand Down

0 comments on commit 7866750

Please sign in to comment.