Example #1
0
/**
 * This file keeps track of upgrades to the certificate module
 *
 * @package    mod_certificate
 * @copyright  Mark Nelson <*****@*****.**>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
function xmldb_certificate_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    // ===== 1.9.0 or older upgrade line ======//
    if ($oldversion < 2007061300) {
        // Add new fields to certificate table
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('emailothers');
        $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'emailteachers');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('printhours');
        $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'gradefmt');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('lockgrade');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'printhours');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('requiredgrade');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'lockgrade');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Rename field save to savecert
        $field = new xmldb_field('save');
        if ($dbman->field_exists($table, $field)) {
            $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'emailothers');
            // Launch rename field savecert
            $dbman->rename_field($table, $field, 'savecert');
        } else {
            $field = new xmldb_field('savecert');
            $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'emailothers');
            $dbman->add_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2007061300, 'certificate');
    }
    if ($oldversion < 2007061301) {
        $table = new xmldb_table('certificate_linked_modules');
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
        $table->add_field('certificate_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
        $table->add_field('linkid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'certificate_id');
        $table->add_field('linkgrade', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'linkid');
        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'linkgrade');
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'), null, null);
        $table->add_index('certificate_id', XMLDB_INDEX_NOTUNIQUE, array('certificate_id'));
        $table->add_index('linkid', XMLDB_INDEX_NOTUNIQUE, array('linkid'));
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2007061301, 'certificate');
    }
    if ($oldversion < 2007102800) {
        // Add new fields to certificate table
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('reportcert');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'savecert');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('certificate_issues');
        $field = new xmldb_field('reportgrade');
        $field->set_attributes(XMLDB_TYPE_CHAR, '10', null, null, null, null, 'certdate');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2007102800, 'certificate');
    }
    if ($oldversion < 2007102806) {
        // Add new fields to certificate table
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('printoutcome');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'gradefmt');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2007102806, 'certificate');
    }
    if ($oldversion < 2008080904) {
        // Add new fields to certificate table if they dont already exist
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('intro');
        $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'name');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2008080904, 'certificate');
    }
    //===== 2.0 or older upgrade line ======//
    // Note, fresh 1.9 installs add the version 2009080900, so they miss this when upgrading from 1.9 -> 2.0.
    if ($oldversion < 2009062900) {
        // Add new field to certificate table
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('orientation', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, ' ', 'certificatetype');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('reissuecert', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'reportcert');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Set default orientation accordingly
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'portrait'));
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'letter_portrait'));
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'unicode_portrait'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'landscape'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'letter_landscape'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'unicode_landscape'));
        // Update all the certificate types
        $DB->set_field('certificate', 'certificatetype', 'A4_non_embedded', array('certificatetype' => 'landscape'));
        $DB->set_field('certificate', 'certificatetype', 'A4_non_embedded', array('certificatetype' => 'portrait'));
        $DB->set_field('certificate', 'certificatetype', 'A4_embedded', array('certificatetype' => 'unicode_landscape'));
        $DB->set_field('certificate', 'certificatetype', 'A4_embedded', array('certificatetype' => 'unicode_portrait'));
        $DB->set_field('certificate', 'certificatetype', 'letter_non_embedded', array('certificatetype' => 'letter_landscape'));
        $DB->set_field('certificate', 'certificatetype', 'letter_non_embedded', array('certificatetype' => 'letter_portrait'));
        // savepoint reached
        upgrade_mod_savepoint(true, 2009062900, 'certificate');
    }
    if ($oldversion < 2011030105) {
        // Define field id to be added to certificate
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 0, 'intro');
        // Conditionally launch add field id
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2011030105, 'certificate');
    }
    // The Moodle 2.0 CVS certificate version sets it to 2011110101, if the user performed an upgrade
    // then this upgrade will take care of several issues, if it's a fresh install then nothing is done.
    if ($oldversion < 2011110102) {
        require_once $CFG->libdir . '/conditionlib.php';
        $table = new xmldb_table('certificate');
        // It is possible for these fields not to be added, ever, it is included in the upgrade
        // process but fresh certificate 1.9 install from CVS MOODLE_19_STABLE set the Moodle version
        // to 2009080900, which means it missed all the earlier code written for upgrading to 2.0.
        $reissuefield = new xmldb_field('reissuecert', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'reportcert');
        $orientationfield = new xmldb_field('orientation', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, ' ', 'certificatetype');
        // Have to check, may be added during earlier upgrade, or may be missing due to not being included in install.xml
        if (!$dbman->field_exists($table, $reissuefield)) {
            $dbman->add_field($table, $reissuefield);
        }
        if (!$dbman->field_exists($table, $orientationfield)) {
            $dbman->add_field($table, $orientationfield);
        }
        // Fresh 2.0 installs won't have this table, but upgrades from 1.9 will
        if ($dbman->table_exists('certificate_linked_modules')) {
            // No longer need lock grade, or required grade, but first need to
            // convert so that the restrictions are still in place for Moodle 2.0
            if ($certs = $DB->get_records('certificate')) {
                foreach ($certs as $cert) {
                    if ($cert->lockgrade == 0) {
                        // Can skip this certificate, no course grade required
                        continue;
                    }
                    if (!($cm = get_coursemodule_from_instance('certificate', $cert->id))) {
                        // Not valid skip it
                        continue;
                    }
                    if (!($gradeitem = $DB->get_record('grade_items', array('courseid' => $cm->course, 'itemtype' => 'course')))) {
                        // Not valid skip it
                        continue;
                    }
                    $condition_info = new condition_info($cm, CONDITION_MISSING_EVERYTHING);
                    $condition_info->add_grade_condition($gradeitem->id, $cert->requiredgrade, '110');
                }
            }
            // Fresh installs won't have this table, but upgrades will
            // Lock grade and required grade field are not needed anymore
            if ($dbman->field_exists($table, 'lockgrade')) {
                $field = new xmldb_field('lockgrade');
                $dbman->drop_field($table, $field);
            }
            if ($dbman->field_exists($table, 'requiredgrade')) {
                $field = new xmldb_field('requiredgrade');
                $dbman->drop_field($table, $field);
            }
            // Now we need to loop through the restrictions in the certificate_linked_modules
            // table and convert it into the new Moodle 2.0 restrictions
            if ($certlinks = $DB->get_records('certificate_linked_modules')) {
                foreach ($certlinks as $link) {
                    // If the link id is '-1' then the setting applies to the time spent in the course and is not
                    // related to a module, meaning we can skip it for this section
                    if ($link->linkid == '-1') {
                        continue;
                    }
                    // Get the course module
                    if (!($cm = get_coursemodule_from_instance('certificate', $link->certificate_id))) {
                        // Not valid skip it
                        continue;
                    }
                    // Get grade item for module specified - is there an API function for this ??
                    $sql = "SELECT gi.id\n                            FROM {course_modules} cm\n                            INNER JOIN {modules} m\n                            ON cm.module = m.id\n                            INNER JOIN {grade_items} gi\n                            ON m.name = gi.itemmodule\n                            WHERE cm.id = :cmid\n                            AND cm.course = :courseid\n                            AND cm.instance = gi.iteminstance";
                    if (!($gradeitem = $DB->get_record_sql($sql, array('cmid' => $link->linkid, 'courseid' => $cm->course)))) {
                        // Not valid skip it
                        continue;
                    }
                    $condition_info = new condition_info($cm, CONDITION_MISSING_EVERYTHING);
                    $condition_info->add_grade_condition($gradeitem->id, $link->linkgrade, '110', true);
                }
            }
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2011110102, 'certificate');
    }
    // Note - the date has not changed as it has been set in the future, so I am incrementing
    // last digits. Actual date - 15/09/11
    if ($oldversion < 2011110103) {
        // New orientation field needs a value in order to view the cert, otherwise you get
        // an issue with FPDF and invalid orientation. This should be done during the upgrade,
        // but due to version number issues it is possible it was not executed, so do it now.
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'portrait'));
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'letter_portrait'));
        $DB->set_field('certificate', 'orientation', 'P', array('certificatetype' => 'unicode_portrait'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'landscape'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'letter_landscape'));
        $DB->set_field('certificate', 'orientation', 'L', array('certificatetype' => 'unicode_landscape'));
        // If the certificate type does not match any of the orientations in the above then set to 'L'
        $sql = "UPDATE {certificate}\n                SET orientation = 'L'\n                WHERE orientation = ''";
        $DB->execute($sql);
        // Update all the certificate types
        $DB->set_field('certificate', 'certificatetype', 'A4_non_embedded', array('certificatetype' => 'landscape'));
        $DB->set_field('certificate', 'certificatetype', 'A4_non_embedded', array('certificatetype' => 'portrait'));
        $DB->set_field('certificate', 'certificatetype', 'A4_embedded', array('certificatetype' => 'unicode_landscape'));
        $DB->set_field('certificate', 'certificatetype', 'A4_embedded', array('certificatetype' => 'unicode_portrait'));
        $DB->set_field('certificate', 'certificatetype', 'letter_non_embedded', array('certificatetype' => 'letter_landscape'));
        $DB->set_field('certificate', 'certificatetype', 'letter_non_embedded', array('certificatetype' => 'letter_portrait'));
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2011110103, 'certificate');
    }
    if ($oldversion < 2012022001) {
        // CONTRIB-3470 - certdate remaining 0 on issued certificates, need to update
        $sql = "UPDATE {certificate_issues}\n                SET certdate = timecreated\n                WHERE certdate = 0";
        $DB->execute($sql);
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2012022001, 'certificate');
    }
    if ($oldversion < 2012060901) {
        // Editing this table
        $table = new xmldb_table('certificate');
        // Get rid of the reissue cert column, this was a hack introduced later
        // in 1.9 when the bug was brought up that grades were not refreshing
        // since they were being stored in the issues table.
        // The certificate will now always return the current grade, student name
        // and course name.
        $field = new xmldb_field('reissuecert');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // The poor certificate_issues table is going to have a lot of
        // duplicates, we don't need that now, just keep the latest one
        $sql = "SELECT MAX(id) id1, MAX(id) as id2\n                FROM {certificate_issues}\n                GROUP BY certificateid, userid";
        if ($arrids = $DB->get_records_sql_menu($sql)) {
            $idstokeep = implode(",", $arrids);
            $sql = "DELETE\n                    FROM {certificate_issues}\n                    WHERE id NOT IN ({$idstokeep})";
            $DB->execute($sql);
        }
        // Going to be editing this table
        $table = new xmldb_table('certificate_issues');
        // Conditionally remove columns no longer needed
        $field = new xmldb_field('studentname');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        $field = new xmldb_field('classname');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        $field = new xmldb_field('certdate');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        $field = new xmldb_field('reportgrade');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        $field = new xmldb_field('mailed');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2012060901, 'certificate');
    }
    if ($oldversion < 2012072501) {
        // Add a column to store the required grade
        $table = new xmldb_table('certificate');
        $requiredtimefield = new xmldb_field('requiredtime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'delivery');
        if (!$dbman->field_exists($table, $requiredtimefield)) {
            $dbman->add_field($table, $requiredtimefield);
        }
        // If this table still exists, then the install was from a 1.9 version
        if ($dbman->table_exists('certificate_linked_modules')) {
            // Now we need to loop through the restrictions in the certificate_linked_modules
            // table and check if there were any required time conditions
            if ($certlinks = $DB->get_records('certificate_linked_modules')) {
                foreach ($certlinks as $link) {
                    // If the link id is '-1' then the setting applies to the time spent in the course
                    if ($link->linkid == '-1') {
                        // Make sure the certificate exists
                        if ($certificate = $DB->get_record('certificate', array('id' => $link->certificate_id))) {
                            $certificate->requiredtime = $link->linkgrade;
                            $DB->update_record('certificate', $certificate);
                        }
                    }
                }
            }
            // We can now get rid of this table
            $table = new xmldb_table('certificate_linked_modules');
            $dbman->drop_table($table);
        }
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2012072501, 'certificate');
    }
    if ($oldversion < 2012082401) {
        $table = new xmldb_table('certificate');
        // Change length of the fields that store images, so longer image names can be stored
        $field = new xmldb_field('borderstyle', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('printwmark', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('printsignature', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('printseal', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0);
        $dbman->change_field_precision($table, $field);
        // Change length of fields that are unnecessarily large
        $field = new xmldb_field('printnumber', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, 0, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('printhours', XMLDB_TYPE_CHAR, '255', null, false, 0, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('emailteachers', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, 0, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('savecert', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, 0, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('reportcert', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, 0, 0);
        $dbman->change_field_precision($table, $field);
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2012082401, 'certificate');
    }
    if ($oldversion < 2012090901) {
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, 0, 0, 'printseal');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Set the time created to the time modified
        $sql = "UPDATE {certificate}\n                SET timecreated = timemodified";
        $DB->execute($sql);
        // Certificate savepoint reached
        upgrade_mod_savepoint(true, 2012090901, 'certificate');
    }
    if ($oldversion < 2014041802) {
        // Fix previous upgrades.
        $table = new xmldb_table('certificate');
        $field = new xmldb_field('borderstyle', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0, '0');
        $dbman->change_field_default($table, $field);
        $field = new xmldb_field('printwmark', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0, '0');
        $dbman->change_field_default($table, $field);
        $field = new xmldb_field('printhours', XMLDB_TYPE_CHAR, '255', null, false, 0, null);
        $dbman->change_field_default($table, $field);
        $field = new xmldb_field('printsignature', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0, '0');
        $dbman->change_field_default($table, $field);
        $field = new xmldb_field('printseal', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, 0, '0');
        $dbman->change_field_default($table, $field);
        // Certificate savepoint reached.
        upgrade_mod_savepoint(true, 2014041802, 'certificate');
    }
    return true;
}
 public function test_cm_info_properties()
 {
     global $DB, $CFG;
     $this->resetAfterTest();
     $oldcfgenableavailability = $CFG->enableavailability;
     $oldcfgenablecompletion = $CFG->enablecompletion;
     set_config('enableavailability', 1);
     set_config('enablecompletion', 1);
     $this->setAdminUser();
     // Generate the course and pre-requisite module.
     $course = $this->getDataGenerator()->create_course(array('format' => 'topics', 'numsections' => 3, 'enablecompletion' => 1, 'groupmode' => SEPARATEGROUPS, 'forcegroupmode' => 0), array('createsections' => true));
     $coursecontext = context_course::instance($course->id);
     $prereqforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), array('completion' => 1));
     // Generate the module and add availability conditions.
     $conditionscompletion = array($prereqforum->cmid => COMPLETION_COMPLETE);
     $conditionsgrade = array(666 => (object) array('min' => 0.4, 'max' => null, 'name' => '!missing'));
     $conditionsfield = array('email' => (object) array('fieldname' => 'email', 'operator' => 'contains', 'value' => 'test'));
     $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id), array('idnumber' => 123, 'groupmode' => VISIBLEGROUPS, 'availablefrom' => time() + 3600, 'availableuntil' => time() + 5 * 3600));
     $ci = new condition_info((object) array('id' => $assign->cmid), CONDITION_MISSING_EVERYTHING);
     foreach ($conditionscompletion as $cmid => $requiredcompletion) {
         $ci->add_completion_condition($cmid, $requiredcompletion);
     }
     foreach ($conditionsgrade as $gradeid => $conditiongrade) {
         $ci->add_grade_condition($gradeid, $conditiongrade->min, $conditiongrade->max, true);
     }
     foreach ($conditionsfield as $conditionfield) {
         $ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
     }
     // Direct access to condition_info functions does not reset course cache, do it manually.
     rebuild_course_cache($course->id, true);
     // Retrieve all related records from DB.
     $assigndb = $DB->get_record('assign', array('id' => $assign->id));
     $moduletypedb = $DB->get_record('modules', array('name' => 'assign'));
     $moduledb = $DB->get_record('course_modules', array('module' => $moduletypedb->id, 'instance' => $assign->id));
     $sectiondb = $DB->get_record('course_sections', array('id' => $moduledb->section));
     $modnamessingular = get_module_types_names(false);
     $modnamesplural = get_module_types_names(true);
     // Create and enrol a student.
     $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
     $student = $this->getDataGenerator()->create_user();
     role_assign($studentrole->id, $student->id, $coursecontext);
     $enrolplugin = enrol_get_plugin('manual');
     $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
     $enrolplugin->enrol_user($enrolinstance, $student->id);
     $this->setUser($student);
     // Emulate data used in building course cache to receive the same instance of cached_cm_info as was used in building modinfo.
     $rawmods = get_course_mods($course->id);
     $cachedcminfo = assign_get_coursemodule_info($rawmods[$moduledb->id]);
     // Get modinfo.
     $modinfo = get_fast_modinfo($course->id);
     $cm = $modinfo->instances['assign'][$assign->id];
     $this->assertEquals($moduledb->id, $cm->id);
     $this->assertEquals($assigndb->id, $cm->instance);
     $this->assertEquals($moduledb->course, $cm->course);
     $this->assertEquals($moduledb->idnumber, $cm->idnumber);
     $this->assertEquals($moduledb->added, $cm->added);
     $this->assertEquals($moduledb->visible, $cm->visible);
     $this->assertEquals($moduledb->visibleold, $cm->visibleold);
     $this->assertEquals($moduledb->groupmode, $cm->groupmode);
     $this->assertEquals(VISIBLEGROUPS, $cm->groupmode);
     $this->assertEquals($moduledb->groupingid, $cm->groupingid);
     $this->assertEquals($moduledb->groupmembersonly, $cm->groupmembersonly);
     $this->assertEquals($course->groupmodeforce, $cm->coursegroupmodeforce);
     $this->assertEquals($course->groupmode, $cm->coursegroupmode);
     $this->assertEquals(SEPARATEGROUPS, $cm->coursegroupmode);
     $this->assertEquals($course->groupmodeforce ? $course->groupmode : $moduledb->groupmode, $cm->effectivegroupmode);
     // (since mod_assign supports groups).
     $this->assertEquals(VISIBLEGROUPS, $cm->effectivegroupmode);
     $this->assertEquals($moduledb->indent, $cm->indent);
     $this->assertEquals($moduledb->completion, $cm->completion);
     $this->assertEquals($moduledb->completiongradeitemnumber, $cm->completiongradeitemnumber);
     $this->assertEquals($moduledb->completionview, $cm->completionview);
     $this->assertEquals($moduledb->completionexpected, $cm->completionexpected);
     $this->assertEquals($moduledb->availablefrom, $cm->availablefrom);
     $this->assertEquals($moduledb->availableuntil, $cm->availableuntil);
     $this->assertEquals($moduledb->showavailability, $cm->showavailability);
     $this->assertEquals($moduledb->showdescription, $cm->showdescription);
     $this->assertEquals(null, $cm->extra);
     // Deprecated field. Used in module types that don't return cached_cm_info.
     $this->assertEquals($cachedcminfo->icon, $cm->icon);
     $this->assertEquals($cachedcminfo->iconcomponent, $cm->iconcomponent);
     $this->assertEquals('assign', $cm->modname);
     $this->assertEquals($moduledb->module, $cm->module);
     $this->assertEquals($cachedcminfo->name, $cm->name);
     $this->assertEquals($sectiondb->section, $cm->sectionnum);
     $this->assertEquals($moduledb->section, $cm->section);
     $this->assertEquals($conditionscompletion, $cm->conditionscompletion);
     $this->assertEquals($conditionsgrade, $cm->conditionsgrade);
     $this->assertEquals($conditionsfield, $cm->conditionsfield);
     $this->assertEquals(context_module::instance($moduledb->id), $cm->context);
     $this->assertEquals($modnamessingular['assign'], $cm->modfullname);
     $this->assertEquals($modnamesplural['assign'], $cm->modplural);
     $this->assertEquals(new moodle_url('/mod/assign/view.php', array('id' => $moduledb->id)), $cm->url);
     $this->assertEquals($cachedcminfo->customdata, $cm->customdata);
     // Dynamic fields, just test that they can be retrieved (must be carefully tested in each activity type).
     $this->assertNotEmpty($cm->availableinfo);
     // Lists all unmet availability conditions.
     $this->assertEquals(0, $cm->uservisible);
     $this->assertEquals('', $cm->extraclasses);
     $this->assertEquals('', $cm->onclick);
     $this->assertEquals(null, $cm->afterlink);
     $this->assertEquals(null, $cm->afterediticons);
     $this->assertEquals('', $cm->content);
     // Attempt to access and set non-existing field.
     $this->assertTrue(empty($modinfo->somefield));
     $this->assertFalse(isset($modinfo->somefield));
     $cm->somefield;
     $this->assertDebuggingCalled();
     $cm->somefield = 'Some value';
     $this->assertDebuggingCalled();
     $this->assertEmpty($cm->somefield);
     $this->assertDebuggingCalled();
     // Attempt to overwrite an existing field.
     $prevvalue = $cm->name;
     $this->assertNotEmpty($cm->name);
     $this->assertFalse(empty($cm->name));
     $this->assertTrue(isset($cm->name));
     $cm->name = 'Illegal overwriting';
     $this->assertDebuggingCalled();
     $this->assertEquals($prevvalue, $cm->name);
     $this->assertDebuggingNotCalled();
     // Restore settings.
     set_config('enableavailability', $oldcfgenableavailability);
     set_config('enablecompletion', $oldcfgenablecompletion);
 }
Example #3
0
 /**
  * Utility function called by modedit.php; updates the 
  * course_modules_availability table based on the module form data.
  *
  * @param object $cm Course-module with as much data as necessary, min id
  * @param object $fromform
  * @param bool $wipefirst Defaults to true
  */
 public static function update_cm_from_form($cm, $fromform, $wipefirst = true)
 {
     $ci = new condition_info($cm, CONDITION_MISSING_EVERYTHING, false);
     if ($wipefirst) {
         $ci->wipe_conditions();
     }
     foreach ($fromform->conditiongradegroup as $record) {
         if ($record['conditiongradeitemid']) {
             $ci->add_grade_condition($record['conditiongradeitemid'], $record['conditiongrademin'], $record['conditiongrademax']);
         }
     }
     if (isset($fromform->conditioncompletiongroup)) {
         foreach ($fromform->conditioncompletiongroup as $record) {
             if ($record['conditionsourcecmid']) {
                 $ci->add_completion_condition($record['conditionsourcecmid'], $record['conditionrequiredcompletion']);
             }
         }
     }
 }
 function test_is_available()
 {
     global $DB, $USER;
     $courseid = $this->make_course();
     // No conditions
     $cmid = $this->make_course_module($courseid);
     $ci = new condition_info((object) array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
     $this->assertTrue($ci->is_available($text, false, 0));
     $this->assertEqual('', $text);
     // Time (from)
     $time = time() + 100;
     $cmid = $this->make_course_module($courseid, array('availablefrom' => $time));
     $ci = new condition_info((object) array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
     $this->assertFalse($ci->is_available($text));
     $this->assert(new PatternExpectation('/' . preg_quote(userdate($time, get_string('strftimedate', 'langconfig'))) . '/'), $text);
     $time = time() - 100;
     $cmid = $this->make_course_module($courseid, array('availablefrom' => $time));
     $ci = new condition_info((object) array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
     $this->assertTrue($ci->is_available($text));
     $this->assertEqual('', $text);
     $this->assert(new PatternExpectation('/' . preg_quote(userdate($time, get_string('strftimedate', 'langconfig'))) . '/'), $ci->get_full_information());
     // Time (until)
     $cmid = $this->make_course_module($courseid, array('availableuntil' => time() - 100));
     $ci = new condition_info((object) array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
     $this->assertFalse($ci->is_available($text));
     $this->assertEqual('', $text);
     // Completion
     $oldid = $cmid;
     $cmid = $this->make_course_module($courseid);
     $this->make_section($courseid, array($oldid, $cmid));
     $oldcm = $DB->get_record('course_modules', array('id' => $oldid));
     $oldcm->completion = COMPLETION_TRACKING_MANUAL;
     $DB->update_record('course_modules', $oldcm);
     $ci = new condition_info((object) array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
     $ci->add_completion_condition($oldid, COMPLETION_COMPLETE);
     $this->assertFalse($ci->is_available($text, false));
     $this->assertEqual(get_string('requires_completion_1', 'condition', 'xxx'), $text);
     $completion = new completion_info($DB->get_record('course', array('id' => $courseid)));
     $completion->update_state($oldcm, COMPLETION_COMPLETE);
     completion_info::wipe_session_cache();
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     $this->assertFalse($ci->is_available($text, false, $USER->id + 1));
     completion_info::wipe_session_cache();
     condition_info::wipe_session_cache();
     $completion = new completion_info($DB->get_record('course', array('id' => $courseid)));
     $completion->update_state($oldcm, COMPLETION_INCOMPLETE);
     $this->assertFalse($ci->is_available($text));
     $ci->wipe_conditions();
     $ci->add_completion_condition($oldid, COMPLETION_INCOMPLETE);
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     $this->assertTrue($ci->is_available($text, false, $USER->id + 1));
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text, true));
     // Grade
     $ci->wipe_conditions();
     // Add a fake grade item
     $gradeitemid = $DB->insert_record('grade_items', (object) array('courseid' => $courseid, 'itemname' => 'frog'));
     // Add a condition on a value existing...
     $ci->add_grade_condition($gradeitemid, null, null, true);
     $this->assertFalse($ci->is_available($text));
     $this->assertEqual(get_string('requires_grade_any', 'condition', 'frog'), $text);
     // Fake it existing
     $DB->insert_record('grade_grades', (object) array('itemid' => $gradeitemid, 'userid' => $USER->id, 'finalgrade' => 3.78));
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text, true));
     // Now require that user gets more than 3.78001
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, 3.78001, null, true);
     condition_info::wipe_session_cache();
     $this->assertFalse($ci->is_available($text));
     $this->assertEqual(get_string('requires_grade_min', 'condition', 'frog'), $text);
     // ...just on 3.78...
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, 3.78, null, true);
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     // ...less than 3.78
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, null, 3.78, true);
     condition_info::wipe_session_cache();
     $this->assertFalse($ci->is_available($text));
     $this->assertEqual(get_string('requires_grade_max', 'condition', 'frog'), $text);
     // ...less than 3.78001
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, null, 3.78001, true);
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     // ...in a range that includes it
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, 3, 4, true);
     condition_info::wipe_session_cache();
     $this->assertTrue($ci->is_available($text));
     // ...in a range that doesn't include it
     $ci->wipe_conditions();
     $ci->add_grade_condition($gradeitemid, 4, 5, true);
     condition_info::wipe_session_cache();
     $this->assertFalse($ci->is_available($text));
     $this->assertEqual(get_string('requires_grade_range', 'condition', 'frog'), $text);
 }