/usr/share/moodle/mod/jclic/lib.php is in jclicmoodle 0.1.0.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | <?PHP // $Id: lib.php,v 1.19 2011-05-25 12:13:03 sarjona Exp $
/// Library of functions and constants for module jclic
require_once($CFG->libdir.'/pagelib.php');
if (!isset($CFG->jclic_jclicpluginjs)) {
set_config("jclic_jclicpluginjs", "http://clic.xtec.cat/dist/jclic/jclicplugin.js");
}
if (!isset($CFG->jclic_lap)) {
set_config("jclic_lap", "5");
}
function jclic_add_instance($jclic) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will create a new instance and return the id number
/// of the new instance.
$jclic->url=trim($jclic->url);
if ($jclic->skin=='') $jclic->skin="default";
return insert_record("jclic", $jclic);
}
function jclic_update_instance($jclic) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will update an existing instance with new data.
$jclic->id = $jclic->instance;
$jclic->url=trim($jclic->url);
if(update_record("jclic", $jclic)){
return jclic_update_gradebook(null, $jclic);
}
return false;
}
function jclic_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
if (! $jclic = get_record("jclic", "id", "$id")) {
return false;
}
$result = true;
# Delete any dependent records here #
$rs = get_record("jclic_sessions", "jclicid", "$id");
foreach($rs as $session){
delete_records("jclic_activities", "session_id", "$rs->session_id");
}
delete_records("jclic_sessions", "jclicid", "$id");
if (! delete_records("jclic", "id", "$id")) {
$result = false;
}
return $result;
}
function jclic_user_outline($course, $user, $mod, $jclic) {
/// Return a small object with summary information about what a
/// user has done with a given particular instance of this module
/// Used for user activity reports.
$jclic_summary = jclic_get_sessions_summary($jclic->id, $user->id);
$return->time = $jclic_summary->totaltime;
$maxgrade = 0;
if (property_exists($jclic, 'maxgrade')) $maxgrade = $jclic->maxgrade;
$return->info = get_string("score", "jclic").": ".$jclic_summary->score."/".$maxgrade;
return $return;
}
function jclic_user_complete($course, $user, $mod, $jclic) {
/// Print a detailed representation of what a user has done with
/// a given particular instance of this module, for user activity reports.
return true;
}
function jclic_print_recent_activity($course, $isteacher, $timestart) {
/// Given a course and a time, this module should find recent activity
/// that has occurred in jclic activities and print it out.
/// Return true if there was output, or false is there was none.
global $CFG;
return false; // True if anything was printed, otherwise false
}
function jclic_cron () {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
return true;
}
function jclic_grades($jclicid, $userid=NULL) {
global $CFG;
/// Must return an array of grades for a given instance of this module,
/// indexed by user. It also returns a maximum allowed grade.
///
/// $return->grades = array of grades;
/// $return->maxgrade = maximum allowed grade;
///
if (!$jclic = get_record('jclic', 'id', $jclicid)) {
return NULL;
}
$return = new object();
$grades = array();
if ($userid==NULL){
if ($participants = jclic_get_participants($jclicid)){
foreach ($participants as $participant) {
$grades = jclic_user_grades($grades, $jclic, $participant->userid);
}
}
} else{
$grades = jclic_user_grades($grades, $jclic, $userid);
}
$return->grades=$grades;
$return->maxgrade=$jclic->maxgrade;
return $return;
}
function jclic_user_grades($grades, $jclic, $userid) {
$sessions_summary=jclic_get_sessions_summary($jclic->id, $userid);
if ($jclic->avaluation=='score'){
$grades[$userid]=$sessions_summary->score;
}else{
$grades[$userid]=$sessions_summary->solved;
}
return $grades;
}
function jclic_update_gradebook($jclic_activity=NULL, $jclic=NULL){
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
if (file_exists($CFG->libdir.'/gradelib.php')) include_once($CFG->libdir.'/gradelib.php');
}
if (!function_exists('grade_update')) {
return true;
}
if ($jclic_activity != NULL){
if (!$jclic_session = get_record('jclic_sessions', 'session_id', $jclic_activity->session_id)) {
return NULL;
}
if (!$jclic = get_record('jclic', 'id', $jclic_session->jclicid)) {
return NULL;
}
}
if ($jclic != NULL){
// Get the grades
$userid = (isset($jclic_session)?$jclic_session->user_id:null);
$jclic_grades = jclic_grades($jclic->id, $userid);
$grades = array();
foreach ($jclic_grades->grades as $k=>$v){
$grades[$k]->userid=$k;
$grades[$k]->rawgrade=$v;
}
// Get the params
$params = array('itemname'=>$jclic->name);
$params['gradetype'] = GRADE_TYPE_VALUE;
if ($jclic->maxgrade==NULL) $jclic_grades->maxgrade = 0;
$params['gradepass'] = $jclic_grades->maxgrade;
$params['grademin'] = 0;
if ($jclic->avaluation=='score') $params['grademax'] = 100;
else $params['grademax'] = $jclic_grades->maxgrade;
grade_update('mod/jclic', $jclic->course, 'mod', 'jclic', $jclic->id, 0, $grades, $params);
return true;
}
return false;
}
function jclic_get_students($cm, $course, $jclicid) {
global $CFG;
$version_moodle = (float)substr($CFG->release,0,3);
if ($version_moodle>=1.7){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$currentgroup = get_and_set_current_group($course, groupmode($course, $cm));
$users = get_users_by_capability($context, 'mod/jclic:submit', 'u.id, u.id', '', '', '', $currentgroup, '', false);
$dbstudents = array();
if (!empty($users)){
$select = 'SELECT u.id AS userid, u.firstname, u.lastname, u.picture ';
$sql = 'FROM '.$CFG->prefix.'user u '.
'LEFT JOIN '.$CFG->prefix.'jclic_sessions a ON u.id = a.user_id AND a.jclicid = '.$jclicid.' '.
'WHERE u.id IN ('.implode(',', array_keys($users)).') ';
$dbstudents=get_records_sql($select.$sql);
}
}else{
$dbstudents = get_records_sql("SELECT DISTINCT us.userid, u.firstname, u.lastname
FROM {$CFG->prefix}user u,{$CFG->prefix}user_students us, {$CFG->prefix}jclic j
WHERE us.course=j.course AND j.id=$jclicid AND u.id=us.userid");
}
return $dbstudents;
}
function jclic_get_participants($jclicid) {
//Must return an array of user records (all data) who are participants
//for a given instance of jclic. Must include every user involved
//in the instance, independient of his role (student, teacher, admin...)
//See other modules as example.
global $CFG;
//Get students
$students = get_records_sql("SELECT DISTINCT u.id, u.id as userid
FROM {$CFG->prefix}user u,
{$CFG->prefix}jclic_sessions js
WHERE js.jclicid = '$jclicid' and
u.id = js.user_id");
//Get teachers
$teachers = get_records_sql("SELECT DISTINCT u.id, u.id as userid
FROM {$CFG->prefix}user u,
{$CFG->prefix}jclic_sessions js
WHERE js.jclicid = '$jclicid' and
u.id = js.user_id");
//Add teachers to students
if ($teachers) {
foreach ($teachers as $teacher) {
$students[$teacher->id] = $teacher;
}
}
//Return students array (it contains an array of unique users)
return ($students);
}
function jclic_scale_used ($jclicid,$scaleid) {
//This function returns if a scale is being used by one jclic
//it it has support for grading and scales. Commented code should be
//modified if necessary. See forum, glossary or journal modules
//as reference.
$return = false;
return $return;
}
/**
* Checks if scale is being used by any instance of jclic
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean
*/
function jclic_scale_used_anywhere($scaleid) {
return false;
}
//////////////////////////////////////////////////////////////////////////////////////
/**
* Get user sessions
*
* @return array [0=>session1,1=>session2...] where session1 is an array with keys: id,score,totaltime,starttime,done,solved,attempts. First sessions are newest.
* @param object $jclicid The jclic to get sessions
* @param object $userid The user id to get sessions
*/
function jclic_get_sessions($jclicid, $userid) {
global $CFG;
$sessions=array();
if($rs = get_records_sql("SELECT js.*
FROM {$CFG->prefix}jclic j, {$CFG->prefix}jclic_sessions js
WHERE j.id=js.jclicid AND js.jclicid=$jclicid AND js.user_id=$userid
ORDER BY js.session_datetime")){
$i=0;
foreach($rs as $session){
$activity=jclic_get_activity($session);
$activity->attempts=$i+1;
$sessions[$i++]=$activity;
}
}
return $sessions;
}
/**
* Get session activities
*
* @return array [0=>act0,1=>act1...] where act0 is an array with keys: activity_id,activity_name,num_actions,score,activity_solved,qualification, total_time. First activity are oldest.
* @param string $session_id The session id to get actitivies
*/
function jclic_get_activities($session_id) {
global $CFG;
if($rs = get_records("jclic_activities", "session_id", "$session_id",'activity_id')){
$i=0;
foreach($rs as $activity){
$activities[$i++]=$activity;
}
}
return $activities;
}
/**
* Get information about activities of specified session
*
* @return array Array has these keys id,score,totaltime,starttime,done,solved,attempts
* @param object $session The session object
*/
function jclic_get_activity($session) {
global $CFG;
$activity->starttime=$session->session_datetime;
$activity->session_id=$session->session_id;
if($rs = get_record_sql("SELECT AVG(ja.qualification) as qualification, SUM(ja.total_time) as totaltime
FROM {$CFG->prefix}jclic_activities ja
WHERE ja.session_id='$session->session_id'")){
$activity->score=round($rs->qualification,0);
$activity->totaltime=round($rs->totaltime/60000,0)."' ".round(fmod($rs->totaltime,60000)/1000,0)."''";
}
if ($rs = get_record_sql("SELECT COUNT(*) as done
FROM (SELECT DISTINCT ja.activity_name
FROM {$CFG->prefix}jclic_activities ja
WHERE ja.session_id='$session->session_id') t")){
$activity->done=$rs->done;
}
if ($rs = get_record_sql("SELECT COUNT(*) as solved
FROM (SELECT DISTINCT ja.activity_name
FROM {$CFG->prefix}jclic_activities ja
WHERE ja.session_id='$session->session_id' AND ja.activity_solved=1) t")){
$activity->solved=$rs->solved;
}
return $activity;
}
/**
* Get user activity summary
*
* @return object session object with score, totaltime, activities done and solved and attempts information
*/
function jclic_get_sessions_summary($jclicid, $userid) {
global $CFG;
$sessions_sumari = array('attempts'=>'','score'=>'','totaltime'=>'','starttime'=>'','done'=>'','solved'=>'');
if ($rs = get_record_sql("SELECT COUNT(*) AS attempts, AVG(t.qualification) AS qualification, SUM(t.totaltime) AS totaltime, MAX(t.starttime) AS starttime
FROM (SELECT AVG(ja.qualification) AS qualification, SUM(ja.total_time) AS totaltime, MAX(js.session_datetime) AS starttime
FROM {$CFG->prefix}jclic j, {$CFG->prefix}jclic_sessions js, {$CFG->prefix}jclic_activities ja
WHERE j.id=js.jclicid AND js.user_id=$userid AND js.jclicid=$jclicid AND ja.session_id=js.session_id
GROUP BY js.session_id) t")){
$sessions_summary->attempts=$rs->attempts;
$sessions_summary->score=round($rs->qualification,0);
$sessions_summary->totaltime=round($rs->totaltime/60000,0)."' ".round(fmod($rs->totaltime,60000)/1000,0)."''";
$sessions_summary->starttime=$rs->starttime;
}
if ($rs = get_record_sql("SELECT COUNT(*) as done
FROM (SELECT DISTINCT ja.activity_name
FROM {$CFG->prefix}jclic j, {$CFG->prefix}jclic_sessions js, {$CFG->prefix}jclic_activities ja
WHERE j.id=js.jclicid AND js.user_id=$userid AND js.jclicid=$jclicid AND js.session_id=ja.session_id) t")){
$sessions_summary->done=$rs->done;
}
if ($rs = get_record_sql("SELECT COUNT(*) as solved
FROM (SELECT DISTINCT ja.activity_name
FROM {$CFG->prefix}jclic j, {$CFG->prefix}jclic_sessions js, {$CFG->prefix}jclic_activities ja
WHERE j.id=js.jclicid AND js.user_id=$userid AND js.jclicid=$jclicid AND js.session_id=ja.session_id AND ja.activity_solved=1) t")){
$sessions_summary->solved=$rs->solved;
}
return $sessions_summary;
}
/**
* Format time from milliseconds to string
*
* @return string Formated string [x' y''], where x are the minutes and y are the seconds.
* @param int $time The time (in ms)
*/
function jclic_time2str($time){
return round($time/60000,0)."' ".round(fmod($time,60000)/1000,0)."''";
}
/**
* Print data in array as a row. Uses jclic_make_row function to get the string representation to print
*
* @param array $row Data
* @param array $align Cell alignment
* @param array $props Style, rowspan, colspan...
* @param boolean $header If true the row will be printed as th; otherwise as td.
*/
function jclic_print_row($row, $align, $props=array(), $header=false){
echo jclic_make_row($row, $align, $props, $header);
}
function jclic_make_row($row, $row_align, $row_props=array(), $header=false){
$strrow = '';
$strtd = $header?'th':'td';
if (sizeof($row)>0){
$strrow.='<tr>';
$i=0;
foreach($row as $cell){
$prop = ''; $align='';
if ($row_props!='' && array_key_exists($i, $row_props) && $row_props[$i]!='') $prop = ' '.$row_props[$i].' ';
if ($row_align!=''&& array_key_exists($i, $row_align) && $row_align[$i]!='') $align = ' align='.$row_align[$i].' ';
$strrow.='<'.$strtd.' '.($header?'':'class="cell"').$prop.$align.'>'.$cell.'</'.$strtd.'>';
$i++;
}
$strrow.='</tr>';
}
return $strrow;
}
/**
* Print a table data with all session activities
*
* @param string $session_id The session identifier
*/
function print_session_activities($session_id){
// Import language strings
$stractivity = get_string("activity", "jclic");
$strsolved = get_string("solved", "jclic");
$stractions = get_string("actions", "jclic");
$strtime = get_string("time", "jclic");
$strscore = get_string("score", "jclic");
$stryes = get_string("yes");
$strno = get_string("no");
// Print activities for each session
$activities=jclic_get_activities($session_id);
if (sizeof($activities)>0){
echo '<tr><td colspan="7"><div id="'.$session_id.'" style="display:none;visible:hidden">';
echo '<table class="generaltable" align="center" border="0" cellpadding="5" cellspacing="1" width="80%"><tbody>';
$subtable_align=array('left','center','center','center','center');
jclic_print_row(array($stractivity, $strsolved, $stractions, $strtime, $strscore),$subtable_align,'',true);
foreach($activities as $activity){
$subtable_props=array('', 'style="background-color:'.($activity->activity_solved?'#DFFFDF':'#FFDFDF').'"','','','');
$act_percent=$activity->num_actions>0?round(($activity->score/$activity->num_actions)*100,0):0;
jclic_print_row(array($activity->activity_name, ($activity->activity_solved?$stryes:$strno), $activity->score.'/'.$activity->num_actions.' ('.$act_percent.'%)', jclic_time2str($activity->total_time), $activity->qualification.'%'), $subtable_align, $subtable_props);
}
echo '</table></div></td></tr>';
}
}
function jclic_get_skins(){
return array('@default.xml' => 'default','@blue.xml' => 'blue','@orange.xml' => 'orange','@green.xml' => 'green','@simple.xml' => 'simple', '@mini.xml' => 'mini');
}
/**
* Get an array with the languages
*
* @return array The array with each language.
*/
function jclic_get_languages(){
global $CFG;
$tmplanglist = get_list_of_languages();
$langlist = array();
foreach ($tmplanglist as $lang=>$langname) {
if (substr($lang, -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
$lang = substr($lang, 0, -5);
}
$langlist[$lang]=$langname;
}
return $langlist;
}
/**
* Get moodle server
*
* @return string myserver.com:port
*/
function jclic_get_server() {
global $CFG;
if (!empty($CFG->wwwroot)) {
$url = parse_url($CFG->wwwroot);
}
if (!empty($url['host'])) {
$hostname = $url['host'];
} else if (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
} else if (!empty($_ENV['SERVER_NAME'])) {
$hostname = $_ENV['SERVER_NAME'];
} else if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
} else if (!empty($_ENV['HTTP_HOST'])) {
$hostname = $_ENV['HTTP_HOST'];
} else {
notify('Warning: could not find the name of this server!');
return false;
}
if (!empty($url['port'])) {
$hostname .= ':'.$url['port'];
} else if (!empty($_SERVER['SERVER_PORT'])) {
if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
$hostname .= ':'.$_SERVER['SERVER_PORT'];
}
}
return $hostname;
}
/**
* Get moodle path
*
* @return string /path_to_moodle
*/
function jclic_get_path() {
global $CFG;
$path = '/';
if (!empty($CFG->wwwroot)) {
$url = parse_url($CFG->wwwroot);
if (array_key_exists('path', $url)){
$path = $url['path'];
}
}
return $path;
}
if (!function_exists('get_coursemodule_from_id')) {
// Add this function for Moodle < 1.6
function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
global $CFG;
return get_record_sql("
SELECT
cm.*, m.name, md.name as modname
FROM
{$CFG->prefix}course_modules cm,
{$CFG->prefix}modules md,
{$CFG->prefix}$modulename m
WHERE
".($courseid ? "cm.course = '$courseid' AND " : '')."
cm.id = '$cmid' AND
cm.instance = m.id AND
md.name = '$modulename' AND
md.id = cm.module
");
}
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* This function will remove all jclic sessions in the specified course.
* @param $data the data submitted from the reset course.
* @return array status array
*/
function jclic_reset_userdata($data) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$status = array();
if (!empty($data->reset_jclic_deleteallsessions)) {
$select = 'session_id IN'
. " (SELECT s.session_id FROM {$CFG->prefix}jclic_sessions s"
. " INNER JOIN {$CFG->prefix}jclic j ON s.jclicid = j.id"
. " WHERE j.course = {$data->courseid})";
delete_records_select('jclic_activities', $select);
$select = 'jclicid IN'
. " (SELECT j.id FROM {$CFG->prefix}jclic j"
. " WHERE j.course = {$data->courseid})";
delete_records_select('jclic_sessions', $select);
$status[] = array('component' => get_string('modulenameplural', 'jclic'),
'item' => get_string('deleteallsessions', 'jclic'),
'error' => false);
}
return $status;
}
/**
* Called by course/reset.php
* @param $mform form passed by reference
*/
function jclic_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'jclicheader', get_string('modulenameplural', 'jclic'));
$mform->addElement('checkbox', 'reset_jclic_deleteallsessions', get_string('deleteallsessions', 'jclic'));
}
/**
* Course reset form defaults.
*/
function jclic_reset_course_form_defaults($course) {
return array('reset_jclic_deleteallsessions' => 1);
}
?>
|