Merge branch 'MDL-78408-402' of https://github.com/snake/moodle into MOODLE_402_STABLE
[moodle.git] / report / participation / locallib.php
blob7ef53b5fd2b5db8dc51039313ecbbf3e6fcc6707
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 * This file contains functions used by the participation reports
20 * @package report_participation
21 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Returns log table name of preferred reader, if leagcy then return empty string.
30 * @return string table name
32 function report_participation_get_log_table_name() {
33 // Get prefered sql_internal_table_reader reader (if enabled).
34 $logmanager = get_log_manager();
35 $readers = $logmanager->get_readers();
36 $logtable = '';
38 // Get preferred reader.
39 if (!empty($readers)) {
40 foreach ($readers as $readerpluginname => $reader) {
41 // If sql_internal_table_reader is preferred reader.
42 if ($reader instanceof \core\log\sql_internal_table_reader) {
43 $logtable = $reader->get_internal_log_table_name();
44 break;
48 return $logtable;
51 /**
52 * Return time options, which should be shown for record filtering.
54 * @param int $minlog Time of first log record available.
55 * @return array time options.
57 function report_participation_get_time_options($minlog) {
58 $timeoptions = array();
59 $now = usergetmidnight(time());
61 // Days.
62 for ($i = 1; $i < 7; $i++) {
63 if (strtotime('-'.$i.' days',$now) >= $minlog) {
64 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
67 // Weeks.
68 for ($i = 1; $i < 10; $i++) {
69 if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
70 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
73 // Months.
74 for ($i = 2; $i < 12; $i++) {
75 if (strtotime('-'.$i.' months',$now) >= $minlog) {
76 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
79 // Try a year.
80 if (strtotime('-1 year',$now) >= $minlog) {
81 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
83 return $timeoptions;
86 /**
87 * Return action sql and params.
89 * @param string $action action to be filtered.
90 * @param string $modname module name.
91 * @return array actionsql and actionparams.
93 function report_participation_get_action_sql($action, $modname) {
94 global $CFG, $DB;
96 $actionsql = '';
97 $actionparams = array();
99 $viewnames = array();
100 $postnames = array();
101 include_once($CFG->dirroot.'/mod/' . $modname . '/lib.php');
103 $viewfun = $modname.'_get_view_actions';
104 $postfun = $modname.'_get_post_actions';
106 if (function_exists($viewfun)) {
107 $viewnames = $viewfun();
110 if (function_exists($postfun)) {
111 $postnames = $postfun();
114 switch ($action) {
115 case 'view':
116 $actions = $viewnames;
117 break;
118 case 'post':
119 $actions = $postnames;
120 break;
121 default:
122 // Some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
123 $actions = array_merge($viewnames, $postnames);
126 if (!empty($actions)) {
127 list($actionsql, $actionparams) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action');
128 $actionsql = " AND action $actionsql";
131 return array($actionsql, $actionparams);
135 * Return crud sql and params.
137 * @param string $action action to be filtered.
138 * @return array crudsql and crudparams.
140 function report_participation_get_crud_sql($action) {
141 global $DB;
143 switch ($action) {
144 case 'view':
145 $crud = 'r';
146 break;
147 case 'post':
148 $crud = array('c', 'u', 'd');
149 break;
150 default:
151 $crud = array('c', 'r', 'u', 'd');
154 list($crudsql, $crudparams) = $DB->get_in_or_equal($crud, SQL_PARAMS_NAMED, 'crud');
155 $crudsql = " AND crud " . $crudsql;
156 return array($crudsql, $crudparams);
160 * List of action filters.
162 * @return array
164 function report_participation_get_action_options() {
165 return array('' => get_string('allactions'),
166 'view' => get_string('view'),
167 'post' => get_string('post'),);
171 * Print filter form.
173 * @param stdClass $course course object.
174 * @param int $timefrom Time from which records should be fetched.
175 * @param int $minlog Time of first record present in log store.
176 * @param string $action action to be filtered.
177 * @param int $roleid Role to be filtered.
178 * @param int $instanceid Instance id of module.
180 function report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid) {
181 global $DB;
183 $timeoptions = report_participation_get_time_options($minlog);
185 $actionoptions = report_participation_get_action_options();
187 $context = context_course::instance($course->id);
188 $roles = get_roles_used_in_context($context);
189 $rolesviewable = get_viewable_roles($context);
191 $guestrole = get_guest_role();
192 $roleoptions = array_intersect_key($rolesviewable, $roles) + [
193 $guestrole->id => role_get_name($guestrole, $context),
196 $modinfo = get_fast_modinfo($course);
198 $modules = $DB->get_records_select('modules', "visible = 1", null, 'name ASC');
200 $instanceoptions = array();
201 foreach ($modules as $module) {
202 if (empty($modinfo->instances[$module->name])) {
203 continue;
205 $instances = array();
206 foreach ($modinfo->instances[$module->name] as $cm) {
207 // Skip modules such as label which do not actually have links;
208 // this means there's nothing to participate in.
209 if (!$cm->has_view()) {
210 continue;
212 $instances[$cm->id] = format_string($cm->name);
214 if (count($instances) == 0) {
215 continue;
217 $instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
220 echo '<form class="participationselectform form-inline" action="index.php" method="get"><div>'."\n".
221 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
222 echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
223 echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
224 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
225 echo html_writer::select($timeoptions,'timefrom',$timefrom);
226 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
227 echo html_writer::select($roleoptions,'roleid',$roleid,false);
228 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
229 echo html_writer::select($actionoptions, 'action', $action, false, ['class' => 'mr-1']);
230 echo '<input type="submit" value="'.get_string('go').'" class="btn btn-primary"/>'."\n</div></form>\n";