Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fa188aa0a2
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 604 lines (529 sloc) 24.459 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file defines a class with "Number of errors" grading strategy logic
*
* @package workshopform
* @subpackage numerrors
* @copyright 2009 David Mudrak <david.mudrak@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

require_once(dirname(dirname(__FILE__)) . '/lib.php'); // interface definition
require_once($CFG->libdir . '/gradelib.php'); // to handle float vs decimal issues

function workshopform_numerrors_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload) {
    global $DB;

    if ($context->contextlevel != CONTEXT_MODULE) {
        return false;
    }

    require_login($course, true, $cm);

    if ($filearea !== 'description') {
        return false;
    }

    $itemid = (int)array_shift($args); // the id of the assessment form dimension
    if (!$workshop = $DB->get_record('workshop', array('id' => $cm->instance))) {
        send_file_not_found();
    }

    if (!$dimension = $DB->get_record('workshopform_numerrors', array('id' => $itemid ,'workshopid' => $workshop->id))) {
        send_file_not_found();
    }

    // TODO now make sure the user is allowed to see the file
    // (media embedded into the dimension description)
    $fs = get_file_storage();
    $relativepath = implode('/', $args);
    $fullpath = "/$context->id/workshopform_numerrors/$filearea/$itemid/$relativepath";
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
        return false;
    }

    // finally send the file
    send_stored_file($file);
}

/**
* "Number of errors" grading strategy logic.
*/
class workshop_numerrors_strategy implements workshop_strategy {

    /** @const default number of dimensions to show */
    const MINDIMS = 3;

    /** @const number of dimensions to add */
    const ADDDIMS = 2;

    /** @var workshop the parent workshop instance */
    protected $workshop;

    /** @var array definition of the assessment form fields */
    protected $dimensions = null;

    /** @var array mapping of the number of errors to a grade */
    protected $mappings = null;

    /** @var array options for dimension description fields */
    protected $descriptionopts;

    /**
* Constructor
*
* @param workshop $workshop The workshop instance record
* @return void
*/
    public function __construct(workshop $workshop) {
        $this->workshop = $workshop;
        $this->dimensions = $this->load_fields();
        $this->mappings = $this->load_mappings();
        $this->descriptionopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => -1);
    }

    /**
* Factory method returning an instance of an assessment form editor class
*
* @param $actionurl URL of form handler, defaults to auto detect the current url
*/
    public function get_edit_strategy_form($actionurl=null) {
        global $CFG; // needed because the included files use it
        global $PAGE;

        require_once(dirname(__FILE__) . '/edit_form.php');

        $fields = $this->prepare_form_fields($this->dimensions, $this->mappings);
        $nodimensions = count($this->dimensions);
        $norepeatsdefault = max($nodimensions + self::ADDDIMS, self::MINDIMS);
        $norepeats = optional_param('norepeats', $norepeatsdefault, PARAM_INT); // number of dimensions
        $noadddims = optional_param('noadddims', '', PARAM_ALPHA); // shall we add more?
        if ($noadddims) {
            $norepeats += self::ADDDIMS;
        }

        // prepare the embeded files
        for ($i = 0; $i < $nodimensions; $i++) {
            // prepare all editor elements
            $fields = file_prepare_standard_editor($fields, 'description__idx_'.$i, $this->descriptionopts,
                $PAGE->context, 'workshopform_numerrors', 'description', $fields->{'dimensionid__idx_'.$i});
        }

        $customdata = array();
        $customdata['workshop'] = $this->workshop;
        $customdata['strategy'] = $this;
        $customdata['norepeats'] = $norepeats;
        $customdata['nodimensions'] = $nodimensions;
        $customdata['descriptionopts'] = $this->descriptionopts;
        $customdata['current'] = $fields;
        $attributes = array('class' => 'editstrategyform');

        return new workshop_edit_numerrors_strategy_form($actionurl, $customdata, 'post', '', $attributes);
    }

    /**
* Save the assessment dimensions into database
*
* Saves data into the main strategy form table. If the record->id is null or zero,
* new record is created. If the record->id is not empty, the existing record is updated. Records with
* empty 'description' field are removed from database.
* The passed data object are the raw data returned by the get_data().
*
* @uses $DB
* @param stdClass $data Raw data returned by the dimension editor form
* @return void
*/
    public function save_edit_strategy_form(stdclass $data) {
        global $DB, $PAGE;

        $workshopid = $data->workshopid;
        $norepeats = $data->norepeats;

        $data = $this->prepare_database_fields($data);
        $records = $data->numerrors; // data to be saved into {workshopform_numerrors}
        $mappings = $data->mappings; // data to be saved into {workshopform_numerrors_map}
        $todelete = array(); // dimension ids to be deleted

        for ($i=0; $i < $norepeats; $i++) {
            $record = $records[$i];
            if (0 == strlen(trim($record->description_editor['text']))) {
                if (!empty($record->id)) {
                    // existing dimension record with empty description - to be deleted
                    $todelete[] = $record->id;
                }
                continue;
            }
            if (empty($record->id)) {
                // new field
                $record->id = $DB->insert_record('workshopform_numerrors', $record);
            } else {
                // exiting field
                $DB->update_record('workshopform_numerrors', $record);
            }
            // re-save with correct path to embeded media files
            $record = file_postupdate_standard_editor($record, 'description', $this->descriptionopts, $PAGE->context,
                                                      'workshopform_numerrors', 'description', $record->id);
            $DB->update_record('workshopform_numerrors', $record);
        }
        $this->delete_dimensions($todelete);

        // re-save the mappings
        $todelete = array();
        foreach ($data->mappings as $nonegative => $grade) {
            if (is_null($grade)) {
                // no grade set for this number of negative responses
                $todelete[] = $nonegative;
                continue;
            }
            if (isset($this->mappings[$nonegative])) {
                $DB->set_field('workshopform_numerrors_map', 'grade', $grade,
                            array('workshopid' => $this->workshop->id, 'nonegative' => $nonegative));
            } else {
                $DB->insert_record('workshopform_numerrors_map',
                            (object)array('workshopid' => $this->workshop->id, 'nonegative' => $nonegative, 'grade' => $grade));
            }
        }
        // clear mappings that are not valid any more
        if (!empty($todelete)) {
            list($insql, $params) = $DB->get_in_or_equal($todelete, SQL_PARAMS_NAMED);
            $insql = "nonegative $insql OR ";
        } else {
            $insql = '';
        }
        $sql = "DELETE FROM {workshopform_numerrors_map}
WHERE (($insql nonegative > :nodimensions) AND (workshopid = :workshopid))";
        $params['nodimensions'] = $norepeats;
        $params['workshopid'] = $this->workshop->id;
        $DB->execute($sql, $params);
    }

    /**
* Factory method returning an instance of an assessment form
*
* @param moodle_url $actionurl URL of form handler, defaults to auto detect the current url
* @param string $mode Mode to open the form in: preview/assessment
* @param stdClass $assessment
* @param bool $editable
* @param array $options
*/
    public function get_assessment_form(moodle_url $actionurl=null, $mode='preview', stdclass $assessment=null, $editable=true, $options=array()) {
        global $CFG; // needed because the included files use it
        global $PAGE;
        global $DB;
        require_once(dirname(__FILE__) . '/assessment_form.php');

        $fields = $this->prepare_form_fields($this->dimensions, $this->mappings);
        $nodimensions = count($this->dimensions);

        // rewrite URLs to the embeded files
        for ($i = 0; $i < $nodimensions; $i++) {
            $fields->{'description__idx_'.$i} = file_rewrite_pluginfile_urls($fields->{'description__idx_'.$i},
                'pluginfile.php', $PAGE->context->id, 'workshopform_numerrors', 'description', $fields->{'dimensionid__idx_'.$i});
        }

        if ('assessment' === $mode and !empty($assessment)) {
            // load the previously saved assessment data
            $grades = $this->get_current_assessment_data($assessment);
            $current = new stdclass();
            for ($i = 0; $i < $nodimensions; $i++) {
                $dimid = $fields->{'dimensionid__idx_'.$i};
                if (isset($grades[$dimid])) {
                    $current->{'gradeid__idx_'.$i} = $grades[$dimid]->id;
                    $current->{'grade__idx_'.$i} = $grades[$dimid]->grade;
                    $current->{'peercomment__idx_'.$i} = $grades[$dimid]->peercomment;
                }
            }
        }

        // set up the required custom data common for all strategies
        $customdata['workshop'] = $this->workshop;
        $customdata['strategy'] = $this;
        $customdata['mode'] = $mode;
        $customdata['options'] = $options;

        // set up strategy-specific custom data
        $customdata['nodims'] = $nodimensions;
        $customdata['fields'] = $fields;
        $customdata['current'] = isset($current) ? $current : null;
        $attributes = array('class' => 'assessmentform numerrors');

        return new workshop_numerrors_assessment_form($actionurl, $customdata, 'post', '', $attributes, $editable);
    }

    /**
* Saves the filled assessment
*
* This method processes data submitted using the form returned by {@link get_assessment_form()}
*
* @param stdClass $assessment Assessment being filled
* @param stdClass $data Raw data as returned by the assessment form
* @return float|null Raw grade (from 0.00000 to 100.00000) for submission as suggested by the peer
*/
    public function save_assessment(stdclass $assessment, stdclass $data) {
        global $DB;

        if (!isset($data->nodims)) {
            throw new coding_exception('You did not send me the number of assessment dimensions to process');
        }
        for ($i = 0; $i < $data->nodims; $i++) {
            $grade = new stdclass();
            $grade->id = $data->{'gradeid__idx_' . $i};
            $grade->assessmentid = $assessment->id;
            $grade->strategy = 'numerrors';
            $grade->dimensionid = $data->{'dimensionid__idx_' . $i};
            $grade->grade = $data->{'grade__idx_' . $i};
            $grade->peercomment = $data->{'peercomment__idx_' . $i};
            $grade->peercommentformat = FORMAT_HTML;
            if (empty($grade->id)) {
                // new grade
                $grade->id = $DB->insert_record('workshop_grades', $grade);
            } else {
                // updated grade
                $DB->update_record('workshop_grades', $grade);
            }
        }
        return $this->update_peer_grade($assessment);
    }

    /**
* Has the assessment form been defined and is ready to be used by the reviewers?
*
* @return boolean
*/
    public function form_ready() {
        if (count($this->dimensions) > 0) {
            return true;
        }
        return false;
    }

    /**
* @see parent::get_assessments_recordset()
*/
    public function get_assessments_recordset($restrict=null) {
       global $DB;

        $sql = 'SELECT s.id AS submissionid,
a.id AS assessmentid, a.weight AS assessmentweight, a.reviewerid, a.gradinggrade,
g.dimensionid, g.grade
FROM {workshop_submissions} s
JOIN {workshop_assessments} a ON (a.submissionid = s.id)
JOIN {workshop_grades} g ON (g.assessmentid = a.id AND g.strategy = :strategy)
WHERE s.example=0 AND s.workshopid=:workshopid'; // to be cont.
        $params = array('workshopid' => $this->workshop->id, 'strategy' => $this->workshop->strategy);

        if (is_null($restrict)) {
            // update all users - no more conditions
        } elseif (!empty($restrict)) {
            list($usql, $uparams) = $DB->get_in_or_equal($restrict, SQL_PARAMS_NAMED);
            $sql .= " AND a.reviewerid $usql";
            $params = array_merge($params, $uparams);
        } else {
            throw new coding_exception('Empty value is not a valid parameter here');
        }

        $sql .= ' ORDER BY s.id'; // this is important for bulk processing

        return $DB->get_recordset_sql($sql, $params);

    }

    /**
* @see parent::get_dimensions_info()
*/
    public function get_dimensions_info() {
        global $DB;

        $params = array('workshopid' => $this->workshop->id);
        $dimrecords = $DB->get_records('workshopform_numerrors', array('workshopid' => $this->workshop->id), 'sort', 'id,weight');
        foreach ($dimrecords as $dimid => $dimrecord) {
            $dimrecords[$dimid]->min = 0;
            $dimrecords[$dimid]->max = 1;
        }
        return $dimrecords;
    }

    /**
* Is a given scale used by the instance of workshop?
*
* This grading strategy does not use scales.
*
* @param int $scaleid id of the scale to check
* @param int|null $workshopid id of workshop instance to check, checks all in case of null
* @return bool
*/
    public static function scale_used($scaleid, $workshopid=null) {
        return false;
    }

    /**
* Delete all data related to a given workshop module instance
*
* @see workshop_delete_instance()
* @param int $workshopid id of the workshop module instance being deleted
* @return void
*/
    public static function delete_instance($workshopid) {
        global $DB;

        $DB->delete_records('workshopform_numerrors', array('workshopid' => $workshopid));
        $DB->delete_records('workshopform_numerrors_map', array('workshopid' => $workshopid));
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Internal methods //
    ////////////////////////////////////////////////////////////////////////////////

    /**
* Loads the fields of the assessment form currently used in this workshop
*
* @return array definition of assessment dimensions
*/
    protected function load_fields() {
        global $DB;

        $sql = 'SELECT *
FROM {workshopform_numerrors}
WHERE workshopid = :workshopid
ORDER BY sort';
        $params = array('workshopid' => $this->workshop->id);

        return $DB->get_records_sql($sql, $params);
    }

    /**
* Loads the mappings of the number of errors to the grade
*
* @return array of records
*/
    protected function load_mappings() {
        global $DB;
        return $DB->get_records('workshopform_numerrors_map', array('workshopid' => $this->workshop->id), 'nonegative',
                                'nonegative,grade'); // we can use nonegative as key here as it must be unique within workshop
    }

    /**
* Prepares the database data to be used by the mform
*
* @param array $dims Array of raw dimension records as returned by {@link load_fields()}
* @param array $maps Array of raw mapping records as returned by {@link load_mappings()}
* @return array Array of fields data to be used by the mform set_data
*/
    protected function prepare_form_fields(array $dims, array $maps) {

        $formdata = new stdclass();
        $key = 0;
        foreach ($dims as $dimension) {
            $formdata->{'dimensionid__idx_' . $key} = $dimension->id;
            $formdata->{'description__idx_' . $key} = $dimension->description;
            $formdata->{'description__idx_' . $key.'format'} = $dimension->descriptionformat;
            $formdata->{'grade0__idx_' . $key} = $dimension->grade0;
            $formdata->{'grade1__idx_' . $key} = $dimension->grade1;
            $formdata->{'weight__idx_' . $key} = $dimension->weight;
            $key++;
        }

        foreach ($maps as $nonegative => $map) {
            $formdata->{'map__idx_' . $nonegative} = $map->grade;
        }

        return $formdata;
    }

    /**
* Deletes dimensions and removes embedded media from its descriptions
*
* todo we may check that there are no assessments done using these dimensions and probably remove them
*
* @param array $ids list to delete
* @return void
*/
    protected function delete_dimensions(array $ids) {
        global $DB, $PAGE;

        $fs = get_file_storage();
        foreach ($ids as $id) {
            $fs->delete_area_files($PAGE->context->id, 'workshopform_numerrors', 'description', $id);
        }
        $DB->delete_records_list('workshopform_numerrors', 'id', $ids);
    }

    /**
* Prepares data returned by {@link workshop_edit_numerrors_strategy_form} so they can be saved into database
*
* It automatically adds some columns into every record. The sorting is
* done by the order of the returned array and starts with 1.
* Called internally from {@link save_edit_strategy_form()} only. Could be private but
* keeping protected for unit testing purposes.
*
* @param stdClass $raw Raw data returned by mform
* @return array Array of objects to be inserted/updated in DB
*/
    protected function prepare_database_fields(stdclass $raw) {
        global $PAGE;

        $cook = new stdclass(); // to be returned
        $cook->numerrors = array(); // to be stored in {workshopform_numerrors}
        $cook->mappings = array(); // to be stored in {workshopform_numerrors_map}

        for ($i = 0; $i < $raw->norepeats; $i++) {
            $cook->numerrors[$i] = new stdclass();
            $cook->numerrors[$i]->id = $raw->{'dimensionid__idx_'.$i};
            $cook->numerrors[$i]->workshopid = $this->workshop->id;
            $cook->numerrors[$i]->sort = $i + 1;
            $cook->numerrors[$i]->description_editor = $raw->{'description__idx_'.$i.'_editor'};
            $cook->numerrors[$i]->grade0 = $raw->{'grade0__idx_'.$i};
            $cook->numerrors[$i]->grade1 = $raw->{'grade1__idx_'.$i};
            $cook->numerrors[$i]->weight = $raw->{'weight__idx_'.$i};
        }

        $i = 1;
        while (isset($raw->{'map__idx_'.$i})) {
            if (is_numeric($raw->{'map__idx_'.$i})) {
                $cook->mappings[$i] = $raw->{'map__idx_'.$i}; // should be a value from 0 to 100
            } else {
                $cook->mappings[$i] = null; // the user did not set anything
            }
            $i++;
        }

        return $cook;
    }

    /**
* Returns the list of current grades filled by the reviewer
*
* @param stdClass $assessment Assessment record
* @return array of filtered records from the table workshop_grades
*/
    protected function get_current_assessment_data(stdclass $assessment) {
        global $DB;

        if (empty($this->dimensions)) {
            return array();
        }
        list($dimsql, $dimparams) = $DB->get_in_or_equal(array_keys($this->dimensions), SQL_PARAMS_NAMED);
        // beware! the caller may rely on the returned array is indexed by dimensionid
        $sql = "SELECT dimensionid, wg.*
FROM {workshop_grades} wg
WHERE assessmentid = :assessmentid AND strategy= :strategy AND dimensionid $dimsql";
        $params = array('assessmentid' => $assessment->id, 'strategy' => 'numerrors');
        $params = array_merge($params, $dimparams);

        return $DB->get_records_sql($sql, $params);
    }

    /**
* Aggregates the assessment form data and sets the grade for the submission given by the peer
*
* @param stdClass $assessment Assessment record
* @return float|null Raw grade (0.00000 to 100.00000) for submission as suggested by the peer
*/
    protected function update_peer_grade(stdclass $assessment) {
        $grades = $this->get_current_assessment_data($assessment);
        $suggested = $this->calculate_peer_grade($grades);
        if (!is_null($suggested)) {
            $this->workshop->set_peer_grade($assessment->id, $suggested);
        }
        return $suggested;
    }

    /**
* Calculates the aggregated grade given by the reviewer
*
* @param array $grades Grade records as returned by {@link get_current_assessment_data}
* @return float|null Raw grade (0.00000 to 100.00000) for submission as suggested by the peer
*/
    protected function calculate_peer_grade(array $grades) {
        if (empty($grades)) {
            return null;
        }
        $sumerrors = 0; // sum of the weighted errors (ie the negative responses)
        foreach ($grades as $grade) {
            if (grade_floats_different($grade->grade, 1.00000)) {
                // negative reviewer's response
                $sumerrors += $this->dimensions[$grade->dimensionid]->weight;
            }
        }
        return $this->errors_to_grade($sumerrors);
    }

    /**
* Returns a grade 0.00000 to 100.00000 for the given number of errors
*
* This is where we use the mapping table defined by the teacher. If a grade for the given
* number of errors (negative assertions) is not defined, the most recently defined one is used.
* Example of the defined mapping:
* Number of errors | Grade
* 0 | 100% (always)
* 1 | - (not defined)
* 2 | 80%
* 3 | 60%
* 4 | -
* 5 | 30%
* 6 | 0%
* With this mapping, one error is mapped to 100% grade and 4 errors is mapped to 60%.
*
* @param mixed $numerrors Number of errors
* @return float Raw grade (0.00000 to 100.00000) for the given number of negative assertions
*/
    protected function errors_to_grade($numerrors) {
        $grade = 100.00000;
        for ($i = 1; $i <= $numerrors; $i++) {
            if (isset($this->mappings[$i])) {
                $grade = $this->mappings[$i]->grade;
            }
        }
        if ($grade > 100.00000) {
            $grade = 100.00000;
        }
        if ($grade < 0.00000) {
            $grade = 0.00000;
        }
        return grade_floatval($grade);
    }
}
Something went wrong with that request. Please try again.