Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/edit/tree/calculation.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 require_once '../../../config.php';
00019 require_once $CFG->dirroot.'/grade/lib.php';
00020 require_once $CFG->libdir.'/mathslib.php';
00021 require_once 'calculation_form.php';
00022 
00023 $courseid  = required_param('courseid', PARAM_INT);
00024 $id        = required_param('id', PARAM_INT);
00025 $section   = optional_param('section', 'calculation', PARAM_ALPHA);
00026 $idnumbers = optional_param('idnumbers', null, PARAM_RAW);
00027 
00028 $url = new moodle_url('/grade/edit/tree/calculation.php', array('id'=>$id, 'courseid'=>$courseid));
00029 if ($section !== 'calculation') {
00030     $url->param('section', $section);
00031 }
00032 $PAGE->set_url($url);
00033 
00034 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
00035     print_error('nocourseid');
00036 }
00037 
00038 require_login($course);
00039 $context = get_context_instance(CONTEXT_COURSE, $course->id);
00040 require_capability('moodle/grade:manage', $context);
00041 
00042 // default return url
00043 $gpr = new grade_plugin_return();
00044 $returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report.php?id='.$course->id);
00045 
00046 if (!$grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$course->id))) {
00047     print_error('invaliditemid');
00048 }
00049 
00050 // activity items and items without grade can not have calculation
00051 if ($grade_item->is_external_item() or ($grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE)) {
00052     redirect($returnurl, get_string('errornocalculationallowed', 'grades'));
00053 }
00054 
00055 $mform = new edit_calculation_form(null, array('gpr'=>$gpr, 'itemid' => $grade_item->id));
00056 
00057 if ($mform->is_cancelled()) {
00058     redirect($returnurl);
00059 
00060 }
00061 
00062 $calculation = calc_formula::localize($grade_item->calculation);
00063 $calculation = grade_item::denormalize_formula($calculation, $grade_item->courseid);
00064 $mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
00065 
00066 $errors = array();
00067 
00068 if ($data = $mform->get_data()) {
00069     $calculation = calc_formula::unlocalize($data->calculation);
00070     $grade_item->set_calculation($calculation);
00071 
00072     redirect($returnurl);
00073 
00074 } elseif (!empty($section) AND $section='idnumbers' AND !empty($idnumbers)) { // Handle idnumbers separately (non-mform)
00075     //first validate and store the new idnumbers
00076     foreach ($idnumbers as $giid => $value) {
00077         if ($gi = grade_item::fetch(array('id' => $giid))) {
00078             if ($gi->itemtype == 'mod') {
00079                 $cm = get_coursemodule_from_instance($gi->itemmodule, $gi->iteminstance, $gi->courseid);
00080             } else {
00081                 $cm = null;
00082             }
00083 
00084             if (!grade_verify_idnumber($value, $COURSE->id, $gi, $cm)) {
00085                 $errors[$giid] = get_string('idnumbertaken');
00086                 continue;
00087             }
00088 
00089             if (empty($gi->idnumber) and !$gi->add_idnumber($idnumbers[$gi->id])) {
00090                 $errors[$giid] = get_string('error');
00091                 continue;
00092             }
00093         } else {
00094             $errors[$giid] = 'Could not fetch the grade_item with id=' . $giid;
00095         }
00096     }
00097 }
00098 
00099 $gtree = new grade_tree($course->id, false, false);
00100 
00101 $strgrades          = get_string('grades');
00102 $strgraderreport    = get_string('graderreport', 'grades');
00103 $strcalculationedit = get_string('editcalculation', 'grades');
00104 
00105 grade_build_nav(__FILE__, $strcalculationedit, array('courseid' => $courseid));
00106 $PAGE->set_title($strgrades . ': ' . $strgraderreport);
00107 $PAGE->set_heading($course->fullname);
00108 echo $OUTPUT->header();
00109 
00110 $mform->display();
00111 // Now show the gradetree with the idnumbers add/edit form
00112 echo '
00113 <form class="mform" id="mform2" method="post" action="' . $CFG->wwwroot . '/grade/edit/tree/calculation.php?courseid='.$courseid.'&amp;id='.$id.'">
00114     <div style="display: none;">
00115         <input type="hidden" value="'.$id.'" name="id"/>
00116         <input type="hidden" value="'.$courseid.'" name="courseid"/>
00117         <input type="hidden" value="'.$gpr->type.'" name="gpr_type"/>
00118         <input type="hidden" value="'.$gpr->plugin.'" name="gpr_plugin"/>
00119         <input type="hidden" value="'.$gpr->courseid.'" name="gpr_courseid"/>
00120         <input type="hidden" value="'.sesskey().'" name="sesskey"/>
00121         <input type="hidden" value="idnumbers" name="section"/>
00122     </div>
00123 
00124     <fieldset id="idnumbers" class="clearfix">
00125         <legend class="ftoggler">'.get_string('idnumbers', 'grades').'</legend>
00126         <div class="fcontainer clearfix">
00127             <ul>
00128             ' . get_grade_tree($gtree, $gtree->top_element, $id, $errors) . '
00129             </ul>
00130         </div>
00131     </fieldset>
00132     <div class="fitem" style="text-align: center;">
00133         <input id="id_addidnumbers" type="submit" value="'.get_string('addidnumbers', 'grades').'" name="addidnumbers" />
00134     </div>
00135 </form>';
00136 
00137 echo $OUTPUT->footer();
00138 die();
00139 
00140 
00151 function get_grade_tree(&$gtree, $element, $current_itemid=null, $errors=null) {
00152     global $CFG;
00153 
00154     $object     = $element['object'];
00155     $eid        = $element['eid'];
00156     $type       = $element['type'];
00157     $grade_item = $object->get_grade_item();
00158 
00159     $name = $object->get_name();
00160     $return_string = '';
00161 
00162     //TODO: improve outcome visualisation
00163     if ($type == 'item' and !empty($object->outcomeid)) {
00164         $name = $name.' ('.get_string('outcome', 'grades').')';
00165     }
00166 
00167     $idnumber = $object->get_idnumber();
00168 
00169     // Don't show idnumber or input field for current item if given to function. Highlight the item instead.
00170     if ($type != 'category') {
00171         if (is_null($current_itemid) OR $grade_item->id != $current_itemid) {
00172             if ($idnumber) {
00173                 $name .= ": [[$idnumber]]";
00174             } else {
00175                 $closingdiv = '';
00176                 if (!empty($errors[$grade_item->id])) {
00177                     $name .= '<div class="error"><span class="error">' . $errors[$grade_item->id].'</span><br />'."\n";
00178                     $closingdiv = "</div>\n";
00179                 }
00180                 $name .= '<input class="idnumber" id="id_idnumber_'.$grade_item->id.'" type="text" name="idnumbers['.$grade_item->id.']" />' . "\n";
00181                 $name .= $closingdiv;
00182             }
00183         } else {
00184             $name = "<strong>$name</strong>";
00185         }
00186     }
00187 
00188     $icon = $gtree->get_element_icon($element, true);
00189     $last = '';
00190     $catcourseitem = ($element['type'] == 'courseitem' or $element['type'] == 'categoryitem');
00191 
00192     if ($type != 'category') {
00193         $return_string .= '<li class="'.$type.'">'.$icon.$name.'</li>' . "\n";
00194     } else {
00195         $return_string .= '<li class="'.$type.'">'.$icon.$name . "\n";
00196         $return_string .= '<ul class="catlevel'.$element['depth'].'">'."\n";
00197         $last = null;
00198         foreach($element['children'] as $child_el) {
00199             $return_string .= get_grade_tree($gtree, $child_el, $current_itemid, $errors);
00200         }
00201         if ($last) {
00202             $return_string .= get_grade_tree($gtree, $last, $current_itemid, $errors);
00203         }
00204         $return_string .= '</ul></li>'."\n";
00205     }
00206 
00207     return $return_string;
00208 }
00209 
00210 
 All Data Structures Namespaces Files Functions Variables Enumerations