MDL-66132 Search: Behat tests should use simpledb, fix mock count
[moodle.git] / report / completion / lib.php
blobd60a5cc18bb84f1467b0f5c183a9e5ed304d8535
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Version details.
20 * @package report
21 * @subpackage completion
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die;
28 /**
29 * This function extends the navigation with the report items
31 * @param navigation_node $navigation The navigation node to extend
32 * @param stdClass $course The course to object for the report
33 * @param stdClass $context The context of the course
35 function report_completion_extend_navigation_course($navigation, $course, $context) {
36 global $CFG;
38 require_once($CFG->libdir.'/completionlib.php');
40 if (has_capability('report/completion:view', $context)) {
41 $completion = new completion_info($course);
42 if ($completion->is_enabled() && $completion->has_criteria()) {
43 $url = new moodle_url('/report/completion/index.php', array('course'=>$course->id));
44 $navigation->add(get_string('pluginname','report_completion'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
49 /**
50 * This function extends the course navigation with the report items
52 * @param navigation_node $navigation The navigation node to extend
53 * @param stdClass $user
54 * @param stdClass $course The course to object for the report
56 function report_completion_extend_navigation_user($navigation, $user, $course) {
58 return; //TODO: this plugin was not linked from navigation in 2.0, let's keep it that way for now --skodak
60 if (report_completion_can_access_user_report($user, $course)) {
61 $url = new moodle_url('/report/completion/user.php', array('id'=>$user->id, 'course'=>$course->id));
62 $navigation->add(get_string('coursecompletion'), $url);
66 /**
67 * Is current user allowed to access this report
69 * @private defined in lib.php for performance reasons
71 * @param stdClass $user
72 * @param stdClass $course
73 * @return bool
75 function report_completion_can_access_user_report($user, $course) {
76 global $USER, $CFG;
78 if (empty($CFG->enablecompletion)) {
79 return false;
82 if ($course->id != SITEID and !$course->enablecompletion) {
83 return false;
86 $coursecontext = context_course::instance($course->id);
87 $personalcontext = context_user::instance($user->id);
89 if ($user->id == $USER->id) {
90 if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) {
91 return true;
93 } else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
94 if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) {
95 return true;
100 // Check if $USER shares group with $user (in case separated groups are enabled and 'moodle/site:accessallgroups' is disabled).
101 if (!groups_user_groups_visible($course, $user->id)) {
102 return false;
105 if (has_capability('report/completion:view', $coursecontext)) {
106 return true;
109 return false;
113 * Return a list of page types
114 * @param string $pagetype current page type
115 * @param stdClass $parentcontext Block's parent context
116 * @param stdClass $currentcontext Current context of block
117 * @return array
119 function report_completion_page_type_list($pagetype, $parentcontext, $currentcontext) {
120 $array = array(
121 '*' => get_string('page-x', 'pagetype'),
122 'report-*' => get_string('page-report-x', 'pagetype'),
123 'report-completion-*' => get_string('page-report-completion-x', 'report_completion'),
124 'report-completion-index' => get_string('page-report-completion-index', 'report_completion'),
125 'report-completion-user' => get_string('page-report-completion-user', 'report_completion')
127 return $array;