Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/edit/letter/index.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 
00026 require_once '../../../config.php';
00027 require_once $CFG->dirroot.'/grade/lib.php';
00028 require_once $CFG->libdir.'/gradelib.php';
00029 
00030 $contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
00031 $action   = optional_param('action', '', PARAM_ALPHA);
00032 $edit     = optional_param('edit', false, PARAM_BOOL); //are we editing?
00033 
00034 $PAGE->set_url('/grade/edit/letter/index.php', array('id' => $contextid));
00035 
00036 list($context, $course, $cm) = get_context_info_array($contextid);
00037 $contextid = null;//now we have a context object throw away the $contextid from the params
00038 
00039 //if viewing
00040 if (!$edit) {
00041     if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
00042         print_error('nopermissiontoviewletergrade');
00043     }
00044 } else {//else we're editing
00045     require_capability('moodle/grade:manageletters', $context);
00046 }
00047 
00048 $returnurl = null;
00049 $editparam = null;
00050 if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
00051     require_once $CFG->libdir.'/adminlib.php';
00052     require_login();
00053 
00054     admin_externalpage_setup('letters');
00055 
00056     $admin = true;
00057     $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
00058     $editparam = '?edit=1';
00059 } else if ($context->contextlevel == CONTEXT_COURSE) {
00060 
00061     $PAGE->set_pagelayout('standard');//calling this here to make blocks display
00062 
00063     require_login($context->instanceid, false, $cm);
00064 
00065     $admin = false;
00066     $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
00067     $editparam = '&edit=1';
00068 
00069     $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
00070 } else {
00071     print_error('invalidcourselevel');
00072 }
00073 
00074 $strgrades = get_string('grades');
00075 $pagename  = get_string('letters', 'grades');
00076 
00077 $letters = grade_get_letters($context);
00078 $num = count($letters) + 3;
00079 
00080 //if were viewing the letters
00081 if (!$edit) {
00082 
00083     $data = array();
00084 
00085     $max = 100;
00086     foreach($letters as $boundary=>$letter) {
00087         $line = array();
00088         $line[] = format_float($max,2).' %';
00089         $line[] = format_float($boundary,2).' %';
00090         $line[] = format_string($letter);
00091         $data[] = $line;
00092         $max = $boundary - 0.01;
00093     }
00094 
00095     print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
00096 
00097     $stredit = get_string('editgradeletters', 'grades');
00098     $editlink = html_writer::nonempty_tag('div', html_writer::link($returnurl.$editparam, $stredit), array('class'=>'mdl-align'));
00099     echo $editlink;
00100 
00101     $table = new html_table();
00102     $table->head  = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
00103     $table->size  = array('30%', '30%', '40%');
00104     $table->align = array('left', 'left', 'left');
00105     $table->width = '30%';
00106     $table->data  = $data;
00107     $table->tablealign  = 'center';
00108     echo html_writer::table($table);
00109 
00110     echo $editlink;
00111 } else { //else we're editing
00112     require_once('edit_form.php');
00113 
00114     $data = new stdClass();
00115     $data->id = $context->id;
00116 
00117     $i = 1;
00118     foreach ($letters as $boundary=>$letter) {
00119         $gradelettername = 'gradeletter'.$i;
00120         $gradeboundaryname = 'gradeboundary'.$i;
00121 
00122         $data->$gradelettername   = $letter;
00123         $data->$gradeboundaryname = $boundary;
00124         $i++;
00125     }
00126     $data->override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
00127 
00128     $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
00129     $mform->set_data($data);
00130 
00131     if ($mform->is_cancelled()) {
00132         redirect($returnurl);
00133 
00134     } else if ($data = $mform->get_data()) {
00135         if (!$admin and empty($data->override)) {
00136             $DB->delete_records('grade_letters', array('contextid' => $context->id));
00137             redirect($returnurl);
00138         }
00139 
00140         $letters = array();
00141         for($i=1; $i<$num+1; $i++) {
00142             $gradelettername = 'gradeletter'.$i;
00143             $gradeboundaryname = 'gradeboundary'.$i;
00144 
00145             if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
00146                 $letter = trim($data->$gradelettername);
00147                 if ($letter == '') {
00148                     continue;
00149                 }
00150                 $letters[$data->$gradeboundaryname] = $letter;
00151             }
00152         }
00153         krsort($letters, SORT_NUMERIC);
00154 
00155         $old_ids = array();
00156         if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC', 'id')) {
00157             $old_ids = array_keys($records);
00158         }
00159 
00160         foreach($letters as $boundary=>$letter) {
00161             $record = new stdClass();
00162             $record->letter        = $letter;
00163             $record->lowerboundary = $boundary;
00164             $record->contextid     = $context->id;
00165 
00166             if ($old_id = array_pop($old_ids)) {
00167                 $record->id = $old_id;
00168                 $DB->update_record('grade_letters', $record);
00169             } else {
00170                 $DB->insert_record('grade_letters', $record);
00171             }
00172         }
00173 
00174         foreach($old_ids as $old_id) {
00175             $DB->delete_records('grade_letters', array('id' => $old_id));
00176         }
00177 
00178         redirect($returnurl);
00179     }
00180 
00181     print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
00182 
00183     $mform->display();
00184 }
00185 
00186 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations