-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
98 lines (81 loc) · 3.81 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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/>.
/**
* This file contains the code for the plugin integration.
*
* @package local_cohort_profile
* @copyright 2024, Yuriy Yurinskiy <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* LOCAL_COHORT_PROFILE_COHORT_LIMIT - number of cohorts to be derived
*/
define('LOCAL_COHORT_PROFILE_COHORT_LIMIT', 10);
/**
* To add the category and node information into the my profile page.
*
* @param core_user\output\myprofile\tree $tree The myprofile tree to add categories and nodes to.
* @param stdClass $user The user object that the profile page belongs to.
* @param bool $iscurrentuser If the $user object is the current user.
* @param stdClass $course The course to determine if we are in a course context or system context.
* @return void
*/
function local_cohort_profile_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . '/cohort/lib.php');
$context = context_user::instance($user->id);
$hascapability = has_capability('local/cohort_profile:view_cohort_list', $context);
$ownprofile = $user->id == $USER->id;
# Don't show if user doesn't have capability or is not viewing their own profile
if (!$hascapability && !$ownprofile) {
return;
}
$showallcohorts = optional_param('showallcohorts', 0, PARAM_INT);
$sql = 'SELECT %s FROM {cohort} c
JOIN {cohort_members} cm ON c.id = cm.cohortid
WHERE cm.userid = ?';
if (!is_siteadmin()) {
$sql .= ' AND c.visible = 1';
}
if ($showallcohorts) {
$cohorts = $DB->get_records_sql(sprintf($sql, 'c.*'), array($user->id));
$countcohorts = count($cohorts);
} else {
$cohorts = $DB->get_records_sql(sprintf($sql, 'c.*'), array($user->id), 0, LOCAL_COHORT_PROFILE_COHORT_LIMIT);
$countcohorts = $DB->count_records_sql(sprintf($sql, 'count(c.id)'), array($user->id));
}
if ($cohorts) {
$cohortdetailscategory = new core_user\output\myprofile\category('cohortdetails', get_string('cohorts', 'core_cohort'));
$tree->add_category($cohortdetailscategory);
$cohortslisting = '';
foreach ($cohorts as $cohort) {
$attr = null;
if (0 == $cohort->visible) {
$attr['class'] = 'dimmed_text';
$attr['title'] = get_string('hidden', 'local_cohort_profile');
}
$cohortslisting .= html_writer::tag('dd', $cohort->name, $attr);
}
if (!$showallcohorts && $countcohorts > count($cohorts)) {
$url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcohorts' => 1));
$cohortslisting .= html_writer::tag('dd', html_writer::link($url, get_string('viewmore'),
array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
}
$node = new core_user\output\myprofile\node('cohortdetails', 'cohortprofile', $cohortslisting);
$tree->add_node($node);
}
}