From d895c6aaf56218336cdc358556a08f2ac8adc991 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Mon, 4 Jan 2010 18:11:35 +0000 Subject: [PATCH] Improved context handling Getting a context from instance is very cheap and I can actually do it just once when constructing the workshop API class. In the future, I will get rid of all $PAGE->context which is actually meant for things like blocks etc. --- mod/workshop/allocation/manual/lib.php | 4 +-- mod/workshop/allocation/random/lib.php | 4 +-- mod/workshop/develtools.php | 4 +-- mod/workshop/locallib.php | 43 +++++++++++++------------- mod/workshop/view.php | 2 +- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/mod/workshop/allocation/manual/lib.php b/mod/workshop/allocation/manual/lib.php index 99fc329203..9758b29e3b 100644 --- a/mod/workshop/allocation/manual/lib.php +++ b/mod/workshop/allocation/manual/lib.php @@ -305,8 +305,8 @@ class workshop_manual_allocator implements workshop_allocator { $data = new stdClass(); $data->allocations = $allocations; $data->userinfo = $userinfo; - $data->authors = $this->workshop->get_potential_authors($PAGE->context); - $data->reviewers = $this->workshop->get_potential_reviewers($PAGE->context); + $data->authors = $this->workshop->get_potential_authors(); + $data->reviewers = $this->workshop->get_potential_reviewers(); $data->hlauthorid = $hlauthorid; $data->hlreviewerid = $hlreviewerid; $data->selfassessment = $this->workshop->useselfassessment; diff --git a/mod/workshop/allocation/random/lib.php b/mod/workshop/allocation/random/lib.php index 497c8bd71b..5150d2b18a 100644 --- a/mod/workshop/allocation/random/lib.php +++ b/mod/workshop/allocation/random/lib.php @@ -80,9 +80,9 @@ class workshop_random_allocator implements workshop_allocator { $addselfassessment = optional_param('addselfassessment', false, PARAM_BOOL); $musthavesubmission = empty($assesswosubmission); - $authors = $this->workshop->get_potential_authors($PAGE->context); + $authors = $this->workshop->get_potential_authors(); $authors = $this->workshop->get_grouped($authors); - $reviewers = $this->workshop->get_potential_reviewers($PAGE->context, $musthavesubmission); + $reviewers = $this->workshop->get_potential_reviewers($musthavesubmission); $reviewers = $this->workshop->get_grouped($reviewers); $assessments = $this->workshop->get_all_assessments(); diff --git a/mod/workshop/develtools.php b/mod/workshop/develtools.php index 5c59022939..802e164935 100644 --- a/mod/workshop/develtools.php +++ b/mod/workshop/develtools.php @@ -50,8 +50,8 @@ $PAGE->set_button($OUTPUT->update_module_button($cm->id, 'workshop')); $PAGE->navbar->add('Development tools'); if ($tool == 'mksubmissions') { - $authors = $workshop->get_potential_authors($PAGE->context, false); - $authorswithsubmission = $workshop->get_potential_authors($PAGE->context, true); + $authors = $workshop->get_potential_authors(false); + $authorswithsubmission = $workshop->get_potential_authors(true); $authors = array_diff_key($authors, $authorswithsubmission); echo $OUTPUT->header(); $c = 0; // counter diff --git a/mod/workshop/locallib.php b/mod/workshop/locallib.php index 94fa114168..d5655a73c4 100644 --- a/mod/workshop/locallib.php +++ b/mod/workshop/locallib.php @@ -89,7 +89,9 @@ class workshop { $this->cm = $cm; $this->course = $course; // beware - this replaces the standard course field in the instance table // this is intentional - IMO there should be no such field as it violates - // 3rd normal form with no real performance gain + // 3rd normal form with no real performance gain. This way I try to + // demonstrate how backwards compatibility could be achieved --mudrd8mz + $this->context = get_context_instance(CONTEXT_MODULE, $this->id); } //////////////////////////////////////////////////////////////////////////////// @@ -154,12 +156,11 @@ class workshop { * The returned objects contain id, lastname and firstname properties and are ordered by lastname,firstname * * @todo handle with limits and groups - * @param stdClass $context * @param bool $musthavesubmission If true, return only users who have already submitted. All possible authors otherwise. * @return array array[userid] => stdClass{->id ->lastname ->firstname} */ - public function get_potential_authors(stdClass $context, $musthavesubmission=true) { - $users = get_users_by_capability($context, 'mod/workshop:submit', + public function get_potential_authors($musthavesubmission=true) { + $users = get_users_by_capability($this->context, 'mod/workshop:submit', 'u.id,u.lastname,u.firstname', 'u.lastname,u.firstname,u.id', 0, 1000, '', '', false, false, true); if ($musthavesubmission) { $users = array_intersect_key($users, $this->users_with_submission(array_keys($users))); @@ -173,12 +174,11 @@ class workshop { * The returned objects contain id, lastname and firstname properties and are ordered by lastname,firstname * * @todo handle with limits and groups - * @param stdClass $context * @param bool $musthavesubmission If true, return only users who have already submitted. All possible users otherwise. * @return array array[userid] => stdClass{->id ->lastname ->firstname} */ - public function get_potential_reviewers(stdClass $context, $musthavesubmission=false) { - $users = get_users_by_capability($context, 'mod/workshop:peerassess', + public function get_potential_reviewers($musthavesubmission=false) { + $users = get_users_by_capability($this->context, 'mod/workshop:peerassess', 'u.id, u.lastname, u.firstname', 'u.lastname,u.firstname,u.id', 0, 1000, '', '', false, false, true); if ($musthavesubmission) { // users without their own submission can not be reviewers @@ -654,7 +654,7 @@ class workshop { * @param stdClass context of the planned workshop * @return stdClass data object to be passed to the renderer */ - public function prepare_user_plan($userid, stdClass $context) { + public function prepare_user_plan($userid) { global $DB; $phases = array(); @@ -663,21 +663,21 @@ class workshop { $phase = new stdClass(); $phase->title = get_string('phasesetup', 'workshop'); $phase->tasks = array(); - if (has_capability('moodle/course:manageactivities', $context, $userid)) { + if (has_capability('moodle/course:manageactivities', $this->context, $userid)) { $task = new stdClass(); $task->title = get_string('taskintro', 'workshop'); $task->link = $this->updatemod_url(); $task->completed = !(trim(strip_tags($this->intro)) == ''); $phase->tasks['intro'] = $task; } - if (has_capability('moodle/course:manageactivities', $context, $userid)) { + if (has_capability('moodle/course:manageactivities', $this->context, $userid)) { $task = new stdClass(); $task->title = get_string('taskinstructauthors', 'workshop'); $task->link = $this->updatemod_url(); $task->completed = !(trim(strip_tags($this->instructauthors)) == ''); $phase->tasks['instructauthors'] = $task; } - if (has_capability('mod/workshop:editdimensions', $context, $userid)) { + if (has_capability('mod/workshop:editdimensions', $this->context, $userid)) { $task = new stdClass(); $task->title = get_string('editassessmentform', 'workshop'); $task->link = $this->editform_url(); @@ -702,7 +702,7 @@ class workshop { $phase = new stdClass(); $phase->title = get_string('phasesubmission', 'workshop'); $phase->tasks = array(); - if (has_capability('moodle/course:manageactivities', $context, $userid)) { + if (has_capability('moodle/course:manageactivities', $this->context, $userid)) { $task = new stdClass(); $task->title = get_string('taskinstructreviewers', 'workshop'); $task->link = $this->updatemod_url(); @@ -713,7 +713,7 @@ class workshop { } $phase->tasks['instructreviewers'] = $task; } - if (has_capability('mod/workshop:submit', $context, $userid, false)) { + if (has_capability('mod/workshop:submit', $this->context, $userid, false)) { $task = new stdClass(); $task->title = get_string('tasksubmit', 'workshop'); $task->link = $this->submission_url(); @@ -727,11 +727,11 @@ class workshop { $phase->tasks['submit'] = $task; } $phases[self::PHASE_SUBMISSION] = $phase; - if (has_capability('mod/workshop:allocate', $context, $userid)) { + if (has_capability('mod/workshop:allocate', $this->context, $userid)) { $task = new stdClass(); $task->title = get_string('allocate', 'workshop'); $task->link = $this->allocation_url(); - $numofauthors = count(get_users_by_capability($context, 'mod/workshop:submit', 'u.id', '', '', '', + $numofauthors = count(get_users_by_capability($this->context, 'mod/workshop:submit', 'u.id', '', '', '', '', '', false, true)); $numofsubmissions = $DB->count_records('workshop_submissions', array('workshopid'=>$this->id, 'example'=>0)); $sql = 'SELECT COUNT(s.id) AS nonallocated @@ -769,7 +769,7 @@ class workshop { $phase = new stdClass(); $phase->title = get_string('phaseassessment', 'workshop'); $phase->tasks = array(); - $phase->isreviewer = has_capability('mod/workshop:peerassess', $context, $userid); + $phase->isreviewer = has_capability('mod/workshop:peerassess', $this->context, $userid); $phase->assessments = $this->get_assessments_by_reviewer($userid); $numofpeers = 0; // number of allocated peer-assessments $numofpeerstodo = 0; // number of peer-assessments to do @@ -850,7 +850,7 @@ class workshop { } // Add phase swithing actions - if (has_capability('mod/workshop:switchphase', $context, $userid)) { + if (has_capability('mod/workshop:switchphase', $this->context, $userid)) { foreach ($phases as $phasecode => $phase) { if (! $phase->active) { $action = new stdClass(); @@ -915,7 +915,6 @@ class workshop { /** * Prepares data object with all workshop grades to be rendered * - * @param stdClass $context of the workshop instance * @param int $userid the user we are preparing the report for * @param mixed $groups single group or array of groups - only show users who are in one of these group(s). Defaults to all * @param int $page the current page (for the pagination) @@ -924,11 +923,11 @@ class workshop { * @param string $sorthow ASC|DESC * @return stdClass data for the renderer */ - public function prepare_grading_report(stdClass $context, $userid, $groups, $page, $perpage, $sortby, $sorthow) { + public function prepare_grading_report($userid, $groups, $page, $perpage, $sortby, $sorthow) { global $DB; - $canviewall = has_capability('mod/workshop:viewallassessments', $context, $userid); - $isparticipant = has_any_capability(array('mod/workshop:submit', 'mod/workshop:peerassess'), $context, $userid); + $canviewall = has_capability('mod/workshop:viewallassessments', $this->context, $userid); + $isparticipant = has_any_capability(array('mod/workshop:submit', 'mod/workshop:peerassess'), $this->context, $userid); if (!$canviewall and !$isparticipant) { // who the hell is this? @@ -946,7 +945,7 @@ class workshop { // get the list of user ids to be displayed if ($canviewall) { // fetch the list of ids of all workshop participants - this may get really long so fetch just id - $participants = get_users_by_capability($context, array('mod/workshop:submit', 'mod/workshop:peerassess'), + $participants = get_users_by_capability($this->context, array('mod/workshop:submit', 'mod/workshop:peerassess'), 'u.id', '', '', '', $groups, '', false, false, true); } else { // this is an ordinary workshop participant (aka student) - display the report just for him/her diff --git a/mod/workshop/view.php b/mod/workshop/view.php index e25d7e52d8..066980687e 100644 --- a/mod/workshop/view.php +++ b/mod/workshop/view.php @@ -163,7 +163,7 @@ case workshop::PHASE_EVALUATION: $sortby = 'totalgrade'; // todo let the user choose the column to sort by $sorthow = 'DESC'; // todo detto - $data = $workshop->prepare_grading_report($PAGE->context, $USER->id, $groups, $page, $perpage, $sortby, $sorthow); + $data = $workshop->prepare_grading_report($USER->id, $groups, $page, $perpage, $sortby, $sorthow); if ($data) { $showauthornames = has_capability('mod/workshop:viewauthornames', $PAGE->context); $showreviewernames = has_capability('mod/workshop:viewreviewernames', $PAGE->context); -- 2.39.5