Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * This file contains functions used by the outline reports
19
 *
20
 * @package    report
21
 * @subpackage outline
22
 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
1441 ariadna 26
use core\output\local\properties\iconsize;
27
use core_course\output\activity_icon;
28
 
1 efrain 29
defined('MOODLE_INTERNAL') || die;
30
 
31
require_once(__DIR__.'/lib.php');
32
require_once($CFG->dirroot.'/course/lib.php');
33
 
34
function report_outline_print_row($mod, $instance, $result) {
35
    global $OUTPUT, $CFG;
36
 
1441 ariadna 37
    $activityicon = activity_icon::from_cm_info($mod)
38
        ->set_icon_size(iconsize::SIZE4);
39
    $image = $OUTPUT->render($activityicon);
1 efrain 40
 
41
    echo "<tr>";
42
    echo "<td valign=\"top\">$image</td>";
43
    echo "<td valign=\"top\" style=\"width:300\">";
44
    echo "   <a title=\"$mod->modfullname\"";
45
    echo "   href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
46
    echo "<td>&nbsp;&nbsp;&nbsp;</td>";
47
    echo "<td valign=\"top\">";
48
    if (isset($result->info)) {
49
        echo "$result->info";
50
    } else {
51
        echo "<p style=\"text-align:center\">-</p>";
52
    }
53
    echo "</td>";
54
    echo "<td>&nbsp;&nbsp;&nbsp;</td>";
55
    if (!empty($result->time)) {
56
        $timeago = format_time(time() - $result->time);
57
        echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
58
    }
59
    echo "</tr>";
60
}
61
 
62
/**
63
 * Returns an array of the commonly used log variables by the outline report.
64
 *
65
 * @return array the array of variables used
66
 */
67
function report_outline_get_common_log_variables() {
68
    global $DB;
69
 
70
    static $uselegacyreader;
71
    static $useinternalreader;
72
    static $minloginternalreader;
73
    static $logtable = null;
74
 
75
    if (isset($uselegacyreader) && isset($useinternalreader) && isset($minloginternalreader)) {
76
        return array($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable);
77
    }
78
 
79
    $uselegacyreader = false; // Flag to determine if we should use the legacy reader.
80
    $useinternalreader = false; // Flag to determine if we should use the internal reader.
81
    $minloginternalreader = 0; // Set this to 0 for now.
82
 
83
    // Get list of readers.
84
    $logmanager = get_log_manager();
85
    $readers = $logmanager->get_readers();
86
 
87
    // Get preferred reader.
88
    if (!empty($readers)) {
89
        foreach ($readers as $readerpluginname => $reader) {
90
            // If sql_internal_table_reader is preferred reader.
91
            if ($reader instanceof \core\log\sql_internal_table_reader) {
92
                $useinternalreader = true;
93
                $logtable = $reader->get_internal_log_table_name();
94
                $minloginternalreader = $DB->get_field_sql('SELECT min(timecreated) FROM {' . $logtable . '}');
95
            }
96
        }
97
    }
98
 
99
    return array($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable);
100
}
101
 
102
/**
103
 * Return the most commonly used user outline information.
104
 *
105
 * @param int $userid the id of the user
106
 * @param int $cmid the course module id
107
 * @param string $module the name of the module (eg. 'book')
108
 * @param int $instanceid (eg. the 'id' in the 'book' table)
109
 * @return stdClass|null if any information is found then a stdClass containing
110
 *  this info is returned, else null is.
111
 */
112
function report_outline_user_outline($userid, $cmid, $module, $instanceid) {
113
    global $DB;
114
 
115
    list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
116
 
117
    // If using legacy log then get users from old table.
118
    if ($uselegacyreader) {
119
        // Create the params for the query.
120
        $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
121
        // If we are going to use the internal (not legacy) log table, we should only get records
122
        // from the legacy table that exist before we started adding logs to the new table.
123
        $limittime = '';
124
        if (!empty($minloginternalreader)) {
125
            $limittime = ' AND time < :timeto ';
126
            $params['timeto'] = $minloginternalreader;
127
        }
128
        $select = "SELECT COUNT(id) ";
129
        $from = "FROM {log} ";
130
        $where = "WHERE userid = :userid
131
                    AND module = :module
132
                    AND action = :action
133
                    AND info = :info ";
134
        if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
135
            $numviews = $legacylogcount;
136
 
137
            // Get the time for the last log.
138
            $select = "SELECT MAX(time) ";
139
            $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
140
 
141
            $result = new stdClass();
142
            $result->info = get_string('numviews', '', $numviews);
143
            $result->time = $lastlogtime;
144
        }
145
    }
146
 
147
    // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
148
    if ($useinternalreader) {
149
        $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
150
            'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
151
            'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
152
        $select = "SELECT COUNT(*) as count ";
153
        $from = "FROM {" . $logtable . "} ";
154
        $where = "WHERE userid = :userid
155
                    AND contextlevel = :contextlevel
156
                    AND contextinstanceid = :contextinstanceid
157
                    AND crud = :crud
158
                    AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
159
                    AND anonymous = :anonymous";
160
        if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
161
            if (!empty($numviews)) {
162
                $numviews = $numviews + $internalreadercount;
163
            } else {
164
                $numviews = $internalreadercount;
165
            }
166
 
167
            // Get the time for the last log.
168
            $select = "SELECT MAX(timecreated) ";
169
            $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
170
 
171
            $result = new stdClass();
172
            $result->info = get_string('numviews', '', $numviews);
173
            $result->time = $lastlogtime;
174
        }
175
    }
176
 
177
    if (!empty($result)) {
178
        return $result;
179
    }
180
 
181
    return null;
182
}
183
 
184
/**
185
 * Display the most commonly used user complete information.
186
 *
187
 * @param int $userid the id of the user
188
 * @param int $cmid the course module id
189
 * @param string $module the name of the module (eg. 'book')
190
 * @param int $instanceid (eg. the 'id' in the 'book' table)
191
 * @return string
192
 */
193
function report_outline_user_complete($userid, $cmid, $module, $instanceid) {
194
    global $DB;
195
 
196
    list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
197
 
198
    // If using legacy log then get users from old table.
199
    if ($uselegacyreader) {
200
        // Create the params for the query.
201
        $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
202
        // If we are going to use the internal (not legacy) log table, we should only get records
203
        // from the legacy table that exist before we started adding logs to the new table.
204
        $limittime = '';
205
        if (!empty($minloginternalreader)) {
206
            $limittime = ' AND time < :timeto ';
207
            $params['timeto'] = $minloginternalreader;
208
        }
209
        $select = "SELECT COUNT(id) ";
210
        $from = "FROM {log} ";
211
        $where = "WHERE userid = :userid
212
                    AND module = :module
213
                    AND action = :action
214
                    AND info = :info ";
215
        if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
216
            $numviews = $legacylogcount;
217
 
218
            // Get the time for the last log.
219
            $select = "SELECT MAX(time) ";
220
            $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
221
 
222
            $strnumviews = get_string('numviews', '', $numviews);
223
        }
224
    }
225
 
226
    // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
227
    if ($useinternalreader) {
228
        $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
229
            'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
230
            'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
231
        $select = "SELECT COUNT(*) as count ";
232
        $from = "FROM {" . $logtable . "} ";
233
        $where = "WHERE userid = :userid
234
                    AND contextlevel = :contextlevel
235
                    AND contextinstanceid = :contextinstanceid
236
                    AND crud = :crud
237
                    AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
238
                    AND anonymous = :anonymous";
239
        if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
240
            if (!empty($numviews)) {
241
                $numviews = $numviews + $internalreadercount;
242
            } else {
243
                $numviews = $internalreadercount;
244
            }
245
 
246
            // Get the time for the last log.
247
            $select = "SELECT MAX(timecreated) ";
248
            $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
249
 
250
            $strnumviews = get_string('numviews', '', $numviews);
251
        }
252
    }
253
 
254
    if (!empty($strnumviews) && (!empty($lastlogtime))) {
255
        return $strnumviews . ' - ' . get_string('mostrecently') . ' ' . userdate($lastlogtime);
256
    } else {
257
        return get_string('neverseen', 'report_outline');
258
    }
259
}