Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Allows to choose a form from the list of available templates
  19   *
  20   * @package    core_grading
  21   * @copyright  2011 David Mudrak <david@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__.'/../../config.php');
  26  require_once($CFG->dirroot.'/grade/grading/lib.php');
  27  require_once($CFG->dirroot.'/grade/grading/pick_form.php');
  28  
  29  $targetid   = required_param('targetid', PARAM_INT); // area we are coming from
  30  $pick       = optional_param('pick', null, PARAM_INT); // create new form from this template
  31  $remove     = optional_param('remove', null, PARAM_INT); // remove this template
  32  $confirmed  = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed
  33  
  34  // the manager of the target area
  35  $targetmanager = get_grading_manager($targetid);
  36  
  37  if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) {
  38      throw new coding_exception('Unsupported gradable area context level');
  39  }
  40  
  41  // currently active method in the target area
  42  $method = $targetmanager->get_active_method();
  43  $targetcontroller = $targetmanager->get_controller($method);
  44  $targetcontrollerclass = get_class($targetcontroller);
  45  
  46  // make sure there is no such form defined in the target area
  47  if ($targetcontroller->is_form_defined()) {
  48      redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
  49  }
  50  
  51  list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id);
  52  
  53  require_login($course, true, $cm);
  54  require_capability('moodle/grade:managegradingforms', $context);
  55  
  56  // user's capability in the templates bank
  57  $canshare   = has_capability('moodle/grade:sharegradingforms', context_system::instance());
  58  $canmanage  = has_capability('moodle/grade:managesharedforms', context_system::instance());
  59  
  60  // setup the page
  61  $PAGE->set_url(new moodle_url('/grade/grading/pick.php', array('targetid' => $targetid)));
  62  navigation_node::override_active_url($targetmanager->get_management_url());
  63  $PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
  64  $PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
  65  $output = $PAGE->get_renderer('core_grading');
  66  
  67  // process picking a template
  68  if ($pick) {
  69      $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST);
  70      $sourcemanager = get_grading_manager($sourceid);
  71      $sourcecontroller = $sourcemanager->get_controller($method);
  72      if (!$sourcecontroller->is_shared_template() and !$sourcecontroller->is_own_form()) {
  73          // note that we don't actually check whether the user has still the capability
  74          // moodle/grade:managegradingforms in the source area. so when users loose
  75          // their teacher role in a course, they can't access the course but they can
  76          // still copy the forms they have created there.
  77          throw new moodle_exception('attempt_to_pick_others_form', 'core_grading');
  78      }
  79      if (!$sourcecontroller->is_form_defined()) {
  80          throw new moodle_exception('form_definition_mismatch', 'core_grading');
  81      }
  82      $definition = $sourcecontroller->get_definition();
  83      if (!$confirmed) {
  84          echo $output->header();
  85          echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array(
  86              'formname'  => s($definition->name),
  87              'component' => $targetmanager->get_component_title(),
  88              'area'      => $targetmanager->get_area_title())),
  89              new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)),
  90              $PAGE->url);
  91          echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
  92          echo $output->footer();
  93          die();
  94      } else {
  95          require_sesskey();
  96          $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller));
  97          $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
  98          redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
  99      }
 100  }
 101  
 102  // process removing a template
 103  if ($remove) {
 104      $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST);
 105      $sourcemanager = get_grading_manager($sourceid);
 106      $sourcecontroller = $sourcemanager->get_controller($method);
 107      if (!$sourcecontroller->is_shared_template()) {
 108          throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading');
 109      }
 110      if (!$sourcecontroller->is_form_defined()) {
 111          throw new moodle_exception('form_definition_mismatch', 'core_grading');
 112      }
 113      $definition = $sourcecontroller->get_definition();
 114      if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) {
 115          // ok, this user can drop the template
 116      } else {
 117          throw new moodle_exception('no_permission_to_remove_template', 'core_grading');
 118      }
 119      if (!$confirmed) {
 120          echo $output->header();
 121          echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)),
 122              new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)),
 123              $PAGE->url);
 124          echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
 125          echo $output->footer();
 126          die();
 127      } else {
 128          require_sesskey();
 129          $sourcecontroller->delete_definition();
 130          redirect($PAGE->url);
 131      }
 132  }
 133  
 134  $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
 135  
 136  if ($searchdata = $searchform->get_data()) {
 137      $tokens = grading_manager::tokenize($searchdata->needle);
 138      $includeownforms = (!empty($searchdata->mode));
 139  } else {
 140      $tokens = array();
 141      $includeownforms = false;
 142  }
 143  
 144  // construct the SQL to find all matching templates
 145  $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.usercreated
 146            FROM {grading_definitions} gd
 147            JOIN {grading_areas} ga ON (gd.areaid = ga.id)
 148            JOIN {context} cx ON (ga.contextid = cx.id)";
 149  // join method-specific tables from the plugin scope
 150  $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
 151  
 152  $sql .= " WHERE gd.method = ?";
 153  
 154  $params = array($method);
 155  
 156  if (!$includeownforms) {
 157      // search for public templates only
 158      $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'";
 159      $params[] = context_system::instance()->id;
 160  
 161  } else {
 162      // search both templates and own forms in other areas
 163      $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading')
 164                     OR (gd.usercreated = ? AND gd.status = ?))";
 165      $params = array_merge($params,  array(context_system::instance()->id, $USER->id,
 166          gradingform_controller::DEFINITION_STATUS_READY));
 167  }
 168  
 169  if ($tokens) {
 170      $subsql = array();
 171  
 172      // search for any of the tokens in the definition name
 173      foreach ($tokens as $token) {
 174          $subsql[] = $DB->sql_like('gd.name', '?', false, false);
 175          $params[] = '%'.$DB->sql_like_escape($token).'%';
 176      }
 177  
 178      // search for any of the tokens in the definition description
 179      foreach ($tokens as $token) {
 180          $subsql[] = $DB->sql_like('gd.description', '?', false, false);
 181          $params[] = '%'.$DB->sql_like_escape($token).'%';
 182      }
 183  
 184      // search for the needle in method-specific tables
 185      foreach ($tokens as $token) {
 186          list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
 187          $subsql = array_merge($subsql, $methodsql);
 188          $params = array_merge($params, $methodparams);
 189      }
 190  
 191      $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
 192  }
 193  
 194  $sql .= " ORDER BY gd.name";
 195  
 196  $rs = $DB->get_recordset_sql($sql, $params);
 197  
 198  echo $output->header();
 199  $searchform->display();
 200  
 201  $found = 0;
 202  foreach ($rs as $template) {
 203      $found++;
 204      $out = '';
 205      $manager = get_grading_manager($template->areaid);
 206      $controller = $manager->get_controller($method);
 207      if ($controller->is_shared_template()) {
 208          $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'),
 209              array('class' => 'type shared'));
 210          $templatesrc  = '';
 211      } else if ($controller->is_own_form()) {
 212          $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'),
 213              array('class' => 'type ownform'));
 214          $templatesrc  = get_string('templatesource', 'core_grading', array(
 215              'component' => $manager->get_component_title(),
 216              'area'      => $manager->get_area_title()));
 217      } else {
 218          throw new coding_exception('Something is wrong, the displayed form must be either template or own form');
 219      }
 220      $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name');
 221      $out .= $output->container($templatesrc, 'template-source');
 222      $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
 223      $actions = array();
 224      if ($controller->is_shared_template()) {
 225          $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
 226              get_string('templatepick', 'core_grading'), 'i/valid', 'pick template');
 227          if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) {
 228              //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
 229              //    get_string('templateedit', 'core_grading'), 'i/edit', 'edit');
 230              $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
 231                  get_string('templatedelete', 'core_grading'), 't/delete', 'remove');
 232          }
 233      } else if ($controller->is_own_form()) {
 234          $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
 235              get_string('templatepickownform', 'core_grading'), 'i/valid', 'pick ownform');
 236      }
 237      $out .= $output->box(join(' ', $actions), 'template-actions');
 238      $out .= $output->box($controller->get_formatted_description(), 'template-description');
 239  
 240      // ideally we should highlight just the name, description and the fields
 241      // in the preview that were actually searched. to make our life easier, we
 242      // simply highlight the tokens everywhere they appear, even if that exact
 243      // piece was not searched.
 244      echo highlight(join(' ', $tokens), $out);
 245  }
 246  $rs->close();
 247  
 248  if (!$found) {
 249      echo $output->heading(get_string('nosharedformfound', 'core_grading'));
 250  }
 251  
 252  echo $output->single_button(
 253      new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)),
 254      get_string('back'), 'get');
 255  
 256  echo $output->footer();
 257