* @param object $course Moodle course object. Must have at least ->id, ->enablecompletion
* @return completion_info
*/
- public function completion_info($course) {
+ public function __construct($course) {
$this->course=$course;
}
SELECT
COUNT(1)
FROM
- {$CFG->prefix}course_modules_completion
+ {course_modules_completion}
WHERE
coursemoduleid=? AND completionstate<>0",array($cm->id));
}
$currentuser=$userid==$USER->id;
if($currentuser) {
- // Make sure cache is present
- if(!isset($SESSION->completioncache)) {
+ // Make sure cache is present and is for current user (loginas
+ // changes this)
+ if(!isset($SESSION->completioncache) || $SESSION->completioncacheuserid!=$USER->id) {
$SESSION->completioncache=array();
+ $SESSION->completioncacheuserid=$USER->id;
}
// Expire any old data from cache
foreach($SESSION->completioncache as $courseid=>$activities) {
SELECT
cmc.*
FROM
- {$CFG->prefix}course_modules cm
- INNER JOIN {$CFG->prefix}course_modules_completion cmc ON cmc.coursemoduleid=cm.id
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cmc.coursemoduleid=cm.id
WHERE
cm.course=? AND cmc.userid=?",array($this->course->id,$userid));
SELECT
cmc.*
FROM
- {$CFG->prefix}course_modules cm
- INNER JOIN {$CFG->prefix}course_modules_completion cmc ON cm.id=cmc.coursemoduleid
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
WHERE
cm.course=? AND cmc.userid $insql
",$params);
*/
function notify_changed($deleted) {
// Ignore during restore
+ // TODO There should be a proper way to determine when we are in restore
+ // so that this hack looking for a $restore global is not needed.
global $restore;
if(!empty($restore->backup_unique_code)) {
return;
}
global $CFG,$COURSE,$DB;
require_once($CFG->libdir.'/completionlib.php');
-
+
+ // Bail out immediately if completion is not enabled for site (saves loading
+ // grade item below)
+ if(!completion_info::is_enabled_for_site()) {
+ return;
+ }
+
+ // Load information about grade item
+ $this->load_grade_item();
+
+ // Only course-modules have completion data
+ if($this->grade_item->itemtype!='mod') {
+ return;
+ }
+
// Use $COURSE if available otherwise get it via item fields
- if(!empty($COURSE)) {
+ if(!empty($COURSE) && $COURSE->id==$this->grade_item->courseid) {
$course=$COURSE;
} else {
- $this->load_grade_item();
- $course=get_record('course','id',$grade_item->courseid);
+ $course=$DB->get_record('course',array('id',$this->grade_item->courseid));
}
- // Bail out immediately if completion is not enabled for course
+ // Bail out if completion is not enabled for course
$completion=new completion_info($course);
if(!$completion->is_enabled()) {
return;
}
- // Get the grade item and course-module which we will need
- $this->load_grade_item();
- if($this->grade_item->itemtype!='mod') {
- return;
- }
- $cm=$DB->get_record_sql("
-SELECT
- cm.*,m.name AS modname
-FROM
- {$CFG->prefix}modules m
- INNER JOIN {$CFG->prefix}course_modules cm ON m.id=cm.module
-WHERE
- m.name=? AND cm.instance=? AND cm.course=?",
- array($this->grade_item->itemmodule,$this->grade_item->iteminstance,
- $this->grade_item->courseid));
+ // Get course-module
+ $cm=get_coursemodule_from_instance($this->grade_item->itemmodule,
+ $this->grade_item->iteminstance,$this->grade_item->courseid);
if(!$cm) {
debugging("Couldn't find course-module for module
'{$this->grade_item->itemmodule}', instance '{$this->grade_item->iteminstance}',
define ('PASSWORD_DIGITS', '0123456789');
define ('PASSWORD_NONALPHANUM', '.,;:!?_-+/*@#&$');
+// Feature constants. Used for plugin_supports() to report features that are,
+// or are not, supported by a module.
+
+/** True if module can provide a grade */
+define('FEATURE_GRADE_HAS_GRADE','grade_has_grade');
+/** True if module has code to track whether somebody viewed it */
+define('FEATURE_COMPLETION_TRACKS_VIEWS','completion_tracks_views');
+/** True if module has custom completion rules */
+define('FEATURE_COMPLETION_HAS_RULES','completion_has_rules');
+
+
+
/// PARAMETER HANDLING ////////////////////////////////////////////////////
/**
return $plugins;
}
-// Feature constants
-/** True if module can provide a grade */
-define('FEATURE_GRADE_HAS_GRADE','grade_has_grade');
-/** True if module has code to track whether somebody viewed it */
-define('FEATURE_COMPLETION_TRACKS_VIEWS','completion_tracks_views');
-/** True if module has custom completion rules */
-define('FEATURE_COMPLETION_HAS_RULES','completion_has_rules');
-
/**
* Checks whether a plugin supports a specified feature.
*
* @param string $type Plugin type e.g. 'mod'
* @param string $name Plugin name
* @param string $feature Feature code (FEATURE_xx constant)
- * @return Feature result (false if not supported, usually true but may have
- * other feature-specific value otherwise)
+ * @return Feature result (false if not supported, null if feature is unknown
+ * [=not supported, usually]; otherwise usually true but may have
+ * other feature-specific value)
*/
function plugin_supports($type,$name,$feature) {
global $CFG;
switch($feature) {
// If some features can also be checked in other ways
// for legacy support, this could be added here
- default: return false;
+ default: return null;
}
}
}
function test_update_state() {
$c=new completion_cutdown2();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
$cm=(object)array('id'=>13,'course'=>42);
// Not enabled, should do nothing
global $DB;
$c=new completion_cutdown3();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
$cm=(object)array('id'=>13,'course'=>42,'completiongradeitemnumber'=>null);
// If view is required, but they haven't viewed it yet
function test_set_module_viewed() {
$c=new completion_cutdown();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
$cm=(object)array('id'=>13,'course'=>42);
// Not tracking completion, should do nothing
$DB->expectOnce('get_field_sql',array(new IgnoreWhitespaceExpectation("SELECT
COUNT(1)
FROM
- test_course_modules_completion
+ {course_modules_completion}
WHERE
coursemoduleid=? AND completionstate<>0"),array(42)));
$c=new completion_info(null);
function test_reset_all_state() {
global $DB;
$c=new completion_cutdown();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
$cm=(object)array('id'=>13,'course'=>42);
SELECT
cmc.*
FROM
- test_course_modules cm
- INNER JOIN test_course_modules_completion cmc ON cmc.coursemoduleid=cm.id
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cmc.coursemoduleid=cm.id
WHERE
cm.course=? AND cmc.userid=?"),array(42,314159)));
global $DB;
$c=new completion_cutdown();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
// 1) Basic usage
$c->expectAt(0,'internal_get_tracked_users',array(false,0));
SELECT
cmc.*
FROM
- test_course_modules cm
- INNER JOIN test_course_modules_completion cmc ON cm.id=cmc.coursemoduleid
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
WHERE
cm.course=? AND cmc.userid IN (100,201)"),array(42)));
$progress1=(object)array('userid'=>100,'coursemoduleid'=>13);
SELECT
cmc.*
FROM
- test_course_modules cm
- INNER JOIN test_course_modules_completion cmc ON cm.id=cmc.coursemoduleid
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
WHERE
cm.course=? AND cmc.userid IN whatever"),array(42)));
$DB->setReturnValueAt(1,'get_recordset_sql',new fake_recordset(array_slice($progress,0,1000)));
SELECT
cmc.*
FROM
- test_course_modules cm
- INNER JOIN test_course_modules_completion cmc ON cm.id=cmc.coursemoduleid
+ {course_modules} cm
+ INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
WHERE
cm.course=? AND cmc.userid IN whatever2"),array(42)));
$DB->setReturnValueAt(2,'get_recordset_sql',new fake_recordset(array_slice($progress,1000)));
function test_inform_grade_changed() {
$c=new completion_cutdown();
- $c->completion_info((object)array('id'=>42));
+ $c->__construct((object)array('id'=>42));
$cm=(object)array('course'=>42,'id'=>13,'completiongradeitemnumber'=>null);
$item=(object)array('itemnumber'=>3);
switch($feature) {
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_COMPLETION_HAS_RULES: return true;
- default: return false;
+ default: return null;
}
}
SELECT
COUNT(1)
FROM
- {$CFG->prefix}forum_posts fp
- INNER JOIN {$CFG->prefix}forum_discussions fd ON fp.discussion=fd.id
+ {forum_posts} fp
+ INNER JOIN {forum_discussions} fd ON fp.discussion=fd.id
WHERE
fp.userid=:userid AND fd.forum=:forumid";
(!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
}
- function get_data($slashed=true) {
- $data=parent::get_data($slashed);
+ function get_data() {
+ $data=parent::get_data();
if(!$data) {
return false;
}
switch($feature) {
case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
- default: return false;
+ default: return null;
}
}