|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 defined('MOODLE_INTERNAL') || die; 00027 00028 if (!defined('REPORT_LOG_MAX_DISPLAY')) { 00029 define('REPORT_LOG_MAX_DISPLAY', 150); // days 00030 } 00031 00032 require_once(dirname(__FILE__).'/lib.php'); 00033 00034 function report_log_print_graph($course, $userid, $type, $date=0) { 00035 global $CFG; 00036 00037 if (empty($CFG->gdversion)) { 00038 echo "(".get_string("gdneed").")"; 00039 } else { 00040 echo '<img src="'.$CFG->wwwroot.'/report/log/graph.php?id='.$course->id. 00041 '&user='.$userid.'&type='.$type.'&date='.$date.'" alt="" />'; 00042 } 00043 } 00044 00045 function report_log_print_mnet_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today', 00046 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') { 00047 00048 global $USER, $CFG, $SITE, $DB, $OUTPUT, $SESSION; 00049 require_once $CFG->dirroot.'/mnet/peer.php'; 00050 00051 $mnet_peer = new mnet_peer(); 00052 $mnet_peer->set_id($hostid); 00053 00054 $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}"; 00055 $courses = $DB->get_records_sql($sql); 00056 $remotecoursecount = count($courses); 00057 00058 // first check to see if we can override showcourses and showusers 00059 $numcourses = $remotecoursecount + $DB->count_records('course'); 00060 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) { 00061 $showcourses = 1; 00062 } 00063 00064 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00065 00066 // Context for remote data is always SITE 00067 // Groups for remote data are always OFF 00068 if ($hostid == $CFG->mnet_localhost_id) { 00069 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00070 00072 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { 00073 $selectedgroup = -1; 00074 $showgroups = false; 00075 } else if ($course->groupmode) { 00076 $showgroups = true; 00077 } else { 00078 $selectedgroup = 0; 00079 $showgroups = false; 00080 } 00081 00082 if ($selectedgroup === -1) { 00083 if (isset($SESSION->currentgroup[$course->id])) { 00084 $selectedgroup = $SESSION->currentgroup[$course->id]; 00085 } else { 00086 $selectedgroup = groups_get_all_groups($course->id, $USER->id); 00087 if (is_array($selectedgroup)) { 00088 $selectedgroup = array_shift(array_keys($selectedgroup)); 00089 $SESSION->currentgroup[$course->id] = $selectedgroup; 00090 } else { 00091 $selectedgroup = 0; 00092 } 00093 } 00094 } 00095 00096 } else { 00097 $context = $sitecontext; 00098 } 00099 00100 // Get all the possible users 00101 $users = array(); 00102 00103 // Define limitfrom and limitnum for queries below 00104 // If $showusers is enabled... don't apply limitfrom and limitnum 00105 $limitfrom = empty($showusers) ? 0 : ''; 00106 $limitnum = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : ''; 00107 00108 // If looking at a different host, we're interested in all our site users 00109 if ($hostid == $CFG->mnet_localhost_id && $course->id != SITEID) { 00110 $courseusers = get_enrolled_users($context, '', $selectedgroup, 'u.id, u.firstname, u.lastname, u.idnumber', 'lastname ASC, firstname ASC', $limitfrom, $limitnum); 00111 } else { 00112 // this may be a lot of users :-( 00113 $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, firstname, lastname, idnumber', $limitfrom, $limitnum); 00114 } 00115 00116 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) { 00117 $showusers = 1; 00118 } 00119 00120 if ($showusers) { 00121 if ($courseusers) { 00122 foreach ($courseusers as $courseuser) { 00123 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context)); 00124 } 00125 } 00126 $users[$CFG->siteguest] = get_string('guestuser'); 00127 } 00128 00129 // Get all the hosts that have log records 00130 $sql = "select distinct 00131 h.id, 00132 h.name 00133 from 00134 {mnet_host} h, 00135 {mnet_log} l 00136 where 00137 h.id = l.hostid 00138 order by 00139 h.name"; 00140 00141 if ($hosts = $DB->get_records_sql($sql)) { 00142 foreach($hosts as $host) { 00143 $hostarray[$host->id] = $host->name; 00144 } 00145 } 00146 00147 $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname; 00148 asort($hostarray); 00149 00150 $dropdown = array(); 00151 00152 foreach($hostarray as $hostid => $name) { 00153 $courses = array(); 00154 $sites = array(); 00155 if ($CFG->mnet_localhost_id == $hostid) { 00156 if (has_capability('report/log:view', $sitecontext) && $showcourses) { 00157 if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) { 00158 foreach ($ccc as $cc) { 00159 if ($cc->id == SITEID) { 00160 $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')'; 00161 } else { 00162 $courses["$hostid/$cc->id"] = format_string($cc->fullname); 00163 } 00164 } 00165 } 00166 } 00167 } else { 00168 if (has_capability('report/log:view', $sitecontext) && $showcourses) { 00169 $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?"; 00170 if ($ccc = $DB->get_records_sql($sql, array($hostid))) { 00171 foreach ($ccc as $cc) { 00172 if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id 00173 $sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('site').')'; 00174 } else { 00175 $courses["$hostid/$cc->course"] = $cc->coursename; 00176 } 00177 } 00178 } 00179 } 00180 } 00181 00182 asort($courses); 00183 $dropdown[] = array($name=>($sites + $courses)); 00184 } 00185 00186 00187 $activities = array(); 00188 $selectedactivity = ""; 00189 00191 if ($modinfo = unserialize((string)$course->modinfo)) { 00192 $section = 0; 00193 $sections = get_all_sections($course->id); 00194 foreach ($modinfo as $mod) { 00195 if ($mod->mod == "label") { 00196 continue; 00197 } 00198 if ($mod->section > 0 and $section <> $mod->section) { 00199 $activities["section/$mod->section"] = '--- '.get_section_name($course, $sections[$mod->section]).' ---'; 00200 } 00201 $section = $mod->section; 00202 $mod->name = strip_tags(format_string($mod->name, true)); 00203 if (textlib::strlen($mod->name) > 55) { 00204 $mod->name = textlib::substr($mod->name, 0, 50)."..."; 00205 } 00206 if (!$mod->visible) { 00207 $mod->name = "(".$mod->name.")"; 00208 } 00209 $activities["$mod->cm"] = $mod->name; 00210 00211 if ($mod->cm == $modid) { 00212 $selectedactivity = "$mod->cm"; 00213 } 00214 } 00215 } 00216 00217 if (has_capability('report/log:view', $sitecontext) && !$course->category) { 00218 $activities["site_errors"] = get_string("siteerrors"); 00219 if ($modid === "site_errors") { 00220 $selectedactivity = "site_errors"; 00221 } 00222 } 00223 00224 $strftimedate = get_string("strftimedate"); 00225 $strftimedaydate = get_string("strftimedaydate"); 00226 00227 asort($users); 00228 00229 // Prepare the list of action options. 00230 $actions = array( 00231 'view' => get_string('view'), 00232 'add' => get_string('add'), 00233 'update' => get_string('update'), 00234 'delete' => get_string('delete'), 00235 '-view' => get_string('allchanges') 00236 ); 00237 00238 // Get all the possible dates 00239 // Note that we are keeping track of real (GMT) time and user time 00240 // User time is only used in displays - all calcs and passing is GMT 00241 00242 $timenow = time(); // GMT 00243 00244 // What day is it now for the user, and when is midnight that day (in GMT). 00245 $timemidnight = $today = usergetmidnight($timenow); 00246 00247 // Put today up the top of the list 00248 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) ); 00249 00250 if (!$course->startdate or ($course->startdate > $timenow)) { 00251 $course->startdate = $course->timecreated; 00252 } 00253 00254 $numdates = 1; 00255 while ($timemidnight > $course->startdate and $numdates < 365) { 00256 $timemidnight = $timemidnight - 86400; 00257 $timenow = $timenow - 86400; 00258 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); 00259 $numdates++; 00260 } 00261 00262 if ($selecteddate == "today") { 00263 $selecteddate = $today; 00264 } 00265 00266 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/report/log/index.php\" method=\"get\">\n"; 00267 echo "<div>\n";//invisible fieldset here breaks wrapping 00268 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n"; 00269 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n"; 00270 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n"; 00271 if (has_capability('report/log:view', $sitecontext) && $showcourses) { 00272 $cid = empty($course->id)? '1' : $course->id; 00273 echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid); 00274 } else { 00275 $courses = array(); 00276 $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : ''); 00277 echo html_writer::select($courses,"id",$course->id, false); 00278 if (has_capability('report/log:view', $sitecontext)) { 00279 $a = new stdClass(); 00280 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" 00281 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers"; 00282 print_string('logtoomanycourses','moodle',$a); 00283 } 00284 } 00285 00286 if ($showgroups) { 00287 if ($cgroups = groups_get_all_groups($course->id)) { 00288 foreach ($cgroups as $cgroup) { 00289 $groups[$cgroup->id] = $cgroup->name; 00290 } 00291 } 00292 else { 00293 $groups = array(); 00294 } 00295 echo html_writer::select($groups, "group", $selectedgroup, get_string("allgroups")); 00296 } 00297 00298 if ($showusers) { 00299 echo html_writer::select($users, "user", $selecteduser, get_string("allparticipants")); 00300 } 00301 else { 00302 $users = array(); 00303 if (!empty($selecteduser)) { 00304 $user = $DB->get_record('user', array('id'=>$selecteduser)); 00305 $users[$selecteduser] = fullname($user); 00306 } 00307 else { 00308 $users[0] = get_string('allparticipants'); 00309 } 00310 echo html_writer::select($users, "user", $selecteduser, false); 00311 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" 00312 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses"; 00313 print_string('logtoomanyusers','moodle',$a); 00314 } 00315 00316 echo html_writer::select($dates, "date", $selecteddate, get_string("alldays")); 00317 echo html_writer::select($activities, "modid", $selectedactivity, get_string("allactivities")); 00318 echo html_writer::select($actions, 'modaction', $modaction, get_string("allactions")); 00319 00320 $logformats = array('showashtml' => get_string('displayonpage'), 00321 'downloadascsv' => get_string('downloadtext'), 00322 'downloadasods' => get_string('downloadods'), 00323 'downloadasexcel' => get_string('downloadexcel')); 00324 echo html_writer::select($logformats, 'logformat', $logformat, false); 00325 echo '<input type="submit" value="'.get_string('gettheselogs').'" />'; 00326 echo '</div>'; 00327 echo '</form>'; 00328 } 00329 00330 function report_log_print_selector_form($course, $selecteduser=0, $selecteddate='today', 00331 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') { 00332 00333 global $USER, $CFG, $DB, $OUTPUT, $SESSION; 00334 00335 // first check to see if we can override showcourses and showusers 00336 $numcourses = $DB->count_records("course"); 00337 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) { 00338 $showcourses = 1; 00339 } 00340 00341 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00342 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00343 00345 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { 00346 $selectedgroup = -1; 00347 $showgroups = false; 00348 } else if ($course->groupmode) { 00349 $showgroups = true; 00350 } else { 00351 $selectedgroup = 0; 00352 $showgroups = false; 00353 } 00354 00355 if ($selectedgroup === -1) { 00356 if (isset($SESSION->currentgroup[$course->id])) { 00357 $selectedgroup = $SESSION->currentgroup[$course->id]; 00358 } else { 00359 $selectedgroup = groups_get_all_groups($course->id, $USER->id); 00360 if (is_array($selectedgroup)) { 00361 $selectedgroup = array_shift(array_keys($selectedgroup)); 00362 $SESSION->currentgroup[$course->id] = $selectedgroup; 00363 } else { 00364 $selectedgroup = 0; 00365 } 00366 } 00367 } 00368 00369 // Get all the possible users 00370 $users = array(); 00371 00372 // Define limitfrom and limitnum for queries below 00373 // If $showusers is enabled... don't apply limitfrom and limitnum 00374 $limitfrom = empty($showusers) ? 0 : ''; 00375 $limitnum = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : ''; 00376 00377 $courseusers = get_enrolled_users($context, '', $selectedgroup, 'u.id, u.firstname, u.lastname', 'lastname ASC, firstname ASC', $limitfrom, $limitnum); 00378 00379 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) { 00380 $showusers = 1; 00381 } 00382 00383 if ($showusers) { 00384 if ($courseusers) { 00385 foreach ($courseusers as $courseuser) { 00386 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context)); 00387 } 00388 } 00389 $users[$CFG->siteguest] = get_string('guestuser'); 00390 } 00391 00392 if (has_capability('report/log:view', $sitecontext) && $showcourses) { 00393 if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) { 00394 foreach ($ccc as $cc) { 00395 if ($cc->category) { 00396 $courses["$cc->id"] = format_string($cc->fullname); 00397 } else { 00398 $courses["$cc->id"] = format_string($cc->fullname) . ' (Site)'; 00399 } 00400 } 00401 } 00402 asort($courses); 00403 } 00404 00405 $activities = array(); 00406 $selectedactivity = ""; 00407 00409 if ($modinfo = unserialize((string)$course->modinfo)) { 00410 $section = 0; 00411 $sections = get_all_sections($course->id); 00412 foreach ($modinfo as $mod) { 00413 if ($mod->mod == "label") { 00414 continue; 00415 } 00416 if ($mod->section > 0 and $section <> $mod->section) { 00417 $activities["section/$mod->section"] = '--- '.get_section_name($course, $sections[$mod->section]).' ---'; 00418 } 00419 $section = $mod->section; 00420 $mod->name = strip_tags(format_string($mod->name, true)); 00421 if (textlib::strlen($mod->name) > 55) { 00422 $mod->name = textlib::substr($mod->name, 0, 50)."..."; 00423 } 00424 if (!$mod->visible) { 00425 $mod->name = "(".$mod->name.")"; 00426 } 00427 $activities["$mod->cm"] = $mod->name; 00428 00429 if ($mod->cm == $modid) { 00430 $selectedactivity = "$mod->cm"; 00431 } 00432 } 00433 } 00434 00435 if (has_capability('report/log:view', $sitecontext) && ($course->id == SITEID)) { 00436 $activities["site_errors"] = get_string("siteerrors"); 00437 if ($modid === "site_errors") { 00438 $selectedactivity = "site_errors"; 00439 } 00440 } 00441 00442 $strftimedate = get_string("strftimedate"); 00443 $strftimedaydate = get_string("strftimedaydate"); 00444 00445 asort($users); 00446 00447 // Prepare the list of action options. 00448 $actions = array( 00449 'view' => get_string('view'), 00450 'add' => get_string('add'), 00451 'update' => get_string('update'), 00452 'delete' => get_string('delete'), 00453 '-view' => get_string('allchanges') 00454 ); 00455 00456 // Get all the possible dates 00457 // Note that we are keeping track of real (GMT) time and user time 00458 // User time is only used in displays - all calcs and passing is GMT 00459 00460 $timenow = time(); // GMT 00461 00462 // What day is it now for the user, and when is midnight that day (in GMT). 00463 $timemidnight = $today = usergetmidnight($timenow); 00464 00465 // Put today up the top of the list 00466 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) ); 00467 00468 if (!$course->startdate or ($course->startdate > $timenow)) { 00469 $course->startdate = $course->timecreated; 00470 } 00471 00472 $numdates = 1; 00473 while ($timemidnight > $course->startdate and $numdates < 365) { 00474 $timemidnight = $timemidnight - 86400; 00475 $timenow = $timenow - 86400; 00476 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); 00477 $numdates++; 00478 } 00479 00480 if ($selecteddate == "today") { 00481 $selecteddate = $today; 00482 } 00483 00484 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/report/log/index.php\" method=\"get\">\n"; 00485 echo "<div>\n"; 00486 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n"; 00487 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n"; 00488 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n"; 00489 if (has_capability('report/log:view', $sitecontext) && $showcourses) { 00490 echo html_writer::select($courses, "id", $course->id, false); 00491 } else { 00492 // echo '<input type="hidden" name="id" value="'.$course->id.'" />'; 00493 $courses = array(); 00494 $courses[$course->id] = $course->fullname . (($course->id == SITEID) ? ' ('.get_string('site').') ' : ''); 00495 echo html_writer::select($courses,"id",$course->id, false); 00496 if (has_capability('report/log:view', $sitecontext)) { 00497 $a = new stdClass(); 00498 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" 00499 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers"; 00500 print_string('logtoomanycourses','moodle',$a); 00501 } 00502 } 00503 00504 if ($showgroups) { 00505 if ($cgroups = groups_get_all_groups($course->id)) { 00506 foreach ($cgroups as $cgroup) { 00507 $groups[$cgroup->id] = $cgroup->name; 00508 } 00509 } 00510 else { 00511 $groups = array(); 00512 } 00513 echo html_writer::select($groups, "group", $selectedgroup, get_string("allgroups")); 00514 } 00515 00516 if ($showusers) { 00517 echo html_writer::select($users, "user", $selecteduser, get_string("allparticipants")); 00518 } 00519 else { 00520 $users = array(); 00521 if (!empty($selecteduser)) { 00522 $user = $DB->get_record('user', array('id'=>$selecteduser)); 00523 $users[$selecteduser] = fullname($user); 00524 } 00525 else { 00526 $users[0] = get_string('allparticipants'); 00527 } 00528 echo html_writer::select($users, "user", $selecteduser, false); 00529 $a = new stdClass(); 00530 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" 00531 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses"; 00532 print_string('logtoomanyusers','moodle',$a); 00533 } 00534 echo html_writer::select($dates, "date", $selecteddate, get_string("alldays")); 00535 00536 echo html_writer::select($activities, "modid", $selectedactivity, get_string("allactivities")); 00537 echo html_writer::select($actions, 'modaction', $modaction, get_string("allactions")); 00538 00539 $logformats = array('showashtml' => get_string('displayonpage'), 00540 'downloadascsv' => get_string('downloadtext'), 00541 'downloadasods' => get_string('downloadods'), 00542 'downloadasexcel' => get_string('downloadexcel')); 00543 00544 echo html_writer::select($logformats, 'logformat', $logformat, false); 00545 echo '<input type="submit" value="'.get_string('gettheselogs').'" />'; 00546 echo '</div>'; 00547 echo '</form>'; 00548 }