wwwroot . '/mod/sloodle/lib/jquery/jquery-1.3.2.min.js');
sloodle_require_js($CFG->wwwroot . '/mod/sloodle/lib/js/backpack.js');
/**
* Class for rendering a view of SLOODLE course information.
* @package sloodle
*/
class sloodle_view_backpack extends sloodle_base_view
{
/**
* The Moodle course object, retrieved directly from database.
* @var object
* @access private
*/
var $course = 0;
var $can_edit = false;
/**
* SLOODLE course object, retrieved directly from database.
* @var object
* @access private
*/
var $sloodle_course = null;
//
var $opensim_helper_file = '';
/**
* Constructor.
*/
//function sloodle_view_backpack()
function __construct()
{
}
/**
* Check the request parameters to see which course was specified.
*/
function process_request()
{
global $USER, $CFG;
$id = required_param('id', PARAM_INT);
//check if valid course
if (!$this->course = sloodle_get_record('course', 'id', $id)) print_error('Could not find course.');
$this->sloodle_course = new SloodleCourse();
if (!$this->sloodle_course->load($this->course)) print_error(get_string('failedcourseload', 'sloodle'));
// for OpenSim DTL/NSL Money Server
if ($CFG->sloodle_opensim_money=='helper') {
if (!empty($CFG->sloodle_helper_dir)) {
if (file_exists($CFG->sloodle_helper_dir.'/helper/helpers.php')) {
$this->opensim_helper_file = $CFG->sloodle_helper_dir.'/helper/helpers.php';
}
}
}
else if ($CFG->sloodle_opensim_money=='modlos') {
if (!empty($CFG->modlos_use_currency_server)) {
if (file_exists(SLOODLE_DIRROOT.'/../../blocks/modlos/helper/helpers.php')) {
$this->opensim_helper_file = SLOODLE_DIRROOT.'/../../blocks/modlos/helper/helpers.php';
}
}
}
}
function process_form()
{
global $CFG, $COURSE;
$prefix = $CFG->prefix;
if ($this->course) $courseid = $this->course->id;
else $courseid = $COURSE->id;
$id = required_param('id', PARAM_INT);
$isItemAdd = optional_param('isItemAdd', 0, PARAM_INT);
$userIds = optional_param_array('userIds', array(), PARAM_INT);
//itemAdd form has been submitted
if ($isItemAdd) {
if (!$this->can_edit) {
print_error("Permission denied");
}
$controllerid = required_param('controllerid', PARAM_INT);
//fetch all currencies
$all_currencies = SloodleCurrency::FetchIDNameHash();
//create controller so we can fetch active round
$controller = new SloodleController();
if(!$controller->load_by_course_module_id($controllerid)) {
print_error('Could not load controller for '.$controllerid);
}
$roundid = $controller->get_active_roundid(true);
// go through each currency and see if it has been set, if it has, we have to update each user who
// has been checked
foreach($all_currencies as $currencyid => $currencyname) {
// check if a currency update is necessary for this currency
// build the currencyname field for this currency
$fieldname = 'currency_'.$currencyid;
$fieldvalue = optional_param($fieldname, 0, PARAM_INT);
if ($fieldvalue==0) continue;
foreach ($userIds as $u) {
// go through each user which was checked and give them the selected currency and amount
// create backpack item
$backpack_item = new stdClass();
$backpack_item->currencyid = intval($currencyid);
$backpack_item->userid = intval($u);
$backpack_item->amount = intval($fieldvalue);
$backpack_item->timeawarded = time();
$backpack_item->roundid = $roundid;
$backpack_item->description = "moodle add by ". $USER->username;
//add it to the users backpack
sloodle_insert_record('sloodle_award_points', $backpack_item);
}
}
// to Grade
foreach($all_currencies as $currencyid => $currencyname) {
//
$fieldname = 'toGrade_'.$currencyid;
$fieldvalue = optional_param($fieldname, '', PARAM_TEXT);
if ($fieldvalue=='') continue;
//
$award = new stdClass();
$award->id = $id; // same $couseid
$award->course = $courseid;
$award->itemname = $currencyname;
$award->itemnumber = intval($currencyid);
$award->grademax = 0;
$award->grademin = 0;
$grades = sloodle_get_user_grades($currencyid, $courseid);
foreach($grades as $grade) {
$award->grademax = max($award->grademax, $grade->rawgrade);
$award->grademin = min($award->grademin, $grade->rawgrade);
}
$sqlstr = "SELECT * FROM {$prefix}grade_items WHERE courseid=? AND itemtype=? AND itemmodule=? AND iteminstance=? AND itemnumber=?";
$params = array($courseid, 'mod', 'sloodle', $courseid, $currencyid);
$grade_items = sloodle_get_records_sql_params($sqlstr, $params);
if (!$grade_items or count($grade_items)==0) {
sloodle_grade_item_update($award); // create
}
sloodle_grade_item_update($award, $grades); // update
}
// to Money of OpenSim
if ($this->opensim_helper_file!='') {
require_once($this->opensim_helper_file);
//
foreach($all_currencies as $currencyid => $currencyname) {
//
$fieldname = 'toMoney_'.$currencyid;
$fieldvalue = optional_param($fieldname, '', PARAM_TEXT);
if ($fieldvalue=='') continue;
//
$fieldname = 'rateMoney_'.$currencyid;
$rate = floatval(optional_param($fieldname, '1.0', PARAM_FLOAT));
$tomoney_grades = sloodle_get_user_grades($currencyid, $courseid, 0, 0); // not transfered money
//
foreach($tomoney_grades as $grade) {
$amount = intval(floatval($grade->rawgrade)*$rate);
if ($amount>0) {
$avatar = sloodle_get_record('sloodle_users', 'userid', $grade->userid);
if ($avatar) {
$ret = send_money($avatar->uuid, $amount, 901); // 901: AwardPoints
if ($ret) {
$sqlstr = "SELECT p.* FROM {$prefix}sloodle_award_points AS p INNER JOIN {$prefix}sloodle_award_rounds AS r ON p.roundid=r.id ".
"WHERE p.userid=? AND p.currencyid=? AND r.courseid=? AND p.tomoney='0'";
$params = array($grade->userid, $currencyid, $courseid);
$points = sloodle_get_records_sql_params($sqlstr, $params);
foreach($points as $point) {
$point->tomoney = 1;
sloodle_update_record('sloodle_award_points', $point);
}
}
}
}
}
}
}
}
}
/**
* Check that the user is logged-in and has permission to alter course settings.
*/
function check_permission()
{
// Ensure the user logs in
require_login($this->course->id);
if (isguestuser()) print_error(get_string('noguestaccess', 'sloodle'));
//add_to_log($this->course->id, 'course', 'view sloodle data', '', "{$this->course->id}");
sloodle_add_to_log($this->course->id, 'module_viewed', 'view.php', array('_type'=>'backpack', 'id'=>$this->course->id), 'backpack: view sloodle data');
// Ensure the user is allowed to update information on this course
//$this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id);
$this->course_context = context_course::instance($this->course->id, IGNORE_MISSING);
if (has_capability('moodle/course:update', $this->course_context)) $this->can_edit = true;
}
/**
* Print the course settings page header.
*/
function sloodle_print_header()
{
global $CFG;
//print breadcrumbs
$navigation = "wwwroot}/mod/sloodle/view.php?_type=backpack&id={$this->course->id}\">";
$navigation .= get_string('backpack:view', 'sloodle');
$navigation .= "";
//print the header
sloodle_print_header_simple(get_string('backpack','sloodle'), ' ', $navigation, "", "", true, '', false);
}
/**
* Render the view of the module or feature.
* This MUST be overridden to provide functionality.
*/
function render()
{
global $CFG, $COURSE, $USER;
$view = optional_param('view', '', PARAM_TEXT);
$controllerid = optional_param('controllerid', 0, PARAM_INT);
$courseid = $this->course->id;
$controllers = array();
$prefix = $CFG->prefix;
if ($controllerid==0) {
$controllers = sloodle_get_records_sql_params("select * from {$prefix}sloodle where type=? AND course=?", array(SLOODLE_TYPE_CTRL, $courseid));
if (!$controllers || count($controllers)==0) {
print_error(get_string('objectauthnocontrollers', 'sloodle'));
exit();
}
$cm = get_coursemodule_from_instance('sloodle', current($controllers)->id);
$controllerid = $cm->id;
}
// Setup our list of tabs
// We will always have a view option
$action = optional_param('action', "", PARAM_TEXT);
$context = context_course::instance($this->course->id, IGNORE_MISSING);
$contextid = $context->id;
echo '
';
//print titles
sloodle_print_box_start('generalbox boxaligncenter center boxheightnarrow leftpara');
echo '