|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once(dirname(dirname(__FILE__)) . '/lib.php'); // interface definition 00030 require_once(dirname(dirname(dirname(__FILE__))) . '/locallib.php'); // workshop internal API 00031 00035 class workshop_manual_allocator implements workshop_allocator { 00036 00038 const MSG_ADDED = 1; 00039 const MSG_NOSUBMISSION = 2; 00040 const MSG_EXISTS = 3; 00041 const MSG_CONFIRM_DEL = 4; 00042 const MSG_DELETED = 5; 00043 const MSG_DELETE_ERROR = 6; 00044 00046 protected $workshop; 00047 00051 public function __construct(workshop $workshop) { 00052 $this->workshop = $workshop; 00053 } 00054 00058 public function init() { 00059 global $PAGE; 00060 00061 $mode = optional_param('mode', 'display', PARAM_ALPHA); 00062 00063 switch ($mode) { 00064 case 'new': 00065 if (!confirm_sesskey()) { 00066 throw new moodle_exception('confirmsesskeybad'); 00067 } 00068 $reviewerid = required_param('by', PARAM_INT); 00069 $authorid = required_param('of', PARAM_INT); 00070 $m = array(); // message object to be passed to the next page 00071 $submission = $this->workshop->get_submission_by_author($authorid); 00072 if (!$submission) { 00073 // nothing submitted by the given user 00074 $m[] = self::MSG_NOSUBMISSION; 00075 $m[] = $authorid; 00076 00077 } else { 00078 // ok, we have the submission 00079 $res = $this->workshop->add_allocation($submission, $reviewerid); 00080 if ($res == workshop::ALLOCATION_EXISTS) { 00081 $m[] = self::MSG_EXISTS; 00082 $m[] = $submission->authorid; 00083 $m[] = $reviewerid; 00084 } else { 00085 $m[] = self::MSG_ADDED; 00086 $m[] = $submission->authorid; 00087 $m[] = $reviewerid; 00088 } 00089 } 00090 $m = implode('-', $m); // serialize message object to be passed via URL 00091 redirect($PAGE->url->out(false, array('m' => $m))); 00092 break; 00093 case 'del': 00094 if (!confirm_sesskey()) { 00095 throw new moodle_exception('confirmsesskeybad'); 00096 } 00097 $assessmentid = required_param('what', PARAM_INT); 00098 $confirmed = optional_param('confirm', 0, PARAM_INT); 00099 $assessment = $this->workshop->get_assessment_by_id($assessmentid); 00100 if ($assessment) { 00101 if (!$confirmed) { 00102 $m[] = self::MSG_CONFIRM_DEL; 00103 $m[] = $assessment->id; 00104 $m[] = $assessment->authorid; 00105 $m[] = $assessment->reviewerid; 00106 if (is_null($assessment->grade)) { 00107 $m[] = 0; 00108 } else { 00109 $m[] = 1; 00110 } 00111 } else { 00112 if($this->workshop->delete_assessment($assessment->id)) { 00113 $m[] = self::MSG_DELETED; 00114 $m[] = $assessment->authorid; 00115 $m[] = $assessment->reviewerid; 00116 } else { 00117 $m[] = self::MSG_DELETE_ERROR; 00118 $m[] = $assessment->authorid; 00119 $m[] = $assessment->reviewerid; 00120 } 00121 } 00122 $m = implode('-', $m); // serialize message object to be passed via URL 00123 redirect($PAGE->url->out(false, array('m' => $m))); 00124 } 00125 break; 00126 } 00127 } 00128 00132 public function ui() { 00133 global $PAGE, $DB; 00134 00135 $output = $PAGE->get_renderer('workshopallocation_manual'); 00136 00137 $pagingvar = 'page'; 00138 $page = optional_param($pagingvar, 0, PARAM_INT); 00139 $perpage = 10; // todo let the user modify this 00140 00141 $hlauthorid = -1; // highlight this author 00142 $hlreviewerid = -1; // highlight this reviewer 00143 00144 $message = new workshop_message(); 00145 00146 $m = optional_param('m', '', PARAM_ALPHANUMEXT); // message code 00147 if ($m) { 00148 $m = explode('-', $m); 00149 switch ($m[0]) { 00150 case self::MSG_ADDED: 00151 $hlauthorid = $m[1]; 00152 $hlreviewerid = $m[2]; 00153 $message = new workshop_message(get_string('allocationadded', 'workshopallocation_manual'), 00154 workshop_message::TYPE_OK); 00155 break; 00156 case self::MSG_EXISTS: 00157 $hlauthorid = $m[1]; 00158 $hlreviewerid = $m[2]; 00159 $message = new workshop_message(get_string('allocationexists', 'workshopallocation_manual'), 00160 workshop_message::TYPE_INFO); 00161 break; 00162 case self::MSG_NOSUBMISSION: 00163 $hlauthorid = $m[1]; 00164 $message = new workshop_message(get_string('nosubmissionfound', 'workshop'), 00165 workshop_message::TYPE_ERROR); 00166 break; 00167 case self::MSG_CONFIRM_DEL: 00168 $hlauthorid = $m[2]; 00169 $hlreviewerid = $m[3]; 00170 if ($m[4] == 0) { 00171 $message = new workshop_message(get_string('areyousuretodeallocate', 'workshopallocation_manual'), 00172 workshop_message::TYPE_INFO); 00173 } else { 00174 $message = new workshop_message(get_string('areyousuretodeallocategraded', 'workshopallocation_manual'), 00175 workshop_message::TYPE_ERROR); 00176 } 00177 $url = new moodle_url($PAGE->url, array('mode' => 'del', 'what' => $m[1], 'confirm' => 1, 'sesskey' => sesskey())); 00178 $label = get_string('iamsure', 'workshop'); 00179 $message->set_action($url, $label); 00180 break; 00181 case self::MSG_DELETED: 00182 $hlauthorid = $m[1]; 00183 $hlreviewerid = $m[2]; 00184 $message = new workshop_message(get_string('assessmentdeleted', 'workshop'), 00185 workshop_message::TYPE_OK); 00186 break; 00187 case self::MSG_DELETE_ERROR: 00188 $hlauthorid = $m[1]; 00189 $hlreviewerid = $m[2]; 00190 $message = new workshop_message(get_string('assessmentnotdeleted', 'workshop'), 00191 workshop_message::TYPE_ERROR); 00192 break; 00193 } 00194 } 00195 00196 // fetch the list of ids of all workshop participants - this may get really long so fetch just id 00197 $participants = get_users_by_capability($PAGE->context, array('mod/workshop:submit', 'mod/workshop:peerassess'), 00198 'u.id', 'u.lastname,u.firstname,u.id', '', '', '', '', false, false, true); 00199 00200 $numofparticipants = count($participants); // we will need later for the pagination 00201 00202 if ($hlauthorid > 0 and $hlreviewerid > 0) { 00203 // display just those two users 00204 $participants = array_intersect_key($participants, array($hlauthorid => null, $hlreviewerid => null)); 00205 $button = $output->single_button($PAGE->url, get_string('showallparticipants', 'workshopallocation_manual'), 'get'); 00206 } else { 00207 // slice the list of participants according to the current page 00208 $participants = array_slice($participants, $page * $perpage, $perpage, true); 00209 $button = ''; 00210 } 00211 00212 // this will hold the information needed to display user names and pictures 00213 $userinfo = $DB->get_records_list('user', 'id', array_keys($participants), '', user_picture::fields()); 00214 00215 // load the participants' submissions 00216 $submissions = $this->workshop->get_submissions(array_keys($participants)); 00217 foreach ($submissions as $submission) { 00218 if (!isset($userinfo[$submission->authorid])) { 00219 $userinfo[$submission->authorid] = new stdclass(); 00220 $userinfo[$submission->authorid]->id = $submission->authorid; 00221 $userinfo[$submission->authorid]->firstname = $submission->authorfirstname; 00222 $userinfo[$submission->authorid]->lastname = $submission->authorlastname; 00223 $userinfo[$submission->authorid]->picture = $submission->authorpicture; 00224 $userinfo[$submission->authorid]->imagealt = $submission->authorimagealt; 00225 $userinfo[$submission->authorid]->email = $submission->authoremail; 00226 } 00227 } 00228 00229 // get current reviewers 00230 $reviewers = array(); 00231 if ($submissions) { 00232 list($submissionids, $params) = $DB->get_in_or_equal(array_keys($submissions), SQL_PARAMS_NAMED); 00233 $sql = "SELECT a.id AS assessmentid, a.submissionid, 00234 r.id AS reviewerid, r.lastname, r.firstname, r.picture, r.imagealt, r.email, 00235 s.id AS submissionid, s.authorid 00236 FROM {workshop_assessments} a 00237 JOIN {user} r ON (a.reviewerid = r.id) 00238 JOIN {workshop_submissions} s ON (a.submissionid = s.id) 00239 WHERE a.submissionid $submissionids"; 00240 $reviewers = $DB->get_records_sql($sql, $params); 00241 foreach ($reviewers as $reviewer) { 00242 if (!isset($userinfo[$reviewer->reviewerid])) { 00243 $userinfo[$reviewer->reviewerid] = new stdclass(); 00244 $userinfo[$reviewer->reviewerid]->id = $reviewer->reviewerid; 00245 $userinfo[$reviewer->reviewerid]->firstname = $reviewer->firstname; 00246 $userinfo[$reviewer->reviewerid]->lastname = $reviewer->lastname; 00247 $userinfo[$reviewer->reviewerid]->picture = $reviewer->picture; 00248 $userinfo[$reviewer->reviewerid]->imagealt = $reviewer->imagealt; 00249 $userinfo[$reviewer->reviewerid]->email = $reviewer->email; 00250 } 00251 } 00252 } 00253 00254 // get current reviewees 00255 $reviewees = array(); 00256 if ($participants) { 00257 list($participantids, $params) = $DB->get_in_or_equal(array_keys($participants), SQL_PARAMS_NAMED); 00258 $params['workshopid'] = $this->workshop->id; 00259 $sql = "SELECT a.id AS assessmentid, a.submissionid, 00260 u.id AS reviewerid, 00261 s.id AS submissionid, 00262 e.id AS revieweeid, e.lastname, e.firstname, e.picture, e.imagealt, e.email 00263 FROM {user} u 00264 JOIN {workshop_assessments} a ON (a.reviewerid = u.id) 00265 JOIN {workshop_submissions} s ON (a.submissionid = s.id) 00266 JOIN {user} e ON (s.authorid = e.id) 00267 WHERE u.id $participantids AND s.workshopid = :workshopid AND s.example = 0"; 00268 $reviewees = $DB->get_records_sql($sql, $params); 00269 foreach ($reviewees as $reviewee) { 00270 if (!isset($userinfo[$reviewee->revieweeid])) { 00271 $userinfo[$reviewee->revieweeid] = new stdclass(); 00272 $userinfo[$reviewee->revieweeid]->id = $reviewee->revieweeid; 00273 $userinfo[$reviewee->revieweeid]->firstname = $reviewee->firstname; 00274 $userinfo[$reviewee->revieweeid]->lastname = $reviewee->lastname; 00275 $userinfo[$reviewee->revieweeid]->picture = $reviewee->picture; 00276 $userinfo[$reviewee->revieweeid]->imagealt = $reviewee->imagealt; 00277 $userinfo[$reviewee->revieweeid]->email = $reviewee->email; 00278 } 00279 } 00280 } 00281 00282 // the information about the allocations 00283 $allocations = array(); 00284 00285 foreach ($participants as $participant) { 00286 $allocations[$participant->id] = new stdClass(); 00287 $allocations[$participant->id]->userid = $participant->id; 00288 $allocations[$participant->id]->submissionid = null; 00289 $allocations[$participant->id]->reviewedby = array(); 00290 $allocations[$participant->id]->reviewerof = array(); 00291 } 00292 unset($participants); 00293 00294 foreach ($submissions as $submission) { 00295 $allocations[$submission->authorid]->submissionid = $submission->id; 00296 $allocations[$submission->authorid]->submissiontitle = $submission->title; 00297 $allocations[$submission->authorid]->submissiongrade = $submission->grade; 00298 } 00299 unset($submissions); 00300 foreach($reviewers as $reviewer) { 00301 $allocations[$reviewer->authorid]->reviewedby[$reviewer->reviewerid] = $reviewer->assessmentid; 00302 } 00303 unset($reviewers); 00304 foreach($reviewees as $reviewee) { 00305 $allocations[$reviewee->reviewerid]->reviewerof[$reviewee->revieweeid] = $reviewee->assessmentid; 00306 } 00307 unset($reviewees); 00308 00309 // prepare data to be rendered 00310 $data = new workshopallocation_manual_allocations(); 00311 $data->allocations = $allocations; 00312 $data->userinfo = $userinfo; 00313 $data->authors = $this->workshop->get_potential_authors(); 00314 $data->reviewers = $this->workshop->get_potential_reviewers(); 00315 $data->hlauthorid = $hlauthorid; 00316 $data->hlreviewerid = $hlreviewerid; 00317 $data->selfassessment = $this->workshop->useselfassessment; 00318 00319 // prepare paging bar 00320 $pagingbar = new paging_bar($numofparticipants, $page, $perpage, $PAGE->url, $pagingvar); 00321 $pagingbarout = $output->render($pagingbar); 00322 00323 return $pagingbarout . $output->render($message) . $output->render($data) . $button . $pagingbarout; 00324 } 00325 00335 public static function delete_instance($workshopid) { 00336 return; 00337 } 00338 } 00339 00345 class workshopallocation_manual_allocations implements renderable { 00346 public $allocations; 00347 public $userinfo; 00348 public $authors; 00349 public $reviewers; 00350 public $hlauthorid; 00351 public $hlreviewerid; 00352 public $selfassessment; 00353 }