:: Version 2.7.0 ::

Count Ticket reopening

Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.

learning:
Count transitions occurence
level:
Beginner
domains:
XML, PHP, Automation, Dictionary, Lifecycle
min version:
2.1.0

In this usecase, we want to count how many times a User Request was re-open. For this will:

  1. add a counter field to UserRequest,
  2. desactivate history tracking (optional)
  3. define a PHP method on UserRequest to increment a counter,
  4. modify UserRequest lifecycle to call the method during the transition from resolved to assigned
  5. modify the presentation to display the counter
  6. add a label for the counter field

For readability, we present this 6 steps with 6 different blocks of XML, but in reality, you just need to write one XML structure with all of them.

iTop XML compilation process, supports an non-optimal tree structure with nodes duplicated, he will merge them

Add a counter field

itop_design
  <classes>
    <class id="UserRequest" _delta="must_exist">
      <fields>
        <field id="reopen_count" xsi:type="AttributeInteger" _delta="define">
          <sql>reopen_count</sql>
          <default_value>0</default_value>
          <is_null_allowed>true</is_null_allowed>
        </field>
      </fields>
    </class>
  </classes>

Deactivate history tracking

You may decide that it is useless to record in the history, each time reopen_count counter is changed
Then add <tracking_level>none</tracking_level> to the field definition and it will do the job. This is just an option, you don't have to do it. If you don't define that tag, iTop will track the changes made on that field, as it is the default when omitted.

itop_design / classes / class@UserRequest / fields
        <field id="reopen_count">
          <!-- No tracking level, mean no entry in History log for this field -->
          <tracking_level>none</tracking_level>
        </field>
Removing history tracking on fields avoid polluting the object history.
  • an ever changing value, mastered by an external source
  • a computed counter, whose modification dates might be useless

Declare the PHP method

class:UserRequest
public function IncrementCounter($sAttCode, $iIncrement=1)
{
   // Defensive programming, ensure that: the field code is valid on current class
   if (MetaModel::IsValidAttCode(get_class($this), $sAttCode)
   // and the increment provided is numeric
   && is_numeric($iIncrement) 
   // and the field code correspond to an Integer type of attribute 
   && (MetaModel::GetAttributeDef(get_class($this),$sAttCode) instanceof AttributeInteger))
   {
       $iNew = $this->Get($sAttCode) + $iIncrement;
       $this->Set($sAttCode, $iNew);
   }
   return true;
}         

Remember that PHP method need to be embeded in a XML structure

Call the method on transition

Let's increment the counter each time the ticket move from resolved to assigned states.

itop_design / classes / class@UserRequest / lifecycle / states
          <!-- When we are in "resolved" state of UserRequest lifecycle -->
          <state id="resolved">
            <!-- We look at the possible transitions from that state -->
            <transitions>
              <!-- if an "ev_reopen" stimulus is applied -->
              <transition id="ev_reopen">
                <actions _delta="redefine">
                  <!-- Because an action has no id, you must redefine ALL actions for that transition -->
                  <action>
                    <!-- Execute on current object the method with id="IncrementCounter" -->
                    <verb>IncrementCounter</verb>
                    <!-- with the following parameters -->
                    <params>
                      <!-- attribute code of the counter to increment -->
                      <param xsi:type="attcode">reopen_count</param>
                      <!-- also the default value is set in PHP, it must be done again in XML -->
                      <param xsi:type="int">1</param>
                    </params>
                  </action>
                </actions>
              </transition>
            </transitions>
          </state>

Add counter to presentation

Let's add this reopen_count on the UserRequest details page, in the 3rd column and in the SLA report fieldset

In order to be able to write such delta XML, you need to decide

  • in which column of the details to display that field: col:col1 / col:col2 / col:col3, if there are columns defined.
  • retrieve the fieldset code, if any defined
  • guess a rank to use to position a particular field after or before another one, in default datamodel rank have a 10 increment between fields within a common container: fieldset, column or page (?).
To get the current XML presentation, check on your installed iTop server, the file itop/data/datamodel-production.xml which contains the compiled XML of your iTop
itop_design / classes
    <class id="UserRequest" _delta="must_exist">
      <presentation>
        <details>
          <items>
            <item id="col:col3">
              <items>
                <item id="fieldset:Ticket:SLA">
                  <items>
                    <item id="reopen_count" _delta="define">
                      <rank>80</rank>
                    </item>
                  </items>
                </item>
              </items>
            </item>
          </items>
        </details>
      </presentation>
    </class>

Add counter label

itop_design / dictionaries / dictionary@EN US / entries
<entry id="Class:UserRequest/Attribute:reopen_count" _delta="define">Reopening counter</entry>
The method is generic, so can be used for another counter
If you declared it on class Ticket instead of UserRequest, then it can be used on any Change classes as well
2_7_0/customization/add-counter.txt ยท Last modified: 2020/04/15 15:23 (external edit)
Back to top
Contact us