2007-07-16 18:58:09 +00:00
|
|
|
<?php //$Id$
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-10-10 06:34:20 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
|
|
|
|
// //
|
|
|
|
// This program 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 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program 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: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
require_once $CFG->libdir.'/gradelib.php';
|
|
|
|
|
2007-08-31 07:33:45 +00:00
|
|
|
/**
|
|
|
|
* This class iterates over all users that are graded in a course.
|
2008-01-09 18:19:42 +00:00
|
|
|
* Returns detailed info about users and their grades.
|
2007-08-31 07:33:45 +00:00
|
|
|
*/
|
|
|
|
class graded_users_iterator {
|
2008-06-02 16:06:33 +00:00
|
|
|
public $course;
|
|
|
|
public $grade_items;
|
|
|
|
public $groupid;
|
|
|
|
public $users_rs;
|
|
|
|
public $grades_rs;
|
|
|
|
public $gradestack;
|
|
|
|
public $sortfield1;
|
|
|
|
public $sortorder1;
|
|
|
|
public $sortfield2;
|
|
|
|
public $sortorder2;
|
2007-08-31 07:33:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2008-01-17 15:08:09 +00:00
|
|
|
* @param $course object
|
2007-08-31 07:33:45 +00:00
|
|
|
* @param array grade_items array of grade items, if not specified only user info returned
|
|
|
|
* @param int $groupid iterate only group users if present
|
2008-01-17 15:08:09 +00:00
|
|
|
* @param string $sortfield1 The first field of the users table by which the array of users will be sorted
|
|
|
|
* @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
|
|
|
|
* @param string $sortfield2 The second field of the users table by which the array of users will be sorted
|
|
|
|
* @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
|
2007-08-31 07:33:45 +00:00
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function graded_users_iterator($course, $grade_items=null, $groupid=0, $sortfield1='lastname', $sortorder1='ASC', $sortfield2='firstname', $sortorder2='ASC') {
|
2007-08-31 07:33:45 +00:00
|
|
|
$this->course = $course;
|
|
|
|
$this->grade_items = $grade_items;
|
|
|
|
$this->groupid = $groupid;
|
2008-01-17 15:08:09 +00:00
|
|
|
$this->sortfield1 = $sortfield1;
|
|
|
|
$this->sortorder1 = $sortorder1;
|
|
|
|
$this->sortfield2 = $sortfield2;
|
|
|
|
$this->sortorder2 = $sortorder2;
|
2007-08-31 07:33:45 +00:00
|
|
|
|
|
|
|
$this->gradestack = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the iterator
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function init() {
|
|
|
|
global $CFG, $DB;
|
2007-08-31 07:33:45 +00:00
|
|
|
|
|
|
|
$this->close();
|
|
|
|
|
|
|
|
grade_regrade_final_grades($this->course->id);
|
|
|
|
$course_item = grade_item::fetch_course_item($this->course->id);
|
|
|
|
if ($course_item->needsupdate) {
|
|
|
|
// can not calculate all final grades - sorry
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-22 15:51:27 +00:00
|
|
|
list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
|
2007-08-31 07:33:45 +00:00
|
|
|
|
|
|
|
$relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $this->course->id));
|
|
|
|
|
|
|
|
if ($this->groupid) {
|
2008-06-02 16:06:33 +00:00
|
|
|
$groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
|
2008-09-22 15:51:27 +00:00
|
|
|
$groupwheresql = "AND gm.groupid = :groupid";
|
2008-06-02 16:06:33 +00:00
|
|
|
// $params contents: gradebookroles
|
2008-09-22 15:51:27 +00:00
|
|
|
$params['groupid'] = $this->groupid;
|
2007-08-31 07:33:45 +00:00
|
|
|
} else {
|
|
|
|
$groupsql = "";
|
|
|
|
$groupwheresql = "";
|
|
|
|
}
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
if (empty($this->sortfield1)) {
|
|
|
|
// we must do some sorting even if not specified
|
|
|
|
$ofields = ", u.id AS usrt";
|
|
|
|
$order = "usrt ASC";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$ofields = ", u.$this->sortfield1 AS usrt1";
|
|
|
|
$order = "usrt1 $this->sortorder1";
|
|
|
|
if (!empty($this->sortfield2)) {
|
|
|
|
$ofields .= ", u.$this->sortfield1 AS usrt2";
|
|
|
|
$order .= ", usrt2 $this->sortorder2";
|
|
|
|
}
|
|
|
|
if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
|
|
|
|
// user order MUST be the same in both queries, must include the only unique user->id if not already present
|
|
|
|
$ofields .= ", u.id AS usrt";
|
|
|
|
$order .= ", usrt ASC";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-02 16:06:33 +00:00
|
|
|
// $params contents: gradebookroles and groupid (for $groupwheresql)
|
2008-02-17 14:49:40 +00:00
|
|
|
$users_sql = "SELECT u.* $ofields
|
2008-06-02 16:06:33 +00:00
|
|
|
FROM {user} u
|
|
|
|
INNER JOIN {role_assignments} ra ON u.id = ra.userid
|
2007-08-31 07:33:45 +00:00
|
|
|
$groupsql
|
2008-06-03 16:10:57 +00:00
|
|
|
WHERE ra.roleid $gradebookroles_sql
|
2007-08-31 07:33:45 +00:00
|
|
|
AND ra.contextid $relatedcontexts
|
2008-02-17 14:49:40 +00:00
|
|
|
$groupwheresql
|
|
|
|
ORDER BY $order";
|
2008-01-17 15:08:09 +00:00
|
|
|
|
2008-06-02 16:06:33 +00:00
|
|
|
$this->users_rs = $DB->get_recordset_sql($users_sql, $params);
|
2007-08-31 07:33:45 +00:00
|
|
|
|
|
|
|
if (!empty($this->grade_items)) {
|
|
|
|
$itemids = array_keys($this->grade_items);
|
2008-09-22 15:51:27 +00:00
|
|
|
list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items0');
|
2008-06-02 16:06:33 +00:00
|
|
|
$params = array_merge($params, $grades_params);
|
2007-08-31 07:33:45 +00:00
|
|
|
|
2008-06-02 16:06:33 +00:00
|
|
|
// $params contents: gradebookroles, groupid (for $groupwheresql) and itemids
|
2008-02-17 14:49:40 +00:00
|
|
|
$grades_sql = "SELECT g.* $ofields
|
2008-06-02 16:06:33 +00:00
|
|
|
FROM {grade_grades} g
|
|
|
|
INNER JOIN {user} u ON g.userid = u.id
|
|
|
|
INNER JOIN {role_assignments} ra ON u.id = ra.userid
|
2007-08-31 07:33:45 +00:00
|
|
|
$groupsql
|
2008-06-02 16:06:33 +00:00
|
|
|
WHERE ra.roleid $gradebookroles_sql
|
2007-08-31 07:33:45 +00:00
|
|
|
AND ra.contextid $relatedcontexts
|
|
|
|
$groupwheresql
|
2008-06-02 16:06:33 +00:00
|
|
|
AND g.itemid $itemidsql
|
2008-02-17 14:49:40 +00:00
|
|
|
ORDER BY $order, g.itemid ASC";
|
2008-06-02 16:06:33 +00:00
|
|
|
$this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
|
2008-02-17 14:49:40 +00:00
|
|
|
} else {
|
|
|
|
$this->grades_rs = false;
|
2007-08-31 07:33:45 +00:00
|
|
|
}
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2007-08-31 07:33:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the next user
|
|
|
|
* @return mixed array of user info, all grades and feedback or null when no more users found
|
|
|
|
*/
|
|
|
|
function next_user() {
|
2007-10-10 12:19:27 +00:00
|
|
|
if (!$this->users_rs) {
|
2007-08-31 07:33:45 +00:00
|
|
|
return false; // no users present
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
if (!$this->users_rs->valid()) {
|
2008-02-17 14:49:40 +00:00
|
|
|
if ($current = $this->_pop()) {
|
|
|
|
// this is not good - user or grades updated between the two reads above :-(
|
|
|
|
}
|
|
|
|
|
2007-08-31 07:33:45 +00:00
|
|
|
return false; // no more users
|
2008-06-03 16:10:57 +00:00
|
|
|
} else {
|
|
|
|
$user = $this->users_rs->current();
|
|
|
|
$this->users_rs->next();
|
2007-08-31 07:33:45 +00:00
|
|
|
}
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
// find grades of this user
|
2007-08-31 07:33:45 +00:00
|
|
|
$grade_records = array();
|
|
|
|
while (true) {
|
|
|
|
if (!$current = $this->_pop()) {
|
|
|
|
break; // no more grades
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
if (empty($current->userid)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
if ($current->userid != $user->id) {
|
|
|
|
// grade of the next user, we have all for this user
|
2007-08-31 07:33:45 +00:00
|
|
|
$this->_push($current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grade_records[$current->itemid] = $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grades = array();
|
|
|
|
$feedbacks = array();
|
|
|
|
|
2008-01-17 15:08:09 +00:00
|
|
|
if (!empty($this->grade_items)) {
|
2008-02-17 14:49:40 +00:00
|
|
|
foreach ($this->grade_items as $grade_item) {
|
|
|
|
if (array_key_exists($grade_item->id, $grade_records)) {
|
|
|
|
$feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
|
|
|
|
$feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
|
|
|
|
unset($grade_records[$grade_item->id]->feedback);
|
|
|
|
unset($grade_records[$grade_item->id]->feedbackformat);
|
|
|
|
$grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
|
|
|
|
} else {
|
|
|
|
$feedbacks[$grade_item->id]->feedback = '';
|
|
|
|
$feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
|
|
|
|
$grades[$grade_item->id] = new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
|
|
|
|
}
|
2007-08-31 07:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = new object();
|
|
|
|
$result->user = $user;
|
|
|
|
$result->grades = $grades;
|
|
|
|
$result->feedbacks = $feedbacks;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the iterator, do not forget to call this function.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function close() {
|
2007-08-31 18:42:50 +00:00
|
|
|
if ($this->users_rs) {
|
2008-06-02 16:06:33 +00:00
|
|
|
$this->users_rs->close();
|
2007-08-31 18:42:50 +00:00
|
|
|
$this->users_rs = null;
|
2007-08-31 07:33:45 +00:00
|
|
|
}
|
2007-08-31 18:42:50 +00:00
|
|
|
if ($this->grades_rs) {
|
2008-06-02 16:06:33 +00:00
|
|
|
$this->grades_rs->close();
|
2007-08-31 18:42:50 +00:00
|
|
|
$this->grades_rs = null;
|
2007-08-31 07:33:45 +00:00
|
|
|
}
|
|
|
|
$this->gradestack = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function
|
|
|
|
*/
|
|
|
|
function _push($grade) {
|
|
|
|
array_push($this->gradestack, $grade);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function
|
|
|
|
*/
|
|
|
|
function _pop() {
|
2008-06-02 16:06:33 +00:00
|
|
|
global $DB;
|
2007-08-31 07:33:45 +00:00
|
|
|
if (empty($this->gradestack)) {
|
2007-10-10 12:19:27 +00:00
|
|
|
if (!$this->grades_rs) {
|
2007-08-31 07:33:45 +00:00
|
|
|
return NULL; // no grades present
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
if ($this->grades_rs->next()) {
|
2007-08-31 07:33:45 +00:00
|
|
|
return NULL; // no more grades
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
return $this->grades_rs->current();
|
2007-08-31 07:33:45 +00:00
|
|
|
} else {
|
|
|
|
return array_pop($this->gradestack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 15:08:09 +00:00
|
|
|
/**
|
|
|
|
* Print a selection popup form of the graded users in a course.
|
|
|
|
*
|
|
|
|
* @param int $courseid id of the course
|
|
|
|
* @param string $actionpage The page receiving the data from the popoup form
|
|
|
|
* @param int $userid id of the currently selected user (or 'all' if they are all selected)
|
|
|
|
* @param bool $return If true, will return the HTML, otherwise, will print directly
|
|
|
|
* @return null
|
|
|
|
*/
|
2008-01-17 16:25:48 +00:00
|
|
|
function print_graded_users_selector($course, $actionpage, $userid=null, $return=false) {
|
|
|
|
global $CFG, $USER;
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2008-01-17 16:25:48 +00:00
|
|
|
if (is_null($userid)) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
2008-01-17 15:08:09 +00:00
|
|
|
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
|
|
|
|
|
|
|
$menu = array(); // Will be a list of userid => user name
|
|
|
|
|
|
|
|
$gui = new graded_users_iterator($course);
|
|
|
|
$gui->init();
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2008-01-17 16:25:48 +00:00
|
|
|
if ($userid !== 0) {
|
|
|
|
$menu[0] = get_string('allusers', 'grades');
|
2008-01-17 15:08:09 +00:00
|
|
|
}
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
$nextuser = $gui->next_user();
|
|
|
|
|
2008-01-17 15:08:09 +00:00
|
|
|
while ($userdata = $gui->next_user()) {
|
|
|
|
$user = $userdata->user;
|
|
|
|
$menu[$user->id] = fullname($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
$gui->close();
|
|
|
|
|
2008-01-17 16:25:48 +00:00
|
|
|
if ($userid !== 0) {
|
|
|
|
$menu[0] .= " (" . (count($menu) - 1) . ")";
|
|
|
|
}
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
return popup_form($CFG->wwwroot.'/grade/' . $actionpage . '&userid=', $menu, 'choosegradeduser', $userid, 'choose', '', '',
|
|
|
|
$return, 'self', get_string('selectalloroneuser', 'grades'));
|
2008-01-17 15:08:09 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Print grading plugin selection popup form.
|
|
|
|
*
|
|
|
|
* @param int $courseid id of course
|
|
|
|
* @param string $active_type type of plugin on current page - import, export, report or edit
|
|
|
|
* @param string $active_plugin active plugin type - grader, user, cvs, ...
|
|
|
|
* @param boolean $return return as string
|
|
|
|
* @return nothing or string if $return true
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) {
|
2005-03-17 16:23:28 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu = array();
|
2007-10-31 09:13:20 +00:00
|
|
|
$count = 0;
|
2007-07-16 18:58:09 +00:00
|
|
|
$active = '';
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// report plugins with its special structure
|
|
|
|
if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports
|
|
|
|
foreach ($reports as $key => $plugin) { // Remove ones we can't see
|
|
|
|
if (!has_capability('gradereport/'.$plugin.':view', $context)) {
|
|
|
|
unset($reports[$key]);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-16 06:21:49 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
$reportnames = array();
|
|
|
|
if (!empty($reports)) {
|
|
|
|
foreach ($reports as $plugin) {
|
2007-07-23 22:20:07 +00:00
|
|
|
$url = 'report/'.$plugin.'/index.php?id='.$courseid;
|
2007-07-16 18:58:09 +00:00
|
|
|
if ($active_type == 'report' and $active_plugin == $plugin ) {
|
|
|
|
$active = $url;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-10-31 09:13:20 +00:00
|
|
|
$reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin);
|
|
|
|
$count++;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
asort($reportnames);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($reportnames)) {
|
2008-02-19 15:00:02 +00:00
|
|
|
$menu['reportgroup']='--'.get_string('view');
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu = $menu+$reportnames;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// standard import plugins
|
2007-07-23 08:13:59 +00:00
|
|
|
if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins
|
2007-07-16 18:58:09 +00:00
|
|
|
foreach ($imports as $key => $plugin) { // Remove ones we can't see
|
|
|
|
if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
|
|
|
|
unset($imports[$key]);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
$importnames = array();
|
|
|
|
if (!empty($imports)) {
|
|
|
|
foreach ($imports as $plugin) {
|
|
|
|
$url = 'import/'.$plugin.'/index.php?id='.$courseid;
|
2007-07-23 22:20:07 +00:00
|
|
|
if ($active_type == 'import' and $active_plugin == $plugin ) {
|
2007-07-16 18:58:09 +00:00
|
|
|
$active = $url;
|
|
|
|
}
|
2007-10-31 09:13:20 +00:00
|
|
|
$importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin);
|
|
|
|
$count++;
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
asort($importnames);
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($importnames)) {
|
2008-02-19 15:00:02 +00:00
|
|
|
$menu['importgroup']='--'.get_string('importfrom', 'grades');
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu = $menu+$importnames;
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// standard export plugins
|
2007-07-23 08:13:59 +00:00
|
|
|
if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins
|
2007-07-16 18:58:09 +00:00
|
|
|
foreach ($exports as $key => $plugin) { // Remove ones we can't see
|
|
|
|
if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
|
|
|
|
unset($exports[$key]);
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
$exportnames = array();
|
|
|
|
if (!empty($exports)) {
|
|
|
|
foreach ($exports as $plugin) {
|
|
|
|
$url = 'export/'.$plugin.'/index.php?id='.$courseid;
|
2007-07-23 22:20:07 +00:00
|
|
|
if ($active_type == 'export' and $active_plugin == $plugin ) {
|
2007-07-16 18:58:09 +00:00
|
|
|
$active = $url;
|
|
|
|
}
|
2007-10-31 09:13:20 +00:00
|
|
|
$exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin);
|
|
|
|
$count++;
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
asort($exportnames);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($exportnames)) {
|
2008-02-19 15:00:02 +00:00
|
|
|
$menu['exportgroup']='--'.get_string('exportto', 'grades');
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu = $menu+$exportnames;
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// editing scripts - not real plugins
|
2007-07-29 23:02:03 +00:00
|
|
|
if (has_capability('moodle/grade:manage', $context)
|
2007-10-03 20:24:59 +00:00
|
|
|
or has_capability('moodle/grade:manageletters', $context)
|
2007-07-31 09:10:35 +00:00
|
|
|
or has_capability('moodle/course:managescales', $context)
|
|
|
|
or has_capability('moodle/course:update', $context)) {
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu['edit']='--'.get_string('edit');
|
2007-07-29 23:02:03 +00:00
|
|
|
|
|
|
|
if (has_capability('moodle/grade:manage', $context)) {
|
|
|
|
$url = 'edit/tree/index.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'tree' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('edittree', 'grades');
|
2007-10-31 09:13:20 +00:00
|
|
|
$count++;
|
2007-07-29 23:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_capability('moodle/course:managescales', $context)) {
|
|
|
|
$url = 'edit/scale/index.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'scale' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('scales');
|
2007-10-31 09:13:20 +00:00
|
|
|
$count++;
|
2007-07-29 23:02:03 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 15:18:54 +00:00
|
|
|
if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or
|
2007-08-02 05:34:48 +00:00
|
|
|
has_capability('moodle/course:update', $context))) {
|
|
|
|
if (has_capability('moodle/course:update', $context)) { // Default to course assignment
|
2007-07-31 09:10:35 +00:00
|
|
|
$url = 'edit/outcome/course.php?id='.$courseid;
|
2007-08-02 05:34:48 +00:00
|
|
|
} else {
|
|
|
|
$url = 'edit/outcome/index.php?id='.$courseid;
|
2007-07-31 09:10:35 +00:00
|
|
|
}
|
2007-07-29 23:02:03 +00:00
|
|
|
if ($active_type == 'edit' and $active_plugin == 'outcome' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('outcomes', 'grades');
|
2007-10-31 09:13:20 +00:00
|
|
|
$count++;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-09-28 20:12:43 +00:00
|
|
|
|
2007-10-03 20:24:59 +00:00
|
|
|
if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:manageletters', $context)) {
|
2007-09-28 20:12:43 +00:00
|
|
|
$url = 'edit/letter/index.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'letter' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('letters', 'grades');
|
2007-10-31 09:13:20 +00:00
|
|
|
$count++;
|
2007-09-28 20:12:43 +00:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:09:10 +00:00
|
|
|
if (has_capability('moodle/grade:manage', $context)) {
|
|
|
|
$url = 'edit/settings/index.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'settings' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('coursesettings', 'grades');
|
2007-10-31 09:13:20 +00:00
|
|
|
$count++;
|
2007-10-08 23:09:10 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 15:51:27 +00:00
|
|
|
if (has_capability('moodle/grade:manage', $context)) {
|
|
|
|
$url = 'edit/weights/index.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'weights' ) {
|
|
|
|
$active = $url;
|
|
|
|
}
|
|
|
|
$menu[$url] = get_string('weights', 'grades');
|
|
|
|
$count++;
|
|
|
|
}
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// finally print/return the popup form
|
2007-10-31 09:13:20 +00:00
|
|
|
if ($count > 1) {
|
2008-02-19 15:00:02 +00:00
|
|
|
return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', '',
|
|
|
|
get_string('chooseaction', 'grades'), '', '', $return, 'self');
|
2007-10-31 09:13:20 +00:00
|
|
|
} else {
|
|
|
|
// only one option - no plugin selector needed
|
|
|
|
return '';
|
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
2007-07-24 07:45:15 +00:00
|
|
|
* Utility class used for return tracking when using edit and other forms in grade plugins
|
2007-07-17 05:52:42 +00:00
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
class grade_plugin_return {
|
2008-06-02 16:06:33 +00:00
|
|
|
public $type;
|
|
|
|
public $plugin;
|
|
|
|
public $courseid;
|
|
|
|
public $userid;
|
|
|
|
public $page;
|
2005-03-28 18:05:45 +00:00
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function grade_plugin_return ($params=null) {
|
2007-07-16 18:58:09 +00:00
|
|
|
if (empty($params)) {
|
|
|
|
$this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
|
|
|
|
$this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR);
|
|
|
|
$this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
|
|
|
|
$this->userid = optional_param('gpr_userid', null, PARAM_INT);
|
|
|
|
$this->page = optional_param('gpr_page', null, PARAM_INT);
|
2005-03-29 09:12:42 +00:00
|
|
|
|
|
|
|
} else {
|
2007-07-16 18:58:09 +00:00
|
|
|
foreach ($params as $key=>$value) {
|
|
|
|
if (array_key_exists($key, $this)) {
|
|
|
|
$this->$key = $value;
|
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-07 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Returns return parameters as options array suitable for buttons.
|
|
|
|
* @return array options
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_options() {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($this->type)) {
|
2007-07-16 18:58:09 +00:00
|
|
|
return array();
|
2006-01-05 15:49:21 +00:00
|
|
|
}
|
2005-04-07 20:47:03 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
$params = array();
|
2005-04-07 20:47:03 +00:00
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
if (!empty($this->plugin)) {
|
|
|
|
$params['plugin'] = $this->plugin;
|
|
|
|
}
|
2005-04-07 20:47:03 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->courseid)) {
|
|
|
|
$params['id'] = $this->courseid;
|
2005-04-07 20:47:03 +00:00
|
|
|
}
|
2006-12-21 08:12:10 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->userid)) {
|
|
|
|
$params['userid'] = $this->userid;
|
2006-12-21 08:12:10 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->page)) {
|
|
|
|
$params['page'] = $this->page;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2006-01-05 15:49:21 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
return $params;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Returns return url
|
|
|
|
* @param string $default default url when params not set
|
|
|
|
* @return string url
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_return_url($default, $extras=null) {
|
2007-07-16 18:58:09 +00:00
|
|
|
global $CFG;
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (empty($this->type) or empty($this->plugin)) {
|
|
|
|
return $default;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-23 22:20:07 +00:00
|
|
|
$url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
|
|
|
|
$glue = '?';
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->courseid)) {
|
|
|
|
$url .= $glue.'id='.$this->courseid;
|
|
|
|
$glue = '&';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->userid)) {
|
|
|
|
$url .= $glue.'userid='.$this->userid;
|
|
|
|
$glue = '&';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2006-08-14 08:46:44 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->page)) {
|
|
|
|
$url .= $glue.'page='.$this->page;
|
2007-07-23 22:20:07 +00:00
|
|
|
$glue = '&';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($extras)) {
|
|
|
|
foreach($extras as $key=>$value) {
|
|
|
|
$url .= $glue.$key.'='.$value;
|
|
|
|
$glue = '&';
|
2007-07-24 07:45:15 +00:00
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
return $url;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Returns string with hidden return tracking form elements.
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_form_fields() {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($this->type)) {
|
2007-07-16 18:58:09 +00:00
|
|
|
return '';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
$result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
|
2007-07-24 07:45:15 +00:00
|
|
|
|
|
|
|
if (!empty($this->plugin)) {
|
|
|
|
$result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
|
|
|
|
}
|
2006-12-22 02:23:52 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->courseid)) {
|
|
|
|
$result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->userid)) {
|
|
|
|
$result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->page)) {
|
|
|
|
$result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Add hidden elements into mform
|
|
|
|
* @param object $mform moodle form object
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function add_mform_elements(&$mform) {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($this->type)) {
|
2007-07-16 18:58:09 +00:00
|
|
|
return;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
$mform->addElement('hidden', 'gpr_type', $this->type);
|
|
|
|
$mform->setType('gpr_type', PARAM_SAFEDIR);
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
if (!empty($this->plugin)) {
|
|
|
|
$mform->addElement('hidden', 'gpr_plugin', $this->plugin);
|
|
|
|
$mform->setType('gpr_plugin', PARAM_SAFEDIR);
|
|
|
|
}
|
2005-06-23 12:37:19 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->courseid)) {
|
|
|
|
$mform->addElement('hidden', 'gpr_courseid', $this->courseid);
|
|
|
|
$mform->setType('gpr_courseid', PARAM_INT);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->userid)) {
|
|
|
|
$mform->addElement('hidden', 'gpr_userid', $this->userid);
|
|
|
|
$mform->setType('gpr_userid', PARAM_INT);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->page)) {
|
|
|
|
$mform->addElement('hidden', 'gpr_page', $this->page);
|
|
|
|
$mform->setType('gpr_page', PARAM_INT);
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-28 18:05:45 +00:00
|
|
|
|
2007-07-17 05:52:42 +00:00
|
|
|
/**
|
|
|
|
* Add return tracking params into url
|
|
|
|
* @param string $url
|
|
|
|
* @return string $url with erturn tracking params
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function add_url_params($url) {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($this->type)) {
|
2007-07-16 18:58:09 +00:00
|
|
|
return $url;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2006-12-22 02:34:42 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (strpos($url, '?') === false) {
|
|
|
|
$url .= '?gpr_type='.$this->type;
|
|
|
|
} else {
|
|
|
|
$url .= '&gpr_type='.$this->type;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
if (!empty($this->plugin)) {
|
|
|
|
$url .= '&gpr_plugin='.$this->plugin;
|
|
|
|
}
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->courseid)) {
|
|
|
|
$url .= '&gpr_courseid='.$this->courseid;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->userid)) {
|
|
|
|
$url .= '&gpr_userid='.$this->userid;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2006-08-15 08:29:29 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
if (!empty($this->page)) {
|
|
|
|
$url .= '&gpr_page='.$this->page;
|
2006-08-15 08:29:29 +00:00
|
|
|
}
|
2006-10-25 08:51:26 +00:00
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
return $url;
|
2006-10-25 08:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-24 07:45:15 +00:00
|
|
|
|
2007-08-01 06:47:35 +00:00
|
|
|
/**
|
|
|
|
* Function central to gradebook for building and printing the navigation (breadcrumb trail).
|
|
|
|
* @param string $path The path of the calling script (using __FILE__?)
|
|
|
|
* @param string $pagename The language string to use as the last part of the navigation (non-link)
|
|
|
|
* @param mixed $id Either a plain integer (assuming the key is 'id') or an array of keys and values (e.g courseid => $courseid, itemid...)
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function grade_build_nav($path, $pagename=null, $id=null) {
|
|
|
|
global $CFG, $COURSE;
|
|
|
|
|
|
|
|
$strgrades = get_string('grades', 'grades');
|
|
|
|
|
|
|
|
// Parse the path and build navlinks from its elements
|
|
|
|
$dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
|
|
|
|
$path = substr($path, $dirroot_length);
|
|
|
|
$path = str_replace('\\', '/', $path);
|
|
|
|
|
|
|
|
$path_elements = explode('/', $path);
|
|
|
|
|
|
|
|
$path_elements_count = count($path_elements);
|
|
|
|
|
|
|
|
// First link is always 'grade'
|
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => $strgrades,
|
|
|
|
'link' => $CFG->wwwroot.'/grade/index.php?id='.$COURSE->id,
|
|
|
|
'type' => 'misc');
|
|
|
|
|
|
|
|
$link = '';
|
|
|
|
$numberofelements = 3;
|
|
|
|
|
|
|
|
// Prepare URL params string
|
|
|
|
$id_string = '?';
|
|
|
|
if (!is_null($id)) {
|
|
|
|
if (is_array($id)) {
|
|
|
|
foreach ($id as $idkey => $idvalue) {
|
|
|
|
$id_string .= "$idkey=$idvalue&";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$id_string .= "id=$id";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$navlink4 = null;
|
|
|
|
|
2007-08-29 11:43:48 +00:00
|
|
|
// Remove file extensions from filenames
|
|
|
|
foreach ($path_elements as $key => $filename) {
|
|
|
|
$path_elements[$key] = str_replace('.php', '', $filename);
|
|
|
|
}
|
|
|
|
|
2007-08-01 06:47:35 +00:00
|
|
|
// Second level links
|
|
|
|
switch ($path_elements[1]) {
|
|
|
|
case 'edit': // No link
|
|
|
|
if ($path_elements[3] != 'index.php') {
|
|
|
|
$numberofelements = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'import': // No link
|
|
|
|
break;
|
|
|
|
case 'export': // No link
|
|
|
|
break;
|
|
|
|
case 'report':
|
|
|
|
// $id is required for this link. Do not print it if $id isn't given
|
|
|
|
if (!is_null($id)) {
|
|
|
|
$link = $CFG->wwwroot . '/grade/report/index.php' . $id_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($path_elements[2] == 'grader') {
|
|
|
|
$numberofelements = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// If this element isn't among the ones already listed above, it isn't supported, throw an error.
|
|
|
|
debugging("grade_build_nav() doesn't support ". $path_elements[1] . " as the second path element after 'grade'.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc');
|
|
|
|
|
|
|
|
// Third level links
|
|
|
|
if (empty($pagename)) {
|
|
|
|
$pagename = get_string($path_elements[2], 'grades');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($numberofelements) {
|
|
|
|
case 3:
|
|
|
|
$navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc');
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
|
|
|
|
if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
|
2007-08-13 02:48:39 +00:00
|
|
|
$navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'),
|
2007-08-01 06:47:35 +00:00
|
|
|
'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string",
|
|
|
|
'type' => 'misc');
|
|
|
|
}
|
|
|
|
$navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$navigation = build_navigation($navlinks);
|
|
|
|
|
|
|
|
return $navigation;
|
2007-08-08 15:48:20 +00:00
|
|
|
}
|
2007-07-24 07:45:15 +00:00
|
|
|
|
2007-10-03 21:14:58 +00:00
|
|
|
/**
|
2007-11-01 11:42:31 +00:00
|
|
|
* General structure representing grade items in course
|
2007-10-03 21:14:58 +00:00
|
|
|
*/
|
2007-11-01 11:42:31 +00:00
|
|
|
class grade_structure {
|
2008-06-02 16:06:33 +00:00
|
|
|
public $context;
|
2007-10-03 21:14:58 +00:00
|
|
|
|
2008-06-02 16:06:33 +00:00
|
|
|
public $courseid;
|
2007-10-03 21:14:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1D array of grade items only
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $items;
|
2007-10-03 21:14:58 +00:00
|
|
|
|
2007-10-17 09:45:14 +00:00
|
|
|
/**
|
2007-11-01 11:42:31 +00:00
|
|
|
* Returns icon of element
|
|
|
|
* @param object $element
|
|
|
|
* @param bool $spacerifnone return spacer if no icon found
|
|
|
|
* @return string icon or spacer
|
2007-10-17 09:45:14 +00:00
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_element_icon(&$element, $spacerifnone=false) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
switch ($element['type']) {
|
|
|
|
case 'item':
|
|
|
|
case 'courseitem':
|
|
|
|
case 'categoryitem':
|
|
|
|
if ($element['object']->is_calculated()) {
|
|
|
|
return '<img src="'.$CFG->pixpath.'/i/calc.gif" class="icon itemicon" alt="'.get_string('calculation', 'grades').'"/>';
|
|
|
|
|
|
|
|
} else if (($element['object']->is_course_item() or $element['object']->is_category_item())
|
|
|
|
and ($element['object']->gradetype == GRADE_TYPE_SCALE or $element['object']->gradetype == GRADE_TYPE_VALUE)) {
|
|
|
|
if ($category = $element['object']->get_item_category()) {
|
|
|
|
switch ($category->aggregation) {
|
|
|
|
case GRADE_AGGREGATE_MEAN:
|
|
|
|
case GRADE_AGGREGATE_MEDIAN:
|
|
|
|
case GRADE_AGGREGATE_WEIGHTED_MEAN:
|
2007-11-15 22:30:01 +00:00
|
|
|
case GRADE_AGGREGATE_WEIGHTED_MEAN2:
|
2007-11-01 11:42:31 +00:00
|
|
|
case GRADE_AGGREGATE_EXTRACREDIT_MEAN:
|
|
|
|
return '<img src="'.$CFG->pixpath.'/i/agg_mean.gif" class="icon itemicon" alt="'.get_string('aggregation', 'grades').'"/>';
|
2007-11-13 15:08:59 +00:00
|
|
|
case GRADE_AGGREGATE_SUM:
|
|
|
|
return '<img src="'.$CFG->pixpath.'/i/agg_sum.gif" class="icon itemicon" alt="'.get_string('aggregation', 'grades').'"/>';
|
2007-11-01 11:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($element['object']->itemtype == 'mod') {
|
|
|
|
return '<img src="'.$CFG->modpixpath.'/'.$element['object']->itemmodule.'/icon.gif" class="icon itemicon" alt="'
|
|
|
|
.get_string('modulename', $element['object']->itemmodule).'"/>';
|
|
|
|
|
|
|
|
} else if ($element['object']->itemtype == 'manual') {
|
|
|
|
if ($element['object']->is_outcome_item()) {
|
|
|
|
return '<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon itemicon" alt="'.get_string('outcome', 'grades').'"/>';
|
|
|
|
} else {
|
2008-02-27 09:17:05 +00:00
|
|
|
return '<img src="'.$CFG->pixpath.'/t/manual_item.gif" class="icon itemicon" alt="'.get_string('manualitem', 'grades').'"/>';
|
2007-11-01 11:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'category':
|
|
|
|
return '<img src="'.$CFG->pixpath.'/f/folder.gif" class="icon itemicon" alt="'.get_string('category', 'grades').'"/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($spacerifnone) {
|
|
|
|
return '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="icon itemicon" alt=""/>';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2007-10-17 09:45:14 +00:00
|
|
|
|
2007-10-03 21:14:58 +00:00
|
|
|
/**
|
2007-11-01 11:42:31 +00:00
|
|
|
* Returns name of element optionally with icon and link
|
|
|
|
* @param object $element
|
|
|
|
* @param bool $withlinks
|
|
|
|
* @param bool $icons
|
|
|
|
* @param bool $spacerifnone return spacer if no icon found
|
|
|
|
* @return header string
|
2007-10-03 21:14:58 +00:00
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
|
|
|
|
if ($icon) {
|
|
|
|
$header .= $this->get_element_icon($element, $spacerifnone);
|
|
|
|
}
|
|
|
|
|
2009-01-16 14:57:43 +00:00
|
|
|
$title = $element['object']->get_name();
|
|
|
|
$header .= $title;
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and $element['type'] != 'courseitem') {
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemtype = $element['object']->itemtype;
|
|
|
|
$itemmodule = $element['object']->itemmodule;
|
|
|
|
$iteminstance = $element['object']->iteminstance;
|
|
|
|
|
|
|
|
if ($withlink and $itemtype=='mod' and $iteminstance and $itemmodule) {
|
2008-02-17 19:04:01 +00:00
|
|
|
if ($cm = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->courseid)) {
|
2007-11-01 11:42:31 +00:00
|
|
|
|
2008-02-17 19:04:01 +00:00
|
|
|
$dir = $CFG->dirroot.'/mod/'.$itemmodule;
|
2007-11-01 11:42:31 +00:00
|
|
|
|
2008-02-17 19:04:01 +00:00
|
|
|
if (file_exists($dir.'/grade.php')) {
|
|
|
|
$url = $CFG->wwwroot.'/mod/'.$itemmodule.'/grade.php?id='.$cm->id;
|
|
|
|
} else {
|
|
|
|
$url = $CFG->wwwroot.'/mod/'.$itemmodule.'/view.php?id='.$cm->id;
|
|
|
|
}
|
2007-11-01 11:42:31 +00:00
|
|
|
|
2009-01-16 14:57:43 +00:00
|
|
|
$header = '<a href="'.$url.'" title="'.$title.'">'.$header.'</a>';
|
2008-02-17 19:04:01 +00:00
|
|
|
}
|
2007-11-01 11:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the grade eid - the grade may not exist yet.
|
|
|
|
* @param $grade_grade object
|
|
|
|
* @return string eid
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_grade_eid($grade_grade) {
|
2007-11-01 11:42:31 +00:00
|
|
|
if (empty($grade_grade->id)) {
|
|
|
|
return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
|
|
|
|
} else {
|
|
|
|
return 'g'.$grade_grade->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the grade_item eid
|
|
|
|
* @param $grade_item object
|
|
|
|
* @return string eid
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_item_eid($grade_item) {
|
2007-11-01 11:42:31 +00:00
|
|
|
return 'i'.$grade_item->id;
|
|
|
|
}
|
|
|
|
|
2007-12-06 09:17:28 +00:00
|
|
|
function get_params_for_iconstr($element) {
|
|
|
|
$strparams = new stdClass();
|
|
|
|
$strparams->category = '';
|
|
|
|
$strparams->itemname = '';
|
|
|
|
$strparams->itemmodule = '';
|
|
|
|
if (!method_exists($element['object'], 'get_name')) {
|
|
|
|
return $strparams;
|
|
|
|
}
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2007-12-06 09:17:28 +00:00
|
|
|
$strparams->itemname = $element['object']->get_name();
|
|
|
|
|
|
|
|
// If element name is categorytotal, get the name of the parent category
|
|
|
|
if ($strparams->itemname == get_string('categorytotal', 'grades')) {
|
|
|
|
$parent = $element['object']->get_parent_category();
|
|
|
|
$strparams->category = $parent->get_name() . ' ';
|
|
|
|
} else {
|
|
|
|
$strparams->category = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$strparams->itemmodule = null;
|
|
|
|
if (isset($element['object']->itemmodule)) {
|
|
|
|
$strparams->itemmodule = $element['object']->itemmodule;
|
2008-02-17 14:49:40 +00:00
|
|
|
}
|
2007-12-06 09:17:28 +00:00
|
|
|
return $strparams;
|
|
|
|
}
|
|
|
|
|
2007-11-01 11:42:31 +00:00
|
|
|
/**
|
|
|
|
* Return edit icon for give element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_edit_icon($element, $gpr) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!has_capability('moodle/grade:manage', $this->context)) {
|
|
|
|
if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) {
|
|
|
|
// oki - let them override grade
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-11 23:59:55 +00:00
|
|
|
static $strfeedback = null;
|
|
|
|
static $streditgrade = null;
|
|
|
|
if (is_null($streditgrade)) {
|
|
|
|
$streditgrade = get_string('editgrade', 'grades');
|
|
|
|
$strfeedback = get_string('feedback');
|
2007-11-01 11:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 09:17:28 +00:00
|
|
|
$strparams = $this->get_params_for_iconstr($element);
|
2008-02-17 14:49:40 +00:00
|
|
|
if ($element['type'] == 'item' or $element['type'] == 'category') {
|
2007-12-06 09:17:28 +00:00
|
|
|
}
|
2008-02-17 14:49:40 +00:00
|
|
|
|
2007-11-01 11:42:31 +00:00
|
|
|
$object = $element['object'];
|
|
|
|
$overlib = '';
|
|
|
|
|
|
|
|
switch ($element['type']) {
|
|
|
|
case 'item':
|
|
|
|
case 'categoryitem':
|
|
|
|
case 'courseitem':
|
2007-12-06 09:17:28 +00:00
|
|
|
$stredit = get_string('editverbose', 'grades', $strparams);
|
2007-11-01 11:42:31 +00:00
|
|
|
if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
} else {
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
}
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'category':
|
2007-12-06 09:17:28 +00:00
|
|
|
$stredit = get_string('editverbose', 'grades', $strparams);
|
2007-11-01 11:42:31 +00:00
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'grade':
|
2008-03-11 23:59:55 +00:00
|
|
|
$stredit = $streditgrade;
|
2007-11-01 11:42:31 +00:00
|
|
|
if (empty($object->id)) {
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&itemid='.$object->itemid.'&userid='.$object->userid;
|
|
|
|
} else {
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
}
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
if (!empty($object->feedback)) {
|
|
|
|
$feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat)));
|
|
|
|
$function = "return overlib('$feedback', BORDER, 0, FGCLASS, 'feedback', "
|
|
|
|
."CAPTIONFONTCLASS, 'caption', CAPTION, '$strfeedback');";
|
|
|
|
$overlib = 'onmouseover="'.s($function).'" onmouseout="return nd();"';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$url = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($url) {
|
2008-04-18 19:30:28 +00:00
|
|
|
return '<a href="'.$url.'" class="edit"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return hiding icon for give element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_hiding_icon($element, $gpr) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
$strparams = $this->get_params_for_iconstr($element);
|
2007-12-06 09:17:28 +00:00
|
|
|
$strshow = get_string('showverbose', 'grades', $strparams);
|
2008-02-17 14:49:40 +00:00
|
|
|
$strhide = get_string('hideverbose', 'grades', $strparams);
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
if ($element['object']->is_hidden()) {
|
|
|
|
$icon = 'show';
|
|
|
|
$tooltip = $strshow;
|
|
|
|
|
|
|
|
if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date
|
|
|
|
$icon = 'hiddenuntil';
|
|
|
|
$tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=show&sesskey='.sesskey()
|
|
|
|
. '&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
2008-04-18 19:30:28 +00:00
|
|
|
$action = '<a href="'.$url.'" class="hide"><img alt="'.$strshow.'" src="'.$CFG->pixpath.'/t/'.$icon.'.gif" class="iconsmall" title="'.$tooltip.'"/></a>';
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=hide&sesskey='.sesskey()
|
|
|
|
. '&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
2008-04-18 19:30:28 +00:00
|
|
|
$action = '<a href="'.$url.'" class="hide"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
|
2007-11-01 11:42:31 +00:00
|
|
|
}
|
|
|
|
return $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-28 15:43:09 +00:00
|
|
|
* Return locking icon for given element
|
2007-11-01 11:42:31 +00:00
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_locking_icon($element, $gpr) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2008-02-17 14:49:40 +00:00
|
|
|
$strparams = $this->get_params_for_iconstr($element);
|
2007-12-06 09:17:28 +00:00
|
|
|
$strunlock = get_string('unlockverbose', 'grades', $strparams);
|
|
|
|
$strlock = get_string('lockverbose', 'grades', $strparams);
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-02-28 15:43:09 +00:00
|
|
|
// Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon
|
|
|
|
if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) {
|
|
|
|
$strparamobj = new stdClass();
|
|
|
|
$strparamobj->itemname = $element['object']->grade_item->itemname;
|
|
|
|
$strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj);
|
2008-06-02 16:06:33 +00:00
|
|
|
$action = '<img src="'.$CFG->pixpath.'/t/unlock_gray.gif" alt="'.$strnonunlockable.'" class="iconsmall" title="'.$strnonunlockable.'"/>';
|
2008-02-28 15:43:09 +00:00
|
|
|
} elseif ($element['object']->is_locked()) {
|
2007-11-01 11:42:31 +00:00
|
|
|
$icon = 'unlock';
|
|
|
|
$tooltip = $strunlock;
|
|
|
|
|
|
|
|
if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date
|
|
|
|
$icon = 'locktime';
|
|
|
|
$tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=unlock&sesskey='.sesskey()
|
|
|
|
. '&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
2008-04-18 19:30:28 +00:00
|
|
|
$action = '<a href="'.$url.'" class="lock"><img src="'.$CFG->pixpath.'/t/'.$icon.'.gif" alt="'.$strunlock.'" class="iconsmall" title="'.$tooltip.'"/></a>';
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=lock&sesskey='.sesskey()
|
|
|
|
. '&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
2008-04-18 19:30:28 +00:00
|
|
|
$action = '<a href="'.$url.'" class="lock"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'
|
2007-11-01 11:42:31 +00:00
|
|
|
. $strlock.'"/></a>';
|
|
|
|
}
|
|
|
|
return $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return calculation icon for given element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function get_calculation_icon($element, $gpr) {
|
2007-11-01 11:42:31 +00:00
|
|
|
global $CFG;
|
|
|
|
if (!has_capability('moodle/grade:manage', $this->context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$calculation_icon = '';
|
|
|
|
|
|
|
|
$type = $element['type'];
|
|
|
|
$object = $element['object'];
|
|
|
|
|
2007-12-06 09:17:28 +00:00
|
|
|
|
2007-11-01 11:42:31 +00:00
|
|
|
if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
|
2008-02-17 14:49:40 +00:00
|
|
|
$strparams = $this->get_params_for_iconstr($element);
|
2007-12-06 09:17:28 +00:00
|
|
|
$streditcalculation = get_string('editcalculationverbose', 'grades', $strparams);
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
// show calculation icon only when calculation possible
|
2008-02-27 10:11:22 +00:00
|
|
|
if (!$object->is_external_item() and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
|
2007-11-01 11:42:31 +00:00
|
|
|
if ($object->is_calculated()) {
|
|
|
|
$icon = 'calc.gif';
|
|
|
|
} else {
|
|
|
|
$icon = 'calc_off.gif';
|
|
|
|
}
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
$url = $gpr->add_url_params($url);
|
2008-04-18 19:30:28 +00:00
|
|
|
$calculation_icon = '<a href="'. $url.'" class="calculation"><img src="'.$CFG->pixpath.'/t/'.$icon.'" class="iconsmall" alt="'
|
2007-11-01 11:42:31 +00:00
|
|
|
. $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calculation_icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flat structure similar to grade tree.
|
|
|
|
*/
|
|
|
|
class grade_seq extends grade_structure {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
|
|
|
|
* @var string $commonvars
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $commonvars;
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1D array of elements
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $elements;
|
2007-10-03 21:14:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, retrieves and stores array of all grade_category and grade_item
|
|
|
|
* objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
|
|
|
|
* @param int $courseid
|
|
|
|
* @param boolean $category_grade_last category grade item is the last child
|
|
|
|
* @param array $collapsed array of collapsed categories
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
|
2007-10-03 21:14:58 +00:00
|
|
|
global $USER, $CFG;
|
|
|
|
|
|
|
|
$this->courseid = $courseid;
|
2009-01-02 10:51:26 +00:00
|
|
|
$this->commonvars = "&sesskey=".sesskey()."&id=$this->courseid";
|
2007-10-03 21:14:58 +00:00
|
|
|
$this->context = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
|
|
|
|
// get course grade tree
|
|
|
|
$top_element = grade_category::fetch_course_tree($courseid, true);
|
|
|
|
|
2007-10-17 09:45:14 +00:00
|
|
|
$this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes);
|
|
|
|
|
|
|
|
foreach ($this->elements as $key=>$unused) {
|
2007-10-17 20:38:52 +00:00
|
|
|
$this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object'];
|
2007-10-17 09:45:14 +00:00
|
|
|
}
|
2007-10-03 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static recursive helper - makes the grade_item for category the last children
|
|
|
|
* @static
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function flatten(&$element, $category_grade_last, $nooutcomes) {
|
2007-10-03 21:14:58 +00:00
|
|
|
if (empty($element['children'])) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$children = array();
|
|
|
|
|
|
|
|
foreach ($element['children'] as $sortorder=>$unused) {
|
|
|
|
if ($nooutcomes and $element['type'] != 'category' and $element['children'][$sortorder]['object']->is_outcome_item()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$children[] = $element['children'][$sortorder];
|
|
|
|
}
|
|
|
|
unset($element['children']);
|
|
|
|
|
|
|
|
if ($category_grade_last and count($children) > 1) {
|
|
|
|
$cat_item = array_shift($children);
|
|
|
|
array_push($children, $cat_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
foreach ($children as $child) {
|
2007-10-08 23:09:10 +00:00
|
|
|
if ($child['type'] == 'category') {
|
2007-10-17 09:45:14 +00:00
|
|
|
$result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes);
|
2007-10-03 21:14:58 +00:00
|
|
|
} else {
|
|
|
|
$child['eid'] = 'i'.$child['object']->id;
|
2007-10-17 09:45:14 +00:00
|
|
|
$result[$child['object']->id] = $child;
|
2007-10-03 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the array in search of a given eid and returns a element object with
|
|
|
|
* information about the element it has found.
|
|
|
|
* @param int $eid
|
|
|
|
* @return object element
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function locate_element($eid) {
|
2007-10-03 21:14:58 +00:00
|
|
|
// it is a grade - construct a new object
|
|
|
|
if (strpos($eid, 'n') === 0) {
|
|
|
|
if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemid = $matches[1];
|
|
|
|
$userid = $matches[2];
|
|
|
|
|
|
|
|
//extra security check - the grade item must be in this tree
|
|
|
|
if (!$item_el = $this->locate_element('i'.$itemid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $gradea->id may be null - means does not exist yet
|
|
|
|
$grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
|
|
|
|
|
|
|
|
$grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
|
|
|
|
return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
|
|
|
|
|
|
|
|
} else if (strpos($eid, 'g') === 0) {
|
|
|
|
$id = (int)substr($eid, 1);
|
|
|
|
if (!$grade = grade_grade::fetch(array('id'=>$id))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//extra security check - the grade item must be in this tree
|
|
|
|
if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
|
|
|
|
return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is a category or item
|
2007-10-17 09:45:14 +00:00
|
|
|
foreach ($this->elements as $element) {
|
2007-11-01 11:42:31 +00:00
|
|
|
if ($element['eid'] == $eid) {
|
|
|
|
return $element;
|
|
|
|
}
|
2007-10-03 21:14:58 +00:00
|
|
|
}
|
2007-11-01 11:42:31 +00:00
|
|
|
|
|
|
|
return null;
|
2007-10-03 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
/**
|
|
|
|
* This class represents a complete tree of categories, grade_items and final grades,
|
|
|
|
* organises as an array primarily, but which can also be converted to other formats.
|
|
|
|
* It has simple method calls with complex implementations, allowing for easy insertion,
|
|
|
|
* deletion and moving of items and categories within the tree.
|
|
|
|
*/
|
2007-11-01 11:42:31 +00:00
|
|
|
class grade_tree extends grade_structure {
|
2007-07-24 07:45:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The basic representation of the tree as a hierarchical, 3-tiered array.
|
|
|
|
* @var object $top_element
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $top_element;
|
2007-07-24 07:45:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
|
|
|
|
* @var string $commonvars
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $commonvars;
|
2007-07-24 07:45:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 2D array of grade items and categories
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $levels;
|
2007-07-24 07:45:15 +00:00
|
|
|
|
2007-10-17 20:38:52 +00:00
|
|
|
/**
|
|
|
|
* Grade items
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public $items;
|
2007-10-17 20:38:52 +00:00
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
|
2007-10-03 21:14:58 +00:00
|
|
|
* objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
|
2007-07-24 07:45:15 +00:00
|
|
|
* @param int $courseid
|
|
|
|
* @param boolean $fillers include fillers and colspans, make the levels var "rectangular"
|
|
|
|
* @param boolean $category_grade_last category grade item is the last child
|
2007-07-25 19:57:47 +00:00
|
|
|
* @param array $collapsed array of collapsed categories
|
2007-07-24 07:45:15 +00:00
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
|
2007-07-24 07:45:15 +00:00
|
|
|
global $USER, $CFG;
|
|
|
|
|
|
|
|
$this->courseid = $courseid;
|
2009-01-02 10:51:26 +00:00
|
|
|
$this->commonvars = "&sesskey=".sesskey()."&id=$this->courseid";
|
2007-07-24 07:45:15 +00:00
|
|
|
$this->levels = array();
|
2007-07-24 21:20:29 +00:00
|
|
|
$this->context = get_context_instance(CONTEXT_COURSE, $courseid);
|
2007-07-24 07:45:15 +00:00
|
|
|
|
|
|
|
// get course grade tree
|
|
|
|
$this->top_element = grade_category::fetch_course_tree($courseid, true);
|
|
|
|
|
2007-07-25 19:57:47 +00:00
|
|
|
// collapse the categories if requested
|
|
|
|
if (!empty($collapsed)) {
|
|
|
|
grade_tree::category_collapse($this->top_element, $collapsed);
|
|
|
|
}
|
|
|
|
|
2007-08-08 08:31:04 +00:00
|
|
|
// no otucomes if requested
|
|
|
|
if (!empty($nooutcomes)) {
|
|
|
|
grade_tree::no_outcomes($this->top_element);
|
|
|
|
}
|
|
|
|
|
2007-07-25 19:57:47 +00:00
|
|
|
// move category item to last position in category
|
2007-07-24 07:45:15 +00:00
|
|
|
if ($category_grade_last) {
|
|
|
|
grade_tree::category_grade_last($this->top_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fillers) {
|
|
|
|
// inject fake categories == fillers
|
|
|
|
grade_tree::inject_fillers($this->top_element, 0);
|
|
|
|
// add colspans to categories and fillers
|
|
|
|
grade_tree::inject_colspans($this->top_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
grade_tree::fill_levels($this->levels, $this->top_element, 0);
|
2007-10-19 08:51:52 +00:00
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 19:57:47 +00:00
|
|
|
/**
|
|
|
|
* Static recursive helper - removes items from collapsed categories
|
|
|
|
* @static
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @param array $collapsed array of collapsed categories
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function category_collapse(&$element, $collapsed) {
|
2007-07-25 19:57:47 +00:00
|
|
|
if ($element['type'] != 'category') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (empty($element['children']) or count($element['children']) < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-09 16:41:39 +00:00
|
|
|
if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
|
2007-07-25 19:57:47 +00:00
|
|
|
$category_item = reset($element['children']); //keep only category item
|
|
|
|
$element['children'] = array(key($element['children'])=>$category_item);
|
|
|
|
|
|
|
|
} else {
|
2007-08-09 16:41:39 +00:00
|
|
|
if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item
|
|
|
|
reset($element['children']);
|
|
|
|
$first_key = key($element['children']);
|
|
|
|
unset($element['children'][$first_key]);
|
|
|
|
}
|
|
|
|
foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
|
2007-07-25 19:57:47 +00:00
|
|
|
grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-24 07:45:15 +00:00
|
|
|
|
2007-08-08 08:31:04 +00:00
|
|
|
/**
|
|
|
|
* Static recursive helper - removes all outcomes
|
|
|
|
* @static
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function no_outcomes(&$element) {
|
2007-08-08 08:31:04 +00:00
|
|
|
if ($element['type'] != 'category') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($element['children'] as $sortorder=>$child) {
|
|
|
|
if ($element['children'][$sortorder]['type'] == 'item'
|
|
|
|
and $element['children'][$sortorder]['object']->is_outcome_item()) {
|
|
|
|
unset($element['children'][$sortorder]);
|
|
|
|
|
2007-08-08 15:48:20 +00:00
|
|
|
} else if ($element['children'][$sortorder]['type'] == 'category') {
|
2007-08-08 08:31:04 +00:00
|
|
|
grade_tree::no_outcomes($element['children'][$sortorder]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
/**
|
|
|
|
* Static recursive helper - makes the grade_item for category the last children
|
|
|
|
* @static
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function category_grade_last(&$element) {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($element['children'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (count($element['children']) < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2007-10-04 08:36:34 +00:00
|
|
|
$first_item = reset($element['children']);
|
2007-10-04 09:00:29 +00:00
|
|
|
if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') {
|
2007-10-04 08:36:34 +00:00
|
|
|
// the category item might have been already removed
|
|
|
|
$order = key($element['children']);
|
|
|
|
unset($element['children'][$order]);
|
|
|
|
$element['children'][$order] =& $first_item;
|
|
|
|
}
|
2007-09-26 12:25:16 +00:00
|
|
|
foreach ($element['children'] as $sortorder => $child) {
|
2007-07-24 07:45:15 +00:00
|
|
|
grade_tree::category_grade_last($element['children'][$sortorder]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static recursive helper - fills the levels array, useful when accessing tree elements of one level
|
|
|
|
* @static
|
|
|
|
* @param int $levels
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @param int $depth
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function fill_levels(&$levels, &$element, $depth) {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (!array_key_exists($depth, $levels)) {
|
|
|
|
$levels[$depth] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare unique identifier
|
|
|
|
if ($element['type'] == 'category') {
|
|
|
|
$element['eid'] = 'c'.$element['object']->id;
|
|
|
|
} else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
|
|
|
|
$element['eid'] = 'i'.$element['object']->id;
|
2007-10-17 20:38:52 +00:00
|
|
|
$this->items[$element['object']->id] =& $element['object'];
|
2007-07-24 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$levels[$depth][] =& $element;
|
|
|
|
$depth++;
|
|
|
|
if (empty($element['children'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$prev = 0;
|
|
|
|
foreach ($element['children'] as $sortorder=>$child) {
|
|
|
|
grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
|
|
|
|
$element['children'][$sortorder]['prev'] = $prev;
|
|
|
|
$element['children'][$sortorder]['next'] = 0;
|
|
|
|
if ($prev) {
|
|
|
|
$element['children'][$prev]['next'] = $sortorder;
|
|
|
|
}
|
|
|
|
$prev = $sortorder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static recursive helper - makes full tree (all leafes are at the same level)
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function inject_fillers(&$element, $depth) {
|
2007-07-24 07:45:15 +00:00
|
|
|
$depth++;
|
|
|
|
|
|
|
|
if (empty($element['children'])) {
|
|
|
|
return $depth;
|
|
|
|
}
|
|
|
|
$chdepths = array();
|
|
|
|
$chids = array_keys($element['children']);
|
|
|
|
$last_child = end($chids);
|
|
|
|
$first_child = reset($chids);
|
|
|
|
|
|
|
|
foreach ($chids as $chid) {
|
|
|
|
$chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
|
|
|
|
}
|
|
|
|
arsort($chdepths);
|
|
|
|
|
|
|
|
$maxdepth = reset($chdepths);
|
|
|
|
foreach ($chdepths as $chid=>$chd) {
|
|
|
|
if ($chd == $maxdepth) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for ($i=0; $i < $maxdepth-$chd; $i++) {
|
|
|
|
if ($chid == $first_child) {
|
|
|
|
$type = 'fillerfirst';
|
|
|
|
} else if ($chid == $last_child) {
|
|
|
|
$type = 'fillerlast';
|
|
|
|
} else {
|
|
|
|
$type = 'filler';
|
|
|
|
}
|
|
|
|
$oldchild =& $element['children'][$chid];
|
|
|
|
$element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $maxdepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static recursive helper - add colspan information into categories
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function inject_colspans(&$element) {
|
2007-07-24 07:45:15 +00:00
|
|
|
if (empty($element['children'])) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
$count = 0;
|
|
|
|
foreach ($element['children'] as $key=>$child) {
|
|
|
|
$count += grade_tree::inject_colspans($element['children'][$key]);
|
|
|
|
}
|
|
|
|
$element['colspan'] = $count;
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the array in search of a given eid and returns a element object with
|
|
|
|
* information about the element it has found.
|
|
|
|
* @param int $eid
|
|
|
|
* @return object element
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function locate_element($eid) {
|
2007-08-07 08:30:25 +00:00
|
|
|
// it is a grade - construct a new object
|
|
|
|
if (strpos($eid, 'n') === 0) {
|
|
|
|
if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemid = $matches[1];
|
|
|
|
$userid = $matches[2];
|
|
|
|
|
|
|
|
//extra security check - the grade item must be in this tree
|
|
|
|
if (!$item_el = $this->locate_element('i'.$itemid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $gradea->id may be null - means does not exist yet
|
|
|
|
$grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
|
|
|
|
|
|
|
|
$grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
|
|
|
|
return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
|
|
|
|
|
|
|
|
} else if (strpos($eid, 'g') === 0) {
|
2007-07-24 07:45:15 +00:00
|
|
|
$id = (int)substr($eid, 1);
|
|
|
|
if (!$grade = grade_grade::fetch(array('id'=>$id))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//extra security check - the grade item must be in this tree
|
|
|
|
if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
|
|
|
|
return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is a category or item
|
|
|
|
foreach ($this->levels as $row) {
|
|
|
|
foreach ($row as $element) {
|
|
|
|
if ($element['type'] == 'filler') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($element['eid'] == $eid) {
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
/**
|
|
|
|
* Returns a well-formed XML representation of the grade-tree using recursion.
|
|
|
|
* @param array $root The current element in the recursion. If null, starts at the top of the tree.
|
|
|
|
* @return string $xml
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function exportToXML($root=null, $tabs="\t") {
|
2008-04-18 19:30:28 +00:00
|
|
|
$xml = null;
|
|
|
|
$first = false;
|
|
|
|
if (is_null($root)) {
|
|
|
|
$root = $this->top_element;
|
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
|
|
|
|
$xml .= "<gradetree>\n";
|
|
|
|
$first = true;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$type = 'undefined';
|
|
|
|
if (strpos($root['object']->table, 'grade_categories') !== false) {
|
|
|
|
$type = 'category';
|
|
|
|
} elseif (strpos($root['object']->table, 'grade_items') !== false) {
|
|
|
|
$type = 'item';
|
|
|
|
} elseif (strpos($root['object']->table, 'grade_outcomes') !== false) {
|
|
|
|
$type = 'outcome';
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$xml .= "$tabs<element type=\"$type\">\n";
|
|
|
|
foreach ($root['object'] as $var => $value) {
|
|
|
|
if (!is_object($value) && !is_array($value) && !empty($value)) {
|
|
|
|
$xml .= "$tabs\t<$var>$value</$var>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($root['children'])) {
|
|
|
|
$xml .= "$tabs\t<children>\n";
|
|
|
|
foreach ($root['children'] as $sortorder => $child) {
|
|
|
|
$xml .= $this->exportToXML($child, $tabs."\t\t");
|
|
|
|
}
|
|
|
|
$xml .= "$tabs\t</children>\n";
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$xml .= "$tabs</element>\n";
|
|
|
|
|
|
|
|
if ($first) {
|
|
|
|
$xml .= "</gradetree>";
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
return $xml;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
/**
|
|
|
|
* Returns a JSON representation of the grade-tree using recursion.
|
|
|
|
* @param array $root The current element in the recursion. If null, starts at the top of the tree.
|
|
|
|
* @param string $tabs Tab characters used to indent the string nicely for humans to enjoy
|
|
|
|
* @param int $switch The position (first or last) of the aggregations
|
|
|
|
* @return string $xml
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public function exportToJSON($root=null, $tabs="\t") {
|
2008-04-18 19:30:28 +00:00
|
|
|
$json = null;
|
|
|
|
$first = false;
|
|
|
|
if (is_null($root)) {
|
|
|
|
$root = $this->top_element;
|
|
|
|
$first = true;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$name = '';
|
|
|
|
|
|
|
|
|
|
|
|
if (strpos($root['object']->table, 'grade_categories') !== false) {
|
|
|
|
$name = $root['object']->fullname;
|
|
|
|
if ($name == '?') {
|
2008-06-02 16:06:33 +00:00
|
|
|
$name = $root['object']->get_name();
|
2008-04-18 19:30:28 +00:00
|
|
|
}
|
|
|
|
} elseif (strpos($root['object']->table, 'grade_items') !== false) {
|
|
|
|
$name = $root['object']->itemname;
|
|
|
|
} elseif (strpos($root['object']->table, 'grade_outcomes') !== false) {
|
|
|
|
$name = $root['object']->itemname;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$json .= "$tabs {\n";
|
|
|
|
$json .= "$tabs\t \"type\": \"{$root['type']}\",\n";
|
|
|
|
$json .= "$tabs\t \"name\": \"$name\",\n";
|
|
|
|
|
|
|
|
foreach ($root['object'] as $var => $value) {
|
|
|
|
if (!is_object($value) && !is_array($value) && !empty($value)) {
|
|
|
|
$json .= "$tabs\t \"$var\": \"$value\",\n";
|
|
|
|
}
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
$json = substr($json, 0, strrpos($json, ','));
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
if (!empty($root['children'])) {
|
|
|
|
$json .= ",\n$tabs\t\"children\": [\n";
|
|
|
|
foreach ($root['children'] as $sortorder => $child) {
|
|
|
|
$json .= $this->exportToJSON($child, $tabs."\t\t");
|
|
|
|
}
|
|
|
|
$json = substr($json, 0, strrpos($json, ','));
|
|
|
|
$json .= "\n$tabs\t]\n";
|
2008-06-02 16:06:33 +00:00
|
|
|
}
|
2008-04-18 19:30:28 +00:00
|
|
|
|
|
|
|
if ($first) {
|
|
|
|
$json .= "\n}";
|
|
|
|
} else {
|
|
|
|
$json .= "\n$tabs},\n";
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
2008-04-18 19:30:28 +00:00
|
|
|
return $json;
|
|
|
|
}
|
2008-06-02 16:06:33 +00:00
|
|
|
|
|
|
|
public function get_levels() {
|
|
|
|
return $this->levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_items() {
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_item($itemid) {
|
|
|
|
if (array_key_exists($itemid, $this->items)) {
|
|
|
|
return $this->items[$itemid];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-07-24 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
2007-07-23 08:13:59 +00:00
|
|
|
?>
|