-
Notifications
You must be signed in to change notification settings - Fork 8
/
reportclasses.php
226 lines (191 loc) · 5.73 KB
/
reportclasses.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?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/>.
/**
* Report Classes.
*
* @package mod_@@newmodule@@
* @copyright COPYRIGHTNOTICE
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Classes for Reports
*
* The important functions are:
* process_raw_data : turns log data for one thig (question attempt) into one row
* fetch_formatted_fields: uses data prepared in process_raw_data to make each field in fields full of formatted data
* The allusers report is the simplest example
*
* @package mod_@@newmodule@@
* @copyright COPYRIGHTNOTICE
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class mod_@@newmodule@@_base_report {
protected $report="";
protected $head=array();
protected $rawdata=null;
protected $fields = array();
protected $dbcache=array();
abstract function process_raw_data($formdata,$paging=false);
abstract function fetch_formatted_heading();
public function fetch_fields(){
return $this->fields;
}
public function fetch_head(){
$head=array();
foreach($this->fields as $field){
$head[]=get_string($field,MOD_NEWMODULE_LANG);
}
return $head;
}
public function fetch_name(){
return $this->report;
}
public function truncate($string, $maxlength){
if(strlen($string)>$maxlength){
$string=substr($string,0,$maxlength - 2) . '..';
}
return $string;
}
public function fetch_cache($table,$rowid){
global $DB;
if(!array_key_exists($table,$this->dbcache)){
$this->dbcache[$table]=array();
}
if(!array_key_exists($rowid,$this->dbcache[$table])){
$this->dbcache[$table][$rowid]=$DB->get_record($table,array('id'=>$rowid));
}
return $this->dbcache[$table][$rowid];
}
public function fetch_formatted_time($seconds){
//return empty string if the timestamps are not both present.
if(!$seconds){return '';}
$time=time();
return $this->fetch_time_difference($time, $time + $seconds);
}
public function fetch_time_difference($starttimestamp,$endtimestamp){
//return empty string if the timestamps are not both present.
if(!$starttimestamp || !$endtimestamp){return '';}
$s = $date = new DateTime();
$s->setTimestamp($starttimestamp);
$e =$date = new DateTime();
$e->setTimestamp($endtimestamp);
$diff = $e->diff($s);
$ret = $diff->format("%H:%I:%S");
return $ret;
}
public function fetch_all_rows_count(){
return $this->rawdata ? count($this->rawdata) : 0;
}
public function fetch_formatted_rows($withlinks=true,$paging=false){
$records = $this->rawdata;
$fields = $this->fields;
$returndata = array();
//if we are paging, prepare start and end
if($paging){
$startrecord = ($paging->perpage * $paging->pageno) + 1;
$endrecord = $startrecord + $paging->perpage - 1;
}
$reccount = 0;
//loop through each record and prepare it for output
foreach($records as $record){
//if paging, limit what we return
//this is a hack method, best to do it in process_raw_data really
$reccount++;
if($paging && ($reccount < $startrecord || $reccount > $endrecord)){
continue;
}
$data = new stdClass();
foreach($fields as $field){
$data->{$field}=$this->fetch_formatted_field($field,$record,$withlinks);
}//end of for each field
$returndata[]=$data;
}//end of for each record
return $returndata;
}
public function fetch_formatted_field($field,$record,$withlinks){
global $DB;
switch($field){
case 'timecreated':
$ret = date("Y-m-d H:i:s",$record->timecreated);
break;
case 'userid':
$u = $this->fetch_cache('user',$record->userid);
$ret =fullname($u);
break;
default:
if(property_exists($record,$field)){
$ret=$record->{$field};
}else{
$ret = '';
}
}
return $ret;
}
}
/*
* Nasic Report
*
*
*/
class mod_@@newmodule@@_basic_report extends mod_@@newmodule@@_base_report {
protected $report="basic";
protected $fields = array('id','name','timecreated');
protected $headingdata = null;
protected $qcache=array();
protected $ucache=array();
public function fetch_formatted_field($field,$record,$withlinks){
global $DB;
switch($field){
case 'id':
$ret = $record->id;
break;
case 'name':
$ret = $record->name;
break;
case 'timecreated':
$ret = date("Y-m-d H:i:s",$record->timecreated);
break;
default:
if(property_exists($record,$field)){
$ret=$record->{$field};
}else{
$ret = '';
}
}
return $ret;
}
public function fetch_formatted_heading(){
$record = $this->headingdata;
$ret='';
if(!$record){return $ret;}
//$ec = $this->fetch_cache(MOD_NEWMODULE_TABLE,$record->englishcentralid);
return get_string('basicheading',MOD_NEWMODULE_LANG);
}
public function process_raw_data($formdata,$paging=false){
global $DB;
//heading data
$this->headingdata = new stdClass();
$emptydata = array();
$alldata = $DB->get_records(MOD_NEWMODULE_TABLE,array());
if($alldata){
$this->rawdata= $alldata;
}else{
$this->rawdata= $emptydata;
}
return true;
}
}