skip to Main Content

I need to extract a specific value from a table knowing the id of the row in moodle
screenshot of the table

I tried previously to get the variable from another php class but no matter what it gives null so I get the idea if there is a method to get this value directly from the database table
the table name ‘mdl_course_modules’
the id is the same as course module id
and need to extract this value from the column ‘availability’

Edit

I tried to use the following

$viewslim = $DB->get_field_select('course_modules', 'availability' , "`availability` LIKE '%maxviews%' AND `id` = '$cm->id'",['id' => $cm->id], IGNORE_MISSING);
preg_match_all('/(d+)/', $viewslim , $matches);
echo '<pre>'; var_dump($matches); echo '</pre>';

it worked fine but if there is another integer in the line due to another active availability plugin, so how to get only the value after viewlimit only not any other possible integer in the line

{"op":"&","c":[{"type":"maxviews","viewslimit":"1"}],"showc":[true]}

that is what I get from var_dump

array(2) {
  [0]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  [1]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    I get it, I hope it works fine till the end

    $getviewslimit = $DB->get_field_select('course_modules', 'availability' , "`availability` LIKE '%maxviews%' AND `id` = '$cm->id'",['id' => $cm->id], IGNORE_MISSING);
    if ($getviewslimit != null){
    preg_match_all('/"type":"maxviews","viewslimit":"(d+)/', $getviewslimit , $matches);
    $viewslimit = $matches[1][0];
    
    $a = new stdclass();
    $a->viewscount = $viewscount;
    $a->viewslimit = ($viewslimit + $reset);
    echo '<pre>'; var_dump($a->viewslimit); echo '</pre>';
    }
    

    and it results exactly what I need

    int(3)
    int(7)
    int(50)
    

  2. There might be more than one availability condition, including more than one maxviews

    Maybe use the existing availability code instead of a database query – it looks like you already know the $cm->id so will go from there

    $modinfo = get_fast_modinfo($courseid);
    
    // Get the module as a cm_info class.
    $mod = $modinfo->get_cm($cm->id);
    
    // Get the availability conditions.
    $ci = new core_availabilityinfo_module($mod);
    $tree = $ci->get_availability_tree();
    
    // Children is a protected property, so use Reflection (available from PHP 8)
    $reflectiontree = new ReflectionClass($tree);
    $reflectionchildren = $reflectiontree->getProperty('children');
    $reflectionchildren->setAccessible(true);
    
    $children = $reflectionchildren->getValue($tree);
    
    $viewslimit = 0;
    
    foreach ($children as $child) {
        if ($child instanceof availability_maxviewscondition) {
            // This is a max views condition.
    
            // Viewslimit is protected so again use reflection.
            $reflectionmaxviews = new ReflectionClass($child);
            $reflectionviewslimit = $reflectionmaxviews->getProperty('viewslimit');
            $reflectionviewslimit->setAccessible(true);
    
            $newviewslimit = (int)$reflectionviewslimit->getValue($child);
    
            // It's unlikely but its possible to add more than one max views condition.
            // So use the largest value.
            if ($newviewslimit > $viewslimit) {
                $viewslimit = $newviewslimit;
            }
    
        }
    }
    
    // Done, this is the views limit for this module.
    echo $viewslimit;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search