2009-07-07 02:26:36 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2007-10-10 06:34:20 +00:00
2012-01-06 11:52:46 +07:00
/**
* A moodleform to allow the editing of a user ' s grade for a particular activity
*
* @ package core_grades
* @ copyright 2007 Petr Skoda
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2010-05-13 02:02:05 +00:00
if ( ! defined ( 'MOODLE_INTERNAL' )) {
die ( 'Direct access to this script is forbidden.' ); /// It must be included from a Moodle page
}
2007-07-11 09:01:55 +00:00
require_once $CFG -> libdir . '/formslib.php' ;
class edit_grade_form extends moodleform {
2007-07-21 14:24:39 +00:00
2007-07-11 09:01:55 +00:00
function definition () {
2008-06-03 16:10:57 +00:00
global $CFG , $COURSE , $DB ;
2007-07-12 08:48:02 +00:00
2007-07-11 09:01:55 +00:00
$mform =& $this -> _form ;
2007-07-12 19:40:28 +00:00
2007-07-21 14:24:39 +00:00
$grade_item = $this -> _customdata [ 'grade_item' ];
$gpr = $this -> _customdata [ 'gpr' ];
2009-05-08 20:07:33 +00:00
if ( $grade_item -> is_course_item ()) {
$grade_category = null ;
} else if ( $grade_item -> is_category_item ()) {
$grade_category = $grade_item -> get_item_category ();
$grade_category = $grade_category -> get_parent_category ();
} else {
$grade_category = $grade_item -> get_parent_category ();
}
2007-07-21 14:24:39 +00:00
/// information fields
$mform -> addElement ( 'static' , 'user' , get_string ( 'user' ));
$mform -> addElement ( 'static' , 'itemname' , get_string ( 'itemname' , 'grades' ));
2007-07-12 19:40:28 +00:00
2007-10-30 11:40:50 +00:00
$mform -> addElement ( 'checkbox' , 'overridden' , get_string ( 'overridden' , 'grades' ));
2010-06-21 06:50:24 +00:00
$mform -> addHelpButton ( 'overridden' , 'overridden' , 'grades' );
2007-10-30 11:40:50 +00:00
2007-07-21 14:24:39 +00:00
/// actual grade - numeric or scale
if ( $grade_item -> gradetype == GRADE_TYPE_VALUE ) {
2007-07-11 09:01:55 +00:00
// numeric grade
$mform -> addElement ( 'text' , 'finalgrade' , get_string ( 'finalgrade' , 'grades' ));
2013-04-18 10:02:52 +08:00
$mform -> setType ( 'finalgrade' , PARAM_RAW );
2010-05-27 20:31:16 +00:00
$mform -> addHelpButton ( 'finalgrade' , 'finalgrade' , 'grades' );
2007-10-30 11:40:50 +00:00
$mform -> disabledIf ( 'finalgrade' , 'overridden' , 'notchecked' );
2007-07-12 22:31:20 +00:00
2007-07-21 14:24:39 +00:00
} else if ( $grade_item -> gradetype == GRADE_TYPE_SCALE ) {
2007-07-11 09:01:55 +00:00
// scale grade
2007-07-21 14:24:39 +00:00
$scaleopt = array ();
2007-08-07 22:50:32 +00:00
if ( empty ( $grade_item -> outcomeid )) {
2007-08-08 06:12:55 +00:00
$scaleopt [ - 1 ] = get_string ( 'nograde' );
2007-08-07 22:50:32 +00:00
} else {
2007-08-08 06:12:55 +00:00
$scaleopt [ - 1 ] = get_string ( 'nooutcome' , 'grades' );
2007-08-07 22:50:32 +00:00
}
2007-07-12 19:40:28 +00:00
2007-07-12 06:42:44 +00:00
$i = 1 ;
2008-06-03 16:10:57 +00:00
if ( $scale = $DB -> get_record ( 'scale' , array ( 'id' => $grade_item -> scaleid ))) {
2010-07-20 03:27:36 +00:00
foreach ( explode ( " , " , $scale -> scale ) as $option ) {
2007-07-12 06:42:44 +00:00
$scaleopt [ $i ] = $option ;
$i ++ ;
2007-07-11 09:01:55 +00:00
}
}
2007-07-12 19:40:28 +00:00
2007-07-11 09:01:55 +00:00
$mform -> addElement ( 'select' , 'finalgrade' , get_string ( 'finalgrade' , 'grades' ), $scaleopt );
2012-04-12 12:34:14 +07:00
$mform -> addHelpButton ( 'finalgrade' , 'finalgrade' , 'grades' );
2007-10-30 11:40:50 +00:00
$mform -> disabledIf ( 'finalgrade' , 'overridden' , 'notchecked' );
2007-07-11 09:01:55 +00:00
}
2007-07-12 19:40:28 +00:00
2014-09-18 15:45:16 +08:00
$mform -> addElement ( 'advcheckbox' , 'excluded' , get_string ( 'excluded' , 'grades' ));
2010-05-27 20:22:18 +00:00
$mform -> addHelpButton ( 'excluded' , 'excluded' , 'grades' );
2007-07-12 19:40:28 +00:00
2007-08-06 20:51:09 +00:00
/// hiding
/// advcheckbox is not compatible with disabledIf !!
$mform -> addElement ( 'checkbox' , 'hidden' , get_string ( 'hidden' , 'grades' ));
2010-06-07 09:35:45 +00:00
$mform -> addHelpButton ( 'hidden' , 'hidden' , 'grades' );
2007-08-07 08:30:25 +00:00
$mform -> addElement ( 'date_time_selector' , 'hiddenuntil' , get_string ( 'hiddenuntil' , 'grades' ), array ( 'optional' => true ));
2007-08-09 16:19:06 +00:00
$mform -> disabledIf ( 'hidden' , 'hiddenuntil[off]' , 'notchecked' );
2007-08-06 20:51:09 +00:00
2007-07-21 14:24:39 +00:00
/// locking
2007-07-12 08:48:02 +00:00
$mform -> addElement ( 'advcheckbox' , 'locked' , get_string ( 'locked' , 'grades' ));
2010-06-07 21:58:16 +00:00
$mform -> addHelpButton ( 'locked' , 'locked' , 'grades' );
2007-07-11 09:01:55 +00:00
$mform -> addElement ( 'date_time_selector' , 'locktime' , get_string ( 'locktime' , 'grades' ), array ( 'optional' => true ));
$mform -> disabledIf ( 'locktime' , 'gradetype' , 'eq' , GRADE_TYPE_NONE );
2007-07-12 22:31:20 +00:00
// Feedback format is automatically converted to html if user has enabled editor
2010-07-03 13:37:13 +00:00
$feedbackoptions = array ( 'maxfiles' => 0 , 'maxbytes' => 0 ); //TODO: no files here for now, if ever gets implemented use component 'grade' and filearea 'feedback'
2009-11-04 06:14:06 +00:00
$mform -> addElement ( 'editor' , 'feedback' , get_string ( 'feedback' , 'grades' ), null , $feedbackoptions );
2010-05-27 20:25:47 +00:00
$mform -> addHelpButton ( 'feedback' , 'feedback' , 'grades' );
2007-07-21 14:24:39 +00:00
$mform -> setType ( 'text' , PARAM_RAW ); // to be cleaned before display, no XSS risk
2007-07-11 09:01:55 +00:00
// hidden params
2007-08-10 15:00:35 +00:00
$mform -> addElement ( 'hidden' , 'oldgrade' );
2009-09-26 17:07:08 +00:00
$mform -> setType ( 'oldgrade' , PARAM_RAW );
2007-10-30 11:40:50 +00:00
$mform -> addElement ( 'hidden' , 'oldfeedback' );
2009-09-26 17:07:08 +00:00
$mform -> setType ( 'oldfeedback' , PARAM_RAW );
2007-08-10 15:00:35 +00:00
2007-07-11 09:01:55 +00:00
$mform -> addElement ( 'hidden' , 'id' , 0 );
2007-07-21 14:24:39 +00:00
$mform -> setType ( 'id' , PARAM_INT );
$mform -> addElement ( 'hidden' , 'itemid' , 0 );
$mform -> setType ( 'itemid' , PARAM_INT );
2007-07-11 09:01:55 +00:00
2007-07-21 14:24:39 +00:00
$mform -> addElement ( 'hidden' , 'userid' , 0 );
$mform -> setType ( 'userid' , PARAM_INT );
$mform -> addElement ( 'hidden' , 'courseid' , $COURSE -> id );
2007-07-11 09:01:55 +00:00
$mform -> setType ( 'courseid' , PARAM_INT );
2007-07-16 18:58:09 +00:00
/// add return tracking info
$gpr -> add_mform_elements ( $mform );
2007-07-11 09:01:55 +00:00
//-------------------------------------------------------------------------------
// buttons
$this -> add_action_buttons ();
}
2007-07-21 14:24:39 +00:00
function definition_after_data () {
2008-06-03 16:10:57 +00:00
global $CFG , $COURSE , $DB ;
2007-08-07 20:33:09 +00:00
2012-07-24 14:04:40 +08:00
$context = context_course :: instance ( $COURSE -> id );
2007-07-21 14:24:39 +00:00
$mform =& $this -> _form ;
$grade_item = $this -> _customdata [ 'grade_item' ];
2007-08-07 20:33:09 +00:00
// fill in user name if user still exists
$userid = $mform -> getElementValue ( 'userid' );
2008-06-03 16:10:57 +00:00
if ( $user = $DB -> get_record ( 'user' , array ( 'id' => $userid ))) {
2007-07-21 14:24:39 +00:00
$username = '<a href="' . $CFG -> wwwroot . '/user/view.php?id=' . $userid . '">' . fullname ( $user ) . '</a>' ;
$user_el =& $mform -> getElement ( 'user' );
$user_el -> setValue ( $username );
}
2007-08-07 20:33:09 +00:00
// add activity name + link
2007-07-21 14:24:39 +00:00
if ( $grade_item -> itemtype == 'mod' ) {
$cm = get_coursemodule_from_instance ( $grade_item -> itemmodule , $grade_item -> iteminstance , $grade_item -> courseid );
$itemname = '<a href="' . $CFG -> wwwroot . '/mod/' . $grade_item -> itemmodule . '/view.php?id=' . $cm -> id . '">' . $grade_item -> get_name () . '</a>' ;
} else {
$itemname = $grade_item -> get_name ();
}
$itemname_el =& $mform -> getElement ( 'itemname' );
$itemname_el -> setValue ( $itemname );
2007-08-07 20:33:09 +00:00
// access control - disable not allowed elements
if ( ! has_capability ( 'moodle/grade:manage' , $context )) {
$mform -> hardFreeze ( 'excluded' );
}
if ( ! has_capability ( 'moodle/grade:manage' , $context ) and ! has_capability ( 'moodle/grade:hide' , $context )) {
$mform -> hardFreeze ( 'hidden' );
$mform -> hardFreeze ( 'hiddenuntil' );
}
$old_grade_grade = new grade_grade ( array ( 'itemid' => $grade_item -> id , 'userid' => $userid ));
2007-10-30 11:40:50 +00:00
if ( ! $grade_item -> is_overridable_item ()) {
$mform -> removeElement ( 'overridden' );
}
2007-08-09 16:19:06 +00:00
if ( $grade_item -> is_hidden ()) {
$mform -> hardFreeze ( 'hidden' );
}
2007-08-09 09:03:14 +00:00
if ( $old_grade_grade -> is_locked ()) {
if ( $grade_item -> is_locked ()) {
$mform -> hardFreeze ( 'locked' );
$mform -> hardFreeze ( 'locktime' );
}
$mform -> hardFreeze ( 'overridden' );
$mform -> hardFreeze ( 'finalgrade' );
$mform -> hardFreeze ( 'feedback' );
} else {
if ( empty ( $old_grade_grade -> id )) {
$old_grade_grade -> locked = $grade_item -> locked ;
$old_grade_grade -> locktime = $grade_item -> locktime ;
}
2007-08-07 20:33:09 +00:00
2007-08-09 09:03:14 +00:00
if (( $old_grade_grade -> locked or $old_grade_grade -> locktime )
and ( ! has_capability ( 'moodle/grade:manage' , $context ) and ! has_capability ( 'moodle/grade:unlock' , $context ))) {
$mform -> hardFreeze ( 'locked' );
$mform -> hardFreeze ( 'locktime' );
} else if (( ! $old_grade_grade -> locked and ! $old_grade_grade -> locktime )
and ( ! has_capability ( 'moodle/grade:manage' , $context ) and ! has_capability ( 'moodle/grade:lock' , $context ))) {
$mform -> hardFreeze ( 'locked' );
$mform -> hardFreeze ( 'locktime' );
}
2007-08-07 20:33:09 +00:00
}
2007-07-21 14:24:39 +00:00
}
2007-07-11 09:01:55 +00:00
}
2009-11-01 12:11:29 +00:00