Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Workshop/fakesubmissions.php

From MoodleDocs
Revision as of 20:38, 14 July 2021 by David Mudrak (talk | contribs) (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

For those who debug or test workshop, the following script might be useful. It simply creates fake submissions for all potential authors in the given workshop.

Put the script into /mod/workshop and execute it via CLI, providing the cmid argument (which is the one passed as view.php?id=xx via the web).

<?php

/**
 * Generate a fake submission for all authors in the workshop
 *
 * This is usefull for testing and debugging the workshop module.
 * Put this into the /mod/workshop folder and execute it via the command line.
 *
 * @package    mod_workshop
 * @copyright  2012 David Mudrak <david.mudrak@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

define('CLI_SCRIPT', true);

require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once($CFG->libdir.'/clilib.php');

list($options, $unrecognized) = cli_get_params(array('cmid' => false));

if (empty($options['cmid'])) {
    cli_error('Missing --cmid (course module id) argument');
} else {
    $cmid = (int)$options['cmid'];
}

$cm       = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
$course   = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
$workshop = new workshop($workshop, $cm, $course);

// get the list of authors we need to generate submission for
$potentialauthors = $workshop->get_potential_authors(false);
$existingauthors = $workshop->get_potential_authors(true);
$authors = array_diff_key($potentialauthors, $existingauthors);

// fetch the submission text from lipsum.com
$lipsum = simplexml_load_file('http://www.lipsum.com/feed/xml?amount=3&what=paras&start=1')->lipsum;
$lipsum = text_to_html(reset($lipsum));

foreach ($authors as $author) {
    // prepare the record to insert into the database
    $record = new stdClass();
    $record->workshopid             = $workshop->id;
    $record->example                = 0;
    $record->authorid               = $author->id;
    $record->timecreated            = time() - rand(0, HOURSECS);
    $record->timemodified           = $record->timecreated;
    $record->feedbackauthorformat   = FORMAT_HTML;
    $record->title                  = fullname($author)."'s submission";
    $record->content                = $lipsum;
    $record->contentformat          = FORMAT_HTML;
    $record->contenttrust           = 0;
    $record->late                   = 0;

    $id = $DB->insert_record('workshop_submissions', $record);

    fputs(STDOUT, sprintf('%-5d %s', $id, $record->title).PHP_EOL);
}