MDL-76652 behat: use data generator and remove duplicated steps
[moodle.git] / report / participation / locallib.php
blob8ab59b47a9b8c6e8772eac937dc82a06872e5c98
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 legacy reader is preferred reader.
42 if ($readerpluginname == 'logstore_legacy') {
43 break;
46 // If sql_internal_table_reader is preferred reader.
47 if ($reader instanceof \core\log\sql_internal_table_reader) {
48 $logtable = $reader->get_internal_log_table_name();
49 break;
53 return $logtable;
56 /**
57 * Return time options, which should be shown for record filtering.
59 * @param int $minlog Time of first log record available.
60 * @return array time options.
62 function report_participation_get_time_options($minlog) {
63 $timeoptions = array();
64 $now = usergetmidnight(time());
66 // Days.
67 for ($i = 1; $i < 7; $i++) {
68 if (strtotime('-'.$i.' days',$now) >= $minlog) {
69 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
72 // Weeks.
73 for ($i = 1; $i < 10; $i++) {
74 if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
75 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
78 // Months.
79 for ($i = 2; $i < 12; $i++) {
80 if (strtotime('-'.$i.' months',$now) >= $minlog) {
81 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
84 // Try a year.
85 if (strtotime('-1 year',$now) >= $minlog) {
86 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
88 return $timeoptions;
91 /**
92 * Return action sql and params.
94 * @param string $action action to be filtered.
95 * @param string $modname module name.
96 * @return array actionsql and actionparams.
98 function report_participation_get_action_sql($action, $modname) {
99 global $CFG, $DB;
101 $actionsql = '';
102 $actionparams = array();
104 $viewnames = array();
105 $postnames = array();
106 include_once($CFG->dirroot.'/mod/' . $modname . '/lib.php');
108 $viewfun = $modname.'_get_view_actions';
109 $postfun = $modname.'_get_post_actions';
111 if (function_exists($viewfun)) {
112 $viewnames = $viewfun();
115 if (function_exists($postfun)) {
116 $postnames = $postfun();
119 switch ($action) {
120 case 'view':
121 $actions = $viewnames;
122 break;
123 case 'post':
124 $actions = $postnames;
125 break;
126 default:
127 // Some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
128 $actions = array_merge($viewnames, $postnames);
131 if (!empty($actions)) {
132 list($actionsql, $actionparams) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action');
133 $actionsql = " AND action $actionsql";
136 return array($actionsql, $actionparams);
140 * Return crud sql and params.
142 * @param string $action action to be filtered.
143 * @return array crudsql and crudparams.
145 function report_participation_get_crud_sql($action) {
146 global $DB;
148 switch ($action) {
149 case 'view':
150 $crud = 'r';
151 break;
152 case 'post':
153 $crud = array('c', 'u', 'd');
154 break;
155 default:
156 $crud = array('c', 'r', 'u', 'd');
159 list($crudsql, $crudparams) = $DB->get_in_or_equal($crud, SQL_PARAMS_NAMED, 'crud');
160 $crudsql = " AND crud " . $crudsql;
161 return array($crudsql, $crudparams);
165 * List of action filters.
167 * @return array
169 function report_participation_get_action_options() {
170 return array('' => get_string('allactions'),
171 'view' => get_string('view'),
172 'post' => get_string('post'),);
176 * Print filter form.
178 * @param stdClass $course course object.
179 * @param int $timefrom Time from which records should be fetched.
180 * @param int $minlog Time of first record present in log store.
181 * @param string $action action to be filtered.
182 * @param int $roleid Role to be filtered.
183 * @param int $instanceid Instance id of module.
185 function report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid) {
186 global $DB;
188 $timeoptions = report_participation_get_time_options($minlog);
190 $actionoptions = report_participation_get_action_options();
192 // TODO: we need a new list of roles that are visible here.
193 $context = context_course::instance($course->id);
194 $roles = get_roles_used_in_context($context);
195 $guestrole = get_guest_role();
196 $roles[$guestrole->id] = $guestrole;
197 $roleoptions = role_fix_names($roles, $context, ROLENAME_ALIAS, true);
199 $modinfo = get_fast_modinfo($course);
201 $modules = $DB->get_records_select('modules', "visible = 1", null, 'name ASC');
203 $instanceoptions = array();
204 foreach ($modules as $module) {
205 if (empty($modinfo->instances[$module->name])) {
206 continue;
208 $instances = array();
209 foreach ($modinfo->instances[$module->name] as $cm) {
210 // Skip modules such as label which do not actually have links;
211 // this means there's nothing to participate in.
212 if (!$cm->has_view()) {
213 continue;
215 $instances[$cm->id] = format_string($cm->name);
217 if (count($instances) == 0) {
218 continue;
220 $instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
223 echo '<form class="participationselectform form-inline" action="index.php" method="get"><div>'."\n".
224 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
225 echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
226 echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
227 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
228 echo html_writer::select($timeoptions,'timefrom',$timefrom);
229 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
230 echo html_writer::select($roleoptions,'roleid',$roleid,false);
231 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
232 echo html_writer::select($actionoptions, 'action', $action, false, ['class' => 'mr-1']);
233 echo '<input type="submit" value="'.get_string('go').'" class="btn btn-primary"/>'."\n</div></form>\n";