Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Allows user to allocate the submissions manually
  20   *
  21   * @package    workshopallocation
  22   * @subpackage manual
  23   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once (__DIR__ . '/../lib.php');            // interface definition
  30  require_once (__DIR__ . '/../../locallib.php');    // workshop internal API
  31  
  32  /**
  33   * Allows users to allocate submissions for review manually
  34   */
  35  class workshop_manual_allocator implements workshop_allocator {
  36  
  37      /** constants that are used to pass status messages between init() and ui() */
  38      const MSG_ADDED         = 1;
  39      const MSG_NOSUBMISSION  = 2;
  40      const MSG_EXISTS        = 3;
  41      const MSG_CONFIRM_DEL   = 4;
  42      const MSG_DELETED       = 5;
  43      const MSG_DELETE_ERROR  = 6;
  44  
  45      /** @var workshop instance */
  46      protected $workshop;
  47  
  48      /**
  49       * @param workshop $workshop Workshop API object
  50       */
  51      public function __construct(workshop $workshop) {
  52          $this->workshop = $workshop;
  53      }
  54  
  55      /**
  56       * Allocate submissions as requested by user
  57       *
  58       * @return workshop_allocation_result
  59       */
  60      public function init() {
  61          global $PAGE;
  62  
  63          $mode = optional_param('mode', 'display', PARAM_ALPHA);
  64          $perpage = optional_param('perpage', null, PARAM_INT);
  65  
  66          if ($perpage and $perpage > 0 and $perpage <= 1000) {
  67              require_sesskey();
  68              set_user_preference('workshopallocation_manual_perpage', $perpage);
  69              redirect($PAGE->url);
  70          }
  71  
  72          $result = new workshop_allocation_result($this);
  73  
  74          switch ($mode) {
  75          case 'new':
  76              if (!confirm_sesskey()) {
  77                  throw new moodle_exception('confirmsesskeybad');
  78              }
  79              $reviewerid = required_param('by', PARAM_INT);
  80              $authorid   = required_param('of', PARAM_INT);
  81              $m          = array();  // message object to be passed to the next page
  82              $submission = $this->workshop->get_submission_by_author($authorid);
  83              if (!$submission) {
  84                  // nothing submitted by the given user
  85                  $m[] = self::MSG_NOSUBMISSION;
  86                  $m[] = $authorid;
  87  
  88              } else {
  89                  // ok, we have the submission
  90                  $res = $this->workshop->add_allocation($submission, $reviewerid);
  91                  if ($res == workshop::ALLOCATION_EXISTS) {
  92                      $m[] = self::MSG_EXISTS;
  93                      $m[] = $submission->authorid;
  94                      $m[] = $reviewerid;
  95                  } else {
  96                      $m[] = self::MSG_ADDED;
  97                      $m[] = $submission->authorid;
  98                      $m[] = $reviewerid;
  99                  }
 100              }
 101              $m = implode('-', $m);  // serialize message object to be passed via URL
 102              redirect($PAGE->url->out(false, array('m' => $m)));
 103              break;
 104          case 'del':
 105              if (!confirm_sesskey()) {
 106                  throw new moodle_exception('confirmsesskeybad');
 107              }
 108              $assessmentid   = required_param('what', PARAM_INT);
 109              $confirmed      = optional_param('confirm', 0, PARAM_INT);
 110              $assessment     = $this->workshop->get_assessment_by_id($assessmentid);
 111              if ($assessment) {
 112                  if (!$confirmed) {
 113                      $m[] = self::MSG_CONFIRM_DEL;
 114                      $m[] = $assessment->id;
 115                      $m[] = $assessment->authorid;
 116                      $m[] = $assessment->reviewerid;
 117                      if (is_null($assessment->grade)) {
 118                          $m[] = 0;
 119                      } else {
 120                          $m[] = 1;
 121                      }
 122                  } else {
 123                      if($this->workshop->delete_assessment($assessment->id)) {
 124                          $m[] = self::MSG_DELETED;
 125                          $m[] = $assessment->authorid;
 126                          $m[] = $assessment->reviewerid;
 127                      } else {
 128                          $m[] = self::MSG_DELETE_ERROR;
 129                          $m[] = $assessment->authorid;
 130                          $m[] = $assessment->reviewerid;
 131                      }
 132                  }
 133                  $m = implode('-', $m);  // serialize message object to be passed via URL
 134                  redirect($PAGE->url->out(false, array('m' => $m)));
 135              }
 136              break;
 137          }
 138  
 139          $result->set_status(workshop_allocation_result::STATUS_VOID);
 140          return $result;
 141      }
 142  
 143      /**
 144       * Prints user interface - current allocation and a form to edit it
 145       */
 146      public function ui() {
 147          global $PAGE, $DB;
 148  
 149          $output     = $PAGE->get_renderer('workshopallocation_manual');
 150  
 151          $page       = optional_param('page', 0, PARAM_INT);
 152          $perpage    = get_user_preferences('workshopallocation_manual_perpage', 10);
 153          $groupid    = groups_get_activity_group($this->workshop->cm, true);
 154  
 155          $hlauthorid     = -1;           // highlight this author
 156          $hlreviewerid   = -1;           // highlight this reviewer
 157  
 158          $message        = new workshop_message();
 159  
 160          $m  = optional_param('m', '', PARAM_ALPHANUMEXT);   // message code
 161          if ($m) {
 162              $m = explode('-', $m);
 163              switch ($m[0]) {
 164              case self::MSG_ADDED:
 165                  $hlauthorid     = $m[1];
 166                  $hlreviewerid   = $m[2];
 167                  $message        = new workshop_message(get_string('allocationadded', 'workshopallocation_manual'),
 168                      workshop_message::TYPE_OK);
 169                  break;
 170              case self::MSG_EXISTS:
 171                  $hlauthorid     = $m[1];
 172                  $hlreviewerid   = $m[2];
 173                  $message        = new workshop_message(get_string('allocationexists', 'workshopallocation_manual'),
 174                      workshop_message::TYPE_INFO);
 175                  break;
 176              case self::MSG_NOSUBMISSION:
 177                  $hlauthorid     = $m[1];
 178                  $message        = new workshop_message(get_string('nosubmissionfound', 'workshop'),
 179                      workshop_message::TYPE_ERROR);
 180                  break;
 181              case self::MSG_CONFIRM_DEL:
 182                  $hlauthorid     = $m[2];
 183                  $hlreviewerid   = $m[3];
 184                  if ($m[4] == 0) {
 185                      $message    = new workshop_message(get_string('areyousuretodeallocate', 'workshopallocation_manual'),
 186                          workshop_message::TYPE_INFO);
 187                  } else {
 188                      $message    = new workshop_message(get_string('areyousuretodeallocategraded', 'workshopallocation_manual'),
 189                          workshop_message::TYPE_ERROR);
 190                  }
 191                  $url = new moodle_url($PAGE->url, array('mode' => 'del', 'what' => $m[1], 'confirm' => 1, 'sesskey' => sesskey()));
 192                  $label = get_string('iamsure', 'workshop');
 193                  $message->set_action($url, $label);
 194                  break;
 195              case self::MSG_DELETED:
 196                  $hlauthorid     = $m[1];
 197                  $hlreviewerid   = $m[2];
 198                  $message        = new workshop_message(get_string('assessmentdeleted', 'workshop'),
 199                      workshop_message::TYPE_OK);
 200                  break;
 201              case self::MSG_DELETE_ERROR:
 202                  $hlauthorid     = $m[1];
 203                  $hlreviewerid   = $m[2];
 204                  $message        = new workshop_message(get_string('assessmentnotdeleted', 'workshop'),
 205                      workshop_message::TYPE_ERROR);
 206                  break;
 207              }
 208          }
 209  
 210          // fetch the list of ids of all workshop participants
 211          $numofparticipants = $this->workshop->count_participants(false, $groupid);
 212          $participants = $this->workshop->get_participants(false, $groupid, $perpage * $page, $perpage);
 213  
 214          if ($hlauthorid > 0 and $hlreviewerid > 0) {
 215              // display just those two users
 216              $participants = array_intersect_key($participants, array($hlauthorid => null, $hlreviewerid => null));
 217              $button = $output->single_button($PAGE->url, get_string('showallparticipants', 'workshopallocation_manual'), 'get');
 218          } else {
 219              $button = '';
 220          }
 221  
 222          // this will hold the information needed to display user names and pictures
 223          $userinfo = $participants;
 224  
 225          // load the participants' submissions
 226          $submissions = $this->workshop->get_submissions(array_keys($participants));
 227          $allnames = get_all_user_name_fields();
 228          foreach ($submissions as $submission) {
 229              if (!isset($userinfo[$submission->authorid])) {
 230                  $userinfo[$submission->authorid]            = new stdclass();
 231                  $userinfo[$submission->authorid]->id        = $submission->authorid;
 232                  $userinfo[$submission->authorid]->picture   = $submission->authorpicture;
 233                  $userinfo[$submission->authorid]->imagealt  = $submission->authorimagealt;
 234                  $userinfo[$submission->authorid]->email     = $submission->authoremail;
 235                  foreach ($allnames as $addname) {
 236                      $temp = 'author' . $addname;
 237                      $userinfo[$submission->authorid]->$addname = $submission->$temp;
 238                  }
 239              }
 240          }
 241  
 242          // get current reviewers
 243          $reviewers = array();
 244          if ($submissions) {
 245              list($submissionids, $params) = $DB->get_in_or_equal(array_keys($submissions), SQL_PARAMS_NAMED);
 246              $picturefields = user_picture::fields('r', array(), 'reviewerid');
 247              $sql = "SELECT a.id AS assessmentid, a.submissionid, $picturefields,
 248                             s.id AS submissionid, s.authorid
 249                        FROM {workshop_assessments} a
 250                        JOIN {user} r ON (a.reviewerid = r.id)
 251                        JOIN {workshop_submissions} s ON (a.submissionid = s.id)
 252                       WHERE a.submissionid $submissionids";
 253              $reviewers = $DB->get_records_sql($sql, $params);
 254              foreach ($reviewers as $reviewer) {
 255                  if (!isset($userinfo[$reviewer->reviewerid])) {
 256                      $userinfo[$reviewer->reviewerid]            = new stdclass();
 257                      $userinfo[$reviewer->reviewerid]->id        = $reviewer->reviewerid;
 258                      $userinfo[$reviewer->reviewerid]->picture   = $reviewer->picture;
 259                      $userinfo[$reviewer->reviewerid]->imagealt  = $reviewer->imagealt;
 260                      $userinfo[$reviewer->reviewerid]->email     = $reviewer->email;
 261                      foreach ($allnames as $addname) {
 262                          $userinfo[$reviewer->reviewerid]->$addname = $reviewer->$addname;
 263                      }
 264                  }
 265              }
 266          }
 267  
 268          // get current reviewees
 269          $reviewees = array();
 270          if ($participants) {
 271              list($participantids, $params) = $DB->get_in_or_equal(array_keys($participants), SQL_PARAMS_NAMED);
 272              $namefields = get_all_user_name_fields(true, 'e');
 273              $params['workshopid'] = $this->workshop->id;
 274              $sql = "SELECT a.id AS assessmentid, a.submissionid,
 275                             u.id AS reviewerid,
 276                             s.id AS submissionid,
 277                             e.id AS revieweeid, e.lastname, e.firstname, $namefields, e.picture, e.imagealt, e.email
 278                        FROM {user} u
 279                        JOIN {workshop_assessments} a ON (a.reviewerid = u.id)
 280                        JOIN {workshop_submissions} s ON (a.submissionid = s.id)
 281                        JOIN {user} e ON (s.authorid = e.id)
 282                       WHERE u.id $participantids AND s.workshopid = :workshopid AND s.example = 0";
 283              $reviewees = $DB->get_records_sql($sql, $params);
 284              foreach ($reviewees as $reviewee) {
 285                  if (!isset($userinfo[$reviewee->revieweeid])) {
 286                      $userinfo[$reviewee->revieweeid]            = new stdclass();
 287                      $userinfo[$reviewee->revieweeid]->id        = $reviewee->revieweeid;
 288                      $userinfo[$reviewee->revieweeid]->firstname = $reviewee->firstname;
 289                      $userinfo[$reviewee->revieweeid]->lastname  = $reviewee->lastname;
 290                      $userinfo[$reviewee->revieweeid]->picture   = $reviewee->picture;
 291                      $userinfo[$reviewee->revieweeid]->imagealt  = $reviewee->imagealt;
 292                      $userinfo[$reviewee->revieweeid]->email     = $reviewee->email;
 293                      foreach ($allnames as $addname) {
 294                          $userinfo[$reviewee->revieweeid]->$addname = $reviewee->$addname;
 295                      }
 296                  }
 297              }
 298          }
 299  
 300          // the information about the allocations
 301          $allocations = array();
 302  
 303          foreach ($participants as $participant) {
 304              $allocations[$participant->id] = new stdClass();
 305              $allocations[$participant->id]->userid = $participant->id;
 306              $allocations[$participant->id]->submissionid = null;
 307              $allocations[$participant->id]->reviewedby = array();
 308              $allocations[$participant->id]->reviewerof = array();
 309          }
 310          unset($participants);
 311  
 312          foreach ($submissions as $submission) {
 313              $allocations[$submission->authorid]->submissionid = $submission->id;
 314              $allocations[$submission->authorid]->submissiontitle = $submission->title;
 315              $allocations[$submission->authorid]->submissiongrade = $submission->grade;
 316          }
 317          unset($submissions);
 318          foreach($reviewers as $reviewer) {
 319              $allocations[$reviewer->authorid]->reviewedby[$reviewer->reviewerid] = $reviewer->assessmentid;
 320          }
 321          unset($reviewers);
 322          foreach($reviewees as $reviewee) {
 323              $allocations[$reviewee->reviewerid]->reviewerof[$reviewee->revieweeid] = $reviewee->assessmentid;
 324          }
 325          unset($reviewees);
 326  
 327          // prepare data to be rendered
 328          $data                   = new workshopallocation_manual_allocations();
 329          $data->workshop         = $this->workshop;
 330          $data->allocations      = $allocations;
 331          $data->userinfo         = $userinfo;
 332          $data->authors          = $this->workshop->get_potential_authors();
 333          $data->reviewers        = $this->workshop->get_potential_reviewers();
 334          $data->hlauthorid       = $hlauthorid;
 335          $data->hlreviewerid     = $hlreviewerid;
 336          $data->selfassessment   = $this->workshop->useselfassessment;
 337  
 338          // prepare the group selector
 339          $groupselector = $output->container(groups_print_activity_menu($this->workshop->cm, $PAGE->url, true), 'groupwidget');
 340  
 341          // prepare paging bar
 342          $pagingbar              = new paging_bar($numofparticipants, $page, $perpage, $PAGE->url, 'page');
 343          $pagingbarout           = $output->render($pagingbar);
 344          $perpageselector        = $output->perpage_selector($perpage);
 345  
 346          return $groupselector . $pagingbarout . $output->render($message) . $output->render($data) . $button . $pagingbarout . $perpageselector;
 347      }
 348  
 349      /**
 350       * Delete all data related to a given workshop module instance
 351       *
 352       * This plugin does not store any data.
 353       *
 354       * @see workshop_delete_instance()
 355       * @param int $workshopid id of the workshop module instance being deleted
 356       * @return void
 357       */
 358      public static function delete_instance($workshopid) {
 359          return;
 360      }
 361  }
 362  
 363  /**
 364   * Contains all information needed to render current allocations and the allocator UI
 365   *
 366   * @see workshop_manual_allocator::ui()
 367   */
 368  class workshopallocation_manual_allocations implements renderable {
 369  
 370      /** @var workshop module instance */
 371      public $workshop;
 372  
 373      /** @var array of stdClass, indexed by userid, properties userid, submissionid, (array)reviewedby, (array)reviewerof */
 374      public $allocations;
 375  
 376      /** @var array of stdClass contains the data needed to display the user name and picture */
 377      public $userinfo;
 378  
 379      /* var array of stdClass potential authors */
 380      public $authors;
 381  
 382      /* var array of stdClass potential reviewers */
 383      public $reviewers;
 384  
 385      /* var int the id of the user to highlight as the author */
 386      public $hlauthorid;
 387  
 388      /* var int the id of the user to highlight as the reviewer */
 389      public $hlreviewerid;
 390  
 391      /* var bool should the selfassessment be allowed */
 392      public $selfassessment;
 393  }