Skip to content

Commit

Permalink
trigger the updating of grades from earlier versions of Moodle
Browse files Browse the repository at this point in the history
merge other fixes from Moodle 19 hotpot/lib.php
  • Loading branch information
gbateson committed Mar 27, 2008
1 parent 6c180b4 commit 5abcfe5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
39 changes: 16 additions & 23 deletions mod/hotpot/db/upgrade.php
@@ -1,36 +1,29 @@
<?php //$Id$

// This file keeps track of upgrades to
// the hotpot module
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// The commands in here will all be database-neutral,
// using the functions defined in lib/ddllib.php
// This file keeps track of upgrades to the hotpot module

function xmldb_hotpot_upgrade($oldversion=0) {

global $CFG, $THEME, $db;

$result = true;

/// And upgrade begins here. For each one, you'll need one
/// block of code similar to the next one. Please, delete
/// this comment lines once this file start handling proper
/// upgrade code.
// update hotpot grades from sites earlier than Moodle 1.9, 27th March 2008
if ($result && $oldversion < 2007101511) {

/// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
/// $result = result of "/lib/ddllib.php" function calls
/// }
// ensure "hotpot_update_grades" function is available
require_once $CFG->dirroot.'/mod/hotpot/lib.php';

// disable display of debugging messages
$db_debug_save = $db->debug;
$db->debug = false;

notify('Processing hotpot grades, this may take a while if there are many hotpots...', 'notifysuccess');
hotpot_update_grades();

// restore $db->debug
$db->debug = $db_debug_save;
}

return $result;
}
Expand Down
84 changes: 58 additions & 26 deletions mod/hotpot/lib.php
Expand Up @@ -430,12 +430,34 @@ function hotpot_get_chain(&$cm) {
return $found ? $chain : false;
}
function hotpot_is_visible(&$cm) {
global $CFG, $COURSE;

// check grouping
$modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
if (empty($CFG->enablegroupings) || empty($cm->groupmembersonly) || has_capability('moodle/site:accessallgroups', $modulecontext)) {
// groupings not applicable
} else if (!isguestuser() && groups_has_membership($cm)) {
// user is in one of the groups in the allowed grouping
} else {
// user is not in the required grouping and does not have sufficiently privileges to view this hotpot activity
return false;
}

// check if user can view hidden activities
if (isset($COURSE->context)) {
$coursecontext = &$COURSE->context;
} else {
$coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course);
}
if (has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
return true; // user can view hidden activities
}

if (!isset($cm->sectionvisible)) {
if ($section = get_record('course_sections', 'id', $cm->section)) {
$cm->sectionvisible = $section->visible;
} else {
if (! $section = get_record('course_sections', 'id', $cm->section)) {
error('Course module record contains invalid section');
}
$cm->sectionvisible = $section->visible;
}

if (empty($cm->sectionvisible)) {
Expand Down Expand Up @@ -1222,61 +1244,70 @@ function hotpot_get_user_grades($hotpot, $userid=0) {
return false;
}
}

/**
* Update grades in central gradebook
* this function is called from db/upgrade.php
* it is initially called with no arguments, which forces it to get a list of all hotpots
* it then iterates through the hotpots, calling itself to create a grade record for each hotpot
*
* @param object $hotpot null means all hotpots
* @param int $userid specific user only, 0 mean all
* @param int $userid specific user only, 0 means all users
*/
function hotpot_update_grades($hotpot=null, $userid=0, $nullifnone=true) {
global $CFG;
if (! function_exists('grade_update')) { //workaround for buggy PHP versions
if (! function_exists('grade_update')) {
require_once($CFG->libdir.'/gradelib.php');
}
if ($hotpot) {
if (is_null($hotpot)) {
// update (=create) grades for all hotpots
$sql = "
SELECT h.*, cm.idnumber as cmidnumber
FROM {$CFG->prefix}hotpot h, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
WHERE m.name='hotpot' AND m.id=cm.module AND cm.instance=h.id"
;
if ($rs = get_recordset_sql($sql)) {
while ($hotpot = rs_fetch_next_record($rs)) {
hotpot_update_grades($hotpot, 0, false);
}
rs_close($rs);
}
} else {
// update (=create) grade for a single hotpot
if ($grades = hotpot_get_user_grades($hotpot, $userid)) {
hotpot_grade_item_update($hotpot, $grades);

} else if ($userid && $nullifnone) {
// no grades for this user, but we must force the creation of a "null" grade record
$grade = new object();
$grade->userid = $userid;
$grade->rawgrade = null;
hotpot_grade_item_update($hotpot, $grade);

} else {
hotpot_grade_item_update($hotpot);
}
} else {
$sql = "SELECT h.*, cm.idnumber as cmidnumber
FROM {$CFG->prefix}hotpot h, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
WHERE m.name='hotpot' AND m.id=cm.module AND cm.instance=s.id";
if ($rs = get_recordset_sql($sql)) {
while ($hotpot = rs_fetch_next_record($rs)) {
hotpot_update_grades($hotpot, 0, false);
}
rs_close($rs);
// no grades and no userid
hotpot_grade_item_update($hotpot);
}
}
}

/**
* Update/create grade item for given hotpot
*
* @param object $hotpot object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return object grade_item
*/
function hotpot_grade_item_update($hotpot, $grades=NULL) {
function hotpot_grade_item_update($hotpot, $grades=null) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
if (! function_exists('grade_update')) {
require_once($CFG->libdir.'/gradelib.php');
}

$params = array('itemname' => $hotpot->name);
if (array_key_exists('cmidnumber', $hotpot)) {
//cmidnumber may not be always present
$params['idnumber'] = $hotpot->cmidnumber;
}

if ($hotpot->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $hotpot->grade;
Expand All @@ -1285,9 +1316,9 @@ function hotpot_grade_item_update($hotpot, $grades=NULL) {
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}

return grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, $grades, $params);
}

/**
* Delete grade item for given hotpot
*
Expand All @@ -1296,9 +1327,10 @@ function hotpot_grade_item_update($hotpot, $grades=NULL) {
*/
function hotpot_grade_item_delete($hotpot) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');

return grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, NULL, array('deleted'=>1));
if (! function_exists('grade_update')) {
require_once($CFG->libdir.'/gradelib.php');
}
return grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, null, array('deleted'=>1));
}

function hotpot_get_participants($hotpotid) {
Expand Down Expand Up @@ -1977,7 +2009,7 @@ function hotpot_convert_relative_url($baseurl, $reference, $opentag, $url, $clos
}

if ($query) {
$search = '#'.'(file|src|thesound)='."([^&]+)".'#ise';
$search = '#'.'(file|src|thesound|mp3)='."([^&]+)".'#ise';
$replace = "'\\1='.hotpot_convert_url('".$baseurl."','".$reference."','\\2')";
$query = preg_replace($search, $replace, $query);
}
Expand Down
2 changes: 1 addition & 1 deletion mod/hotpot/version.php
Expand Up @@ -3,7 +3,7 @@
/// Code fragment to define the version of hotpot
/// This fragment is called by moodle_needs_upgrading() and /admin/index.php
/////////////////////////////////////////////////////////////////////////////////
$module->version = 2007101509; // release date of this version (see note below)
$module->version = 2007101511; // release date of this version (see note below)
$module->release = 'v2.4.2'; // human-friendly version name (used in mod/hotpot/lib.php)
$module->requires = 2007101509; // Requires this Moodle version
$module->cron = 0; // period for cron to check this module (secs)
Expand Down

0 comments on commit 5abcfe5

Please sign in to comment.