|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00027 require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); 00028 require_once($CFG->dirroot.'/grade/grading/lib.php'); 00029 require_once($CFG->dirroot.'/grade/grading/pick_form.php'); 00030 00031 $targetid = required_param('targetid', PARAM_INT); // area we are coming from 00032 $pick = optional_param('pick', null, PARAM_INT); // create new form from this template 00033 $remove = optional_param('remove', null, PARAM_INT); // remove this template 00034 $confirmed = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed 00035 00036 // the manager of the target area 00037 $targetmanager = get_grading_manager($targetid); 00038 00039 if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) { 00040 throw new coding_exception('Unsupported gradable area context level'); 00041 } 00042 00043 // currently active method in the target area 00044 $method = $targetmanager->get_active_method(); 00045 $targetcontroller = $targetmanager->get_controller($method); 00046 $targetcontrollerclass = get_class($targetcontroller); 00047 00048 // make sure there is no such form defined in the target area 00049 if ($targetcontroller->is_form_defined()) { 00050 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); 00051 } 00052 00053 list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id); 00054 00055 require_login($course, true, $cm); 00056 require_capability('moodle/grade:managegradingforms', $context); 00057 00058 // user's capability in the templates bank 00059 $canshare = has_capability('moodle/grade:sharegradingforms', context_system::instance()); 00060 $canmanage = has_capability('moodle/grade:managesharedforms', context_system::instance()); 00061 00062 // setup the page 00063 $PAGE->set_url(new moodle_url('/grade/grading/pick.php', array('targetid' => $targetid))); 00064 navigation_node::override_active_url($targetmanager->get_management_url()); 00065 $PAGE->set_title(get_string('gradingmanagement', 'core_grading')); 00066 $PAGE->set_heading(get_string('gradingmanagement', 'core_grading')); 00067 $output = $PAGE->get_renderer('core_grading'); 00068 00069 // process picking a template 00070 if ($pick) { 00071 $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST); 00072 $sourcemanager = get_grading_manager($sourceid); 00073 $sourcecontroller = $sourcemanager->get_controller($method); 00074 if (!$sourcecontroller->is_shared_template() and !$sourcecontroller->is_own_form()) { 00075 // note that we don't actually check whether the user has still the capability 00076 // moodle/grade:managegradingforms in the source area. so when users loose 00077 // their teacher role in a course, they can't access the course but they can 00078 // still copy the forms they have created there. 00079 throw new moodle_exception('attempt_to_pick_others_form', 'core_grading'); 00080 } 00081 if (!$sourcecontroller->is_form_defined()) { 00082 throw new moodle_exception('form_definition_mismatch', 'core_grading'); 00083 } 00084 $definition = $sourcecontroller->get_definition(); 00085 if (!$confirmed) { 00086 echo $output->header(); 00087 echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array( 00088 'formname' => s($definition->name), 00089 'component' => $targetmanager->get_component_title(), 00090 'area' => $targetmanager->get_area_title())), 00091 new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)), 00092 $PAGE->url); 00093 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); 00094 echo $output->footer(); 00095 die(); 00096 } else { 00097 require_sesskey(); 00098 $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller)); 00099 $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id)); 00100 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); 00101 } 00102 } 00103 00104 // process removing a template 00105 if ($remove) { 00106 $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST); 00107 $sourcemanager = get_grading_manager($sourceid); 00108 $sourcecontroller = $sourcemanager->get_controller($method); 00109 if (!$sourcecontroller->is_shared_template()) { 00110 throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading'); 00111 } 00112 if (!$sourcecontroller->is_form_defined()) { 00113 throw new moodle_exception('form_definition_mismatch', 'core_grading'); 00114 } 00115 $definition = $sourcecontroller->get_definition(); 00116 if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) { 00117 // ok, this user can drop the template 00118 } else { 00119 throw new moodle_exception('no_permission_to_remove_template', 'core_grading'); 00120 } 00121 if (!$confirmed) { 00122 echo $output->header(); 00123 echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)), 00124 new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)), 00125 $PAGE->url); 00126 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); 00127 echo $output->footer(); 00128 die(); 00129 } else { 00130 require_sesskey(); 00131 $sourcecontroller->delete_definition(); 00132 redirect($PAGE->url); 00133 } 00134 } 00135 00136 $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform')); 00137 00138 if ($searchdata = $searchform->get_data()) { 00139 $tokens = grading_manager::tokenize($searchdata->needle); 00140 $includeownforms = (!empty($searchdata->mode)); 00141 } else { 00142 $tokens = array(); 00143 $includeownforms = false; 00144 } 00145 00146 // construct the SQL to find all matching templates 00147 $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.usercreated 00148 FROM {grading_definitions} gd 00149 JOIN {grading_areas} ga ON (gd.areaid = ga.id) 00150 JOIN {context} cx ON (ga.contextid = cx.id)"; 00151 // join method-specific tables from the plugin scope 00152 $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id'); 00153 00154 $sql .= " WHERE gd.method = ?"; 00155 00156 $params = array($method); 00157 00158 if (!$includeownforms) { 00159 // search for public templates only 00160 $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'"; 00161 $params[] = context_system::instance()->id; 00162 00163 } else { 00164 // search both templates and own forms in other areas 00165 $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading') 00166 OR (gd.usercreated = ? AND gd.status = ?))"; 00167 $params = array_merge($params, array(context_system::instance()->id, $USER->id, 00168 gradingform_controller::DEFINITION_STATUS_READY)); 00169 } 00170 00171 if ($tokens) { 00172 $subsql = array(); 00173 00174 // search for any of the tokens in the definition name 00175 foreach ($tokens as $token) { 00176 $subsql[] = $DB->sql_like('gd.name', '?', false, false); 00177 $params[] = '%'.$DB->sql_like_escape($token).'%'; 00178 } 00179 00180 // search for any of the tokens in the definition description 00181 foreach ($tokens as $token) { 00182 $subsql[] = $DB->sql_like('gd.description', '?', false, false); 00183 $params[] = '%'.$DB->sql_like_escape($token).'%'; 00184 } 00185 00186 // search for the needle in method-specific tables 00187 foreach ($tokens as $token) { 00188 list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token); 00189 $subsql = array_merge($subsql, $methodsql); 00190 $params = array_merge($params, $methodparams); 00191 } 00192 00193 $sql .= " AND ((" . join(")\n OR (", $subsql) . "))"; 00194 } 00195 00196 $sql .= " ORDER BY gd.name"; 00197 00198 $rs = $DB->get_recordset_sql($sql, $params); 00199 00200 echo $output->header(); 00201 $searchform->display(); 00202 00203 $found = 0; 00204 foreach ($rs as $template) { 00205 $found++; 00206 $out = ''; 00207 $manager = get_grading_manager($template->areaid); 00208 $controller = $manager->get_controller($method); 00209 if ($controller->is_shared_template()) { 00210 $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'), 00211 array('class' => 'type shared')); 00212 $templatesrc = ''; 00213 } else if ($controller->is_own_form()) { 00214 $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'), 00215 array('class' => 'type ownform')); 00216 $templatesrc = get_string('templatesource', 'core_grading', array( 00217 'component' => $manager->get_component_title(), 00218 'area' => $manager->get_area_title())); 00219 } else { 00220 throw new coding_exception('Something is wrong, the displayed form must be either template or own form'); 00221 } 00222 $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name'); 00223 $out .= $output->container($templatesrc, 'template-source'); 00224 $out .= $output->box($controller->render_preview($PAGE), 'template-preview'); 00225 $actions = array(); 00226 if ($controller->is_shared_template()) { 00227 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)), 00228 get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick template'); 00229 if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) { 00230 //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)), 00231 // get_string('templateedit', 'core_grading'), 'i/edit', 'edit'); 00232 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)), 00233 get_string('templatedelete', 'core_grading'), 't/delete', 'remove'); 00234 } 00235 } else if ($controller->is_own_form()) { 00236 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)), 00237 get_string('templatepickownform', 'core_grading'), 'i/tick_green_big', 'pick ownform'); 00238 } 00239 $out .= $output->box(join(' ', $actions), 'template-actions'); 00240 $out .= $output->box($controller->get_formatted_description(), 'template-description'); 00241 00242 // ideally we should highlight just the name, description and the fields 00243 // in the preview that were actually searched. to make our life easier, we 00244 // simply highlight the tokens everywhere they appear, even if that exact 00245 // piece was not searched. 00246 echo highlight(join(' ', $tokens), $out); 00247 } 00248 $rs->close(); 00249 00250 if (!$found) { 00251 echo $output->heading(get_string('nosharedformfound', 'core_grading')); 00252 } 00253 00254 echo $output->single_button( 00255 new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)), 00256 get_string('back'), 'get'); 00257 00258 echo $output->footer(); 00259 00261 00262