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; /** * SLOODLE course object, retrieved directly from database. * @var object * @access private */ var $sloodle_course = null; /** * Constructor. */ function sloodle_view_backpack() { } /** * Check the request parameters to see which course was specified. */ function process_request() { global $USER; $id = required_param('id', PARAM_INT); //check if valid course if (!$this->course = sloodle_get_record('course', 'id', $id)) error('Could not find course.'); $this->sloodle_course = new SloodleCourse(); if (!$this->sloodle_course->load($this->course)) error(get_string('failedcourseload', 'sloodle')); } function process_form() { $id = required_param('id', PARAM_INT); //has itemAdd forum been submitted? $isItemAdd= optional_param('isItemAdd',0, PARAM_INT); $userIds = optional_param('userIds', 0, 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; //now see if it was submitted $fieldvalue= optional_param($fieldname,0,PARAM_INT); //if no value has been submitted for this currency, we can skip adding this currency to the users! 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); } } } } /** * 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()) error(get_string('noguestaccess', 'sloodle')); add_to_log($this->course->id, 'course', 'view sloodle data', '', "{$this->course->id}"); // Ensure the user is allowed to update information on this course $this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id); 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; global $COURSE; $view = optional_param('view', "", PARAM_TEXT); // Setup our list of tabs // We will always have a view option $action = optional_param('action', "", PARAM_TEXT); $context = get_context_instance(CONTEXT_COURSE,$this->course->id); echo "
"; //print titles sloodle_print_box_start('generalbox boxaligncenter center boxheightnarrow leftpara'); echo '
'; echo ''; //print return to backpack icon echo ''; //print return to backpack title text echo s(get_string('backpacks:backpacks', 'sloodle')); echo ''; echo ''; echo ''; echo s(get_string('currency:viewcurrencies', 'sloodle')).'
'; //print return to currencies icon echo '
'; echo '
'; echo '
'; echo '
'; echo ''; echo ''; //get all currency names $all_currencies = SloodleCurrency::FetchIDNameHash(); $active_currency_ids = array(); $contextid = $context->id; $courseid = $this->course->id; $prefix = $CFG->prefix; //build scoresql $scoresql = "select max(p.id) as id, p.userid as userid, p.currencyid as currencyid, sum(amount) as balance from {$prefix}sloodle_award_points p inner join {$prefix}sloodle_award_rounds ro on ro.id=p.roundid where ro.courseid = ? group by p.userid, p.currencyid order by balance desc; "; $scores = sloodle_get_records_sql_params( $scoresql, array( $courseid ) ); //build usersql $usersql = "select max(u.id) as userid, u.firstname as firstname, u.lastname as lastname, su.avname as avname from {$prefix}user u inner join ${prefix}role_assignments ra on u.id=ra.userid left outer join ${prefix}sloodle_users su on u.id=su.userid where ra.contextid=? group by u.id order by avname asc; "; $students = sloodle_get_records_sql_params( $usersql, array( $contextid ) ); //create an array by userid $students_by_userid = array(); foreach($students as $student) { $students_by_userid[ $student->userid ] = $student; } // students with scores, in score order $student_scores_by_currency_id = array(); //creating a two dimensional array keyed by user id, then by currency for our display table foreach($scores as $score) { $userid = $score->userid; $currencyid = $score->currencyid; $active_currency_ids[ $currencyid ] = true; // if student is deleted from course but their score is still there, dont display their score if (!isset($students_by_userid[ $userid ])) { continue; } //makes sure every student has an array entry if ( !isset( $student_scores_by_currency_id[$userid] ) ) { $student_scores_by_currency_id[$userid] = array(); } //put the students balance in the currency into the array $student_scores_by_currency_id[$userid][$currencyid] = $score->balance; } // students without scores to the end of the array, in scored order foreach($students_by_userid as $userid => $student) { if (isset($student_scores_by_currency_id[ $userid ] )) { continue; // already done } $student_scores_by_currency_id[ $userid ] = array(); } //now build display table $sloodletable = new stdClass(); //create header $headerrow = array(); $headerrow[] = s(get_string('awards:avname', 'sloodle')); $headerrow[] = s(get_string('awards:username', 'sloodle')); foreach($all_currencies as $currencyid => $currencyname) { $headerrow[] = s($currencyname); } $headerrow[] = $this->can_edit ? '' : ' '; //now add the header we just built $sloodletable->head = $headerrow; //set alignment of table cells $aligns = array('center','center'); // name columns foreach($all_currencies as $curr) { $aligns[] = 'right'; // each currency } $aligns[] = 'center'; // checkboxes $sloodletable->align = $aligns; $sloodletable->width="95%"; //now display scores foreach($student_scores_by_currency_id as $userid => $currencybalancearray) { $student = $students_by_userid[ $userid ]; $row = array(); $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$userid}&course={$COURSE->id}"; $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&id={$userid}&course={$COURSE->id}"; $row[] = "".s($student->avname).""; $row[] = "".s($student->firstname).' '.s($student->lastname).""; foreach(array_keys($all_currencies) as $currencyid) { if ( isset($currencybalancearray[ $currencyid ] ) ) { $row[] = s($currencybalancearray[ $currencyid ]); } else { $row[] = ' 0 '; } } $row[] = $this->can_edit ? '' : ' '; $sloodletable->data[] = $row; } if ($this->can_edit) { //create an extra row for the modify currency fields $row = array(); $row[] = '   '; $row[] = ""; $row[]=''.s(get_string('backpacks:selectcontroller', 'sloodle')).""; //build select drop down for the controllers in the course that any point updates will be linked too $rowText=''; //add controller select cell to row $row[] =$rowText; //now add the row to the table $sloodletable->data[] = $row; //create another row for the submit button $row = array(); $row[] = ' '; $row[] = ' '; foreach($all_currencies as $currencyid => $currencynames) { $row[] = ''; } $row[]=''; } $sloodletable->data[] = $row; print('
'); echo ''; sloodle_print_table($sloodletable); print '
'; sloodle_print_box_end(); } }