Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * A single gradable area management page
  19   *
  20   * This page alows the user to set the current active method in the given
  21   * area, provides access to the plugin editor and allows user to save the
  22   * current form as a template or re-use some existing form.
  23   *
  24   * @package    core_grading
  25   * @copyright  2011 David Mudrak <david@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  require(__DIR__.'/../../config.php');
  30  require_once($CFG->dirroot.'/grade/grading/lib.php');
  31  
  32  // identify gradable area by its id
  33  $areaid     = optional_param('areaid', null, PARAM_INT);
  34  // alternatively the context, component and areaname must be provided
  35  $contextid  = optional_param('contextid', null, PARAM_INT);
  36  $component  = optional_param('component', null, PARAM_COMPONENT);
  37  $area       = optional_param('area', null, PARAM_AREA);
  38  // keep the caller's URL so that we know where to send the user finally
  39  $returnurl  = optional_param('returnurl', null, PARAM_LOCALURL);
  40  // active method selector
  41  $setmethod  = optional_param('setmethod', null, PARAM_PLUGIN);
  42  // publish the given form definition as a new template in the forms bank
  43  $shareform  = optional_param('shareform', null, PARAM_INT);
  44  // delete the given form definition
  45  $deleteform = optional_param('deleteform', null, PARAM_INT);
  46  // consider the required action as confirmed
  47  $confirmed  = optional_param('confirmed', false, PARAM_BOOL);
  48  // a message to display, typically a previous action's result
  49  $message    = optional_param('message', null, PARAM_NOTAGS);
  50  
  51  if (!is_null($areaid)) {
  52      // get manager by id
  53      $manager = get_grading_manager($areaid);
  54  } else {
  55      // get manager by context and component
  56      if (is_null($contextid) or is_null($component) or is_null($area)) {
  57          throw new coding_exception('The caller script must identify the gradable area.');
  58      }
  59      $context = context::instance_by_id($contextid, MUST_EXIST);
  60      $manager = get_grading_manager($context, $component, $area);
  61  }
  62  
  63  if ($manager->get_context()->contextlevel < CONTEXT_COURSE) {
  64      throw new coding_exception('Unsupported gradable area context level');
  65  }
  66  
  67  // get the currently active method
  68  $method = $manager->get_active_method();
  69  
  70  list($context, $course, $cm) = get_context_info_array($manager->get_context()->id);
  71  
  72  require_login($course, true, $cm);
  73  require_capability('moodle/grade:managegradingforms', $context);
  74  
  75  if (!empty($returnurl)) {
  76      $returnurl = new moodle_url($returnurl);
  77  } else {
  78      $returnurl = null;
  79  }
  80  
  81  $PAGE->set_url($manager->get_management_url($returnurl));
  82  navigation_node::override_active_url($manager->get_management_url());
  83  $PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
  84  $PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
  85  // We don't need to show the default header on a management page.
  86  $PAGE->activityheader->disable();
  87  $output = $PAGE->get_renderer('core_grading');
  88  
  89  // process the eventual change of the active grading method
  90  if (!empty($setmethod)) {
  91      require_sesskey();
  92      if ($setmethod == 'none') {
  93          // here we expect that noone would actually want to call their plugin as 'none'
  94          $setmethod = null;
  95      }
  96      $manager->set_active_method($setmethod);
  97      redirect($PAGE->url);
  98  }
  99  
 100  // publish the form as a template
 101  if (!empty($shareform)) {
 102      require_capability('moodle/grade:sharegradingforms', context_system::instance());
 103      $controller = $manager->get_controller($method);
 104      $definition = $controller->get_definition();
 105      if (!$confirmed) {
 106          // let the user confirm they understand what they are doing (haha ;-)
 107          echo $output->header();
 108          echo $output->confirm(get_string('manageactionshareconfirm', 'core_grading', s($definition->name)),
 109              new moodle_url($PAGE->url, array('shareform' => $shareform, 'confirmed' => 1)),
 110              $PAGE->url);
 111          echo $output->footer();
 112          die();
 113      } else {
 114          require_sesskey();
 115          $newareaid = $manager->create_shared_area($method);
 116          $targetarea = get_grading_manager($newareaid);
 117          $targetcontroller = $targetarea->get_controller($method);
 118          $targetcontroller->update_definition($controller->get_definition_copy($targetcontroller));
 119          $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
 120          redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactionsharedone', 'core_grading'))));
 121      }
 122  }
 123  
 124  // delete the form definition
 125  if (!empty($deleteform)) {
 126      $controller = $manager->get_controller($method);
 127      $definition = $controller->get_definition();
 128      if (!$confirmed) {
 129          // let the user confirm they understand the consequences (also known as WTF-effect)
 130          echo $output->header();
 131          echo $output->confirm(markdown_to_html(get_string('manageactiondeleteconfirm', 'core_grading', array(
 132              'formname'  => s($definition->name),
 133              'component' => $manager->get_component_title(),
 134              'area'      => $manager->get_area_title()))),
 135              new moodle_url($PAGE->url, array('deleteform' => $deleteform, 'confirmed' => 1)), $PAGE->url);
 136          echo $output->footer();
 137          die();
 138      } else {
 139          require_sesskey();
 140          $controller->delete_definition();
 141          redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactiondeletedone', 'core_grading'))));
 142      }
 143  }
 144  
 145  echo $output->header();
 146  
 147  if (!empty($message)) {
 148      echo $output->management_message($message);
 149  }
 150  
 151  if ($PAGE->has_secondary_navigation()) {
 152      echo $output->heading(get_string('gradingmanagement', 'core_grading'));
 153  } else {
 154      echo $output->heading(get_string('gradingmanagementtitle', 'core_grading', array(
 155          'component' => $manager->get_component_title(), 'area' => $manager->get_area_title())));
 156  }
 157  
 158  // display the active grading method information and selector
 159  echo $output->management_method_selector($manager, $PAGE->url);
 160  
 161  // get the currently active method's controller
 162  if (!empty($method)) {
 163      $controller = $manager->get_controller($method);
 164      // display relevant actions
 165      echo $output->container_start('actions');
 166      if ($controller->is_form_defined()) {
 167          $definition = $controller->get_definition();
 168          // icon to edit the form definition
 169          echo $output->management_action_icon($controller->get_editor_url($returnurl),
 170              get_string('manageactionedit', 'core_grading'), 'b/document-edit');
 171          // icon to delete the current form definition
 172          echo $output->management_action_icon(new moodle_url($PAGE->url, array('deleteform' => $definition->id)),
 173              get_string('manageactiondelete', 'core_grading'), 'b/edit-delete');
 174          // icon to save the form as a new template
 175          if (has_capability('moodle/grade:sharegradingforms', context_system::instance())) {
 176              if (empty($definition->copiedfromid)) {
 177                  $hasoriginal = false;
 178              } else {
 179                  $hasoriginal = $DB->record_exists('grading_definitions', array('id' => $definition->copiedfromid));
 180              }
 181              if (!$controller->is_form_available()) {
 182                  // drafts can not be shared
 183                  $allowshare = false;
 184              } else if (!$hasoriginal) {
 185                  // was created from scratch or is orphaned
 186                  if (empty($definition->timecopied)) {
 187                      // was never shared before
 188                      $allowshare = true;
 189                  } else if ($definition->timemodified > $definition->timecopied) {
 190                      // was modified since last time shared
 191                      $allowshare = true;
 192                  } else {
 193                      // was not modified since last time shared
 194                      $allowshare = false;
 195                  }
 196              } else {
 197                  // was created from a template and the template still exists
 198                  if ($definition->timecreated == $definition->timemodified) {
 199                      // was not modified since created
 200                      $allowshare = false;
 201                  } else if (empty($definition->timecopied)) {
 202                      // was modified but was not re-shared yet
 203                      $allowshare = true;
 204                  } else if ($definition->timemodified > $definition->timecopied) {
 205                      // was modified since last time re-shared
 206                      $allowshare = true;
 207                  } else {
 208                      // was not modified since last time re-shared
 209                      $allowshare = false;
 210                  }
 211              }
 212              if ($allowshare) {
 213                  echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)),
 214                      get_string('manageactionshare', 'core_grading'), 'b/bookmark-new');
 215              }
 216          }
 217      } else {
 218          echo $output->management_action_icon($controller->get_editor_url($returnurl),
 219              get_string('manageactionnew', 'core_grading'), 'b/document-new');
 220          $pickurl = new moodle_url('/grade/grading/pick.php', array('targetid' => $controller->get_areaid()));
 221          if (!is_null($returnurl)) {
 222              $pickurl->param('returnurl', $returnurl->out(false));
 223          }
 224          echo $output->management_action_icon($pickurl,
 225              get_string('manageactionclone', 'core_grading'), 'b/edit-copy');
 226      }
 227      echo $output->container_end();
 228  
 229      // display the message if the form is currently not available (if applicable)
 230      if ($message = $controller->form_unavailable_notification()) {
 231          echo $output->notification($message);
 232      }
 233      // display the grading form preview
 234      if ($controller->is_form_defined()) {
 235          if ($definition->status == gradingform_controller::DEFINITION_STATUS_READY) {
 236              $tag = html_writer::tag('span', get_string('statusready', 'core_grading'), array('class' => 'status ready'));
 237          } else {
 238              $tag = html_writer::tag('span', get_string('statusdraft', 'core_grading'), array('class' => 'status draft'));
 239          }
 240          echo $output->heading(format_string($definition->name) . ' ' . $tag, 3, 'definition-name');
 241          echo $output->box($controller->render_preview($PAGE), 'definition-preview');
 242      }
 243  }
 244  
 245  
 246  echo $output->footer();