Esempio n. 1
0
/**
 * Function to move any custom fields with an invalid category
 * into a category called Miscellaneous
 *
 */
function pm_fix_orphaned_fields()
{
    global $DB;
    $misc_cat = get_string('misc_category', 'local_elisprogram');
    //set up context array
    $context_array = \local_eliscore\context\helper::get_all_levels();
    foreach ($context_array as $contextlevel => $contextname) {
        //find all fields with non existant category assignments
        $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_contextlevel::TABLE . "} ctx ON ctx.fieldid = field.id AND ctx.contextlevel = ?\n                  WHERE NOT EXISTS (\n                    SELECT 'x' FROM {" . field_category::TABLE . "} category\n                    WHERE category.id = field.categoryid)";
        $params = array($contextlevel);
        $rs = $DB->get_recordset_sql($sql, $params);
        //if any are found - then check if miscellaneous category exists - if not, create it
        foreach ($rs as $field) {
            $sql = "SELECT category.id\n                    FROM {" . field_category::TABLE . "} category\n                    JOIN {" . field_category_contextlevel::TABLE . "} categorycontext\n                      ON categorycontext.categoryid = category.id\n                    WHERE categorycontext.contextlevel = ?\n                      AND category.name = ?";
            $params = array($contextlevel, $misc_cat);
            $categoryid = $DB->get_field_sql($sql, $params);
            //create a miscellaneous category if it doesn't already exist
            if (!$categoryid) {
                // create an empty category
                $category = new field_category(array('name' => $misc_cat));
                $category->save();
                $categorycontext = new field_category_contextlevel();
                $categorycontext->categoryid = $category->id;
                $categorycontext->contextlevel = $contextlevel;
                $categorycontext->save();
                $categoryid = $category->id;
            }
            $field = new field($field->id);
            // set the field category to the Miscellaneous category
            $field->categoryid = $categoryid;
            $field->save();
        }
        $rs->close();
    }
}
Esempio n. 2
0
 /**
  * Test that the pm_ensure_role_assignable function works correctly
  */
 public function test_pmensureroleassignable()
 {
     global $DB;
     // This test needs to have the role_context_levels table completely empty before beginning.
     $DB->delete_records('role_context_levels');
     $contextlevels = \local_eliscore\context\helper::get_all_levels();
     $managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
     $programadminroleid = $DB->get_field('role', 'id', array('shortname' => 'curriculumadmin'));
     // Test that the function works with the 'manager' role.
     $this->assertEquals($managerroleid, pm_ensure_role_assignable('manager'));
     foreach ($contextlevels as $ctxlevel => $ctxclass) {
         $params = array('roleid' => $managerroleid, 'contextlevel' => $ctxlevel);
         $this->assertTrue($DB->record_exists('role_context_levels', $params));
     }
     // Test that the function works with the 'curriculumadmin' role.
     $this->assertEquals($programadminroleid, pm_ensure_role_assignable('curriculumadmin'));
     foreach ($contextlevels as $ctxlevel => $ctxclass) {
         $params = array('roleid' => $programadminroleid, 'contextlevel' => $ctxlevel);
         $this->assertTrue($DB->record_exists('role_context_levels', $params));
     }
 }