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 * mod_workshop data generator.
19 *
20 * @package    mod_workshop
21 * @category   test
22 * @copyright  2013 Marina Glancy
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * mod_workshop data generator class.
30 *
31 * @package    mod_workshop
32 * @category   test
33 * @copyright  2013 Marina Glancy
34 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_workshop_generator extends testing_module_generator {
37
38    public function create_instance($record = null, array $options = null) {
39        global $CFG;
40        require_once($CFG->libdir.'/filelib.php');
41
42        $workshopconfig = get_config('workshop');
43
44        // Add default values for workshop.
45        $record = (array)$record + array(
46            'strategy' => $workshopconfig->strategy,
47            'grade' => $workshopconfig->grade,
48            'gradinggrade' => $workshopconfig->gradinggrade,
49            'gradedecimals' => $workshopconfig->gradedecimals,
50            'nattachments' => 1,
51            'submissionfiletypes' => null,
52            'maxbytes' => $workshopconfig->maxbytes,
53            'latesubmissions' => 0,
54            'useselfassessment' => 0,
55            'overallfeedbackmode' => 1,
56            'overallfeedbackfiles' => 0,
57            'overallfeedbackfiletypes' => null,
58            'overallfeedbackmaxbytes' => $workshopconfig->maxbytes,
59            'useexamples' => 0,
60            'examplesmode' => $workshopconfig->examplesmode,
61            'submissionstart' => 0,
62            'submissionend' => 0,
63            'phaseswitchassessment' => 0,
64            'assessmentstart' => 0,
65            'assessmentend' => 0,
66        );
67        if (!isset($record['gradecategory']) || !isset($record['gradinggradecategory'])) {
68            require_once($CFG->libdir.'/gradelib.php');
69            $courseid = is_object($record['course']) ? $record['course']->id : $record['course'];
70            $gradecategories = grade_get_categories_menu($courseid);
71            reset($gradecategories);
72            $defaultcategory = key($gradecategories);
73            $record += array(
74                'gradecategory' => $defaultcategory,
75                'gradinggradecategory' => $defaultcategory
76            );
77        }
78        if (!isset($record['instructauthorseditor'])) {
79            $record['instructauthorseditor'] = array(
80                'text' => 'Instructions for submission '.($this->instancecount+1),
81                'format' => FORMAT_MOODLE,
82                'itemid' => file_get_unused_draft_itemid()
83            );
84        }
85        if (!isset($record['instructreviewerseditor'])) {
86            $record['instructreviewerseditor'] = array(
87                'text' => 'Instructions for assessment '.($this->instancecount+1),
88                'format' => FORMAT_MOODLE,
89                'itemid' => file_get_unused_draft_itemid()
90            );
91        }
92        if (!isset($record['conclusioneditor'])) {
93            $record['conclusioneditor'] = array(
94                'text' => 'Conclusion '.($this->instancecount+1),
95                'format' => FORMAT_MOODLE,
96                'itemid' => file_get_unused_draft_itemid()
97            );
98        }
99
100        return parent::create_instance($record, (array)$options);
101    }
102
103    /**
104     * Generates a submission authored by the given user.
105     *
106     * @param int $workshopid Workshop instance id.
107     * @param int $authorid Author user id.
108     * @param stdClass|array $options Optional explicit properties.
109     * @return int The new submission id.
110     */
111    public function create_submission($workshopid, $authorid, $options = null) {
112        global $DB;
113
114        $timenow = time();
115        $options = (array)$options;
116
117        $record = $options + array(
118            'workshopid' => $workshopid,
119            'example' => 0,
120            'authorid' => $authorid,
121            'timecreated' => $timenow,
122            'timemodified' => $timenow,
123            'title' => 'Generated submission',
124            'content' => 'Generated content',
125            'contentformat' => FORMAT_MARKDOWN,
126            'contenttrust' => 0,
127        );
128
129        $id = $DB->insert_record('workshop_submissions', $record);
130
131        return $id;
132    }
133
134    /**
135     * Generates an allocation of the given submission for peer-assessment by the given user
136     *
137     * @param int $submissionid Submission id.
138     * @param int $reviewerid Reviewer's user id.
139     * @param stdClass|array $options Optional explicit properties.
140     * @return int The new assessment id.
141     */
142    public function create_assessment($submissionid, $reviewerid, $options = null) {
143        global $DB;
144
145        $timenow = time();
146        $options = (array)$options;
147
148        $record = $options + array(
149            'submissionid' => $submissionid,
150            'reviewerid' => $reviewerid,
151            'weight' => 1,
152            'timecreated' => $timenow,
153            'timemodified' => $timenow,
154            'grade' => null,
155            'feedbackauthor' => '',
156            'feedbackreviewer' => '',
157        );
158
159        $id = $DB->insert_record('workshop_assessments', $record);
160
161        return $id;
162    }
163}
164