dexter@aeron7.com

My experience with Kohona

Private  /   /  By Amit Ghosh

Url

http://artretreat.org/amit/quiz/v1/

Ok, as you are seeing it; it is a work in progress website. It works like this way. There are 25 questions and there were 5 answers to each of them. 25 questions are segmented into some sections. Like Section A contains 5 questions.

 

Got the first error

I got the files and the database; I uploaded them in localhost and got an error.

download (46)

My URL was http://localhost/quiz/ ; So I googled with the term Kohona and came to know that Kohona is some PHP platform and ended up with this link after sometime searching where to change URL in Kohona.

http://stackoverflow.com/questions/20935184/kohana-404-error-requested-url-not-found

Certainly it was not a htaccess error. So I closed it. Then

Here is the copy paste from StackOverFlow’s answer with my slight modification that helped me finally – But well, it was wrong too. What you’re seeing is the error that is thrown when you haven’t set up your environment to run for the first time to use clean urls. Do this:

First, Go into your .htaccess file and set the correct path for RewriteBase if your localhost path where http://localhost/quiz, your htaccess should be:

  • 1.
    # Turn on URL rewriting
  • 2.
    RewriteEngine On
  • 3.
    # Installation directory
  • 4.
    RewriteBase /
  • 5.
    # Protect hidden files from being viewed
  • 6.
    <Files .*>
  • 7.
    Order Deny,Allow
  • 8.
    Deny From All
  • 9.
    </Files>
  • 10.
    # Protect application and system files from being viewed
  • 11.
    RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
  • 12.
    # Allow any files or directories that exist to be displayed directly
  • 13.
    RewriteCond %{REQUEST_FILENAME} !-f
  • 14.
    RewriteCond %{REQUEST_FILENAME} !-d
  • 15.
    # Rewrite all other URLs to index.php/URL
  • 16.
    RewriteRule ^(.*)$ /quiz/ [L]

(make sure you rename the file so it is ONLY called .htaccess ; if you are on Windows Xampp; it is _htaccess)

Second, Go into your /application/bootstrap.php file and make sure Kohana::init has:

‘base_url’ => ‘/quiz/’ in its array.

Then I saw another problem regarding database. So I hunt out for a folder named config or something like database.php cause that’s the common instinct and I founded in /application/config/database.php

That’s a good start towards solving the problem. The old site worked. 

How the old site works

Will write later

What I am supposed to do

Will write later

So, What I am doing

I opened the image in new tab and found that all the images are in assets/img/ . So I opened all the PHP files in Notepad++ and hunted out that term and landed on /application/views/index.php

I got the function where from the image was coming.

  • 1.
    function images_no($i) {
  • 2.
    $i--;
  • 3.
    if($i < 4)
  • 4.
    return 1;
  • 5.
    elseif($i < 10)
  • 6.
    return 2;
  • 7.
    elseif($i < 15)
  • 8.
    return 3;
  • 9.
    elseif($i < 19)
  • 10.
    return 4;
  • 11.
    elseif($i < 22)
  • 12.
    return 5;
  • 13.
    return 6;
  • 14.
    }

Hence I have 31 questions. If I count 0 as first then i=0 to 30 makes 31. So I tuned it to

  • 1.
    function images_no($i) {
  • 2.
    $i--;
  • 3.
    if($i < 4)
  • 4.
    return 1;
  • 5.
    elseif($i < 8)
  • 6.
    return 2;
  • 7.
    elseif($i < 12)
  • 8.
    return 3;
  • 9.
    elseif($i < 16)
  • 10.
    return 4;
  • 11.
    elseif($i < 20)
  • 12.
    return 5;
  • 13.
    elseif($i < 25)
  • 14.
    return 6;
  • 15.
    return 7;
  • 16.
    }

So, the images started to come out correctly. That’s a start. Now I hunted out from where the questions are coming. It’s from application/i18n/fr.php. So I changed the codes. You can see th structure below. It had 25 questions with 5 options to each. I made it 31 and changed the texts accordingly .

  • 1.
    'questionnaire.q2' => 'I have a clear vision for my “ultimate professional success”',
  • 2.
    'questionnaire.q2.r1' => 'Irritated',
  • 3.
    'questionnaire.q2.r2' => 'Annoyed',
  • 4.
    'questionnaire.q2.r3' => 'Neutral',
  • 5.
    'questionnaire.q2.r4' => 'Interested',
  • 6.
    'questionnaire.q2.r5' => 'Grateful',
  • 7.
     

Now there is two steps remaining –

One is to make the score act like I want in 25 questions.

Make the steps to 31 from 25. Now I am thinking on which one to do. Well I don’t know I thought of inspecting the database there are four prefixes. quiz1_,quiz2_,quiz3_,quiz4_.  I quickly look at the database file and it says the prefix here is quiz2_.

Each of them have four types; like for quiz2_ it is

quiz2_responses, quiz2_roles, quiz2_roles_users, quiz2_user_tokens

So I started comparing quiz2_reponses with other _responses files ; It seems there were quizes earlierly but with different sets of questions; so I started comparing them.

Also here are some recent investigative facts

/application/view/index.php

  • 1.
    <script>
  • 2.
    document.addEventListener('DOMContentLoaded', function() {
  • 3.
    $('div.tab-pane:nth-child(25) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1)').click(function() { $('form').submit() })
  • 4.
    });
  • 5.
    </script>

I made it 31 from 25. This is an event  listener than gives the result page after 25th answers.

  • 1.
    <span id="num-question">1</span>/25
  • 1.
    $questions = Arr::range(1,31);
  • 2.
    ?>
  • 3.
    <?php foreach($questions as $i): ?>
  • 4.
    <?php $attributes = array(
  • 5.
    'role' => 'tabpanel',
  • 6.
    'class' => 'tab-pane',
  • 7.
    );
  • 8.
    if($i === 1) {
  • 9.
    $attributes['class'] .= ' active';
  • 10.
    } else if($i === 31) {
  • 11.
    $attributes['class'] .= ' last';
  • 12.
    }
  • 13.
    ?>

Then I changed it to 31 and added q26,q27,q28,q29,q30,q31 in quiz2_responses and get this error. download

After a bit of digging I found it on /application/classes/Model/Response.php

I saw

  • 1.
    $scores = array(
  • 2.
    'motivation' => 0,
  • 3.
    'curiosity' => 0,
  • 4.
    'insight' => 0,
  • 5.
    'engagement' => 0,
  • 6.
    'determination' => 0,
  • 7.
    'international' => 0,
  • 8.
    );

There are 7 types of categories here, I added another one named dexter. I searched for the value ‘motivation’ in all the PHP files and it got hit at /application/views/score.php

  • 1.
    'motivation' => '1. The right kind of motivation',
  • 2.
    'curiosity' => '2. Curiousity',
  • 3.
    'insight' => '3. Insight',
  • 4.
    'engagement' => '4. Engagement',
  • 5.
    'determination' => '5. Determination',
  • 6.
    'international' => '6. International dimension',

I changed it too along with I added a new float type field in database preceding the others. Also I replicated one of these

  • 1.
    <div class="row">
  • 2.
    <div class="col-md-8">
  • 3.
    <strong>6. International dimension</strong>: Creating opportunities in a globalized economy
  • 4.
    </div>
  • 5.
    <div class="col-md-4 text-center">
  • 6.
    <h3><span style="padding: 10px;border-radius: 5px;color: black;background-color: <?php echo HTML::color(Arr::get($reponse->score(), 'international')) ?>"><?php echo round(Arr::get($reponse->score(), 'international')) ?>%</span></h3>
  • 7.
    </div>
  • 8.
    </div>
  • 9.
    <br />
to dexter attribute

Well it worked !! Atleast, the error is no more. Now after playing the quiz I realized it is not calculating my dexter score. Huh ? Let’s check again. Another problem was  download

This bar is showing 31 but was filling for 25. WTF? Haha, it’s fun! Now my target is to get the percentage correct, then I will display the text according to it. Viola!

Now I see in the Response.php that some $num variable is dealing with the scores. Here are the snapshot of the logic.

  • 1.
     $num = preg_replace('/q(\d+)_?.*/', '$1', $field);
  • 1.
    $categories = array_keys($scores);
  • 2.
    $questions = array_filter(array_keys($this->table_columns()), function($key) {
  • 3.
    return preg_match('/^q\d+$/', $key);
  • 4.
    });
  • 5.
    foreach($questions as $i => $question) {
  • 6.
    $score_type = '';
  • 7.
    $num = (int) preg_replace('/\D+/', '', $question);
  • 8.
    if($num <= 17) {
  • 9.
    $score_type = $categories[($num - 1) % 6];
  • 10.
    } elseif($num <= 23) {
  • 11.
    $score_type = $categories[($num - 18)];
  • 12.
    } elseif($num == 24) {
  • 13.
    $score_type = 'curiosity';
  • 14.
    } else {
  • 15.
    $score_type = 'international';
  • 16.
    }
  • 17.
    $val = $this->{$question} - 1;
  • 18.
    $scores[$score_type] += $val;
  • 19.
    }

I have no idea what preg_replace or preg_match is. Here is what I found on php.net

  • 1.
    mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [,int &$count ]] )

Searches subject for matches to pattern and replaces them with replacement. Seems quite cranky stuff.

download (47)

Well this issue got solved, as I searched the term ‘aria-valuemax’ and found it on /application/views/index.php

  • 1.
    <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="1" aria-valuemin="1" aria-valuemax="25" style="width: 0%">
  • 2.
    <span id="num-question">1</span>/31
  • 3.
    </div>

Now fixing the score –

  • 1.
    if($num <= 17) {
  • 2.
    $score_type = $categories[($num - 1) % 6];
  • 3.
    } elseif($num <= 23) {
  • 4.
    $score_type = $categories[($num - 18)];
  • 5.
    } elseif($num == 24) {
  • 6.
    $score_type = 'curiosity';
  • 7.
    } else {
  • 8.
    $score_type = 'international';
  • 9.
    }

Replaced this following code

  • 1.
    if($num <= 4) {
  • 2.
    $score_type = 'motivation';
  • 3.
    } elseif($num >= 5 && $num <= 8) {
  • 4.
    $score_type = 'curiosity';
  • 5.
    } elseif($num >= 9 && $num <= 12) {
  • 6.
    $score_type = 'insight';
  • 7.
    } elseif($num >= 13 && $num <= 16) {
  • 8.
    $score_type = 'engagement';
  • 9.
    }elseif($num >= 17 && $num <= 20) {
  • 10.
    $score_type = 'determination';
  • 11.
    }elseif($num >= 21 && $num <= 25) {
  • 12.
    $score_type = 'international';
  • 13.
    }else {
  • 14.
    $score_type = 'dexter';
  • 15.
    }

Removed this

  • 1.
    foreach($categories as $cat) {
  • 2.
    if($cat == 'curiosity') {
  • 3.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 4.
    } else {
  • 5.
    $scores[$cat] = round($scores[$cat] / 16 * 100);
  • 6.
    }
  • 7.
    }

Well, it worked. Now the score showing part. Now I see some anomaly, it needs to be percentage. Ah, I messed up this score thing. I need to work on it. Let’s continue tomorrow!

Ok, I can’t sleep and the I have to add this part

  • 1.
    foreach($categories as $cat) {
  • 2.
    if($cat == 'motivation') {
  • 3.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 4.
    } elseif($cat == 'curiosity') {
  • 5.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 6.
    } elseif($cat == 'insight') {
  • 7.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 8.
    } elseif($cat == 'engagement') {
  • 9.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 10.
    } elseif($cat == 'determination') {
  • 11.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 12.
    } elseif($cat == 'international') {
  • 13.
    $scores[$cat] = round($scores[$cat] / 25 * 100);
  • 14.
    }else {
  • 15.
    $scores[$cat] = round($scores[$cat] / 30 * 100);
  • 16.
    }
  • 17.
    }

Well, so the rest is done. I added this following set of variables in the /application/views/score.php

  • 1.
    $dex1 = Arr::get($reponse->score(), 'motivation');
  • 2.
    $dex2 = Arr::get($reponse->score(), 'curiosity');
  • 3.
    $dex3 = Arr::get($reponse->score(), 'insight');
  • 4.
    $dex4 = Arr::get($reponse->score(), 'engagement');
  • 5.
    $dex5 = Arr::get($reponse->score(), 'determination');
  • 6.
    $dex6 = Arr::get($reponse->score(), 'international');
  • 7.
    $dex7 = Arr::get($reponse->score(), 'dexter');

and replaced

  • 1.
    <strong>1. Creativity and ideation</strong>: You're probably unsure of your innovative talent or you may like consistency and routine. Maybe you haven't been given opportunities to innovate and lead innovation, or maybe you’re just used to focus on issues that are clearly defined and well understood.

with

 

  • 1.
    <?php
  • 2.
    if($dex1 <= 35) {
  • 3.
    echo "<strong>1. Creativity and ideation</strong>: You're probably unsure of your innovative talent or you may like consistency and routine. Maybe you haven't been given opportunities to innovate and lead innovation, or maybe you’re just used to focus on issues that are clearly defined and well understood.";
  • 4.
    } elseif($dex1 >= 35 && $dex1 <= 79) {
  • 5.
    echo "<strong>1. Creativity and ideation</strong>: Your creativity is a work in progress. You've had some successes, so now it's time to let loose and stretch yourself.";
  • 6.
    } else {
  • 7.
    echo "<strong>1. Creativity and ideation</strong>: Creativity is one of your strengths, and you seem to have an innovative mindset. So don't hide your ability! Look for ways to share your creative process with others. Engage colleagues and teammates in creative pursuits, and promote creativity in your team and organization.
  • 8.
    ";
  • 9.
    } ?>

It was still wrong cause why it shows 0 in somewhere cause if the minimum value is 1 then it must be >0 right?

So I fixed the code here as the problem was it was taking i=0 rather than 1 ; common problem.

  • 1.
    foreach($categories as $cat) {
  • 2.
    $scores[$cat] = $scores[$cat] + 4;
  • 3.
    if($cat == 'motivation') {
  • 4.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 5.
    } elseif($cat == 'curiosity') {
  • 6.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 7.
    } elseif($cat == 'insight') {
  • 8.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 9.
    } elseif($cat == 'engagement') {
  • 10.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 11.
    } elseif($cat == 'determination') {
  • 12.
    $scores[$cat] = round($scores[$cat] / 20 * 100);
  • 13.
    } elseif($cat == 'international') {
  • 14.
    $scores[$cat] = round($scores[$cat] / 25 * 100);
  • 15.
    }else {
  • 16.
    $scores[$cat] = round($scores[$cat] / 30 * 100);
  • 17.
    }
  • 18.
    }
About the Author

Aloha, I'm Amit Ghosh, a web entrepreneur and avid blogger. Bitten by entrepreneurial bug, I got kicked out from college and ended up being millionaire and running a digital media company named Aeron7 headquartered at Lithuania.

Related Posts

Picking the best products to sell is the next big challenge after deciding to start your eCommerce...

Yes, I need to work   Cause there are three things First you do not know even value of 10 INR...

First and foremost, it is not a love story scripted in heaven where the hero always wins. The...

Leave a Reply