2007-07-16 18:58:09 +00:00
|
|
|
<?php //$Id$
|
2005-03-17 16:23:28 +00:00
|
|
|
|
2007-07-24 07:45:15 +00:00
|
|
|
require_once $CFG->libdir.'/gradelib.php';
|
|
|
|
|
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();
|
2005-03-17 16:23:28 +00:00
|
|
|
|
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-07-23 08:13:59 +00:00
|
|
|
$reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'lang/');
|
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)) {
|
|
|
|
$menu['reportgroup']='--'.get_string('reportplugins', 'grades');
|
|
|
|
$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-07-23 08:13:59 +00:00
|
|
|
$importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin, NULL, $CFG->dirroot.'/grade/import/'.$plugin.'lang/');
|
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)) {
|
|
|
|
$menu['importgroup']='--'.get_string('importplugins', 'grades');
|
|
|
|
$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-07-23 08:13:59 +00:00
|
|
|
$exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin, NULL, $CFG->dirroot.'/grade/export/'.$plugin.'lang/');
|
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)) {
|
|
|
|
$menu['exportgroup']='--'.get_string('exportplugins', 'grades');
|
|
|
|
$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-24 07:45:15 +00:00
|
|
|
if (has_capability('moodle/grade:manage', $context)) { //TODO: add proper capability here
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu['edit']='--'.get_string('edit');
|
|
|
|
$url = 'edit/tree.php?id='.$courseid;
|
|
|
|
if ($active_type == 'edit' and $active_plugin == 'tree' ) {
|
|
|
|
$active = $url;
|
2005-03-17 16:23:28 +00:00
|
|
|
}
|
2007-07-16 18:58:09 +00:00
|
|
|
$menu[$url] = get_string('edittree', 'grades');
|
2005-03-28 18:05:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:58:09 +00:00
|
|
|
/// finally print/return the popup form
|
|
|
|
return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('selectplugin', 'grades'));
|
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 {
|
|
|
|
var $type;
|
|
|
|
var $plugin;
|
|
|
|
var $courseid;
|
|
|
|
var $userid;
|
|
|
|
var $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
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
function grade_plugin_return ($params=null) {
|
|
|
|
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
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
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
|
|
|
|
*/
|
2007-07-23 22:20:07 +00:00
|
|
|
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-24 07:45:15 +00:00
|
|
|
if ($this->type == 'edit') {
|
|
|
|
return $CFG->wwwroot.'/grade/edit/tree.php?id='.$this->courseid;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
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
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
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
|
|
|
|
*/
|
2007-07-16 18:58:09 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class grade_tree {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The basic representation of the tree as a hierarchical, 3-tiered array.
|
|
|
|
* @var object $top_element
|
|
|
|
*/
|
|
|
|
var $top_element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
|
|
|
|
* @var string $commonvars
|
|
|
|
*/
|
|
|
|
var $commonvars;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 2D array of grade items and categories
|
|
|
|
*/
|
|
|
|
var $levels;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
|
|
|
|
* objects for the given courseid. Full objects are instantiated.
|
|
|
|
* and renumbering.
|
|
|
|
* @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
|
|
|
|
* @param boolean $aggregation_view Either full view (0) or compact view (1)
|
|
|
|
*/
|
|
|
|
function grade_tree($courseid, $fillers=true, $category_grade_last=false,
|
|
|
|
$aggregation_view=GRADE_REPORT_AGGREGATION_VIEW_FULL) {
|
|
|
|
global $USER, $CFG;
|
|
|
|
|
|
|
|
$this->courseid = $courseid;
|
|
|
|
$this->commonvars = "&sesskey=$USER->sesskey&id=$this->courseid";
|
|
|
|
$this->levels = array();
|
|
|
|
|
|
|
|
// get course grade tree
|
|
|
|
$this->top_element = grade_category::fetch_course_tree($courseid, true);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static recursive helper - makes the grade_item for category the last children
|
|
|
|
* @static
|
|
|
|
* @param array $element The seed of the recursion
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function category_grade_last(&$element) {
|
|
|
|
if (empty($element['children'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (count($element['children']) < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$category_item = reset($element['children']);
|
|
|
|
$order = key($element['children']);
|
|
|
|
unset($element['children'][$order]);
|
|
|
|
$element['children'][$order] =& $category_item;
|
|
|
|
foreach ($element['children'] as $sortorder=>$child) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function fill_levels(&$levels, &$element, $depth) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)
|
|
|
|
*/
|
|
|
|
function inject_fillers(&$element, $depth) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function inject_colspans(&$element) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function locate_element($eid) {
|
|
|
|
if (strpos($eid, 'g') === 0) {
|
|
|
|
// it is a grade construct a new object
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return edit icon for give element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_edit_icon($element, $gpr) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $this->courseid);
|
|
|
|
if (!has_capability('moodle/grade:manage', $context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = $element['object'];
|
|
|
|
|
|
|
|
switch ($element['type']) {
|
|
|
|
case 'item':
|
|
|
|
case 'categoryitem':
|
|
|
|
case 'courseitem':
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/item.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'category':
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/category.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'grade':
|
|
|
|
//TODO: improve dealing with new grades
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/grade.php?courseid='.$this->courseid.'&id='.$object->id;
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$url = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($url) {
|
|
|
|
$stredit = get_string('edit');
|
|
|
|
return '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return hiding icon for give element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_hiding_icon($element, $gpr) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $this->courseid);
|
|
|
|
if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($element['object']->is_hidden()) {
|
|
|
|
$strshow = get_string('show');
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/action.php?id='.$this->courseid.'&action=show&sesskey='.sesskey().'&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
$action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" title="'.$strshow.'"/></a>';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$strhide = get_string('hide');
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/action.php?id='.$this->courseid.'&action=hide&sesskey='.sesskey().'&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
$action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
|
|
|
|
}
|
|
|
|
return $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return locking icon for give element
|
|
|
|
* @param object $element
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_locking_icon($element, $gpr) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $this->courseid);
|
|
|
|
|
|
|
|
if ($element['object']->is_locked()) {
|
|
|
|
if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$strunlock = get_string('unlock', 'grades');
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/action.php?id='.$this->courseid.'&action=unlock&sesskey='.sesskey().'&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
$action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/unlock.gif" class="iconsmall" alt="'.$strunlock.'" title="'.$strunlock.'"/></a>';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$strlock = get_string('lock', 'grades');
|
|
|
|
$url = $CFG->wwwroot.'/grade/edit/action.php?id='.$this->courseid.'&action=lock&sesskey='.sesskey().'&eid='.$element['eid'];
|
|
|
|
$url = $gpr->add_url_params($url);
|
|
|
|
$action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'.$strlock.'"/></a>';
|
|
|
|
}
|
|
|
|
return $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-07-23 08:13:59 +00:00
|
|
|
?>
|