/usr/share/moodle/mod/jclic/locallib.php is in jclicmoodle 2.7.0-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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | <?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/>.
/**
* Internal library of functions for module jclic
*
* All the jclic specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod
* @subpackage jclic
* @copyright 2011 Departament d'Ensenyament de la Generalitat de Catalunya
* @author Sara Arjona Téllez <sarjona@xtec.cat>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/filelib.php");
/**
* Standard base class for mod_jclic
*
* @package mod_jclic
* @copyright 2014 Pau Ferrer Ocaña
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class jclic {
/** @var stdClass the jclic record that contains the global settings for this jclic instance */
private $instance;
/** @var stdClass the course this jclic instance belongs to */
private $course;
/** @var context the context of the course module for this jclic instance
* (or just the course if we are creating a new one)
*/
private $context;
/** @var stdClass the course module for this jclic instance */
private $coursemodule;
/**
* Constructor for the base jclic class.
*
* @param mixed $coursemodulecontext context|null the course module context
* (or the course context if the coursemodule has not been
* created yet).
* @param mixed $coursemodule the current course module if it was already loaded,
* otherwise this class will load one from the context as required.
* @param mixed $course the current course if it was already loaded,
* otherwise this class will load one from the context as required.
*/
public function __construct($coursemodulecontext, $coursemodule, $course) {
global $DB;
$this->context = $coursemodulecontext;
if ($coursemodule) {
$this->coursemodule = $coursemodule;
} else if ($this->context && $this->context->contextlevel == CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_instance('jclic', $this->context->instanceid);
}
if ($course) {
$this->course = $course;
} else if ($this->coursemodule) {
$params = array('id' => $this->coursemodule->course);
$this->course = $DB->get_record('course', $params);
}
if ($this->coursemodule) {
$params = array('id' => $this->coursemodule->instance);
$this->instance = $DB->get_record('jclic', $params);
}
}
/**
* Set the submitted form data.
*
* @param stdClass $data The form data (instance)
*/
public function set_instance(stdClass $data) {
global $DB;
$this->instance = $data;
// Cache some info.
$this->course = $DB->get_record('course', array('id' => $data->course), '*', MUST_EXIST);
$this->coursemodule = get_coursemodule_from_instance('jclic', $data->id, 0, false, MUST_EXIST);
$this->context = context_module::instance($this->coursemodule->id);
}
/**
* Get the settings for the current instance of this jclic
*
* @return stdClass The settings
*/
public function get_instance() {
global $DB;
if ($this->instance) {
return $this->instance;
}
if ($this->get_course_module()) {
$params = array('id' => $this->get_course_module()->instance);
$this->instance = $DB->get_record('jclic', $params, '*', MUST_EXIST);
}
if (!$this->instance) {
throw new coding_exception('Improper use of the jclic class. ' .
'Cannot load the jclic record.');
}
return $this->instance;
}
/**
* Get the current course module.
*
* @return mixed stdClass|null The course module
*/
public function get_course_module() {
if ($this->coursemodule) {
return $this->coursemodule;
}
if (!$this->context) {
return null;
}
if ($this->context->contextlevel == CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_id('jclic',
$this->context->instanceid,
0,
false,
MUST_EXIST);
return $this->coursemodule;
}
return null;
}
/**
* Add this instance to the database.
*
* @param stdClass $formdata The data submitted from the form
* @return mixed false if an error occurs or the int id of the new instance
*/
public function add_instance(stdClass $formdata) {
global $DB;
// Add the database record.
$update = new stdClass();
$update->name = $formdata->name;
$update->timemodified = time();
$update->timecreated = $update->timemodified;
$update->course = $formdata->course;
$update->intro = $formdata->intro;
$update->introformat = $formdata->introformat;
if (isset($formdata->filetype) && $formdata->filetype == JCLIC_FILE_TYPE_LOCAL) {
$update->url = $formdata->jclicfile;
} else {
$formdata->filetype = JCLIC_FILE_TYPE_EXTERNAL;
$update->url = $formdata->url;
}
$update->skin = empty($formdata->skin) ? 'default' : $formdata->skin;
$update->avaluation = $formdata->avaluation;
$update->grade = $formdata->grade;
if ($update->grade >= 0 ) {
$update->maxgrade = $update->grade;
}
$update->lang = $formdata->lang;
$update->exiturl = $formdata->exiturl;
$update->timeavailable = $formdata->timeavailable;
$update->timedue = $formdata->timedue;
if (!empty($formdata->maxattempts)) {
$update->maxattempts = $formdata->maxattempts;
}
// Store the JClic and verify
$returnid = $DB->insert_record('jclic', $update);
// We need to use context now, so we need to make sure all needed info is already in db
$DB->set_field('course_modules', 'instance', $returnid, array('id' => $formdata->coursemodule));
$instance = $DB->get_record('jclic', array('id' => $returnid), '*', MUST_EXIST);
$this->set_instance($instance);
if ($formdata->filetype == JCLIC_FILE_TYPE_LOCAL) {
$update = new StdClass();
$update->id = $returnid;
$update->url = $this->set_mainfile();
$DB->update_record('jclic', $update);
}
// In the case of upgrades the coursemodule has not been set,
// so we need to wait before calling these two.
$this->update_calendar($formdata->coursemodule);
$this->update_gradebook(false, $formdata->coursemodule);
return $returnid;
}
/**
* Update this instance in the database.
*
* @param stdClass $formdata - the data submitted from the form
* @return bool false if an error occurs
*/
public function update_instance($formdata) {
global $DB;
$update = new stdClass();
$update->id = $formdata->instance;
$update->name = $formdata->name;
$update->timemodified = time();
$update->course = $formdata->course;
$update->intro = $formdata->intro;
$update->introformat = $formdata->introformat;
if (isset($formdata->filetype) && $formdata->filetype == JCLIC_FILE_TYPE_LOCAL) {
$update->url = $formdata->jclicfile;
} else {
$formdata->filetype = JCLIC_FILE_TYPE_EXTERNAL;
$update->url = $formdata->url;
}
$update->skin = empty($formdata->skin) ? 'default' : $formdata->skin;
$update->avaluation = $formdata->avaluation;
$update->grade = $formdata->grade;
if ($update->grade >= 0 ) {
$update->maxgrade = $update->grade;
}
$update->lang = $formdata->lang;
$update->exiturl = $formdata->exiturl;
$update->timeavailable = $formdata->timeavailable;
$update->timedue = $formdata->timedue;
if (!empty($formdata->maxattempts)) {
$update->maxattempts = $formdata->maxattempts;
}
// Store the JClic and verify
$result = $DB->update_record('jclic', $update);
$instance = $DB->get_record('jclic', array('id' => $update->id), '*', MUST_EXIST);
$this->set_instance($instance);
$this->update_calendar($this->coursemodule->id);
$this->update_gradebook(false, $this->coursemodule->id);
return $result;
}
/**
* Delete this instance from the database.
*
* @return bool false if an error occurs
*/
public function delete_instance() {
global $DB;
$result = true;
$id = $this->instance->id;
// Delete files associated with this jclic.
$fs = get_file_storage();
if (! $fs->delete_area_files($this->context->id) ) {
$result = false;
}
// Delete any dependent records.
$rs = $DB->get_records('jclic_sessions', array('jclicid' => $id));
foreach ($rs as $session) {
$DB->delete_records('jclic_activities', array('session_id' => $session->session_id));
}
$DB->delete_records('jclic_sessions', array('jclicid' => $id));
// Delete items from the gradebook.
if (! $this->delete_grades()) {
$result = false;
}
// Delete the instance.
$DB->delete_records('jclic', array('id' => $id));
return $result;
}
/**
* Delete all grades from the gradebook for this jclic.
*
* @return bool
*/
protected function delete_grades() {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
$result = grade_update('mod/jclic',
$this->course->id,
'mod',
'jclic',
$this->instance->id,
0,
null,
array('deleted' => 1));
return $result == GRADE_UPDATE_OK;
}
/**
* Update the gradebook information for this jclic.
*
* @param bool $reset If true, will reset all grades in the gradbook for this jclic
* @param int $coursemoduleid This is required because it might not exist in the database yet
* @return bool
*/
public function update_gradebook($reset, $coursemoduleid) {
global $CFG;
require_once($CFG->dirroot.'/mod/jclic/lib.php');
$jclic = clone $this->instance;
$jclic->cmidnumber = $coursemoduleid;
$param = null;
if ($reset) {
$param = 'reset';
}
return jclic_grade_item_update($jclic, $param);
}
/**
* Update the calendar entries for this jclic.
*
* @param int $coursemoduleid - Required to pass this in because it might
* not exist in the database yet.
* @return bool
*/
public function update_calendar($coursemoduleid) {
global $DB, $CFG;
require_once($CFG->dirroot.'/calendar/lib.php');
// Special case for add_instance as the coursemodule has not been set yet.
$instance = $this->instance;
if ($instance->timedue) {
$event = new stdClass();
$params = array('modulename' => 'jclic', 'instance' => $instance->id);
$event->id = $DB->get_field('event', 'id', $params);
$event->name = $instance->name;
$event->timestart = $instance->timedue;
$event->description = format_module_intro('jclic', $instance, $this->coursemodule->id);
if ($event->id) {
$calendarevent = calendar_event::load($event->id);
$calendarevent->update($event);
} else {
unset($event->id);
$event->courseid = $instance->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'jclic';
$event->instance = $instance->id;
$event->eventtype = 'due';
$event->timeduration = 0;
calendar_event::create($event);
}
} else {
$DB->delete_records('event', array('modulename' => 'jclic', 'instance' => $instance->id));
}
}
public function set_mainfile() {
$fs = get_file_storage();
$fileid = $this->instance->url;
if ($fileid) {
file_save_draft_area_files($fileid, $this->context->id, 'mod_jclic', 'content', 0, self::get_filemanager_options());
}
$files = $fs->get_area_files($this->context->id, 'mod_jclic', 'content', 0, 'sortorder', false);
if (count($files) == 1) {
// only one file attached, set it as main file automatically
$file = reset($files);
file_set_sortorder($this->context->id, 'mod_jclic', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
return $file->get_filename();
}
return null;
}
public static function get_filemanager_options() {
return array('return_types' => 3, // 3 == FILE_EXTERNAL & FILE_INTERNAL. These two constant names are defined in repository/lib.php
'accepted_types' => 'archive',
'maxbytes' => 0,
'subdirs' => 0,
'maxfiles' => 1,
);
}
/**
* Get an array with the languages
*
* @return array The array with each language.
*/
public static function get_languages() {
return get_string_manager()->get_list_of_translations();
}
/**
* Get an array with the skins
*
* @return array The array with each skin.
*/
public static function 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 file types
*
* @return array The array with each file type
*/
public static function get_file_types() {
return array(
JCLIC_FILE_TYPE_LOCAL => get_string('filetypelocal', 'jclic'),
JCLIC_FILE_TYPE_EXTERNAL => get_string('filetypeexternal', 'jclic')
);
}
}
/**
* Display the jclic intro
*
*/
function jclic_view_intro($jclic, $cm) {
global $OUTPUT;
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
echo format_module_intro('jclic', $jclic, $cm->id);
echo $OUTPUT->box_end();
}
/**
* Display the jclic dates
*
* Prints the jclic start and end dates in a box.
*/
function jclic_view_dates($jclic, $cm) {
global $OUTPUT;
if (!$jclic->timeavailable && !$jclic->timedue) {
return;
}
$timenow = time();
echo $OUTPUT->box_start('generalbox boxaligncenter jclicdates', 'dates');
if ($jclic->timeavailable) {
echo '<div class="title-time">'.get_string('availabledate', 'jclic').': </div>';
echo '<div class="data-time">'.userdate($jclic->timeavailable).'</div>';
}
if ($jclic->timedue) {
echo '<div class="title-time">'.get_string('duedate', 'jclic').': </div>';
echo '<div class="data-time">'.userdate($jclic->timedue).'</div>';
}
echo $OUTPUT->box_end();
}
/**
* Display the jclic applet
*
*/
function jclic_view_applet($jclic, $context, $ispreview = false) {
global $OUTPUT, $PAGE, $CFG, $USER;
$timenow = time();
$isopen = (empty($jclic->timeavailable) || $jclic->timeavailable < $timenow);
if (!$isopen) {
echo $OUTPUT->box(get_string('notopenyet', 'jclic', userdate($jclic->timeavailable)), 'generalbox boxaligncenter jclicdates');
if (!$ispreview) {
return;
}
}
$isclosed = (!empty($jclic->timedue) && $jclic->timedue < $timenow);
if ($isclosed) {
echo $OUTPUT->box(get_string('expired', 'jclic', userdate($jclic->timedue)), 'generalbox boxaligncenter jclicdates');
if (!$ispreview) {
return;
}
}
$sessions = jclic_get_sessions($jclic->id, $USER->id);
$attempts = count($sessions);
if (!$ispreview && ($attempts > 0 || $isopen)) {
echo '<br><A href="#" onclick="window.open(\'action/student_results.php?id='.$context->instanceid.'\',\'JClic\',\'navigation=0,toolbar=0,resizable=1,scrollbars=1,width=700,height=400\');" >'.get_string('show_my_results', 'jclic').'</A>';
}
$config = get_config('jclic');
if ($jclic->maxattempts < 0 || $attempts < $jclic->maxattempts) {
echo '<div id="jclic_applet" style="text-align:center;padding-top:10px;">';
echo '</div>';
if (isset($config->pluginjs) && !empty($config->pluginjs)) {
echo '<script type="text/javascript" src="'.$config->pluginjs.'"></script>';
} else {
$PAGE->requires->js('/mod/jclic/jclicplugin.js');
}
$PAGE->requires->js('/mod/jclic/jclic.js');
$params = get_object_vars($jclic);
$params['jclic_url'] = jclic_get_url($jclic, $context);
$params['jclic_path'] = jclic_get_server();
$params['jclic_service'] = jclic_get_path().'/mod/jclic/action/beans.php';
$params['jclic_user'] = $USER->id;
$params['jclic_jarbase'] = $config->jarbase;
$params['jclic_lap'] = $config->lap;
if ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443
|| substr($CFG->wwwroot, 0, strlen('https')) === 'https' ) {
$params['jclic_protocol'] = 'https';
} else {
$params['jclic_protocol'] = 'http';
}
$PAGE->requires->js_init_call('M.mod_jclic.init', array($params));
} else {
echo $OUTPUT->box(get_string('msg_noattempts', 'jclic'), 'generalbox boxaligncenter');
}
jclic_view_dates($jclic, $context, $timenow);
}
function jclic_get_url($jclic, $context){
global $CFG;
$url = '';
if (jclic_is_valid_external_url($jclic->url)) {
$url = $jclic->url;
} else {
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_jclic', 'content', 0, 'sortorder DESC, id ASC', false);
if (count($files) < 1) {
//resource_print_filenotfound($resource, $cm, $course);
die;
} else {
$file = reset($files);
unset($files);
}
$path = '/'.$context->id.'/mod_jclic/content/0'.$file->get_filepath().$file->get_filename();
$url = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
}
return $url;
}
/**
* 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;
}
function jclic_set_mainfile($data) {
$jclic = new jclic();
$jclic->set_instance($data);
return $jclic->set_mainfile();
}
function jclic_is_valid_external_url($url){
return preg_match('/(http:\/\/|https:\/\/|www).*\/*.jclic.zip(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?$/i', $url);
}
function jclic_is_valid_file($filename){
return preg_match('/.jclic.zip$/i', $filename);
}
////////////////////////////////////////////////////////////////////////////////
// Activity sessions //
////////////////////////////////////////////////////////////////////////////////
/**
* 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, $DB;
$sessions=array();
jclic_normalize_date();
$sql = "SELECT js.*
FROM {jclic} j, {jclic_sessions} js
WHERE j.id=js.jclicid AND js.jclicid=? AND js.user_id=?
ORDER BY js.session_datetime";
$params = array($jclicid, $userid);
if($rs = $DB->get_records_sql($sql, $params)){
$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 $DB;
$activities = array();
if($rs = $DB->get_records('jclic_activities', array('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, $DB;
$activity = new stdClass();
$activity->starttime=$session->session_datetime;
$activity->session_id=$session->session_id;
if($rs = $DB->get_record_sql("SELECT AVG(ja.qualification) as qualification, SUM(ja.total_time) as totaltime
FROM {jclic_activities} ja
WHERE ja.session_id='$session->session_id'")){
$activity->score = round($rs->qualification,0);
$activity->totaltime = jclic_format_time($rs->totaltime);
}
if ($rs = $DB->get_record_sql("SELECT COUNT(*) as done
FROM (SELECT DISTINCT ja.activity_name
FROM {jclic_activities} ja
WHERE ja.session_id='$session->session_id') t")){
$activity->done=$rs->done;
}
if ($rs = $DB->get_record_sql("SELECT COUNT(*) as solved
FROM (SELECT DISTINCT ja.activity_name
FROM {jclic_activities} ja
WHERE ja.session_id='$session->session_id' AND ja.activity_solved=1) t")){
$activity->solved=$rs->solved;
}
return $activity;
}
/**
* Print a table data with all session activities
*
* @param string $session_id The session identifier
*/
function jclic_get_session_activities_html($session_id){
$table_html='';
// 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){
$table = new html_table();
$table->attributes = array('class'=>'jclic-activities-table');
$table->head = array($stractivity, $strsolved, $stractions, $strtime, $strscore);
foreach($activities as $activity){
$act_percent=$activity->num_actions>0?round(($activity->score/$activity->num_actions)*100,0):0;
$row = new html_table_row();
$row->attributes = array('class' => ($activity->activity_solved?'jclic-activity-solved':'jclic-activity-unsolved') ) ;
$row->cells = array($activity->activity_name, ($activity->activity_solved?$stryes:$strno), $activity->score.'/'.$activity->num_actions.' ('.$act_percent.'%)', jclic_time2str($activity->total_time), $activity->qualification.'%');
$table->data[] = $row;
}
$table_html = html_writer::table($table);
}
return $table_html;
}
/**
* Convert specified time (in milliseconds) to XX' YY'' format
*
* @param type $time time (in milliseconds) to format
*/
function jclic_format_time($time){
return floor($time/60)."' ".round(fmod($time,60))."''";
}
/**
* 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 $DB;
jclic_normalize_date();
$sessions_summary = new stdClass();
$sessions_summary->attempts = '';
$sessions_summary->score = '';
$sessions_summary->totaltime = '';
$sessions_summary->starttime = '';
$sessions_summary->done = '';
$sessions_summary->solved = '';
if ($rs = $DB->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 {jclic} j, {jclic_sessions} js, {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 = jclic_format_time($rs->totaltime);
$sessions_summary->starttime=$rs->starttime;
}
if ($rs = $DB->get_record_sql("SELECT COUNT(*) as done
FROM (SELECT DISTINCT ja.activity_name
FROM {jclic} j, {jclic_sessions} js, {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 = $DB->get_record_sql("SELECT COUNT(*) as solved
FROM (SELECT DISTINCT ja.activity_name
FROM {jclic} j, {jclic_sessions} js, {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)."''";
}
/**
* Workaround to fix an Oracle's bug when inserting a row with date
*/
function jclic_normalize_date () {
global $CFG, $DB;
if ($CFG->dbtype == 'oci'){
$sql = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
$DB->execute($sql);
}
}
|